Skip to content

Commit e7a4f59

Browse files
committed
feat(#115): adds $eq directive
1 parent 808a5c9 commit e7a4f59

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed

app-config-default-extensions/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
unescape$Directives,
1818
tryDirective,
1919
ifDirective,
20+
eqDirective,
2021
envDirective,
2122
extendsDirective,
2223
extendsSelfDirective,
@@ -33,6 +34,7 @@ module.exports = {
3334
unescape$Directives(),
3435
tryDirective(),
3536
ifDirective(),
37+
eqDirective(),
3638
v1Compat(),
3739
envDirective(aliases, environmentOverride, environmentSourceNames),
3840
extendsDirective(),
@@ -54,6 +56,7 @@ module.exports = {
5456
unescape$Directives,
5557
tryDirective,
5658
ifDirective,
59+
eqDirective,
5760
extendsDirective,
5861
extendsSelfDirective,
5962
overrideDirective,
@@ -63,6 +66,7 @@ module.exports = {
6366
unescape$Directives(),
6467
tryDirective(),
6568
ifDirective(),
69+
eqDirective(),
6670
extendsDirective(),
6771
extendsSelfDirective(),
6872
overrideDirective(),

app-config-extensions/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
"@app-config/extension-utils": "^2.1.7",
3535
"@app-config/logging": "^2.1.7",
3636
"@app-config/node": "^2.1.7",
37-
"@app-config/utils": "^2.1.7"
37+
"@app-config/utils": "^2.1.7",
38+
"lodash.isequal": "4"
3839
},
3940
"devDependencies": {
40-
"@app-config/test-utils": "^2.1.7"
41+
"@app-config/test-utils": "^2.1.7",
42+
"@types/lodash.isequal": "4"
4143
},
4244
"prettier": "@lcdev/prettier",
4345
"jest": {

app-config-extensions/src/index.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { forKey } from '@app-config/extension-utils';
55
import {
66
tryDirective,
77
ifDirective,
8+
eqDirective,
89
envDirective,
910
extendsDirective,
1011
extendsSelfDirective,
@@ -176,6 +177,60 @@ describe('$if directive', () => {
176177
});
177178
});
178179

180+
describe('$eq directive', () => {
181+
it('returns true for empty', async () => {
182+
const source = new LiteralSource({
183+
$eq: [],
184+
});
185+
186+
expect(await source.readToJSON([eqDirective()])).toBe(true);
187+
});
188+
189+
it('returns true for two numbers', async () => {
190+
const source = new LiteralSource({
191+
$eq: [42, 42],
192+
});
193+
194+
expect(await source.readToJSON([eqDirective()])).toBe(true);
195+
});
196+
197+
it('returns false for two numbers', async () => {
198+
const source = new LiteralSource({
199+
$eq: [42, 44],
200+
});
201+
202+
expect(await source.readToJSON([eqDirective()])).toBe(false);
203+
});
204+
205+
it('returns true for two objects', async () => {
206+
const source = new LiteralSource({
207+
$eq: [{ a: true }, { a: true }],
208+
});
209+
210+
expect(await source.readToJSON([eqDirective()])).toBe(true);
211+
});
212+
213+
it('returns false for two objects', async () => {
214+
const source = new LiteralSource({
215+
$eq: [{ a: true }, { b: true }],
216+
});
217+
218+
expect(await source.readToJSON([eqDirective()])).toBe(false);
219+
});
220+
221+
it('parses before checking equality', async () => {
222+
process.env.APP_CONFIG_ENV = 'test';
223+
const source = new LiteralSource({
224+
$eq: [
225+
{ $env: { default: { a: true } }},
226+
{ $env: { test: { a: true } }},
227+
],
228+
});
229+
230+
expect(await source.readToJSON([eqDirective(), envDirective()])).toBe(true);
231+
});
232+
});
233+
179234
describe('$extends directive', () => {
180235
it('fails if file is missing', async () => {
181236
const source = new LiteralSource({

app-config-extensions/src/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { join, dirname, resolve, isAbsolute } from 'path';
2+
import isEqual from 'lodash.isequal';
23
import {
34
forKey,
45
validateOptions,
@@ -95,6 +96,28 @@ export function ifDirective(): ParsingExtension {
9596
);
9697
}
9798

99+
/** Checks if two values are equal */
100+
export function eqDirective(): ParsingExtension {
101+
return forKey(
102+
'$eq',
103+
validateOptions(
104+
(SchemaBuilder) => SchemaBuilder.arraySchema(SchemaBuilder.fromJsonSchema({})),
105+
(values) => async (parse) => {
106+
for (const a of values) {
107+
for (const b of values) {
108+
if (a === b) continue;
109+
if (isEqual(a, b)) continue;
110+
111+
return parse(false, { shouldFlatten: true });
112+
}
113+
}
114+
115+
return parse(true, { shouldFlatten: true });
116+
},
117+
),
118+
);
119+
}
120+
98121
/** Uses another file as overriding values, layering them on top of current file */
99122
export function overrideDirective(): ParsingExtension {
100123
return fileReferenceDirective('$override', { shouldOverride: true });

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,6 +2898,13 @@
28982898
"@types/node" "*"
28992899
"@types/webpack" "*"
29002900

2901+
"@types/lodash.isequal@4":
2902+
version "4.5.5"
2903+
resolved "https://registry.yarnpkg.com/@types/lodash.isequal/-/lodash.isequal-4.5.5.tgz#4fed1b1b00bef79e305de0352d797e9bb816c8ff"
2904+
integrity sha512-4IKbinG7MGP131wRfceK6W4E/Qt3qssEFLF30LnJbjYiSfHGGRU/Io8YxXrZX109ir+iDETC8hw8QsDijukUVg==
2905+
dependencies:
2906+
"@types/lodash" "*"
2907+
29012908
"@types/lodash.merge@4":
29022909
version "4.6.6"
29032910
resolved "https://registry.yarnpkg.com/@types/lodash.merge/-/lodash.merge-4.6.6.tgz#b84b403c1d31bc42d51772d1cd5557fa008cd3d6"
@@ -11202,6 +11209,11 @@ lodash.get@^4.4.2:
1120211209
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
1120311210
integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=
1120411211

11212+
lodash.isequal@4:
11213+
version "4.5.0"
11214+
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
11215+
integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=
11216+
1120511217
lodash.ismatch@^4.4.0:
1120611218
version "4.4.0"
1120711219
resolved "https://registry.yarnpkg.com/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz#756cb5150ca3ba6f11085a78849645f188f85f37"

0 commit comments

Comments
 (0)