@featurevisor/core 3.0.1 → 3.2.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.
Files changed (208) hide show
  1. package/lib/datasource/datasource.d.ts +1 -1
  2. package/lib/datasource/datasource.js +4 -14
  3. package/lib/datasource/datasource.js.map +1 -1
  4. package/lib/datasource/requiredFeatures.d.ts +7 -0
  5. package/lib/datasource/requiredFeatures.js +38 -0
  6. package/lib/datasource/requiredFeatures.js.map +1 -0
  7. package/lib/linter/checkCircularDependency.d.ts +1 -1
  8. package/lib/linter/checkCircularDependency.js +3 -22
  9. package/lib/linter/checkCircularDependency.js.map +1 -1
  10. package/lib/linter/conditionSchema.js +27 -3
  11. package/lib/linter/conditionSchema.js.map +1 -1
  12. package/lib/tester/testFeature.js +17 -16
  13. package/lib/tester/testFeature.js.map +1 -1
  14. package/package.json +10 -7
  15. package/CHANGELOG.md +0 -2238
  16. package/coverage/clover.xml +0 -5456
  17. package/coverage/coverage-final.json +0 -64
  18. package/coverage/lcov-report/base.css +0 -224
  19. package/coverage/lcov-report/block-navigation.js +0 -87
  20. package/coverage/lcov-report/favicon.png +0 -0
  21. package/coverage/lcov-report/index.html +0 -371
  22. package/coverage/lcov-report/prettify.css +0 -1
  23. package/coverage/lcov-report/prettify.js +0 -2
  24. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  25. package/coverage/lcov-report/sorter.js +0 -210
  26. package/coverage/lcov-report/src/assess-distribution/index.html +0 -116
  27. package/coverage/lcov-report/src/assess-distribution/index.ts.html +0 -754
  28. package/coverage/lcov-report/src/benchmark/index.html +0 -116
  29. package/coverage/lcov-report/src/benchmark/index.ts.html +0 -793
  30. package/coverage/lcov-report/src/builder/allocator.ts.html +0 -220
  31. package/coverage/lcov-report/src/builder/applyContextToConditions.ts.html +0 -502
  32. package/coverage/lcov-report/src/builder/applyContextToDatafile.ts.html +0 -772
  33. package/coverage/lcov-report/src/builder/applyContextToSegments.ts.html +0 -580
  34. package/coverage/lcov-report/src/builder/buildDatafile.ts.html +0 -2053
  35. package/coverage/lcov-report/src/builder/buildProject.ts.html +0 -1117
  36. package/coverage/lcov-report/src/builder/buildRuntimeDatafiles.ts.html +0 -256
  37. package/coverage/lcov-report/src/builder/getFeatureRanges.ts.html +0 -268
  38. package/coverage/lcov-report/src/builder/hashes.ts.html +0 -412
  39. package/coverage/lcov-report/src/builder/index.html +0 -311
  40. package/coverage/lcov-report/src/builder/index.ts.html +0 -91
  41. package/coverage/lcov-report/src/builder/mutateVariables.ts.html +0 -421
  42. package/coverage/lcov-report/src/builder/mutator.ts.html +0 -796
  43. package/coverage/lcov-report/src/builder/revision.ts.html +0 -151
  44. package/coverage/lcov-report/src/builder/traffic.ts.html +0 -661
  45. package/coverage/lcov-report/src/cli/cli.ts.html +0 -400
  46. package/coverage/lcov-report/src/cli/index.html +0 -131
  47. package/coverage/lcov-report/src/cli/plugins.ts.html +0 -214
  48. package/coverage/lcov-report/src/config/index.html +0 -131
  49. package/coverage/lcov-report/src/config/index.ts.html +0 -88
  50. package/coverage/lcov-report/src/config/projectConfig.ts.html +0 -925
  51. package/coverage/lcov-report/src/datasource/adapter.ts.html +0 -253
  52. package/coverage/lcov-report/src/datasource/datasource.ts.html +0 -1000
  53. package/coverage/lcov-report/src/datasource/filesystemAdapter.ts.html +0 -1576
  54. package/coverage/lcov-report/src/datasource/index.html +0 -161
  55. package/coverage/lcov-report/src/datasource/index.ts.html +0 -94
  56. package/coverage/lcov-report/src/evaluate/index.html +0 -116
  57. package/coverage/lcov-report/src/evaluate/index.ts.html +0 -814
  58. package/coverage/lcov-report/src/find-duplicate-segments/findDuplicateSegments.ts.html +0 -313
  59. package/coverage/lcov-report/src/find-duplicate-segments/index.html +0 -131
  60. package/coverage/lcov-report/src/find-duplicate-segments/index.ts.html +0 -307
  61. package/coverage/lcov-report/src/find-usage/index.html +0 -116
  62. package/coverage/lcov-report/src/find-usage/index.ts.html +0 -1780
  63. package/coverage/lcov-report/src/generate-code/index.html +0 -131
  64. package/coverage/lcov-report/src/generate-code/index.ts.html +0 -403
  65. package/coverage/lcov-report/src/generate-code/typescript.ts.html +0 -1909
  66. package/coverage/lcov-report/src/index.html +0 -131
  67. package/coverage/lcov-report/src/info/index.html +0 -116
  68. package/coverage/lcov-report/src/info/index.ts.html +0 -472
  69. package/coverage/lcov-report/src/init/index.html +0 -116
  70. package/coverage/lcov-report/src/init/index.ts.html +0 -280
  71. package/coverage/lcov-report/src/linter/attributeSchema.ts.html +0 -1963
  72. package/coverage/lcov-report/src/linter/checkCircularDependency.ts.html +0 -220
  73. package/coverage/lcov-report/src/linter/checkPercentageExceedingSlot.ts.html +0 -268
  74. package/coverage/lcov-report/src/linter/conditionSchema.ts.html +0 -2224
  75. package/coverage/lcov-report/src/linter/featureSchema.ts.html +0 -5851
  76. package/coverage/lcov-report/src/linter/groupSchema.ts.html +0 -226
  77. package/coverage/lcov-report/src/linter/index.html +0 -326
  78. package/coverage/lcov-report/src/linter/index.ts.html +0 -88
  79. package/coverage/lcov-report/src/linter/lintProject.ts.html +0 -2704
  80. package/coverage/lcov-report/src/linter/mutationNotation.ts.html +0 -1411
  81. package/coverage/lcov-report/src/linter/printError.ts.html +0 -289
  82. package/coverage/lcov-report/src/linter/schema.ts.html +0 -1558
  83. package/coverage/lcov-report/src/linter/segmentSchema.ts.html +0 -139
  84. package/coverage/lcov-report/src/linter/targetSchema.ts.html +0 -223
  85. package/coverage/lcov-report/src/linter/testSchema.ts.html +0 -529
  86. package/coverage/lcov-report/src/linter/zodHelpers.ts.html +0 -133
  87. package/coverage/lcov-report/src/list/index.html +0 -131
  88. package/coverage/lcov-report/src/list/index.ts.html +0 -2065
  89. package/coverage/lcov-report/src/list/matrix.ts.html +0 -721
  90. package/coverage/lcov-report/src/promoter/index.html +0 -116
  91. package/coverage/lcov-report/src/promoter/index.ts.html +0 -3883
  92. package/coverage/lcov-report/src/sets.ts.html +0 -313
  93. package/coverage/lcov-report/src/targeting.ts.html +0 -403
  94. package/coverage/lcov-report/src/tester/cliFormat.ts.html +0 -133
  95. package/coverage/lcov-report/src/tester/helpers.ts.html +0 -313
  96. package/coverage/lcov-report/src/tester/index.html +0 -206
  97. package/coverage/lcov-report/src/tester/prettyDuration.ts.html +0 -187
  98. package/coverage/lcov-report/src/tester/printTestResult.ts.html +0 -337
  99. package/coverage/lcov-report/src/tester/testFeature.ts.html +0 -1186
  100. package/coverage/lcov-report/src/tester/testProject.ts.html +0 -1018
  101. package/coverage/lcov-report/src/tester/testSegment.ts.html +0 -316
  102. package/coverage/lcov-report/src/utils/extractKeys.ts.html +0 -493
  103. package/coverage/lcov-report/src/utils/git.ts.html +0 -559
  104. package/coverage/lcov-report/src/utils/index.html +0 -161
  105. package/coverage/lcov-report/src/utils/index.ts.html +0 -94
  106. package/coverage/lcov-report/src/utils/pretty.ts.html +0 -121
  107. package/coverage/lcov.info +0 -11615
  108. package/jest.config.js +0 -22
  109. package/json-schema/README.md +0 -5
  110. package/json-schema/attribute.json +0 -40
  111. package/json-schema/feature.json +0 -394
  112. package/json-schema/segment.json +0 -137
  113. package/src/assess-distribution/index.ts +0 -223
  114. package/src/benchmark/index.ts +0 -236
  115. package/src/builder/allocator.spec.ts +0 -105
  116. package/src/builder/allocator.ts +0 -45
  117. package/src/builder/applyContextToConditions.spec.ts +0 -2677
  118. package/src/builder/applyContextToConditions.ts +0 -139
  119. package/src/builder/applyContextToDatafile.spec.ts +0 -2626
  120. package/src/builder/applyContextToDatafile.ts +0 -229
  121. package/src/builder/applyContextToSegments.spec.ts +0 -1593
  122. package/src/builder/applyContextToSegments.ts +0 -165
  123. package/src/builder/buildDatafile.spec.ts +0 -445
  124. package/src/builder/buildDatafile.ts +0 -656
  125. package/src/builder/buildProject.ts +0 -344
  126. package/src/builder/buildRuntimeDatafiles.ts +0 -57
  127. package/src/builder/getFeatureRanges.ts +0 -61
  128. package/src/builder/hashes.ts +0 -109
  129. package/src/builder/index.ts +0 -2
  130. package/src/builder/mutateVariables.spec.ts +0 -1179
  131. package/src/builder/mutateVariables.ts +0 -112
  132. package/src/builder/mutator.spec.ts +0 -413
  133. package/src/builder/mutator.ts +0 -237
  134. package/src/builder/revision.spec.ts +0 -22
  135. package/src/builder/revision.ts +0 -22
  136. package/src/builder/traffic.spec.ts +0 -946
  137. package/src/builder/traffic.ts +0 -192
  138. package/src/cli/cli.ts +0 -105
  139. package/src/cli/index.ts +0 -2
  140. package/src/cli/plugins.ts +0 -43
  141. package/src/config/index.ts +0 -1
  142. package/src/config/projectConfig.spec.ts +0 -194
  143. package/src/config/projectConfig.ts +0 -280
  144. package/src/datasource/adapter.ts +0 -56
  145. package/src/datasource/datasource.ts +0 -305
  146. package/src/datasource/filesystemAdapter.spec.ts +0 -212
  147. package/src/datasource/filesystemAdapter.ts +0 -497
  148. package/src/datasource/index.ts +0 -3
  149. package/src/dependencies.ts +0 -13
  150. package/src/evaluate/index.ts +0 -243
  151. package/src/find-duplicate-segments/findDuplicateSegments.ts +0 -76
  152. package/src/find-duplicate-segments/index.ts +0 -74
  153. package/src/find-usage/index.ts +0 -565
  154. package/src/generate-code/index.ts +0 -106
  155. package/src/generate-code/typescript.spec.ts +0 -214
  156. package/src/generate-code/typescript.ts +0 -608
  157. package/src/index.ts +0 -20
  158. package/src/info/index.ts +0 -129
  159. package/src/init/index.ts +0 -65
  160. package/src/linter/attributeSchema.spec.ts +0 -342
  161. package/src/linter/attributeSchema.ts +0 -626
  162. package/src/linter/checkCircularDependency.ts +0 -45
  163. package/src/linter/checkPercentageExceedingSlot.ts +0 -61
  164. package/src/linter/conditionSchema.spec.ts +0 -510
  165. package/src/linter/conditionSchema.ts +0 -713
  166. package/src/linter/featureSchema.spec.ts +0 -1985
  167. package/src/linter/featureSchema.ts +0 -1922
  168. package/src/linter/groupSchema.ts +0 -47
  169. package/src/linter/index.ts +0 -1
  170. package/src/linter/lintProject.spec.ts +0 -336
  171. package/src/linter/lintProject.ts +0 -873
  172. package/src/linter/mutationNotation.spec.ts +0 -704
  173. package/src/linter/mutationNotation.ts +0 -442
  174. package/src/linter/printError.ts +0 -68
  175. package/src/linter/schema.spec.ts +0 -690
  176. package/src/linter/schema.ts +0 -491
  177. package/src/linter/segmentSchema.spec.ts +0 -358
  178. package/src/linter/segmentSchema.ts +0 -18
  179. package/src/linter/targetSchema.spec.ts +0 -159
  180. package/src/linter/targetSchema.ts +0 -46
  181. package/src/linter/testSchema.spec.ts +0 -258
  182. package/src/linter/testSchema.ts +0 -148
  183. package/src/linter/zodHelpers.ts +0 -16
  184. package/src/list/index.spec.ts +0 -135
  185. package/src/list/index.ts +0 -660
  186. package/src/list/matrix.spec.ts +0 -49
  187. package/src/list/matrix.ts +0 -212
  188. package/src/promoter/index.spec.ts +0 -617
  189. package/src/promoter/index.ts +0 -1266
  190. package/src/sets.ts +0 -76
  191. package/src/targeting.spec.ts +0 -74
  192. package/src/targeting.ts +0 -106
  193. package/src/tester/cliFormat.ts +0 -16
  194. package/src/tester/helpers.spec.ts +0 -149
  195. package/src/tester/helpers.ts +0 -76
  196. package/src/tester/index.ts +0 -2
  197. package/src/tester/prettyDuration.ts +0 -34
  198. package/src/tester/printTestResult.ts +0 -84
  199. package/src/tester/testFeature.ts +0 -367
  200. package/src/tester/testProject.spec.ts +0 -50
  201. package/src/tester/testProject.ts +0 -311
  202. package/src/tester/testSegment.ts +0 -77
  203. package/src/utils/extractKeys.ts +0 -136
  204. package/src/utils/git.ts +0 -158
  205. package/src/utils/index.ts +0 -3
  206. package/src/utils/pretty.ts +0 -12
  207. package/tsconfig.cjs.json +0 -13
  208. package/tsconfig.typecheck.json +0 -9
