assign-gingerly 0.0.62 → 0.0.63
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/object-extension.js +5 -9
- package/object-extension.ts +18 -19
- package/package.json +1 -1
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/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,5 @@
|
|
|
1
1
|
import assignGingerly, { EnhancementRegistry, ItemscopeRegistry, IAssignGingerlyOptions, getInstanceMap, INSTANCE_MAP_GUID } from './assignGingerly.js';
|
|
2
|
+
import assignTentatively from './assignTentatively.js';
|
|
2
3
|
import { EnhancementConfig, SpawnContext } from './types/assign-gingerly/types.js';
|
|
3
4
|
import { parseWithAttrs } from './parseWithAttrs.js';
|
|
4
5
|
|
|
@@ -60,22 +61,24 @@ declare global {
|
|
|
60
61
|
): this;
|
|
61
62
|
|
|
62
63
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
64
|
+
* Carefully merge properties from a source object into this object, tracking changes
|
|
65
|
+
* for reversibility. Returns a reversal object that can undo all modifications.
|
|
65
66
|
*
|
|
66
67
|
* @param source - The source object to merge
|
|
67
|
-
* @param options - Optional configuration with
|
|
68
|
-
* @returns
|
|
68
|
+
* @param options - Optional configuration with reversal tracking
|
|
69
|
+
* @returns A reversal object that, when passed to assignGingerly, undoes the changes
|
|
69
70
|
*
|
|
70
71
|
* @example
|
|
71
|
-
* const
|
|
72
|
-
*
|
|
73
|
-
*
|
|
72
|
+
* const obj = { name: 'Alice', age: 30 };
|
|
73
|
+
* const reversal = obj.assignTentatively({ name: 'Bob', score: 100 });
|
|
74
|
+
* // obj = { name: 'Bob', age: 30, score: 100 }
|
|
75
|
+
* obj.assignGingerly(reversal);
|
|
76
|
+
* // obj = { name: 'Alice', age: 30 } — score removed, name restored
|
|
74
77
|
*/
|
|
75
78
|
assignTentatively(
|
|
76
79
|
source: Record<string | symbol, any>,
|
|
77
|
-
options?:
|
|
78
|
-
):
|
|
80
|
+
options?: { reversal?: Record<string | symbol, any> }
|
|
81
|
+
): Record<string | symbol, any>;
|
|
79
82
|
}
|
|
80
83
|
}
|
|
81
84
|
|
|
@@ -567,21 +570,17 @@ Object.defineProperty(Object.prototype, 'assignGingerly', {
|
|
|
567
570
|
|
|
568
571
|
/**
|
|
569
572
|
* Adds assignTentatively method to all objects via the Object prototype
|
|
570
|
-
*
|
|
573
|
+
* Returns a reversal object that can undo the changes when passed to assignGingerly.
|
|
571
574
|
*/
|
|
572
575
|
Object.defineProperty(Object.prototype, 'assignTentatively', {
|
|
573
576
|
value: function <T extends object>(
|
|
574
577
|
this: T,
|
|
575
578
|
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;
|
|
579
|
+
options?: { reversal?: Record<string | symbol, any> }
|
|
580
|
+
): Record<string | symbol, any> {
|
|
581
|
+
const reversal = options?.reversal ?? {};
|
|
582
|
+
assignTentatively(this, source, { reversal });
|
|
583
|
+
return reversal;
|
|
585
584
|
},
|
|
586
585
|
writable: true,
|
|
587
586
|
enumerable: false,
|
package/package.json
CHANGED