Skip to content

Commit

Permalink
Drop support for node < 10
Browse files Browse the repository at this point in the history
  • Loading branch information
simonepri committed Jul 2, 2020
1 parent a78e0ea commit 179c59b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup node
uses: actions/setup-node@v1
with:
node-version: 6
node-version: 10
- name: Install dev dependencies
run: |
npm install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [14, 12, 10, 8, 6, 4]
node: [14, 12, 10]
steps:
- name: Setup repo
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [14, 12, 10, 8, 6, 4]
node: [14, 12, 10]
steps:
- name: Setup repo
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [14, 12, 10, 8, 6, 4]
node: [14, 12, 10]
steps:
- name: Setup repo
uses: actions/checkout@v2
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function transform(str, from, to) {
out += str[i];
}
}

return out;
}

Expand Down Expand Up @@ -65,6 +66,7 @@ function keys(path, opts) {
key.toUpperCase().startsWith(env)
);
}

return Object.keys(process.env).filter(key => key.startsWith(env));
}

Expand All @@ -84,6 +86,7 @@ function parse(str, opts) {

try {
return JSON.parse(str);
// eslint-disable-next-line no-unused-vars
} catch (error) {
return str;
}
Expand All @@ -93,6 +96,7 @@ function stringify(val, opts) {
if (typeof val === 'string' || !opts.stringify) {
return val;
}

if (typeof val === 'object') {
return JSON.stringify(val);
}
Expand Down Expand Up @@ -123,6 +127,7 @@ function get(path, defaultValue, opts) {
} else {
opts = {};
}

defaultValue = args.pop();

keys(path, opts)
Expand All @@ -132,13 +137,15 @@ function get(path, defaultValue, opts) {
if (!opts.caseSensitive) {
dotp = dotp.toLowerCase();
}

const val = parse(process.env[key], opts);
if (dotp === '') {
obj = val;
} else {
if (typeof obj !== 'object') {
obj = {};
}

dotProp.set(obj, dotp, val);
}
});
Expand All @@ -147,9 +154,11 @@ function get(path, defaultValue, opts) {
if (!opts.caseSensitive) {
prefix = prefix.toLowerCase();
}

if (path === '') {
return obj;
}

return dotProp.get(obj, prefix, defaultValue);
}

Expand Down Expand Up @@ -221,5 +230,5 @@ module.exports = {
get: get,
set: set,
delete: del,
has: has,
has: has
};
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"index.js"
],
"engines": {
"node": ">=4"
"node": ">=10"
},
"scripts": {
"lint": "xo",
Expand All @@ -43,12 +43,12 @@
"update": "npx npm-check -u"
},
"dependencies": {
"dot-prop": "^4.2.0"
"dot-prop": "^5.2.0"
},
"devDependencies": {
"ava": "~0.25.0",
"nyc": "^11.6.0",
"xo": "~0.20.3"
"ava": "^3.9.0",
"nyc": "^15.1.0",
"xo": "~0.27.2"
},
"ava": {
"verbose": true
Expand All @@ -66,7 +66,8 @@
"object-shorthand": "off",
"prefer-rest-params": "off",
"camelcase": "off",
"prefer-destructuring": "off"
"prefer-destructuring": "off",
"prefer-object-spread": "off"
}
}
}
42 changes: 21 additions & 21 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import m from '.';
const test = require('ava');
const m = require('.');