@@ -1,112 +0,0 @@
1
- import type { VariableSchema, VariableValue } from "@featurevisor/types";
2
- import { mutate } from "./mutator";
3
-
4
- const MUTATION_OP_SUFFIX = /:((?:append|prepend|after|before|remove))$/;
5
-
6
- /**
7
- * Get the root variable name from an override key (e.g. "tags:append" -> "tags", "payload.rows:append" -> "payload").
8
- */
9
- function rootVariableFromOverrideKey(overrideKey: string): string {
10
- const withoutSuffix = overrideKey.replace(MUTATION_OP_SUFFIX, "").trim();
11
- const firstSegment = withoutSuffix.includes(".") ? withoutSuffix.split(".")[0] : withoutSuffix;
12
- return firstSegment.replace(/\s*\[.*\]\s*$/, "").trim();
13
- }
14
-
15
- /**
16
- * Resolve variable values from schema defaults and overrides.
17
- * Override keys may be variable keys or dot-notation paths (e.g. "foo", "foo.a.b", "tags:append", "items[id=2]:after").
18
- * Uses the mutator so nested paths and mutation notations are supported.
19
- * Returns only variables that were desired to be overridden (i.e. appear in overrides).
20
- */
21
- export function resolveMutationsForMultipleVariables(
22
- variablesSchema: Record<string, VariableSchema> | undefined,
23
- overrides: Record<string, VariableValue> | undefined,
24
- ): Record<string, VariableValue> | undefined {
25
- if (!overrides || Object.keys(overrides).length === 0) {
26
- return undefined;
27
- }
28
- if (!variablesSchema || Object.keys(variablesSchema).length === 0) {
29
- return undefined;
30
- }
31
-
32
- const variableKeysToOutput = new Set<string>();
33
- for (const overrideKey of Object.keys(overrides)) {
34
- const variableKey = rootVariableFromOverrideKey(overrideKey);
35
- if (variableKey && variablesSchema[variableKey]) {
36
- variableKeysToOutput.add(variableKey);
37
- }
38
- }
39
-
40
- const result: Record<string, VariableValue> = {};
41
-
42
- for (const variableKey of variableKeysToOutput) {
43
- const schema = variablesSchema[variableKey];
44
- let value: VariableValue =
45
- schema.defaultValue !== undefined && schema.defaultValue !== null
46
- ? (JSON.parse(JSON.stringify(schema.defaultValue)) as VariableValue)
47
- : undefined;
48
-
49
- const keysForThisVariable = Object.keys(overrides)
50
- .filter(
51
- (k) =>
52
- rootVariableFromOverrideKey(k) === variableKey &&
53
- (k === variableKey ||
54
- k.startsWith(variableKey + ".") ||
55
- k.startsWith(variableKey + "[") ||
56
- k.startsWith(variableKey + ":")),
57
- )
58
- .sort((a, b) => a.length - b.length);
59
-
60
- for (const overrideKey of keysForThisVariable) {
61
- const overrideValue = overrides[overrideKey];
62
- if (overrideKey === variableKey) {
63
- value =
64
- overrideValue !== undefined && overrideValue !== null
65
- ? (JSON.parse(JSON.stringify(overrideValue)) as VariableValue)
66
- : overrideValue;
67
- } else {
68
- const notation = overrideKey.startsWith(variableKey + "[")
69
- ? overrideKey.slice(variableKey.length)
70
- : overrideKey.startsWith(variableKey + ":")
71
- ? overrideKey.slice(variableKey.length)
72
- : overrideKey.slice(variableKey.length + 1);
73
- value = mutate(schema, value, notation, overrideValue);
74
- }
75
- }
76
-
77
- result[variableKey] = value;
78
- }
79
-
80
- return Object.keys(result).length > 0 ? result : undefined;
81
- }
82
-
83
- /**
84
- * Resolve a single variable's override value (e.g. from variableOverrides).
85
- * If the value is a plain object with path-like keys, it is merged with the variable's default;
86
- * otherwise the value is returned as-is (full replacement).
87
- */
88
- export function resolveMutationsForSingleVariable(
89
- variablesSchema: Record<string, VariableSchema> | undefined,
90
- variableKey: string,
91
- overrideValue: VariableValue,
92
- baseValue?: VariableValue,
93
- ): VariableValue {
94
- if (!variablesSchema || !variablesSchema[variableKey]) return overrideValue;
95
- if (overrideValue === null || overrideValue === undefined) return overrideValue;
96
- if (typeof overrideValue !== "object" || Array.isArray(overrideValue)) {
97
- return overrideValue;
98
- }
99
-
100
- const pathMap = overrideValue as Record<string, VariableValue>;
101
- const flat: Record<string, VariableValue> = {};
102
-
103
- if (typeof baseValue !== "undefined") {
104
- flat[variableKey] = JSON.parse(JSON.stringify(baseValue)) as VariableValue;
105
- }
106
-
107
- for (const [k, v] of Object.entries(pathMap)) {
108
- flat[k === variableKey ? variableKey : variableKey + "." + k] = v;
109
- }
110
- const resolved = resolveMutationsForMultipleVariables(variablesSchema, flat);
111
- return resolved && variableKey in resolved ? resolved[variableKey] : overrideValue;
112
- }
@@ -1,413 +0,0 @@
1
- import type { VariableSchema, VariableValue } from "@featurevisor/types";
2
- import { mutate } from "./mutator";
3
-
4
- const schema: VariableSchema = {
5
- type: "object",
6
- defaultValue: {},
7
- };
8
-
9
- describe("mutator", function () {
10
- describe("mutate", function () {
11
- test("is a function", function () {
12
- expect(mutate).toBeInstanceOf(Function);
13
- });
14
-
15
- test("returns a deep clone and does not mutate original value", function () {
16
- const value = { a: 1, b: { c: 2 } };
17
- const result = mutate(schema, value, "a", 99);
18
- expect(result).toEqual({ a: 99, b: { c: 2 } });
19
- expect(value).toEqual({ a: 1, b: { c: 2 } });
20
- });
21
-
22
- test("returns value unchanged when notation is empty or only whitespace", function () {
23
- const value = { a: 1 };
24
- expect(mutate(schema, value, "", undefined)).toEqual({ a: 1 });
25
- expect(mutate(schema, value, " ", undefined)).toEqual({ a: 1 });
26
- });
27
-
28
- test("returns null when value is null", function () {
29
- expect(mutate(schema, null, "key", 1)).toBeNull();
30
- });
31
-
32
- test("returns undefined when value is undefined", function () {
33
- expect(mutate(schema, undefined, "key", 1)).toBeUndefined();
34
- });
35
- });
36
-
37
- describe("notation: key (set value at key)", function () {
38
- test("sets top-level key", function () {
39
- const value = { a: 1, b: 2 };
40
- expect(mutate(schema, value, "a", 10)).toEqual({ a: 10, b: 2 });
41
- expect(mutate(schema, value, "c", 3)).toEqual({ a: 1, b: 2, c: 3 });
42
- });
43
-
44
- test("sets key to undefined (allows clearing)", function () {
45
- const value = { a: 1, b: 2 };
46
- expect(mutate(schema, value, "a", undefined)).toEqual({ a: undefined, b: 2 });
47
- });
48
-
49
- test("sets key with string, number, boolean, object, array", function () {
50
- const value = {};
51
- expect(mutate(schema, value, "s", "hello")).toEqual({ s: "hello" });
52
- expect(mutate(schema, value, "n", 42)).toEqual({ n: 42 });
53
- expect(mutate(schema, value, "b", true)).toEqual({ b: true });
54
- expect(mutate(schema, value, "o", { x: 1 })).toEqual({ o: { x: 1 } });
55
- expect(mutate(schema, value, "arr", [1, 2])).toEqual({ arr: [1, 2] });
56
- });
57
- });
58
-
59
- describe("notation: nested.key (set value at nested path)", function () {
60
- test("sets nested key one level deep", function () {
61
- const value = { a: { b: 1 } };
62
- expect(mutate(schema, value, "a.b", 2)).toEqual({ a: { b: 2 } });
63
- });
64
-
65
- test("sets nested key multiple levels deep", function () {
66
- const value = { a: { b: { c: 1 } } };
67
- expect(mutate(schema, value, "a.b.c", 99)).toEqual({ a: { b: { c: 99 } } });
68
- });
69
-
70
- test("creates intermediate objects when path does not exist (no-op per current spec: returns result)", function () {
71
- const value = { a: 1 };
72
- const result = mutate(schema, value, "x.y.z", 10);
73
- expect(result).toEqual({ a: 1 });
74
- });
75
-
76
- test("sets when intermediate path exists", function () {
77
- const value = { level1: { level2: { level3: "old" } } };
78
- expect(mutate(schema, value, "level1.level2.level3", "new")).toEqual({
79
- level1: { level2: { level3: "new" } },
80
- });
81
- });
82
- });
83
-
84
- describe("notation: key:append", function () {
85
- test("appends element to array", function () {
86
- const value = { items: [1, 2, 3] };
87
- expect(mutate(schema, value, "items:append", 4)).toEqual({
88
- items: [1, 2, 3, 4],
89
- });
90
- });
91
-
92
- test("appends to empty array", function () {
93
- const value = { items: [] };
94
- expect(mutate(schema, value, "items:append", "a")).toEqual({ items: ["a"] });
95
- });
96
-
97
- test("creates array when key is missing and appends", function () {
98
- const value: VariableValue = {};
99
- expect(mutate(schema, value, "items:append", 1)).toEqual({ items: [1] });
100
- });
101
-
102
- test("root-level array: notation :append appends to array value", function () {
103
- const arraySchema: VariableSchema = { type: "array", defaultValue: [] };
104
- expect(mutate(arraySchema, ["a", "b"], ":append", "c")).toEqual(["a", "b", "c"]);
105
- });
106
-
107
- test("appends object to array", function () {
108
- const value = { list: [{ id: 1 }] };
109
- expect(mutate(schema, value, "list:append", { id: 2, name: "b" })).toEqual({
110
- list: [{ id: 1 }, { id: 2, name: "b" }],
111
- });
112
- });
113
- });
114
-
115
- describe("notation: key:prepend", function () {
116
- test("prepends element to array", function () {
117
- const value = { items: [1, 2, 3] };
118
- expect(mutate(schema, value, "items:prepend", 0)).toEqual({
119
- items: [0, 1, 2, 3],
120
- });
121
- });
122
-
123
- test("prepends to empty array", function () {
124
- const value = { items: [] };
125
- expect(mutate(schema, value, "items:prepend", "first")).toEqual({
126
- items: ["first"],
127
- });
128
- });
129
-
130
- test("creates array when key is missing and prepends", function () {
131
- const value: VariableValue = {};
132
- expect(mutate(schema, value, "items:prepend", "x")).toEqual({ items: ["x"] });
133
- });
134
-
135
- test("root-level array: notation :prepend prepends to array value", function () {
136
- const arraySchema: VariableSchema = { type: "array", defaultValue: [] };
137
- expect(mutate(arraySchema, ["b", "c"], ":prepend", "a")).toEqual(["a", "b", "c"]);
138
- });
139
- });
140
-
141
- describe("notation: key[id=123]:after", function () {
142
- test("inserts element after matching item by selector", function () {
143
- const value = {
144
- items: [
145
- { id: "1", name: "a" },
146
- { id: "123", name: "b" },
147
- { id: "3", name: "c" },
148
- ],
149
- };
150
- const result = mutate(schema, value, "items[id=123]:after", {
151
- id: "2",
152
- name: "inserted",
153
- });
154
- expect(result).toEqual({
155
- items: [
156
- { id: "1", name: "a" },
157
- { id: "123", name: "b" },
158
- { id: "2", name: "inserted" },
159
- { id: "3", name: "c" },
160
- ],
161
- });
162
- });
163
-
164
- test("inserts after when id is numeric in data", function () {
165
- const value = {
166
- list: [
167
- { id: 123, name: "a" },
168
- { id: 456, name: "b" },
169
- ],
170
- };
171
- const result = mutate(schema, value, "list[id=123]:after", {
172
- id: 124,
173
- name: "after 123",
174
- });
175
- expect(result).toEqual({
176
- list: [
177
- { id: 123, name: "a" },
178
- { id: 124, name: "after 123" },
179
- { id: 456, name: "b" },
180
- ],
181
- });
182
- });
183
-
184
- test("no-op when no element matches selector", function () {
185
- const value = { items: [{ id: "1" }] };
186
- const result = mutate(schema, value, "items[id=999]:after", { id: "new" });
187
- expect(result).toEqual({ items: [{ id: "1" }] });
188
- });
189
- });
190
-
191
- describe("notation: key[id=123]:before", function () {
192
- test("inserts element before matching item by selector", function () {
193
- const value = {
194
- items: [
195
- { id: "1", name: "a" },
196
- { id: "123", name: "b" },
197
- { id: "3", name: "c" },
198
- ],
199
- };
200
- const result = mutate(schema, value, "items[id=123]:before", {
201
- id: "0",
202
- name: "inserted",
203
- });
204
- expect(result).toEqual({
205
- items: [
206
- { id: "1", name: "a" },
207
- { id: "0", name: "inserted" },
208
- { id: "123", name: "b" },
209
- { id: "3", name: "c" },
210
- ],
211
- });
212
- });
213
-
214
- test("inserts before first element when first matches", function () {
215
- const value = { list: [{ id: 1 }, { id: 2 }] };
216
- const result = mutate(schema, value, "list[id=1]:before", { id: 0 });
217
- expect(result).toEqual({
218
- list: [{ id: 0 }, { id: 1 }, { id: 2 }],
219
- });
220
- });
221
- });
222
-
223
- describe("notation: key[n] (set value at index)", function () {
224
- test("sets element at array index", function () {
225
- const value = { items: [10, 20, 30] };
226
- expect(mutate(schema, value, "items[0]", 1)).toEqual({
227
- items: [1, 20, 30],
228
- });
229
- expect(mutate(schema, value, "items[1]", 200)).toEqual({
230
- items: [10, 200, 30],
231
- });
232
- expect(mutate(schema, value, "items[2]", 300)).toEqual({
233
- items: [10, 20, 300],
234
- });
235
- });
236
-
237
- test("sets object at index", function () {
238
- const value = { list: [{ id: 1 }, { id: 2 }] };
239
- expect(mutate(schema, value, "list[0]", { id: 10 })).toEqual({
240
- list: [{ id: 10 }, { id: 2 }],
241
- });
242
- });
243
-
244
- test("root-level array: notation [n] sets element at index", function () {
245
- const arraySchema: VariableSchema = { type: "array", defaultValue: [] };
246
- const value = [10, 20, 30];
247
- expect(mutate(arraySchema, value, "[0]", 1)).toEqual([1, 20, 30]);
248
- expect(mutate(arraySchema, value, "[1]", 99)).toEqual([10, 99, 30]);
249
- expect(mutate(arraySchema, [100], "[0]", 200)).toEqual([200]);
250
- });
251
- });
252
-
253
- describe("notation: key[n].key (set property of item at index)", function () {
254
- test("sets property of element at index", function () {
255
- const value = {
256
- items: [
257
- { id: 1, name: "a" },
258
- { id: 2, name: "b" },
259
- ],
260
- };
261
- expect(mutate(schema, value, "items[0].name", "A")).toEqual({
262
- items: [
263
- { id: 1, name: "A" },
264
- { id: 2, name: "b" },
265
- ],
266
- });
267
- expect(mutate(schema, value, "items[1].id", 20)).toEqual({
268
- items: [
269
- { id: 1, name: "a" },
270
- { id: 20, name: "b" },
271
- ],
272
- });
273
- });
274
-
275
- test("sets nested property of element at index", function () {
276
- const value = {
277
- list: [{ meta: { count: 1 } }],
278
- };
279
- expect(mutate(schema, value, "list[0].meta.count", 5)).toEqual({
280
- list: [{ meta: { count: 5 } }],
281
- });
282
- });
283
- });
284
-
285
- describe("notation: key[n]:remove", function () {
286
- test("removes element at index", function () {
287
- const value = { items: [1, 2, 3] };
288
- expect(mutate(schema, value, "items[1]:remove", undefined)).toEqual({
289
- items: [1, 3],
290
- });
291
- });
292
-
293
- test("removes first element", function () {
294
- const value = { items: ["a", "b", "c"] };
295
- expect(mutate(schema, value, "items[0]:remove", undefined)).toEqual({
296
- items: ["b", "c"],
297
- });
298
- });
299
-
300
- test("removes last element", function () {
301
- const value = { items: ["a", "b", "c"] };
302
- expect(mutate(schema, value, "items[2]:remove", undefined)).toEqual({
303
- items: ["a", "b"],
304
- });
305
- });
306
-
307
- test("removes only element", function () {
308
- const value = { items: [1] };
309
- expect(mutate(schema, value, "items[0]:remove", undefined)).toEqual({
310
- items: [],
311
- });
312
- });
313
- });
314
-
315
- describe("notation: key[id=123]:remove", function () {
316
- test("removes element matching selector", function () {
317
- const value = {
318
- items: [
319
- { id: "1", name: "a" },
320
- { id: "123", name: "b" },
321
- { id: "3", name: "c" },
322
- ],
323
- };
324
- expect(mutate(schema, value, "items[id=123]:remove", undefined)).toEqual({
325
- items: [
326
- { id: "1", name: "a" },
327
- { id: "3", name: "c" },
328
- ],
329
- });
330
- });
331
-
332
- test("removes by numeric id", function () {
333
- const value = { list: [{ id: 123 }, { id: 456 }] };
334
- expect(mutate(schema, value, "list[id=123]:remove", undefined)).toEqual({
335
- list: [{ id: 456 }],
336
- });
337
- });
338
-
339
- test("no-op when no element matches", function () {
340
- const value = { items: [{ id: "1" }] };
341
- expect(mutate(schema, value, "items[id=999]:remove", undefined)).toEqual({
342
- items: [{ id: "1" }],
343
- });
344
- });
345
- });
346
-
347
- describe("notation: key:remove", function () {
348
- test("removes top-level property", function () {
349
- const value = { a: 1, b: 2, c: 3 };
350
- expect(mutate(schema, value, "b:remove", undefined)).toEqual({
351
- a: 1,
352
- c: 3,
353
- });
354
- });
355
-
356
- test("removes nested property", function () {
357
- const value = { a: { b: 1, c: 2 } };
358
- expect(mutate(schema, value, "a.c:remove", undefined)).toEqual({
359
- a: { b: 1 },
360
- });
361
- });
362
-
363
- test("removes when value is undefined (key no longer present)", function () {
364
- const value = { x: "present" };
365
- const result = mutate(schema, value, "x:remove", undefined);
366
- expect(result).toEqual({});
367
- expect(Object.prototype.hasOwnProperty.call(result, "x")).toBe(false);
368
- });
369
- });
370
-
371
- describe("edge cases and combined paths", function () {
372
- test("key with brackets in selector (prop=value)", function () {
373
- const value = { list: [{ id: "a=1", name: "first" }] };
374
- const result = mutate(schema, value, "list[id=a=1].name", "updated");
375
- expect(result).toEqual({ list: [{ id: "a=1", name: "updated" }] });
376
- });
377
-
378
- test("array of primitives: set and remove by index", function () {
379
- const value = { nums: [1, 2, 3] };
380
- expect(mutate(schema, value, "nums[1]", 20)).toEqual({ nums: [1, 20, 3] });
381
- expect(mutate(schema, value, "nums[2]:remove", undefined)).toEqual({
382
- nums: [1, 2],
383
- });
384
- });
385
-
386
- test("deep path: a.b.c.d", function () {
387
- const value = { a: { b: { c: { d: "old" } } } };
388
- expect(mutate(schema, value, "a.b.c.d", "new")).toEqual({
389
- a: { b: { c: { d: "new" } } },
390
- });
391
- });
392
-
393
- test("selector with different property name", function () {
394
- const value = {
395
- users: [
396
- { userId: "u1", name: "Alice" },
397
- { userId: "u2", name: "Bob" },
398
- ],
399
- };
400
- expect(mutate(schema, value, "users[userId=u2]:remove", undefined)).toEqual({
401
- users: [{ userId: "u1", name: "Alice" }],
402
- });
403
- });
404
-
405
- test("append and prepend in sequence (each from fresh clone)", function () {
406
- const value = { arr: [2] };
407
- const afterAppend = mutate(schema, value, "arr:append", 3);
408
- expect(afterAppend).toEqual({ arr: [2, 3] });
409
- const afterPrepend = mutate(schema, value, "arr:prepend", 1);
410
- expect(afterPrepend).toEqual({ arr: [1, 2] });
411
- });
412
- });
413
- });