assign-gingerly 0.0.14 → 0.0.15

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
@@ -1400,14 +1400,48 @@ console.log(instance.count); // 42 (parsed from attribute)
1400
1400
  console.log(instance.theme); // 'dark' (parsed from attribute)
1401
1401
  ```
1402
1402
 
1403
+ **Example without enhKey:**
1404
+
1405
+ ```TypeScript
1406
+ // withAttrs works even without enhKey
1407
+ class SimpleEnhancement {
1408
+ element;
1409
+ ctx;
1410
+ value = null;
1411
+
1412
+ constructor(oElement, ctx, initVals) {
1413
+ this.element = oElement;
1414
+ this.ctx = ctx;
1415
+ if (initVals) {
1416
+ Object.assign(this, initVals);
1417
+ }
1418
+ }
1419
+ }
1420
+
1421
+ const element = document.createElement('div');
1422
+ element.setAttribute('data-value', 'test123');
1423
+
1424
+ const config = {
1425
+ spawn: SimpleEnhancement,
1426
+ // No enhKey - attributes still parsed!
1427
+ withAttrs: {
1428
+ base: 'data-',
1429
+ value: '${base}value'
1430
+ }
1431
+ };
1432
+
1433
+ const instance = element.enh.get(config);
1434
+ console.log(instance.value); // 'test123' (parsed from attribute)
1435
+ ```
1436
+
1403
1437
  **How it works:**
1404
1438
  1. When an enhancement is spawned via `enh.get()`, `enh.set`, or `assignGingerly()`
1405
1439
  2. If the registry item has a `withAttrs` property defined
1406
1440
  3. `parseWithAttrs(element, registryItem.withAttrs)` is automatically called
1407
- 4. The parsed attributes are merged into `initVals` (along with any existing values from `element.enh[enhKey]`)
1408
- 5. The merged `initVals` is passed to the enhancement constructor
1441
+ 4. The parsed attributes are passed to the enhancement constructor as `initVals`
1442
+ 5. If the registry item also has an `enhKey`, the parsed attributes are merged with any existing values from `element.enh[enhKey]` (existing values take precedence)
1409
1443
 
1410
- **Precedence**: If both parsed attributes and existing `element.enh[enhKey]` values exist, the existing values take precedence over parsed attributes.
1444
+ **Note**: `withAttrs` works with or without `enhKey`. When there's no `enhKey`, the parsed attributes are passed directly to the constructor. When there is an `enhKey`, they're merged with any pre-existing values on the enh container.
1411
1445
 
1412
1446
 
1413
1447
 
@@ -82,21 +82,21 @@ class ElementEnhancementContainer {
82
82
  return undefined;
83
83
  }
84
84
  }
85
+ // Parse attributes if withAttrs is defined (regardless of enhKey)
86
+ let attrInitVals = undefined;
87
+ if (registryItem.withAttrs && element) {
88
+ try {
89
+ attrInitVals = parseWithAttrs(element, registryItem.withAttrs, registryItem.allowUnprefixed || false);
90
+ }
91
+ catch (e) {
92
+ console.error('Error parsing attributes:', e);
93
+ throw e;
94
+ }
95
+ }
85
96
  // Check if there's an enhKey
86
97
  if (registryItem.enhKey) {
87
98
  const ctx = { config: registryItem, mountCtx };
88
99
  const self = this;
89
- // Parse attributes if withAttrs is defined
90
- let attrInitVals = undefined;
91
- if (registryItem.withAttrs && element) {
92
- try {
93
- attrInitVals = parseWithAttrs(element, registryItem.withAttrs, registryItem.allowUnprefixed || false);
94
- }
95
- catch (e) {
96
- console.error('Error parsing attributes:', e);
97
- throw e;
98
- }
99
- }
100
100
  // Get existing initVals from enhKey
101
101
  const existingInitVals = self[registryItem.enhKey] &&
102
102
  !(self[registryItem.enhKey] instanceof SpawnClass)
@@ -111,9 +111,9 @@ class ElementEnhancementContainer {
111
111
  self[registryItem.enhKey] = instance;
112
112
  }
113
113
  else {
114
- // No enhKey, just spawn with element
114
+ // No enhKey, still pass attrInitVals
115
115
  const ctx = { config: registryItem, mountCtx };
116
- instance = new SpawnClass(element, ctx);
116
+ instance = new SpawnClass(element, ctx, attrInitVals);
117
117
  }
118
118
  // Store in global instance map
119
119
  instances.set(registryItem, instance);
@@ -156,26 +156,26 @@ class ElementEnhancementContainer {
156
156
  }
157
157
  }
158
158
 
159
+ // Parse attributes if withAttrs is defined (regardless of enhKey)
160
+ let attrInitVals: any = undefined;
161
+ if (registryItem.withAttrs && element) {
162
+ try {
163
+ attrInitVals = parseWithAttrs(
164
+ element,
165
+ registryItem.withAttrs,
166
+ registryItem.allowUnprefixed || false
167
+ );
168
+ } catch (e) {
169
+ console.error('Error parsing attributes:', e);
170
+ throw e;
171
+ }
172
+ }
173
+
159
174
  // Check if there's an enhKey
160
175
  if (registryItem.enhKey) {
161
176
  const ctx = { config: registryItem, mountCtx };
162
177
  const self = this as any;
163
178
 
164
- // Parse attributes if withAttrs is defined
165
- let attrInitVals: any = undefined;
166
- if (registryItem.withAttrs && element) {
167
- try {
168
- attrInitVals = parseWithAttrs(
169
- element,
170
- registryItem.withAttrs,
171
- registryItem.allowUnprefixed || false
172
- );
173
- } catch (e) {
174
- console.error('Error parsing attributes:', e);
175
- throw e;
176
- }
177
- }
178
-
179
179
  // Get existing initVals from enhKey
180
180
  const existingInitVals = self[registryItem.enhKey] &&
181
181
  !(self[registryItem.enhKey] instanceof SpawnClass)
@@ -192,9 +192,9 @@ class ElementEnhancementContainer {
192
192
  // Store on enh container
193
193
  self[registryItem.enhKey] = instance;
194
194
  } else {
195
- // No enhKey, just spawn with element
195
+ // No enhKey, still pass attrInitVals
196
196
  const ctx = { config: registryItem, mountCtx };
197
- instance = new SpawnClass(element, ctx);
197
+ instance = new SpawnClass(element, ctx, attrInitVals);
198
198
  }
199
199
 
200
200
  // Store in global instance map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "assign-gingerly",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
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": {
@@ -54,8 +54,8 @@
54
54
  },
55
55
  "devDependencies": {
56
56
  "@playwright/test": "^1.58.2",
57
- "spa-ssi": "0.0.26",
58
- "@types/node": "^25.2.3",
57
+ "spa-ssi": "0.0.27",
58
+ "@types/node": "^25.3.0",
59
59
  "typescript": "^5.9.3"
60
60
  }
61
61
  }