zcb 0.2.5 → 0.2.6
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/README.md +14 -2
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/cjs/createConfigReader.d.cts +4 -1
- package/dist/types/cjs/createConfigReader.d.cts.map +1 -1
- package/dist/types/esm/createConfigReader.d.ts +4 -1
- package/dist/types/esm/createConfigReader.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/__testUtils__/builtConfig.ts +1 -0
- package/src/createConfigReader.test.ts +28 -0
- package/src/createConfigReader.ts +23 -3
package/package.json
CHANGED
|
@@ -17,6 +17,34 @@ describe('createConfigReader', () => {
|
|
|
17
17
|
});
|
|
18
18
|
});
|
|
19
19
|
|
|
20
|
+
describe('when vars options are passed in', () => {
|
|
21
|
+
const vars = {
|
|
22
|
+
name: 'Simon',
|
|
23
|
+
profession: 'pieman',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
describe('when a user accesses a property that is not a string', () => {
|
|
27
|
+
it('should throw the correct error', () => {
|
|
28
|
+
const reader = createReader();
|
|
29
|
+
|
|
30
|
+
// @ts-expect-error path does not resovle to a string
|
|
31
|
+
expect(() => reader.read('pages.contactDetails', { vars })).toThrow(
|
|
32
|
+
new Error(
|
|
33
|
+
'config reader received variables to use in string template, but the path did not resolve to a string.'
|
|
34
|
+
)
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('when a user accesses a property that is a string', () => {
|
|
40
|
+
it('should return the correct string value with template placeholders populated', () => {
|
|
41
|
+
const reader = createReader();
|
|
42
|
+
const value = reader.read('templateString', { vars });
|
|
43
|
+
expect(value).toBe('Simple Simon met a pieman going to the fair');
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
20
48
|
describe('when the reader is scoped', () => {
|
|
21
49
|
describe('when a user accesses a known property', () => {
|
|
22
50
|
it('should return the correct value', () => {
|
|
@@ -1,15 +1,35 @@
|
|
|
1
|
-
import { get } from 'lodash-es';
|
|
1
|
+
import { get, isString } from 'lodash-es';
|
|
2
2
|
import { type Get } from 'type-fest';
|
|
3
3
|
import { type Path, type Scope } from './types.ts';
|
|
4
4
|
|
|
5
|
+
export type ReadOptions = {
|
|
6
|
+
vars?: Record<string, string | number>;
|
|
7
|
+
};
|
|
8
|
+
|
|
5
9
|
export interface ConfigReader<Config extends object> {
|
|
6
|
-
read: <P extends Path<Config>>(path: P) => Get<Config, P>;
|
|
10
|
+
read: <P extends Path<Config>>(path: P, options?: ReadOptions) => Get<Config, P>;
|
|
7
11
|
scope: <S extends Scope<Config>>(scope: S) => ConfigReader<Get<Config, S>>;
|
|
8
12
|
}
|
|
9
13
|
|
|
10
14
|
export const createConfigReader = <Config extends object>(config: Config): ConfigReader<Config> => {
|
|
11
15
|
const configReader = {
|
|
12
|
-
read: <P extends Path<Config>>(path: P) =>
|
|
16
|
+
read: <P extends Path<Config>>(path: P, { vars }: ReadOptions = {}) => {
|
|
17
|
+
if (!vars) {
|
|
18
|
+
return get(config, path);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const output = get(config, path);
|
|
22
|
+
|
|
23
|
+
if (!isString(output)) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
'config reader received variables to use in string template, but the path did not resolve to a string.'
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return Object.keys(vars).reduce<string>((acc, key) => {
|
|
30
|
+
return acc.replace(new RegExp(`{{${key}}}`), String(vars[key]));
|
|
31
|
+
}, output);
|
|
32
|
+
},
|
|
13
33
|
scope: <S extends Scope<Config>>(scope: S) => createConfigReader(get(config, scope) as Get<Config, S>),
|
|
14
34
|
} as ConfigReader<Config>;
|
|
15
35
|
|