test.beforeEach(t => {
t.context.env = Object.assign({}, process.env);
Expand Down Expand Up @@ -65,7 +65,7 @@ test.serial('should get the envs correctly when parse is enabled', t => {
process.env.TEST = '{"a":42}';
t.deepEqual(m.get('test', {parse: true}), {a: 42});
process.env.TEST = '}{';
t.deepEqual(m.get('test', {parse: true}), '}{');
t.is(m.get('test', {parse: true}), '}{');
});

test.serial('should set the envs correctly when stringify is enabled', t => {
Expand Down Expand Up @@ -147,7 +147,7 @@ test.serial('should get the envs correctly when parse is disabled', t => {
'2',
{a: 3, b: 4},
true,
null,
null
]);
process.env.TEST = {};
t.deepEqual(m.get('test', {parse: false}), {});
Expand Down Expand Up @@ -175,13 +175,13 @@ test.serial('should get the envs correctly when parse is disabled', t => {
process.env.TEST = 'NaN';
t.is(m.get('test', {parse: false}), 'NaN');
process.env.TEST = '[]';
t.deepEqual(m.get('test', {parse: false}), '[]');
t.is(m.get('test', {parse: false}), '[]');
process.env.TEST = '[1,"2",{"a":3,"b":4},true,null]';
t.deepEqual(m.get('test', {parse: false}), '[1,"2",{"a":3,"b":4},true,null]');
t.is(m.get('test', {parse: false}), '[1,"2",{"a":3,"b":4},true,null]');
process.env.TEST = '{}';
t.deepEqual(m.get('test', {parse: false}), '{}');
t.is(m.get('test', {parse: false}), '{}');
process.env.TEST = '{"a":42}';
t.deepEqual(m.get('test', {parse: false}), '{"a":42}');
t.is(m.get('test', {parse: false}), '{"a":42}');
});

test.serial('should set the envs correctly when stringify is disabled', t => {
Expand Down Expand Up @@ -239,7 +239,7 @@ test.serial('should return a value for full env path', t => {
test.serial('should return the default value', t => {
t.is(m.get('i.n.v.a.l.i.d', 42), 42);
t.deepEqual(m.get('i.n.v.a.l.i.d', {a: 1}, {}), {a: 1});
t.deepEqual(m.get('i.n.v.a.l.i.d', {}), undefined);
t.is(m.get('i.n.v.a.l.i.d', {}), undefined);
});

test.serial(
Expand Down Expand Up @@ -287,36 +287,36 @@ test.serial('should allow to get non-capitalized env keys', t => {

test.serial('should work with the empty string env variable', t => {
process.env = {
'': 'test',
'': 'test'
};
t.deepEqual(m.get(''), 'test');
t.is(m.get(''), 'test');
process.env = {
_a: 'a',
_b: 'b',
_b: 'b'
};
t.deepEqual(m.get(''), {'': {a: 'a', b: 'b'}});
m.set('', 'only this should exists');
t.deepEqual(m.get(''), 'only this should exists');
t.is(m.get(''), 'only this should exists');
});

test.serial('should work if some env vars overlap', t => {
process.env = {
KEY: 'this must be ignored',
KEY_A: 'a',
KEY_B: 'b',
KEY_B: 'b'
};
t.deepEqual(m.get(''), {key: {a: 'a', b: 'b'}});
t.deepEqual(m.get('key'), {a: 'a', b: 'b'});
});

test.serial('should pass README examples', t => {
test.serial('should pass readme examples', t => {
process.env = {
FOO_BAR: 'unicorn',
'FOO_DOT.DOT': 'pony',
'FOO_UND\\_UND': 'whale',
'FOO_UND\\_UND': 'whale'
};
t.deepEqual(m.get(''), {
foo: {bar: 'unicorn', 'dot.dot': 'pony', und_und: 'whale'},
foo: {bar: 'unicorn', 'dot.dot': 'pony', und_und: 'whale'}
});
t.is(m.get('foo.bar'), 'unicorn');
t.is(m.get('foo.notDefined.deep'), undefined);
Expand All @@ -325,20 +325,20 @@ test.serial('should pass README examples', t => {
m.set('foo.bar', 'b');
t.is(m.get('foo.bar'), 'b');
t.deepEqual(m.get(''), {
foo: {bar: 'b', 'dot.dot': 'pony', und_und: 'whale'},
foo: {bar: 'b', 'dot.dot': 'pony', und_und: 'whale'}
});
m.set('foo.baz.e', 'x');
t.is(m.get('foo.baz.e'), 'x');
t.deepEqual(m.get('foo.baz'), {e: 'x'});
t.deepEqual(m.get(''), {
foo: {bar: 'b', baz: {e: 'x'}, 'dot.dot': 'pony', und_und: 'whale'},
foo: {bar: 'b', baz: {e: 'x'}, 'dot.dot': 'pony', und_und: 'whale'}
});
t.is(m.has('foo.bar'), true);
m.delete('foo.bar');
t.deepEqual(m.get('foo'), {
baz: {e: 'x'},
'dot.dot': 'pony',
und_und: 'whale',
und_und: 'whale'
});
m.delete('foo.baz.e');
t.is(m.get('foo.baz'), undefined);
Expand All @@ -357,6 +357,6 @@ test.serial('should pass README examples', t => {
n1: '42',
n2: 42,
n3: 42,
n4: '42',
n4: '42'
});
});

0 comments on commit 179c59b

Please sign in to comment.