assign-gingerly 0.0.60 → 0.0.61

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 CHANGED
@@ -300,7 +300,7 @@ In real-world use cases, you often need to replace one object with another of a
300
300
 
301
301
  **Exception: classes with `static assignTo`**
302
302
 
303
- If the current value is an instance of a class that defines [`static assignTo`](#custom-assignment-with-static-assignto-protocol), that method is called instead of replacing. This allows classes to opt into custom assignment behavior (e.g., reactive models, validated records, iterable collections with private lists):
303
+ If the current value is an instance of a class that defines `static assignTo`, that method is called instead of replacing. This allows classes to opt into custom assignment behavior (e.g., reactive models, validated records, iterable collections with private lists). See the full [Custom Assignment with `static assignTo` Protocol](#custom-assignment-with-static-assignto-protocol) section below for details, examples, and the method signature.
304
304
 
305
305
  ```TypeScript
306
306
  class TodoList {
@@ -4237,6 +4237,42 @@ get: {
4237
4237
 
4238
4238
  For full details, see [docs/manage-template-list.md](docs/manage-template-list.md).
4239
4239
 
4240
+ ### Built-in handler: `builtIns.rangeSelector`
4241
+
4242
+ Evaluates a value against a series of range conditions and merges the matched case's properties into the target. Converts imperative if/else-if chains into declarative JSON configs.
4243
+
4244
+ ```JavaScript
4245
+ assignFrom(element, {
4246
+ '?. =>': {
4247
+ do: 'builtIns.rangeSelector',
4248
+ get: {
4249
+ value: '?.count',
4250
+ when: [
4251
+ { '<=': 10, merge: { status: 'low', statusMessage: 'Low count' } },
4252
+ { '<': 20, merge: { status: 'medium', statusMessage: 'Medium count' } },
4253
+ { merge: { status: 'high', statusMessage: 'High count!' } }
4254
+ ]
4255
+ }
4256
+ }
4257
+ }, { from: vm });
4258
+ ```
4259
+
4260
+ **How it works:**
4261
+
4262
+ 1. Resolves `value` from the source (e.g., `vm.count`)
4263
+ 2. Iterates `when` cases in order — first match wins
4264
+ 3. Each case can have operator keys (`<=`, `<`, `>=`, `>`, `===`, `!==`) as conditions
4265
+ 4. Multiple operators per case = AND logic (e.g., `{ '>=': 10, '<': 20, merge: {...} }`)
4266
+ 5. No operator keys = default/catch-all
4267
+ 6. Merges the matched case's `merge` object into the target via `assignGingerly`
4268
+
4269
+ **Supported operators:** `<=`, `<`, `>=`, `>`, `===`, `!==`
4270
+
4271
+ **Notes:**
4272
+ - Comparison uses JavaScript semantics (`false < true`, strings compare lexicographically)
4273
+ - Fully JSON-serializable — no functions, no special types
4274
+ - Lazy-loaded on demand like all built-in handlers
4275
+
4240
4276
  ## Typed Path Authoring with `paths`, `sp`, and `md`
4241
4277
 
4242
4278
  For JSON generated config files generated from TypeScript/`.mts`/`mjs` files during a build or server-side rendering, the `paths` utility provides compile-time autocomplete and type safety for `?.`-prefixed path strings. The `sp` tagged template literal ("split into parts") produces arrays suitable for `builtIns.join`. The `md` tagged template literal produces `{prop, val}` objects suitable for `builtIns.microDataJoin`.
@@ -4595,7 +4631,7 @@ assignGingerly(shadowRoot, {
4595
4631
 
4596
4632
  ## Custom Assignment with `static assignTo` Protocol
4597
4633
 
4598
- Classes can opt into custom assignment behavior by defining a `static assignTo` method. When `assignGingerly` encounters a property whose current value is an instance of such a class, it delegates the assignment to `assignTo` instead of performing the default merge/replace logic.
4634
+ As [introduced earlier](#example-3b---class-instances-are-normally-replaced), classes can opt into custom assignment behavior by defining a `static assignTo` method. When `assignGingerly` encounters a property whose current value is an instance of such a class, it delegates the assignment to `assignTo` instead of performing the default merge/replace logic. This section covers the full API, method signature, and advanced use cases.
4599
4635
 
4600
4636
  ```JavaScript
4601
4637
  class ReactiveModel {
package/builtInEmoji.js CHANGED
@@ -20,6 +20,7 @@ export const builtInEmoji = {
20
20
  '🔗': 'builtIns.join',
21
21
  '🏷️': 'builtIns.microDataJoin',
22
22
  '📋': 'builtIns.manageTemplateList',
23
+ '📊': 'builtIns.rangeSelector',
23
24
  };
24
25
 
25
26
  export default builtInEmoji;
package/builtInEmoji.ts CHANGED
@@ -28,6 +28,7 @@ export const builtInEmoji: Record<string, string> = {
28
28
  '🔗': 'builtIns.join',
29
29
  '🏷️': 'builtIns.microDataJoin',
30
30
  '📋': 'builtIns.manageTemplateList',
31
+ '📊': 'builtIns.rangeSelector',
31
32
  };
32
33
 
33
34
  export default builtInEmoji;
@@ -0,0 +1,52 @@
1
+ /**
2
+ * builtIns.rangeSelector handler for assignFrom.
3
+ *
4
+ * Evaluates a value against a series of range conditions and merges
5
+ * the matched case's properties into the target.
6
+ */
7
+
8
+ import assignGingerly from '../assignGingerly.js';
9
+
10
+ const OPERATORS = new Set(['<=', '<', '>=', '>', '===', '!==']);
11
+
12
+ function checkCondition(value, op, threshold) {
13
+ switch (op) {
14
+ case '<=': return value <= threshold;
15
+ case '<': return value < threshold;
16
+ case '>=': return value >= threshold;
17
+ case '>': return value > threshold;
18
+ case '===': return value === threshold;
19
+ case '!==': return value !== threshold;
20
+ default: return false;
21
+ }
22
+ }
23
+
24
+ function caseMatches(value, caseObj) {
25
+ for (const key of Object.keys(caseObj)) {
26
+ if (OPERATORS.has(key)) {
27
+ if (!checkCondition(value, key, caseObj[key])) {
28
+ return false;
29
+ }
30
+ }
31
+ }
32
+ return true;
33
+ }
34
+
35
+ export class RangeSelectorHandler {
36
+ config;
37
+ constructor(config) {
38
+ this.config = config;
39
+ }
40
+ async assign(lhsTarget, resolvedParams) {
41
+ const { value, when } = resolvedParams;
42
+ if (!Array.isArray(when)) return;
43
+ for (const caseObj of when) {
44
+ if (caseMatches(value, caseObj)) {
45
+ if (caseObj.merge && typeof caseObj.merge === 'object') {
46
+ assignGingerly(lhsTarget, caseObj.merge);
47
+ }
48
+ return;
49
+ }
50
+ }
51
+ }
52
+ }
@@ -0,0 +1,90 @@
1
+ /**
2
+ * builtIns.rangeSelector handler for assignFrom.
3
+ *
4
+ * Evaluates a value against a series of range conditions and merges
5
+ * the matched case's properties into the target. Useful for converting
6
+ * imperative if/else-if chains into declarative JSON configs.
7
+ *
8
+ * @example
9
+ * assignFrom(element, {
10
+ * '?. =>': {
11
+ * do: 'builtIns.rangeSelector',
12
+ * get: {
13
+ * value: '?.count',
14
+ * when: [
15
+ * { '<=': 10, merge: { status: 'low' } },
16
+ * { '<': 20, merge: { status: 'medium' } },
17
+ * { merge: { status: 'high' } }
18
+ * ]
19
+ * }
20
+ * }
21
+ * }, { from: vm });
22
+ */
23
+
24
+ import type { AssignFromHandler } from '../assignFromAsync.js';
25
+ import assignGingerly from '../assignGingerly.js';
26
+
27
+ /**
28
+ * Operator keys recognized in case objects.
29
+ */
30
+ const OPERATORS = new Set(['<=', '<', '>=', '>', '===', '!==']);
31
+
32
+ /**
33
+ * Check if a single operator condition is satisfied.
34
+ */
35
+ function checkCondition(value: any, op: string, threshold: any): boolean {
36
+ switch (op) {
37
+ case '<=': return value <= threshold;
38
+ case '<': return value < threshold;
39
+ case '>=': return value >= threshold;
40
+ case '>': return value > threshold;
41
+ case '===': return value === threshold;
42
+ case '!==': return value !== threshold;
43
+ default: return false;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Check if all operator conditions in a case object are satisfied (AND logic).
49
+ * Returns true if no operator keys present (catch-all/default case).
50
+ */
51
+ function caseMatches(value: any, caseObj: Record<string, any>): boolean {
52
+ let hasCondition = false;
53
+ for (const key of Object.keys(caseObj)) {
54
+ if (OPERATORS.has(key)) {
55
+ hasCondition = true;
56
+ if (!checkCondition(value, key, caseObj[key])) {
57
+ return false;
58
+ }
59
+ }
60
+ }
61
+ // No operator keys = default/catch-all
62
+ return true;
63
+ }
64
+
65
+ /**
66
+ * RangeSelectorHandler — declarative range-based conditional merge.
67
+ */
68
+ export class RangeSelectorHandler implements AssignFromHandler {
69
+ config: any;
70
+
71
+ constructor(config: any) {
72
+ this.config = config;
73
+ }
74
+
75
+ async assign(lhsTarget: any, resolvedParams: any): Promise<void> {
76
+ const { value, when } = resolvedParams;
77
+
78
+ if (!Array.isArray(when)) return;
79
+
80
+ // Find first matching case (short-circuit)
81
+ for (const caseObj of when) {
82
+ if (caseMatches(value, caseObj)) {
83
+ if (caseObj.merge && typeof caseObj.merge === 'object') {
84
+ assignGingerly(lhsTarget, caseObj.merge);
85
+ }
86
+ return; // First match wins
87
+ }
88
+ }
89
+ }
90
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "assign-gingerly",
3
- "version": "0.0.60",
3
+ "version": "0.0.61",
4
4
  "description": "This package provides a utility function for carefully merging one object into another.",
5
5
  "homepage": "https://github.com/bahrus/assign-gingerly#readme",
6
6
  "bugs": {
@@ -17,6 +17,7 @@ const BUILT_IN_MAP = {
17
17
  'builtIns.join': './handlers/join.js',
18
18
  'builtIns.microDataJoin': './handlers/microDataJoin.js',
19
19
  'builtIns.manageTemplateList': './handlers/manageTemplateList.js',
20
+ 'builtIns.rangeSelector': './handlers/rangeSelector.js',
20
21
  };
21
22
  /**
22
23
  * Find a handler class in a dynamically imported module.
@@ -21,6 +21,7 @@ const BUILT_IN_MAP: Record<string, string> = {
21
21
  'builtIns.join': './handlers/join.js',
22
22
  'builtIns.microDataJoin': './handlers/microDataJoin.js',
23
23
  'builtIns.manageTemplateList': './handlers/manageTemplateList.js',
24
+ 'builtIns.rangeSelector': './handlers/rangeSelector.js',
24
25
  };
25
26
 
26
27
  /**