@trenskow/config 1.2.5 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,11 @@
12
12
  "<node_internals>/**"
13
13
  ],
14
14
  "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
15
- "args": ["--bail"]
15
+ "args": [
16
+ "--bail",
17
+ "--timeout",
18
+ "0"
19
+ ]
16
20
  }
17
21
  ]
18
- }
22
+ }
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "cSpell.words": [
3
+ "caseit",
3
4
  "isvalid",
4
5
  "keyd"
5
6
  ]
6
- }
7
+ }
package/index.js CHANGED
@@ -9,7 +9,7 @@ const
9
9
  let config = {};
10
10
 
11
11
  // We first split the env names at `_` and put them into objects.
12
- Object.keys(process.env)
12
+ Object.keys(process.env).filter((key) => process.env[key])
13
13
  .forEach((key) => {
14
14
 
15
15
  // Split and filter.
@@ -35,7 +35,7 @@ Object.keys(process.env)
35
35
 
36
36
  const fillArray = (value, length) => {
37
37
  let result = [];
38
- for (let idx = 0 ; idx < length ; idx++) result.push(value);
38
+ for (let idx = 0; idx < length; idx++) result.push(value);
39
39
  return result;
40
40
  };
41
41
 
@@ -45,7 +45,7 @@ const makeArrayIfNecessary = (obj) => {
45
45
  const arrayKeys = fillArray(undefined, keys.length)
46
46
  .map((_, idx) => idx);
47
47
 
48
- for (let idx = 0 ; idx < keys.length; idx++) {
48
+ for (let idx = 0; idx < keys.length; idx++) {
49
49
  if (keys[idx] != arrayKeys[idx]) return obj;
50
50
  }
51
51
 
@@ -57,12 +57,12 @@ const makeArrayIfNecessary = (obj) => {
57
57
  const cleanIt = (obj, expanded = [], keyPath = []) => {
58
58
 
59
59
  if (typeof obj === 'undefined') return;
60
-
60
+
61
61
  if (!Array.isArray(expanded)) expanded = expanded.split(/, ?/);
62
62
 
63
63
  // If the object is a string we just return it.
64
64
  if (typeof obj === 'string') return obj;
65
-
65
+
66
66
  // Now clean all the keys.
67
67
  obj = Object.keys(obj).reduce((res, key) => {
68
68
  res[key.toLowerCase()] = cleanIt(obj[key], expanded, keyPath.concat([key]));
@@ -94,32 +94,27 @@ const cleanIt = (obj, expanded = [], keyPath = []) => {
94
94
  // Give it back.
95
95
  module.exports = cleanIt(config);
96
96
 
97
- module.exports.expand = (keyPaths) => {
98
- module.exports = merge(module.exports, cleanIt(config, keyPaths), {
99
- validate: module.exports.validate,
100
- expand: module.exports.expand
101
- });
102
- return module.exports;
103
- };
104
-
105
97
  module.exports.validate = async (schema, options = {}) => {
106
98
 
99
+ schema = isvalid.formalize(schema);
100
+
101
+ module.exports = merge(module.exports, cleanIt(config, isvalid.keyPaths(schema).all(Object).filter((keyPath) => keyPath)), {
102
+ validate: module.exports.validate
103
+ });
104
+
107
105
  options = merge({
108
106
  defaults: {
109
107
  unknownKeys: 'allow'
110
- },
111
- throwDeepKeyOnImplicit: true
108
+ }
112
109
  }, options);
113
110
 
114
111
  try {
115
112
  return module.exports = merge(module.exports, await isvalid(
116
113
  module.exports,
117
- merge(schema, {
114
+ isvalid.merge(schema).with({
118
115
  'validate': {
119
- type: 'AsyncFunction'
120
- },
121
- 'expand': {
122
- type: 'Function'
116
+ type: 'AsyncFunction',
117
+ required: true
123
118
  }
124
119
  }),
125
120
  options));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/config",
3
- "version": "1.2.5",
3
+ "version": "2.1.2",
4
4
  "description": "Converts `process.env` into a neatly packed object.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,12 +26,12 @@
26
26
  "homepage": "https://github.com/trenskow/config#readme",
27
27
  "dependencies": {
28
28
  "@trenskow/caseit": "^1.0.1",
29
- "isvalid": "^2.7.21",
30
- "keyd": "^1.3.4",
29
+ "isvalid": "^2.10.2",
30
+ "keyd": "^1.4.0",
31
31
  "merge": "^2.1.1"
32
32
  },
33
33
  "devDependencies": {
34
- "chai": "^4.3.0",
35
- "mocha": "^7.2.0"
34
+ "chai": "^4.3.4",
35
+ "mocha": "^9.0.3"
36
36
  }
37
37
  }
package/test/index.js CHANGED
@@ -30,7 +30,7 @@ describe('config', () => {
30
30
  config.validate({
31
31
  'configTest': {
32
32
  'obj': {
33
- 'string': { type: Number, required: true}
33
+ 'string': { type: Number, required: true }
34
34
  }
35
35
  }
36
36
  }).then(() => {
@@ -61,6 +61,20 @@ describe('config', () => {
61
61
  done();
62
62
  }).catch(done);
63
63
  });
64
+ it ('should convert arrays into arrays.', () => {
65
+ const result = require('../');
66
+ expect(result)
67
+ .to.have.property('configTest')
68
+ .to.have.property('array')
69
+ .to.eql(['0', '1', '2', '3']);
70
+ });
71
+ it ('should convert even small arrays.', () => {
72
+ const result = require('../');
73
+ expect(result)
74
+ .to.have.property('configTest')
75
+ .to.have.property('smallArray')
76
+ .to.eql(['0']);
77
+ });
64
78
  it ('not remove validate method when unknown keys are set to be removed', (done) => {
65
79
  config.validate({
66
80
  'configTest': {
@@ -79,28 +93,4 @@ describe('config', () => {
79
93
  done();
80
94
  }).catch(done);
81
95
  });
82
- it ('should not collapse when set to expand.', () => {
83
- expect(config.expand('config.test.some.objects'))
84
- .to.have.property('config')
85
- .to.have.property('test')
86
- .to.have.property('some')
87
- .to.have.property('objects')
88
- .to.have.property('key', 'value');
89
- });
90
- it ('should convert arrays into arrays.', () => {
91
- const result = require('../');
92
- expect(result)
93
- .to.have.property('config')
94
- .to.have.property('test')
95
- .to.have.property('array')
96
- .to.eql(['0', '1', '2', '3']);
97
- });
98
- it ('should convert even small arrays.', () => {
99
- const result = require('../');
100
- expect(result)
101
- .to.have.property('config')
102
- .to.have.property('test')
103
- .to.have.property('smallArray')
104
- .to.eql(['0']);
105
- });
106
96
  });