@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/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
@@ -1,8 +0,0 @@
1
- import { PropertyPath } from 'lodash';
2
-
3
- export interface IConfig {
4
- get<T = unknown>(path: PropertyPath, def?: T): T;
5
- has(path: PropertyPath): boolean;
6
- set(path: PropertyPath, value: unknown): void;
7
- merge(data: unknown): void;
8
- }