assign-gingerly 0.0.62 → 0.0.64
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 +9 -1
- package/assignTentatively.ts +3 -4
- package/object-extension.js +5 -9
- package/object-extension.ts +19 -19
- package/package.json +1 -1
- package/types/assign-gingerly/types.d.ts +28 -0
package/README.md
CHANGED
|
@@ -1312,7 +1312,15 @@ obj
|
|
|
1312
1312
|
console.log(obj); // { a: 1, b: { c: 2 }, d: 3 }
|
|
1313
1313
|
```
|
|
1314
1314
|
|
|
1315
|
-
**Note**: The `assignTentatively` method on Object.prototype
|
|
1315
|
+
**Note**: The `assignTentatively` method on Object.prototype provides full reversibility — it calls the standalone `assignTentatively` function and returns a reversal object. Apply the reversal with `obj.assignGingerly(reversal)` to undo all changes:
|
|
1316
|
+
|
|
1317
|
+
```TypeScript
|
|
1318
|
+
const obj = { name: 'Alice', age: 30 };
|
|
1319
|
+
const reversal = obj.assignTentatively({ name: 'Bob', score: 100 });
|
|
1320
|
+
// obj = { name: 'Bob', age: 30, score: 100 }
|
|
1321
|
+
obj.assignGingerly(reversal);
|
|
1322
|
+
// obj = { name: 'Alice', age: 30 } — restored
|
|
1323
|
+
```
|
|
1316
1324
|
|
|
1317
1325
|
The prototype extensions are non-enumerable and won't appear in `Object.keys()` or `for...in` loops.
|
|
1318
1326
|
|
package/assignTentatively.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* assignTentatively — reversible assignment with change tracking.
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
4
|
+
import type { IAssignTentativelyOptions } from './types/assign-gingerly/types.js';
|
|
5
|
+
export type { IAssignTentativelyOptions };
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Helper function to check if a string key represents an += command
|
package/object-extension.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import assignGingerly, { EnhancementRegistry, ItemscopeRegistry, getInstanceMap } from './assignGingerly.js';
|
|
2
|
+
import assignTentatively from './assignTentatively.js';
|
|
2
3
|
import { parseWithAttrs } from './parseWithAttrs.js';
|
|
3
4
|
/**
|
|
4
5
|
* Normalizes lifecycleKeys to always return an object with dispose and resolved keys
|
|
@@ -426,18 +427,13 @@ Object.defineProperty(Object.prototype, 'assignGingerly', {
|
|
|
426
427
|
});
|
|
427
428
|
/**
|
|
428
429
|
* Adds assignTentatively method to all objects via the Object prototype
|
|
429
|
-
*
|
|
430
|
+
* Returns a reversal object that can undo the changes when passed to assignGingerly.
|
|
430
431
|
*/
|
|
431
432
|
Object.defineProperty(Object.prototype, 'assignTentatively', {
|
|
432
433
|
value: function (source, options) {
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
options = {};
|
|
437
|
-
options.registry = this.customElementRegistry?.enhancementRegistry;
|
|
438
|
-
}
|
|
439
|
-
assignGingerly(this, source, options);
|
|
440
|
-
return this;
|
|
434
|
+
const reversal = options?.reversal ?? {};
|
|
435
|
+
assignTentatively(this, source, { reversal });
|
|
436
|
+
return reversal;
|
|
441
437
|
},
|
|
442
438
|
writable: true,
|
|
443
439
|
enumerable: false,
|
package/object-extension.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import assignGingerly, { EnhancementRegistry, ItemscopeRegistry, IAssignGingerlyOptions, getInstanceMap, INSTANCE_MAP_GUID } from './assignGingerly.js';
|
|
2
|
+
import assignTentatively from './assignTentatively.js';
|
|
3
|
+
import type { IAssignTentativelyOptions } from './types/assign-gingerly/types.js';
|
|
2
4
|
import { EnhancementConfig, SpawnContext } from './types/assign-gingerly/types.js';
|
|
3
5
|
import { parseWithAttrs } from './parseWithAttrs.js';
|
|
4
6
|
|
|
@@ -60,22 +62,24 @@ declare global {
|
|
|
60
62
|
): this;
|
|
61
63
|
|
|
62
64
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
+
* Carefully merge properties from a source object into this object, tracking changes
|
|
66
|
+
* for reversibility. Returns a reversal object that can undo all modifications.
|
|
65
67
|
*
|
|
66
68
|
* @param source - The source object to merge
|
|
67
|
-
* @param options - Optional configuration with
|
|
68
|
-
* @returns
|
|
69
|
+
* @param options - Optional configuration with reversal tracking
|
|
70
|
+
* @returns A reversal object that, when passed to assignGingerly, undoes the changes
|
|
69
71
|
*
|
|
70
72
|
* @example
|
|
71
|
-
* const
|
|
72
|
-
*
|
|
73
|
-
*
|
|
73
|
+
* const obj = { name: 'Alice', age: 30 };
|
|
74
|
+
* const reversal = obj.assignTentatively({ name: 'Bob', score: 100 });
|
|
75
|
+
* // obj = { name: 'Bob', age: 30, score: 100 }
|
|
76
|
+
* obj.assignGingerly(reversal);
|
|
77
|
+
* // obj = { name: 'Alice', age: 30 } — score removed, name restored
|
|
74
78
|
*/
|
|
75
79
|
assignTentatively(
|
|
76
80
|
source: Record<string | symbol, any>,
|
|
77
|
-
options?:
|
|
78
|
-
):
|
|
81
|
+
options?: IAssignTentativelyOptions
|
|
82
|
+
): Record<string | symbol, any>;
|
|
79
83
|
}
|
|
80
84
|
}
|
|
81
85
|
|
|
@@ -567,21 +571,17 @@ Object.defineProperty(Object.prototype, 'assignGingerly', {
|
|
|
567
571
|
|
|
568
572
|
/**
|
|
569
573
|
* Adds assignTentatively method to all objects via the Object prototype
|
|
570
|
-
*
|
|
574
|
+
* Returns a reversal object that can undo the changes when passed to assignGingerly.
|
|
571
575
|
*/
|
|
572
576
|
Object.defineProperty(Object.prototype, 'assignTentatively', {
|
|
573
577
|
value: function <T extends object>(
|
|
574
578
|
this: T,
|
|
575
579
|
source: Record<string | symbol, any>,
|
|
576
|
-
options?:
|
|
577
|
-
):
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
options.registry = (this as any).customElementRegistry?.enhancementRegistry;
|
|
582
|
-
}
|
|
583
|
-
assignGingerly(this, source, options);
|
|
584
|
-
return this;
|
|
580
|
+
options?: IAssignTentativelyOptions
|
|
581
|
+
): Record<string | symbol, any> {
|
|
582
|
+
const reversal = options?.reversal ?? {};
|
|
583
|
+
assignTentatively(this, source, { ...options, reversal });
|
|
584
|
+
return reversal;
|
|
585
585
|
},
|
|
586
586
|
writable: true,
|
|
587
587
|
enumerable: false,
|
package/package.json
CHANGED
|
@@ -261,6 +261,34 @@ export interface IAssignGingerlyOptions {
|
|
|
261
261
|
signal?: AbortSignal;
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
+
/**
|
|
265
|
+
* Options for assignTentatively — reversible assignment with change tracking.
|
|
266
|
+
*
|
|
267
|
+
* Supports a subset of assignGingerly's path features (nested paths, +=, =!, -=)
|
|
268
|
+
* with the addition of reversal tracking.
|
|
269
|
+
*/
|
|
270
|
+
export interface IAssignTentativelyOptions {
|
|
271
|
+
/**
|
|
272
|
+
* Object to accumulate reversal entries into.
|
|
273
|
+
* If omitted, a new object is created internally.
|
|
274
|
+
* Pass an existing object to accumulate reversals across multiple calls.
|
|
275
|
+
*
|
|
276
|
+
* The reversal object can be passed to assignGingerly to undo all changes:
|
|
277
|
+
* @example
|
|
278
|
+
* const reversal = {};
|
|
279
|
+
* assignTentatively(obj, { name: 'Bob' }, { reversal });
|
|
280
|
+
* // Later:
|
|
281
|
+
* assignGingerly(obj, reversal); // restores name to original value
|
|
282
|
+
*/
|
|
283
|
+
reversal?: Record<string | symbol, any>;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Alias mappings for property and method names.
|
|
287
|
+
* Same semantics as IAssignGingerlyOptions.aka — substituted before path evaluation.
|
|
288
|
+
*/
|
|
289
|
+
aka?: Record<string, string>;
|
|
290
|
+
}
|
|
291
|
+
|
|
264
292
|
/**
|
|
265
293
|
* Options for synchronous value resolution (getValues / getValue).
|
|
266
294
|
* Extends IAssignGingerlyOptions with synchronous protocol handlers.
|