@biorate/config 0.10.0
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.
- package/CHANGELOG.md +12 -0
- package/LICENSE +22 -0
- package/README.md +92 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/src/errors.js +11 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/index.js +56 -0
- package/dist/src/index.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/index.ts +1 -0
- package/interfaces.ts +0 -0
- package/package.json +19 -0
- package/src/errors.ts +8 -0
- package/src/index.ts +144 -0
- package/tests/__mocks__/index.ts +11 -0
- package/tests/index.spec.ts +29 -0
- package/tsconfig.build.json +5 -0
- package/tsconfig.json +6 -0
package/CHANGELOG.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
5
|
+
|
6
|
+
# [0.10.0](https://github.com/biorate/core/compare/v0.9.3...v0.10.0) (2021-09-24)
|
7
|
+
|
8
|
+
|
9
|
+
### Features
|
10
|
+
|
11
|
+
* **config:** release ([f7ba2b9](https://github.com/biorate/core/commit/f7ba2b91a8a5c766c4440dfc0243d42a7cc833b9))
|
12
|
+
* **config:** release ([0fce238](https://github.com/biorate/core/commit/0fce238c553057a6eec7fa3ddb2e2448d79eb3d2))
|
package/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2021-present Leonid Levkin (llevkin)
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Config
|
2
|
+
|
3
|
+
Application configurator
|
4
|
+
|
5
|
+
#### Example:
|
6
|
+
|
7
|
+
#### Get:
|
8
|
+
|
9
|
+
```ts
|
10
|
+
import { Config } from '@biorate/config';
|
11
|
+
|
12
|
+
export const data = {
|
13
|
+
a: {
|
14
|
+
b: {
|
15
|
+
c: 1,
|
16
|
+
d: 2,
|
17
|
+
},
|
18
|
+
},
|
19
|
+
b: 'world!',
|
20
|
+
c: { d: 'Hello ${b}' },
|
21
|
+
d: '${c.d} Repeat for all ${b}',
|
22
|
+
};
|
23
|
+
|
24
|
+
const config = new Config(data);
|
25
|
+
|
26
|
+
console.log(config.get('a')); // { b: { c: 1, d: 2 } }
|
27
|
+
console.log(config.get('a.b')); // { c: 1, d: 2 }
|
28
|
+
console.log(config.get('b')); // world!
|
29
|
+
console.log(config.get('c')); // { d: 'Hello world!' }
|
30
|
+
console.log(config.get('c.d')); // 'Hello world!'
|
31
|
+
console.log(config.get('d')); // Hello world! Repeat for all world!
|
32
|
+
```
|
33
|
+
|
34
|
+
#### Set:
|
35
|
+
|
36
|
+
```ts
|
37
|
+
import { Config } from '@biorate/config';
|
38
|
+
|
39
|
+
const config = new Config();
|
40
|
+
|
41
|
+
config.set('a', 1);
|
42
|
+
|
43
|
+
console.log(config.get('a')); // 1
|
44
|
+
```
|
45
|
+
|
46
|
+
#### Has:
|
47
|
+
|
48
|
+
```ts
|
49
|
+
import { Config } from '@biorate/config';
|
50
|
+
|
51
|
+
const config = new Config();
|
52
|
+
|
53
|
+
config.set('a', 1);
|
54
|
+
|
55
|
+
console.log(config.has('a')); // true
|
56
|
+
console.log(config.has('b')); // false
|
57
|
+
```
|
58
|
+
|
59
|
+
#### Merge:
|
60
|
+
|
61
|
+
```ts
|
62
|
+
import { Config } from '@biorate/config';
|
63
|
+
|
64
|
+
const config = new Config();
|
65
|
+
|
66
|
+
config.merge({
|
67
|
+
a: { b: { c: 1 } },
|
68
|
+
});
|
69
|
+
|
70
|
+
config.merge({
|
71
|
+
a: { b: { d: 2 } },
|
72
|
+
});
|
73
|
+
|
74
|
+
console.log(config.has('a')); // true
|
75
|
+
console.log(config.has('a.b')); // true
|
76
|
+
console.log(config.get('a.b.c')); // 1
|
77
|
+
console.log(config.get('a.b.d')); // 2
|
78
|
+
```
|
79
|
+
|
80
|
+
### Learn
|
81
|
+
|
82
|
+
- Documentation can be found here - [docs](https://biorate.github.io/core/modules/config.html).
|
83
|
+
|
84
|
+
### Release History
|
85
|
+
|
86
|
+
See the [CHANGELOG](https://github.com/biorate/core/blob/master/packages/%40biorate/config/CHANGELOG.md)
|
87
|
+
|
88
|
+
### License
|
89
|
+
|
90
|
+
[MIT](https://github.com/biorate/core/blob/master/packages/%40biorate/config/LICENSE)
|
91
|
+
|
92
|
+
Copyright (c) 2021-present [Leonid Levkin (llevkin)](mailto:llevkin@yandex.ru)
|
package/dist/index.js
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5
|
+
}) : (function(o, m, k, k2) {
|
6
|
+
if (k2 === undefined) k2 = k;
|
7
|
+
o[k2] = m[k];
|
8
|
+
}));
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
11
|
+
};
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
13
|
+
__exportStar(require("./src"), exports);
|
14
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,wCAAsB"}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.UndefinedConfigPathError = void 0;
|
4
|
+
const errors_1 = require("@biorate/errors");
|
5
|
+
class UndefinedConfigPathError extends errors_1.BaseError {
|
6
|
+
constructor(path) {
|
7
|
+
super(`Undefined config path [%s]`, [path]);
|
8
|
+
}
|
9
|
+
}
|
10
|
+
exports.UndefinedConfigPathError = UndefinedConfigPathError;
|
11
|
+
//# sourceMappingURL=errors.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":";;;AAAA,4CAA4C;AAG5C,MAAa,wBAAyB,SAAQ,kBAAS;IACrD,YAAmB,IAAkB;QACnC,KAAK,CAAC,4BAA4B,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAC9C,CAAC;CACF;AAJD,4DAIC"}
|
@@ -0,0 +1,56 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Config = void 0;
|
4
|
+
const lodash_1 = require("lodash");
|
5
|
+
const traverse = require("traverse");
|
6
|
+
const errors_1 = require("./errors");
|
7
|
+
class Config {
|
8
|
+
constructor(data) {
|
9
|
+
this.data = {};
|
10
|
+
if (data)
|
11
|
+
this.merge(data);
|
12
|
+
}
|
13
|
+
template(value) {
|
14
|
+
let res, result;
|
15
|
+
const regExp = /\${([^}{]+)+?}/g;
|
16
|
+
result = value;
|
17
|
+
while ((res = regExp.exec(value)))
|
18
|
+
if (this.has(res[1]))
|
19
|
+
result = result.replace(res[0], this.get(res[1]));
|
20
|
+
return result;
|
21
|
+
}
|
22
|
+
templatize(object) {
|
23
|
+
const template = this.template.bind(this);
|
24
|
+
return traverse(object).forEach(function (value) {
|
25
|
+
if (typeof value === 'string')
|
26
|
+
this.update(template(value));
|
27
|
+
});
|
28
|
+
}
|
29
|
+
get(path, def) {
|
30
|
+
if (!this.has(path)) {
|
31
|
+
if (def === undefined)
|
32
|
+
throw new errors_1.UndefinedConfigPathError(path);
|
33
|
+
return def;
|
34
|
+
}
|
35
|
+
const result = (0, lodash_1.get)(this.data, path);
|
36
|
+
switch (typeof result) {
|
37
|
+
case 'string':
|
38
|
+
return this.template(result);
|
39
|
+
case 'object':
|
40
|
+
return result ? this.templatize(result) : result;
|
41
|
+
default:
|
42
|
+
return result;
|
43
|
+
}
|
44
|
+
}
|
45
|
+
has(path) {
|
46
|
+
return (0, lodash_1.has)(this.data, path);
|
47
|
+
}
|
48
|
+
set(path, value) {
|
49
|
+
(0, lodash_1.set)(this.data, path, value);
|
50
|
+
}
|
51
|
+
merge(data) {
|
52
|
+
(0, lodash_1.merge)(this.data, data);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
exports.Config = Config;
|
56
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAA0E;AAC1E,qCAAqC;AACrC,qCAAoD;AAEpD,MAAa,MAAM;IAajB,YAAmB,IAAoC;QAZ7C,SAAI,GAAG,EAAE,CAAC;QAalB,IAAI,IAAI;YAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAKS,QAAQ,CAAC,KAAa;QAC9B,IAAI,GAAG,EAAE,MAAM,CAAC;QAChB,MAAM,MAAM,GAAG,iBAAiB,CAAC;QACjC,MAAM,GAAG,KAAK,CAAC;QACf,OAAO,CAAC,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC/B,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;gBAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1E,OAAO,MAAM,CAAC;IAChB,CAAC;IAKS,UAAU,CAAC,MAAe;QAClC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC1C,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,UAAU,KAAK;YAC7C,IAAI,OAAO,KAAK,KAAK,QAAQ;gBAAE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;QAC9D,CAAC,CAAC,CAAC;IACL,CAAC;IAuBM,GAAG,CAAC,IAAkB,EAAE,GAAa;QAC1C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;YACnB,IAAI,GAAG,KAAK,SAAS;gBAAE,MAAM,IAAI,iCAAwB,CAAC,IAAI,CAAC,CAAC;YAChE,OAAO,GAAG,CAAC;SACZ;QACD,MAAM,MAAM,GAAG,IAAA,YAAG,EAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACpC,QAAQ,OAAO,MAAM,EAAE;YACrB,KAAK,QAAQ;gBACX,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;YAC/B,KAAK,QAAQ;gBACX,OAAO,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;YACnD;gBACE,OAAO,MAAM,CAAC;SACjB;IACH,CAAC;IAgBM,GAAG,CAAC,IAAkB;QAC3B,OAAO,IAAA,YAAG,EAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAC9B,CAAC;IAiBM,GAAG,CAAC,IAAkB,EAAE,KAAc;QAC3C,IAAA,YAAG,EAAC,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;IAC9B,CAAC;IAyBM,KAAK,CAAC,IAAa;QACxB,IAAA,cAAK,EAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACzB,CAAC;CACF;AA3ID,wBA2IC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"program":{"fileNames":["../../../../node_modules/typescript/lib/lib.es5.d.ts","../../../../node_modules/typescript/lib/lib.es2015.d.ts","../../../../node_modules/typescript/lib/lib.es2016.d.ts","../../../../node_modules/typescript/lib/lib.es2017.d.ts","../../../../node_modules/typescript/lib/lib.es2018.d.ts","../../../../node_modules/typescript/lib/lib.es2019.d.ts","../../../../node_modules/typescript/lib/lib.es2020.d.ts","../../../../node_modules/typescript/lib/lib.dom.d.ts","../../../../node_modules/typescript/lib/lib.dom.iterable.d.ts","../../../../node_modules/typescript/lib/lib.webworker.importscripts.d.ts","../../../../node_modules/typescript/lib/lib.scripthost.d.ts","../../../../node_modules/typescript/lib/lib.es2015.core.d.ts","../../../../node_modules/typescript/lib/lib.es2015.collection.d.ts","../../../../node_modules/typescript/lib/lib.es2015.generator.d.ts","../../../../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../../../node_modules/typescript/lib/lib.es2015.promise.d.ts","../../../../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../../../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../../../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../../../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../../../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../../../node_modules/typescript/lib/lib.es2017.object.d.ts","../../../../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../../../node_modules/typescript/lib/lib.es2017.string.d.ts","../../../../node_modules/typescript/lib/lib.es2017.intl.d.ts","../../../../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../../../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../../../../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../../../../node_modules/typescript/lib/lib.es2018.intl.d.ts","../../../../node_modules/typescript/lib/lib.es2018.promise.d.ts","../../../../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../../../../node_modules/typescript/lib/lib.es2019.array.d.ts","../../../../node_modules/typescript/lib/lib.es2019.object.d.ts","../../../../node_modules/typescript/lib/lib.es2019.string.d.ts","../../../../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../../../../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../../../../node_modules/typescript/lib/lib.es2020.promise.d.ts","../../../../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../../../../node_modules/typescript/lib/lib.es2020.string.d.ts","../../../../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../../../../node_modules/typescript/lib/lib.es2020.intl.d.ts","../../../../node_modules/typescript/lib/lib.esnext.intl.d.ts","../../../../node_modules/typescript/lib/lib.es2017.full.d.ts","../../../../node_modules/@types/lodash/common/common.d.ts","../../../../node_modules/@types/lodash/common/array.d.ts","../../../../node_modules/@types/lodash/common/collection.d.ts","../../../../node_modules/@types/lodash/common/date.d.ts","../../../../node_modules/@types/lodash/common/function.d.ts","../../../../node_modules/@types/lodash/common/lang.d.ts","../../../../node_modules/@types/lodash/common/math.d.ts","../../../../node_modules/@types/lodash/common/number.d.ts","../../../../node_modules/@types/lodash/common/object.d.ts","../../../../node_modules/@types/lodash/common/seq.d.ts","../../../../node_modules/@types/lodash/common/string.d.ts","../../../../node_modules/@types/lodash/common/util.d.ts","../../../../node_modules/@types/lodash/index.d.ts","../../errors/index.ts","../src/errors.ts","../src/index.ts","../index.ts","../../../../node_modules/@types/babel-types/index.d.ts","../../../../node_modules/@types/babylon/index.d.ts","../../../../node_modules/@types/chai/index.d.ts","../../../../node_modules/@types/eslint/helpers.d.ts","../../../../node_modules/@types/eslint/lib/rules/index.d.ts","../../../../node_modules/@types/json-schema/index.d.ts","../../../../node_modules/@types/estree/index.d.ts","../../../../node_modules/@types/eslint/index.d.ts","../../../../node_modules/@types/eslint-scope/index.d.ts","../../../../node_modules/@types/node/assert.d.ts","../../../../node_modules/@types/node/assert/strict.d.ts","../../../../node_modules/@types/node/globals.d.ts","../../../../node_modules/@types/node/async_hooks.d.ts","../../../../node_modules/@types/node/buffer.d.ts","../../../../node_modules/@types/node/child_process.d.ts","../../../../node_modules/@types/node/cluster.d.ts","../../../../node_modules/@types/node/console.d.ts","../../../../node_modules/@types/node/constants.d.ts","../../../../node_modules/@types/node/crypto.d.ts","../../../../node_modules/@types/node/dgram.d.ts","../../../../node_modules/@types/node/diagnostics_channel.d.ts","../../../../node_modules/@types/node/dns.d.ts","../../../../node_modules/@types/node/dns/promises.d.ts","../../../../node_modules/@types/node/domain.d.ts","../../../../node_modules/@types/node/events.d.ts","../../../../node_modules/@types/node/fs.d.ts","../../../../node_modules/@types/node/fs/promises.d.ts","../../../../node_modules/@types/node/http.d.ts","../../../../node_modules/@types/node/http2.d.ts","../../../../node_modules/@types/node/https.d.ts","../../../../node_modules/@types/node/inspector.d.ts","../../../../node_modules/@types/node/module.d.ts","../../../../node_modules/@types/node/net.d.ts","../../../../node_modules/@types/node/os.d.ts","../../../../node_modules/@types/node/path.d.ts","../../../../node_modules/@types/node/perf_hooks.d.ts","../../../../node_modules/@types/node/process.d.ts","../../../../node_modules/@types/node/punycode.d.ts","../../../../node_modules/@types/node/querystring.d.ts","../../../../node_modules/@types/node/readline.d.ts","../../../../node_modules/@types/node/repl.d.ts","../../../../node_modules/@types/node/stream.d.ts","../../../../node_modules/@types/node/stream/promises.d.ts","../../../../node_modules/@types/node/stream/consumers.d.ts","../../../../node_modules/@types/node/stream/web.d.ts","../../../../node_modules/@types/node/string_decoder.d.ts","../../../../node_modules/@types/node/timers.d.ts","../../../../node_modules/@types/node/timers/promises.d.ts","../../../../node_modules/@types/node/tls.d.ts","../../../../node_modules/@types/node/trace_events.d.ts","../../../../node_modules/@types/node/tty.d.ts","../../../../node_modules/@types/node/url.d.ts","../../../../node_modules/@types/node/util.d.ts","../../../../node_modules/@types/node/v8.d.ts","../../../../node_modules/@types/node/vm.d.ts","../../../../node_modules/@types/node/wasi.d.ts","../../../../node_modules/@types/node/worker_threads.d.ts","../../../../node_modules/@types/node/zlib.d.ts","../../../../node_modules/@types/node/globals.global.d.ts","../../../../node_modules/@types/node/index.d.ts","../../../../node_modules/@types/minimatch/index.d.ts","../../../../node_modules/@types/glob/index.d.ts","../../../../node_modules/@types/minimist/index.d.ts","../../../../node_modules/@types/mocha/index.d.ts","../../../../node_modules/@types/normalize-package-data/index.d.ts","../../../../node_modules/@types/parse-json/index.d.ts"],"fileInfos":[{"version":"aa9fb4c70f369237c2f45f9d969c9a59e0eae9a192962eb48581fe864aa609db","affectsGlobalScope":true},"dc47c4fa66b9b9890cf076304de2a9c5201e94b740cffdf09f87296d877d71f6","7a387c58583dfca701b6c85e0adaf43fb17d590fb16d5b2dc0a2fbd89f35c467","8a12173c586e95f4433e0c6dc446bc88346be73ffe9ca6eec7aa63c8f3dca7f9","5f4e733ced4e129482ae2186aae29fde948ab7182844c3a5a51dd346182c7b06","e6b724280c694a9f588847f754198fb96c43d805f065c3a5b28bbc9594541c84","e21c071ca3e1b4a815d5f04a7475adcaeea5d64367e840dd0154096d705c3940",{"version":"e54c8715a4954cfdc66cd69489f2b725c09ebf37492dbd91cff0a1688b1159e8","affectsGlobalScope":true},{"version":"e34eb9339171ec45da2801c1967e4d378bd61a1dceaa1b1b4e1b6d28cb9ca962","affectsGlobalScope":true},{"version":"7fac8cb5fc820bc2a59ae11ef1c5b38d3832c6d0dfaec5acdb5569137d09a481","affectsGlobalScope":true},{"version":"097a57355ded99c68e6df1b738990448e0bf170e606707df5a7c0481ff2427cd","affectsGlobalScope":true},{"version":"51b8b27c21c066bf877646e320bf6a722b80d1ade65e686923cd9d4494aef1ca","affectsGlobalScope":true},{"version":"43fb1d932e4966a39a41b464a12a81899d9ae5f2c829063f5571b6b87e6d2f9c","affectsGlobalScope":true},{"version":"cdccba9a388c2ee3fd6ad4018c640a471a6c060e96f1232062223063b0a5ac6a","affectsGlobalScope":true},{"version":"2c8c5ee58f30e7c944e04ab1fb5506fdbb4dd507c9efa6972cf4b91cec90c503","affectsGlobalScope":true},{"version":"2bb4b3927299434052b37851a47bf5c39764f2ba88a888a107b32262e9292b7c","affectsGlobalScope":true},{"version":"810627a82ac06fb5166da5ada4159c4ec11978dfbb0805fe804c86406dab8357","affectsGlobalScope":true},{"version":"62d80405c46c3f4c527ee657ae9d43fda65a0bf582292429aea1e69144a522a6","affectsGlobalScope":true},{"version":"3013574108c36fd3aaca79764002b3717da09725a36a6fc02eac386593110f93","affectsGlobalScope":true},{"version":"75ec0bdd727d887f1b79ed6619412ea72ba3c81d92d0787ccb64bab18d261f14","affectsGlobalScope":true},{"version":"3be5a1453daa63e031d266bf342f3943603873d890ab8b9ada95e22389389006","affectsGlobalScope":true},{"version":"17bb1fc99591b00515502d264fa55dc8370c45c5298f4a5c2083557dccba5a2a","affectsGlobalScope":true},{"version":"7ce9f0bde3307ca1f944119f6365f2d776d281a393b576a18a2f2893a2d75c98","affectsGlobalScope":true},{"version":"6a6b173e739a6a99629a8594bfb294cc7329bfb7b227f12e1f7c11bc163b8577","affectsGlobalScope":true},{"version":"12a310447c5d23c7d0d5ca2af606e3bd08afda69100166730ab92c62999ebb9d","affectsGlobalScope":true},{"version":"b0124885ef82641903d232172577f2ceb5d3e60aed4da1153bab4221e1f6dd4e","affectsGlobalScope":true},{"version":"0eb85d6c590b0d577919a79e0084fa1744c1beba6fd0d4e951432fa1ede5510a","affectsGlobalScope":true},{"version":"da233fc1c8a377ba9e0bed690a73c290d843c2c3d23a7bd7ec5cd3d7d73ba1e0","affectsGlobalScope":true},{"version":"df9c8a72ca8b0ed62f5470b41208a0587f0f73f0a7db28e5a1272cf92537518e","affectsGlobalScope":true},{"version":"bb2d3fb05a1d2ffbca947cc7cbc95d23e1d053d6595391bd325deb265a18d36c","affectsGlobalScope":true},{"version":"c80df75850fea5caa2afe43b9949338ce4e2de086f91713e9af1a06f973872b8","affectsGlobalScope":true},{"version":"9d57b2b5d15838ed094aa9ff1299eecef40b190722eb619bac4616657a05f951","affectsGlobalScope":true},{"version":"6c51b5dd26a2c31dbf37f00cfc32b2aa6a92e19c995aefb5b97a3a64f1ac99de","affectsGlobalScope":true},{"version":"93544ca2f26a48716c1b6c5091842cad63129daac422dfa4bc52460465f22bb1","affectsGlobalScope":true},{"version":"2ad234885a4240522efccd77de6c7d99eecf9b4de0914adb9a35c0c22433f993","affectsGlobalScope":true},{"version":"1b3fe904465430e030c93239a348f05e1be80640d91f2f004c3512c2c2c89f34","affectsGlobalScope":true},{"version":"7435b75fdf3509622e79622dbe5091cf4b09688410ee2034e4fc17d0c99d0862","affectsGlobalScope":true},{"version":"e7e8e1d368290e9295ef18ca23f405cf40d5456fa9f20db6373a61ca45f75f40","affectsGlobalScope":true},{"version":"faf0221ae0465363c842ce6aa8a0cbda5d9296940a8e26c86e04cc4081eea21e","affectsGlobalScope":true},{"version":"06393d13ea207a1bfe08ec8d7be562549c5e2da8983f2ee074e00002629d1871","affectsGlobalScope":true},{"version":"9f1817f7c3f02f6d56e0f403b927e90bb133f371dcebc36fa7d6d208ef6899da","affectsGlobalScope":true},{"version":"4632665b87204bb1caa8b44d165bce0c50dfab177df5b561b345a567cabacf9a","affectsGlobalScope":true},"d2f31f19e1ba6ed59be9259d660a239d9a3fcbbc8e038c6b2009bde34b175fed","675e702f2032766a91eeadee64f51014c64688525da99dccd8178f0c599f13a8","378df8bbbb9e3f6fca05d58f644aab538e1062eab5e778fb0b83d41125df246d","0c75b204aed9cf6ff1c7b4bed87a3ece0d9d6fc857a6350c0c95ed0c38c814e8","187119ff4f9553676a884e296089e131e8cc01691c546273b1d0089c3533ce42","c24ad9be9adf28f0927e3d9d9e9cec1c677022356f241ccbbfb97bfe8fb3d1a1","0ec0998e2d085e8ea54266f547976ae152c9dd6cdb9ac4d8a520a230f5ebae84","9364c7566b0be2f7b70ff5285eb34686f83ccb01bda529b82d23b2a844653bfb","00baffbe8a2f2e4875367479489b5d43b5fc1429ecb4a4cc98cfc3009095f52a","82251920b05f30981c9a4109cb5f3169dce4b477effc845c6d781044a30e7672","3c92b6dfd43cc1c2485d9eba5ff0b74a19bb8725b692773ef1d66dac48cda4bd","3e59f00ab03c33717b3130066d4debb272da90eeded4935ff0604c2bc25a5cae","9fa6b83a35e897f340858995ca5d77e901d89fd18644cd4c9e8a4afe0b2e6363",{"version":"0714e2046df66c0e93c3330d30dbc0565b3e8cd3ee302cf99e4ede6220e5fec8","affectsGlobalScope":true},"8c0ff84150d92307ceb8ece5b64fde7168577c70637d723891967bf406188d92","0e2becb7a24e44ae138fe34d93c7a6583d738cd3347720b725e986e9c59a6d19","336ffe9ffdebc8f4a40a3b508f004867eeca90b36b379618db6395b8d7fba228","80a3bbe0e4fdfaac2c0a8b92a3ed66d3ded5e8f454c5790c8d593872fc1197f1","75925c52e539a150d822acf3d43f41075d6c371215be27e0c81215a79a802fb4","b76275b4a94b85da9e4b6b1f8c2b619b996560f9e6030a454260240210a39dd8",{"version":"f7834c76f2d4db5fb156f2f18fdf855431843cf0d32620fcf24b836cd4a85096","affectsGlobalScope":true},{"version":"64d4b35c5456adf258d2cf56c341e203a073253f229ef3208fc0d5020253b241","affectsGlobalScope":true},"0133ebdd17a823ae56861948870cde4dac18dd8818ab641039c85bbb720429e0","0359682c54e487c4cab2b53b2b4d35cc8dea4d9914bc6abcdb5701f8b8e745a4","7d7c8ef7d48a035142c07dae60545ecc0e4af4c337742760cb09726f2f8e31db","e300bf65972ac08167a72787e19d1b43c285c5424707194d0ba64422f6b02c77","82772e5d55062a042a2715a555d347275a663940926fc785705eb082010cb9f6","0d5a2ee1fdfa82740e0103389b9efd6bfe145a20018a2da3c02b89666181f4d9","a69c09dbea52352f479d3e7ac949fde3d17b195abe90b045d619f747b38d6d1a",{"version":"92d63add669d18ebc349efbacd88966d6f2ccdddfb1b880b2db98ae3aa7bf7c4","affectsGlobalScope":true},"ccc94049a9841fe47abe5baef6be9a38fc6228807974ae675fb15dc22531b4be",{"version":"658a70ff0b4d8298739566835c4b324a9ecef1676a2cd1fabfb5660a821d38ef","affectsGlobalScope":true},"43978f18d1165eea81040bc9bfac1a551717f5cc9bd0f13b31bf490c5fcdc75f","afc6e96061af46bcff47246158caee7e056f5288783f2d83d6858cd25be1c565",{"version":"c3ad91d23259b68e10a9bef08c5f9fa1d19f27ef740ac9af98ed226b030c09c6","affectsGlobalScope":true},"82408ed3e959ddc60d3e9904481b5a8dc16469928257af22a3f7d1a3bc7fd8c4","38643f040239c18923ef39b5e1fe2651eaa8a633d2e371d48ae7e951ec55a923","260aad3a6bd3fc510b7f97cfb05859bfc045ce185f8c2b4d73ddb9c43b0eb3c0","cb428529763c6c8e38e42db2a39f333ffcc6d3aab396b24ac84b22da752c1de0","ad4b60488fb1e562bf375dac9299815f7028bf667d9b5887b2d01d501b7d1ddd","246341c3a7a2638cf830d690e69de1e6085a102c6a30596435b050e6ac86c11a","6972fca26f6e9bd56197568d4379f99071a90766e06b4fcb5920a0130a9202be",{"version":"4a2628e95962c8ab756121faa3ac2ed348112ff7a87b5c286dd2cc3326546b4c","affectsGlobalScope":true},"ccded627c1c57aa9e021aea914c3197899e7335fdc65426a6d61a8e65e751c00","2fa7fb7be51fa9924ebef6bd61d290c91bfebb93de00fd4949d836e68668a9de","f2f0941f8d09218dfabd1b89ee9b4fb4a193568deb9b44c13a08ac5a386d81ae","b287b810b5035d5685f1df6e1e418f1ca452a3ed4f59fd5cc081dbf2045f0d9b","4b9a003b5c556c96784132945bb41c655ea11273b1917f5c8d0c154dd5fd20dd","62a00c9cc0c78d9f282dcd7b0a7776aefe220106c3bc327e259e5f6484c6f556",{"version":"e8b18c6385ff784228a6f369694fcf1a6b475355ba89090a88de13587a9391d5","affectsGlobalScope":true},"f3e8bcce378a26bc672fce0ec05affabbbbfa18493b76f24c39136dea87100d0","abc1c425b2ad6720433f40f1877abfa4223f0f3dd486c9c28c492179ca183cb6","cd4854d38f4eb5592afd98ab95ca17389a7dfe38013d9079e802d739bdbcc939","94eed4cc2f5f658d5e229ff1ccd38860bddf4233e347bf78edd2154dee1f2b99",{"version":"e51bee3200733b1f58818b5a9ea90fcd61c5b8afa3a0378391991f3696826a65","affectsGlobalScope":true},"9f1069b9e2c051737b1f9b4f1baf50e4a63385a6a89c32235549ae87fc3d5492","ee18f2da7a037c6ceeb112a084e485aead9ea166980bf433474559eac1b46553","e70339a3d63f806c43f24250c42aa0000093923457b0ed7dfc10e0ac910ebca9","0acbf26bf958f9e80c1ffa587b74749d2697b75b484062d36e103c137c562bc3","2c20a91799640fc6bf027056fd56e5b8db5c427c1cc016bdb04cde5c9a0603b9","1b952304137851e45bc009785de89ada562d9376177c97e37702e39e60c2f1ff",{"version":"806ef4cac3b3d9fa4a48d849c8e084d7c72fcd7b16d76e06049a9ed742ff79c0","affectsGlobalScope":true},"a279435e7813d1f061c0cab6ab77b1b9377e8d96851e5ed4a76a1ce6eb6e628f","c33a6ea7147af60d8e98f1ac127047f4b0d4e2ce28b8f08ff3de07ca7cc00637",{"version":"b42b47e17b8ece2424ae8039feb944c2e3ba4b262986aebd582e51efbdca93dc","affectsGlobalScope":true},"664d8f2d59164f2e08c543981453893bc7e003e4dfd29651ce09db13e9457980","2408611d9b4146e35d1dbd1f443ccd8e187c74614a54b80300728277529dbf11","998a3de5237518c0b3ac00a11b3b4417affb008aa20aedee52f3fdae3cb86151","3accc1301749459bba2d1ba0084fdf399e5471508300956a3a2a1a8f4bc35f2e","b810390059fc34122556c644f586e7a2b4598ded8afe5ba70bb82fc2e50577b1","ba9de5c5823e06ee3314f959c138cdaf4477d3a1a0769f0d24e62911020e8088","c3db860bcaaaeb3bbc23f353bbda1f8ab82756c8d5e973bebb3953cb09ea68f2","235a53595bd20b0b0eeb1a29cb2887c67c48375e92f03749b2488fbd46d0b1a0","bc09393cd4cd13f69cf1366d4236fbae5359bb550f0de4e15767e9a91d63dfb1","9c266243b01545e11d2733a55ad02b4c00ecdbda99c561cd1674f96e89cdc958","c71155c05fc76ff948a4759abc1cb9feec036509f500174bc18dad4c7827a60c",{"version":"ab9b9a36e5284fd8d3bf2f7d5fcbc60052f25f27e4d20954782099282c60d23e","affectsGlobalScope":true},"ba229259c8b01c6733dc6a3e7b2ae3e2b76e502bb640a1691238a4c9cae5cfed","8841e2aa774b89bd23302dede20663306dc1b9902431ac64b24be8b8d0e3f649","725b884357ba84171341a8e4cc08edf11417854fd069842ca6d22afb2e340e45","209e814e8e71aec74f69686a9506dd7610b97ab59dcee9446266446f72a76d05",{"version":"5f186a758a616c107c70e8918db4630d063bd782f22e6e0b17573b125765b40b","affectsGlobalScope":true},"6fa0008bf91a4cc9c8963bace4bba0bd6865cbfa29c3e3ccc461155660fb113a","2b8264b2fefd7367e0f20e2c04eed5d3038831fe00f5efbc110ff0131aab899b"],"options":{"allowSyntheticDefaultImports":true,"declaration":false,"emitDecoratorMetadata":true,"experimentalDecorators":true,"jsx":2,"module":1,"outDir":"./","removeComments":true,"sourceMap":true,"target":4},"fileIdsList":[[113],[61,113],[67,68,113],[64,65,66,67,113],[68,113],[85,113,120,121],[44,46,47,48,49,50,51,52,53,54,55,56,113],[44,45,47,48,49,50,51,52,53,54,55,56,113],[45,46,47,48,49,50,51,52,53,54,55,56,113],[44,45,46,48,49,50,51,52,53,54,55,56,113],[44,45,46,47,49,50,51,52,53,54,55,56,113],[44,45,46,47,48,50,51,52,53,54,55,56,113],[44,45,46,47,48,49,51,52,53,54,55,56,113],[44,45,46,47,48,49,50,52,53,54,55,56,113],[44,45,46,47,48,49,50,51,53,54,55,56,113],[44,45,46,47,48,49,50,51,52,54,55,56,113],[44,45,46,47,48,49,50,51,52,53,55,56,113],[44,45,46,47,48,49,50,51,52,53,54,56,113],[44,45,46,47,48,49,50,51,52,53,54,55,113],[70,113],[73,113],[74,79,113],[75,85,86,93,102,112,113],[75,76,85,93,113],[77,113],[78,79,86,94,113],[79,102,109,113],[80,82,85,93,113],[81,113],[82,83,113],[84,85,113],[85,113],[85,86,87,102,112,113],[85,86,87,102,113],[88,93,102,112,113],[85,86,88,89,93,102,109,112,113],[88,90,102,109,112,113],[70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119],[85,91,113],[92,112,113],[82,85,93,102,113],[94,113],[95,113],[73,96,113],[97,111,113,117],[98,113],[99,113],[85,100,113],[100,101,113,115],[85,102,103,104,113],[102,104,113],[102,103,113],[105,113],[106,113],[85,107,108,113],[107,108,113],[79,93,109,113],[110,113],[93,111,113],[74,88,99,112,113],[79,113],[102,113,114],[113,115],[113,116],[74,79,85,87,96,102,112,113,115,117],[102,113,118],[59,113],[56,57,113],[56,58,113]],"referencedMap":[[61,1],[62,2],[63,1],[69,3],[64,1],[68,4],[65,5],[67,1],[122,6],[66,1],[45,7],[46,8],[44,9],[47,10],[48,11],[49,12],[50,13],[51,14],[52,15],[53,16],[54,17],[55,18],[56,19],[121,1],[123,1],[124,1],[70,20],[71,20],[73,21],[74,22],[75,23],[76,24],[77,25],[78,26],[79,27],[80,28],[81,29],[82,30],[83,30],[84,31],[85,32],[86,33],[87,34],[72,1],[119,1],[88,35],[89,36],[90,37],[120,38],[91,39],[92,40],[93,41],[94,42],[95,43],[96,44],[97,45],[98,46],[99,47],[100,48],[101,49],[102,50],[104,51],[103,52],[105,53],[106,54],[107,55],[108,56],[109,57],[110,58],[111,59],[112,60],[113,61],[114,62],[115,63],[116,64],[117,65],[118,66],[125,1],[126,1],[8,1],[9,1],[13,1],[12,1],[2,1],[14,1],[15,1],[16,1],[17,1],[18,1],[19,1],[20,1],[21,1],[3,1],[4,1],[43,1],[25,1],[22,1],[23,1],[24,1],[26,1],[27,1],[28,1],[5,1],[29,1],[30,1],[31,1],[32,1],[6,1],[33,1],[34,1],[35,1],[36,1],[7,1],[41,1],[37,1],[38,1],[39,1],[40,1],[1,1],[42,1],[11,1],[10,1],[60,67],[58,68],[59,69],[57,1]],"exportedModulesMap":[[61,1],[62,2],[63,1],[69,3],[64,1],[68,4],[65,5],[67,1],[122,6],[66,1],[45,7],[46,8],[44,9],[47,10],[48,11],[49,12],[50,13],[51,14],[52,15],[53,16],[54,17],[55,18],[56,19],[121,1],[123,1],[124,1],[70,20],[71,20],[73,21],[74,22],[75,23],[76,24],[77,25],[78,26],[79,27],[80,28],[81,29],[82,30],[83,30],[84,31],[85,32],[86,33],[87,34],[72,1],[119,1],[88,35],[89,36],[90,37],[120,38],[91,39],[92,40],[93,41],[94,42],[95,43],[96,44],[97,45],[98,46],[99,47],[100,48],[101,49],[102,50],[104,51],[103,52],[105,53],[106,54],[107,55],[108,56],[109,57],[110,58],[111,59],[112,60],[113,61],[114,62],[115,63],[116,64],[117,65],[118,66],[125,1],[126,1],[8,1],[9,1],[13,1],[12,1],[2,1],[14,1],[15,1],[16,1],[17,1],[18,1],[19,1],[20,1],[21,1],[3,1],[4,1],[43,1],[25,1],[22,1],[23,1],[24,1],[26,1],[27,1],[28,1],[5,1],[29,1],[30,1],[31,1],[32,1],[6,1],[33,1],[34,1],[35,1],[36,1],[7,1],[41,1],[37,1],[38,1],[39,1],[40,1],[1,1],[42,1],[11,1],[10,1],[60,67],[58,68],[59,69],[57,1]],"semanticDiagnosticsPerFile":[61,62,63,69,64,68,65,67,122,66,45,46,44,47,48,49,50,51,52,53,54,55,56,121,123,124,70,71,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,72,119,88,89,90,120,91,92,93,94,95,96,97,98,99,100,101,102,104,103,105,106,107,108,109,110,111,112,113,114,115,116,117,118,125,126,8,9,13,12,2,14,15,16,17,18,19,20,21,3,4,43,25,22,23,24,26,27,28,5,29,30,31,32,6,33,34,35,36,7,41,37,38,39,40,1,42,11,10,60,58,59,57]},"version":"4.4.2"}
|
package/index.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './src';
|
package/interfaces.ts
ADDED
File without changes
|
package/package.json
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"name": "@biorate/config",
|
3
|
+
"version": "0.10.0",
|
4
|
+
"description": "Application configurator",
|
5
|
+
"main": "dist",
|
6
|
+
"scripts": {
|
7
|
+
"build": "npx tsc -p ./tsconfig.build.json --outDir ./dist",
|
8
|
+
"test": "npx nyc --reporter=lcov --reporter=text-summary -- npx mocha -r ts-node/register tests/**/*.spec.ts",
|
9
|
+
"prepublishOnly": "npm run build"
|
10
|
+
},
|
11
|
+
"keywords": [],
|
12
|
+
"author": "llevkin",
|
13
|
+
"license": "MIT",
|
14
|
+
"dependencies": {
|
15
|
+
"@biorate/errors": "0.10.0",
|
16
|
+
"lodash": "^4.17.21",
|
17
|
+
"traverse": "^0.6.6"
|
18
|
+
}
|
19
|
+
}
|
package/src/errors.ts
ADDED
package/src/index.ts
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
import { merge, get, has, set, PropertyPath, PropertyName } from 'lodash';
|
2
|
+
import * as traverse from 'traverse';
|
3
|
+
import { UndefinedConfigPathError } from './errors';
|
4
|
+
|
5
|
+
export class Config {
|
6
|
+
protected data = {};
|
7
|
+
|
8
|
+
/**
|
9
|
+
* @example
|
10
|
+
* ```
|
11
|
+
* import { Config } from '@biorate/config';
|
12
|
+
*
|
13
|
+
* const config = new Config({ a: { b: { c: 1 } } });
|
14
|
+
*
|
15
|
+
* config.get('a'); // { b: { c: 1 } }
|
16
|
+
* ```
|
17
|
+
* */
|
18
|
+
public constructor(data?: Record<PropertyName, unknown>) {
|
19
|
+
if (data) this.merge(data);
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
* @description Set template value
|
24
|
+
* */
|
25
|
+
protected template(value: string) {
|
26
|
+
let res, result;
|
27
|
+
const regExp = /\${([^}{]+)+?}/g;
|
28
|
+
result = value;
|
29
|
+
while ((res = regExp.exec(value)))
|
30
|
+
if (this.has(res[1])) result = result.replace(res[0], this.get(res[1]));
|
31
|
+
return result;
|
32
|
+
}
|
33
|
+
|
34
|
+
/**
|
35
|
+
* @description Walk object recursive to templatize internal values
|
36
|
+
* */
|
37
|
+
protected templatize(object: unknown) {
|
38
|
+
const template = this.template.bind(this);
|
39
|
+
return traverse(object).forEach(function (value) {
|
40
|
+
if (typeof value === 'string') this.update(template(value));
|
41
|
+
});
|
42
|
+
}
|
43
|
+
|
44
|
+
/**
|
45
|
+
* @description Get config property by path
|
46
|
+
* @param path - data path
|
47
|
+
* @param def - default value
|
48
|
+
* @example
|
49
|
+
* ```
|
50
|
+
* import { Config } from '@biorate/config';
|
51
|
+
*
|
52
|
+
* const config = new Config();
|
53
|
+
*
|
54
|
+
* config.set('a', 1);
|
55
|
+
*
|
56
|
+
* console.log(config.get('a')); // 1
|
57
|
+
* console.log(config.get('b', 2)); // 2
|
58
|
+
* console.log(config.get('b')); // UndefinedConfigPathError: Undefined config path [b]
|
59
|
+
* // at Config.get (src/index.ts:2:1608)
|
60
|
+
* // at Context.<anonymous> (tests/index.spec.ts:19:24)
|
61
|
+
* // at processImmediate (node:internal/timers:464:21)
|
62
|
+
*
|
63
|
+
* ```
|
64
|
+
* */
|
65
|
+
public get(path: PropertyPath, def?: unknown) {
|
66
|
+
if (!this.has(path)) {
|
67
|
+
if (def === undefined) throw new UndefinedConfigPathError(path);
|
68
|
+
return def;
|
69
|
+
}
|
70
|
+
const result = get(this.data, path);
|
71
|
+
switch (typeof result) {
|
72
|
+
case 'string':
|
73
|
+
return this.template(result);
|
74
|
+
case 'object':
|
75
|
+
return result ? this.templatize(result) : result;
|
76
|
+
default:
|
77
|
+
return result;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
/**
|
82
|
+
* @description Has config property by path
|
83
|
+
* @param path - data path
|
84
|
+
* @example
|
85
|
+
* ```
|
86
|
+
* import { Config } from '@biorate/config';
|
87
|
+
*
|
88
|
+
* const config = new Config();
|
89
|
+
*
|
90
|
+
* config.set('a', 1);
|
91
|
+
*
|
92
|
+
* console.log(config.has('a')); // true
|
93
|
+
* ```
|
94
|
+
* */
|
95
|
+
public has(path: PropertyPath) {
|
96
|
+
return has(this.data, path);
|
97
|
+
}
|
98
|
+
|
99
|
+
/**
|
100
|
+
* @description Set config property by path
|
101
|
+
* @param path - data path
|
102
|
+
* @param value - data value
|
103
|
+
* @example
|
104
|
+
* ```
|
105
|
+
* import { Config } from '@biorate/config';
|
106
|
+
*
|
107
|
+
* const config = new Config();
|
108
|
+
*
|
109
|
+
* config.set('a', 1);
|
110
|
+
*
|
111
|
+
* console.log(config.get('a')); // 1
|
112
|
+
* ```
|
113
|
+
* */
|
114
|
+
public set(path: PropertyPath, value: unknown): void {
|
115
|
+
set(this.data, path, value);
|
116
|
+
}
|
117
|
+
|
118
|
+
/**
|
119
|
+
* @description Merge config data
|
120
|
+
* @param data - data object
|
121
|
+
* @example
|
122
|
+
* ```
|
123
|
+
* import { Config } from '@biorate/config';
|
124
|
+
*
|
125
|
+
* const config = new Config();
|
126
|
+
*
|
127
|
+
* config.merge({
|
128
|
+
* a: { b: { c: 1 } },
|
129
|
+
* });
|
130
|
+
*
|
131
|
+
* config.merge({
|
132
|
+
* a: { b: { d: 2 } },
|
133
|
+
* });
|
134
|
+
*
|
135
|
+
* console.log(config.has('a')); // true
|
136
|
+
* console.log(config.has('a.b')); // true
|
137
|
+
* console.log(config.get('a.b.c')); // 1
|
138
|
+
* console.log(config.get('a.b.d')); // 2
|
139
|
+
* ```
|
140
|
+
* */
|
141
|
+
public merge(data: unknown): void {
|
142
|
+
merge(this.data, data);
|
143
|
+
}
|
144
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import { assert } from 'chai';
|
2
|
+
import { config, data } from './__mocks__';
|
3
|
+
import { Config } from '../';
|
4
|
+
|
5
|
+
describe('@biorate/config', () => {
|
6
|
+
it('get', () => assert.equal(config.get('two.one'), data.two.one));
|
7
|
+
|
8
|
+
it('get default', () => assert.equal(config.get('inexistent-property', 'default'), 'default'));
|
9
|
+
|
10
|
+
it('get inexistent', () => assert.throw(() => config.get('inexistent-property')));
|
11
|
+
|
12
|
+
it('has', () => assert(config.has('two.one')));
|
13
|
+
|
14
|
+
it('has no', () => assert(!config.has('two.three')));
|
15
|
+
|
16
|
+
it('set', () => {
|
17
|
+
const value = 1;
|
18
|
+
config.set('three.one', value);
|
19
|
+
assert(config.has('three.one'));
|
20
|
+
assert.equal(config.get('three.one'), value);
|
21
|
+
});
|
22
|
+
|
23
|
+
it('merge', () => {
|
24
|
+
const value = { a: { b: { c: true } } };
|
25
|
+
config.merge(value);
|
26
|
+
assert(config.has('a.b.c'));
|
27
|
+
assert.equal(config.get('a.b.c'), value.a.b.c);
|
28
|
+
});
|
29
|
+
});
|