@biorate/config 0.27.0 → 0.30.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.nyc_output/df57ca4e-1145-47e5-832e-77eb09008648.json +1 -0
- package/.nyc_output/processinfo/df57ca4e-1145-47e5-832e-77eb09008648.json +1 -0
- package/.nyc_output/processinfo/index.json +1 -0
- package/CHANGELOG.md +38 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/config/index.html +116 -0
- package/coverage/lcov-report/config/index.ts.html +88 -0
- package/coverage/lcov-report/config/src/errors.ts.html +109 -0
- package/coverage/lcov-report/config/src/index.html +131 -0
- package/coverage/lcov-report/config/src/index.ts.html +499 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +89 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/index.ts +0 -1
- package/src/errors.ts +0 -8
- package/src/index.ts +0 -138
- package/src/interfaces.ts +0 -8
package/src/index.ts
DELETED
@@ -1,138 +0,0 @@
|
|
1
|
-
import { injectable } from '@biorate/inversion';
|
2
|
-
import { merge, get, has, set, PropertyPath, PropertyName } from 'lodash';
|
3
|
-
import * as traverse from 'traverse';
|
4
|
-
import { UndefinedConfigPathError } from './errors';
|
5
|
-
import { IConfig } from './interfaces';
|
6
|
-
export * from './errors';
|
7
|
-
export * from './interfaces';
|
8
|
-
|
9
|
-
@injectable()
|
10
|
-
export class Config implements IConfig {
|
11
|
-
/**
|
12
|
-
* @description Data storage
|
13
|
-
* */
|
14
|
-
protected data = {};
|
15
|
-
|
16
|
-
/**
|
17
|
-
* @description Set template value
|
18
|
-
* */
|
19
|
-
protected template(value: string) {
|
20
|
-
let res, result;
|
21
|
-
const regExp = /\${([^}{]+)+?}/g;
|
22
|
-
result = value;
|
23
|
-
while ((res = regExp.exec(value)))
|
24
|
-
if (this.has(res[1])) result = result.replace(res[0], this.get(res[1]));
|
25
|
-
return result;
|
26
|
-
}
|
27
|
-
|
28
|
-
/**
|
29
|
-
* @description Walk object recursive to templatize internal values
|
30
|
-
* */
|
31
|
-
protected templatize(object: unknown) {
|
32
|
-
const template = this.template.bind(this);
|
33
|
-
return traverse(object).forEach(function (value) {
|
34
|
-
if (typeof value === 'string') this.update(template(value));
|
35
|
-
});
|
36
|
-
}
|
37
|
-
|
38
|
-
/**
|
39
|
-
* @description Get config property by path
|
40
|
-
* @param path - data path
|
41
|
-
* @param def - default value
|
42
|
-
* @example
|
43
|
-
* ```
|
44
|
-
* import { Config } from '@biorate/config';
|
45
|
-
*
|
46
|
-
* const config = new Config();
|
47
|
-
*
|
48
|
-
* config.set('a', 1);
|
49
|
-
*
|
50
|
-
* console.log(config.get('a')); // 1
|
51
|
-
* console.log(config.get('b', 2)); // 2
|
52
|
-
* console.log(config.get('b')); // UndefinedConfigPathError: Undefined config path [b]
|
53
|
-
* // at Config.get (src/index.ts:2:1608)
|
54
|
-
* // at Context.<anonymous> (tests/index.spec.ts:19:24)
|
55
|
-
* // at processImmediate (node:internal/timers:464:21)
|
56
|
-
*
|
57
|
-
* ```
|
58
|
-
* */
|
59
|
-
public get<T = unknown>(path: PropertyPath, def?: T): T {
|
60
|
-
if (!this.has(path)) {
|
61
|
-
if (def === undefined) throw new UndefinedConfigPathError(path);
|
62
|
-
return def;
|
63
|
-
}
|
64
|
-
const result = get(this.data, path);
|
65
|
-
switch (typeof result) {
|
66
|
-
case 'string':
|
67
|
-
return this.template(result);
|
68
|
-
case 'object':
|
69
|
-
return result ? this.templatize(result) : result;
|
70
|
-
default:
|
71
|
-
return result;
|
72
|
-
}
|
73
|
-
}
|
74
|
-
|
75
|
-
/**
|
76
|
-
* @description Has config property by path
|
77
|
-
* @param path - data path
|
78
|
-
* @example
|
79
|
-
* ```
|
80
|
-
* import { Config } from '@biorate/config';
|
81
|
-
*
|
82
|
-
* const config = new Config();
|
83
|
-
*
|
84
|
-
* config.set('a', 1);
|
85
|
-
*
|
86
|
-
* console.log(config.has('a')); // true
|
87
|
-
* ```
|
88
|
-
* */
|
89
|
-
public has(path: PropertyPath) {
|
90
|
-
return has(this.data, path);
|
91
|
-
}
|
92
|
-
|
93
|
-
/**
|
94
|
-
* @description Set config property by path
|
95
|
-
* @param path - data path
|
96
|
-
* @param value - data value
|
97
|
-
* @example
|
98
|
-
* ```
|
99
|
-
* import { Config } from '@biorate/config';
|
100
|
-
*
|
101
|
-
* const config = new Config();
|
102
|
-
*
|
103
|
-
* config.set('a', 1);
|
104
|
-
*
|
105
|
-
* console.log(config.get('a')); // 1
|
106
|
-
* ```
|
107
|
-
* */
|
108
|
-
public set(path: PropertyPath, value: unknown) {
|
109
|
-
set(this.data, path, value);
|
110
|
-
}
|
111
|
-
|
112
|
-
/**
|
113
|
-
* @description Merge config data
|
114
|
-
* @param data - data object
|
115
|
-
* @example
|
116
|
-
* ```
|
117
|
-
* import { Config } from '@biorate/config';
|
118
|
-
*
|
119
|
-
* const config = new Config();
|
120
|
-
*
|
121
|
-
* config.merge({
|
122
|
-
* a: { b: { c: 1 } },
|
123
|
-
* });
|
124
|
-
*
|
125
|
-
* config.merge({
|
126
|
-
* a: { b: { d: 2 } },
|
127
|
-
* });
|
128
|
-
*
|
129
|
-
* console.log(config.has('a')); // true
|
130
|
-
* console.log(config.has('a.b')); // true
|
131
|
-
* console.log(config.get('a.b.c')); // 1
|
132
|
-
* console.log(config.get('a.b.d')); // 2
|
133
|
-
* ```
|
134
|
-
* */
|
135
|
-
public merge(data: unknown) {
|
136
|
-
merge(this.data, data);
|
137
|
-
}
|
138
|
-
}
|
package/src/interfaces.ts
DELETED