assign-gingerly
v0.0.24
Published
This package provides a utility function for carefully merging one object into another.
Readme
assign-gingerly and assign-tentatively
Introduction
This package starts out innocently enough -- it provides two utility functions for carefully merging one object into another. This is a primitive sorely lacking in the web, and this package is a polyfill for what we (me with a lot of help from AI) would like to see built into the platform. We make no apologies about adding these features directly to the underlying API's, as it is part of a proposal which is sitting there gathering dust, with no apparent alternatives under consideration. In particular the reference:
import 'assign-gingerly/object-extension.js';has the "side effect" of enhancing the platform API in a way that this proposal can only hope the platform chooses to adopt in the future (or some variation).
One can achieve the same functionality with a little more work, and "playing nicer" with the platform by importing assign-gingerly.js and/or assign-tentatively.js, which has no such side effects.
Object Extension Pattern
Not only does this polyfill package allow merging data properties onto objects that are expecting them, this polyfill also provides the ability to merge augmented behavior onto run-time objects without sub classing all such objects of the same type. This includes the ability to spawn an instance of a class and "merge" it into the API of the original object in an elegant way that is easy to wrap one's brain around, without ever blocking access to the original object or breaking it.
So we are providing a form of the "Decorator Pattern" or perhaps more accurately the Extension Object Pattern as tailored for the quirks of the web.
Custom Registries
On top of that, this polyfill package builds on the newly minted Custom Element Registry, adding additional sub-registries:
enhancementRegistry object on top of the customElementRegistry object associated with all elements, to be able to lazy load object extensions on demand while avoiding namespace conflicts, and, importantly, as a basis for defining custom attributes associated with the enhancements.
itemscopeRegistry for Itemscope Managers to automatically associate a function prototype or class instance with the itemscope attribute of an HTMLElement.
Default Support For Not Replacing one object with another if it is a subclass. [TODO]
Custom Element Features [TODO]
So in our view this package helps fill the void left by not supporting the "is" attribute for built-in elements (but is not a complete solution, just a critical building block). Mount-observer, mount-observer-script-element, and custom enhancements builds on top of the critical role that assign-gingerly plays.
- Iterator upgrade support [TODO] -- limited to ish?
Anyway, let's start out detailing the more innocent features of this package / polyfill.
The two utility functions are:
assignGingerly
assignGingerly builds on Object.assign. Like Object.assign, the object getting assigned can often be a JSON stringified object. Some of the unusual syntax we see with assignGingerly is there to continue to support JSON deserialized objects as a viable argument to be passed.
assign-gingerly adds support for:
- Carefully merging in nested properties.
- Dependency injection based on a mapping protocol.
and
assignTentatively
assignTentatively provides a far more limited subset of functionality compared to assignGingerly. The tradeoff is that assignTentatively can do something important assignGingerly cannot do -- be "reversed". This can be quite useful for some scenarios. Think of how css "turns on" visual effects while conditions are met, then reverts to how things were before the conditions were met when needed without as if nothing happened. Another example is allowing user edits to be rolled back as they repeatedly hit "ctrl+z".
Example 1 - assignGingerly as a "superset" of Object.assign:
const sourceObj = {hello: 'world'};
sourceObj.assignGingerly({hello: 'Venus', foo: 'bar'});
// Because none of the keys of the second parameter start with "?.",
// nor includes any symbols keys,
// assign gingerly produces identical results
// as Object.assign, and is synchronous:
console.log(sourceObj);
//{hello: 'Venus', foo: 'bar'}Example 2 Merging into an existing sub object
<body>
<input id=myInput>
</body>const oInput = document.querySelector('#myInput');
oInput.assignGingerly({'?.style?.height': '15px'});
console.log(oInput.style.height);
// 15pxThis can go many levels deep.
Example 3 Deeply nested
const obj = {};
assignGingerly(obj, {
'?.style?.height': '15px',
'?.a?.b?.c': {
d: 'hello',
e: 'world'
}
});
console.log(obj);
// {
// a: {b: c: {d: 'hello', e: 'world'}},
// style: {height: '15px'}
// }When the right hand side of an expression is an object, assignGingerly is recursively applied (passing the third argument in if applicable, which will be discussed below).
Of course, just as Object.assign led to object spread notation, assignGingerly could lead to some sort of deep structural JavaScript syntax, but that is outside the scope of this polyfill package.
Example 3a - Automatic Readonly Property Detection
assignGingerly automatically detects readonly properties and merges into them instead of attempting to replace them. This makes working with DOM properties like style and dataset much more ergonomic:
// Instead of this verbose syntax:
const div = document.createElement('div');
assignGingerly(div, {
'?.style?.height': '15px',
'?.style?.width': '20px'
});
// You can now use this cleaner syntax:
assignGingerly(div, {
style: {
height: '15px',
width: '20px'
}
});
console.log(div.style.height); // '15px'
console.log(div.style.width); // '20px'How it works:
When assignGingerly encounters an object value being assigned to an existing property, it checks if that property is readonly:
- Data properties with
writable: false - Accessor properties with a getter but no setter
If the property is readonly and its current value is an object, assignGingerly automatically merges into it recursively.
Examples of readonly properties:
HTMLElement.style- The CSSStyleDeclaration objectHTMLElement.dataset- The DOMStringMap object- Custom objects with
Object.defineProperty(obj, 'prop', { value: {}, writable: false }) - Accessor properties with getter only:
Object.defineProperty(obj, 'prop', { get() { return {}; } })
Error handling:
If you try to merge an object into a readonly property whose current value is a primitive, assignGingerly throws a descriptive error:
const obj = {};
Object.defineProperty(obj, 'readonlyString', {
value: 'immutable',
writable: false
});
assignGingerly(obj, {
readonlyString: { nested: 'value' }
});
// Error: Cannot merge object into readonly primitive property 'readonlyString'Additional examples:
// Dataset property
const div = document.createElement('div');
assignGingerly(div, {
dataset: {
userId: '123',
userName: 'Alice'
}
});
console.log(div.dataset.userId); // '123'
console.log(div.dataset.userName); // 'Alice'
// Custom readonly property
const config = {};
Object.defineProperty(config, 'settings', {
value: {},
writable: false
});
assignGingerly(config, {
settings: {
theme: 'dark',
lang: 'en'
}
});
console.log(config.settings.theme); // 'dark'Example 3b - Automatic Class Instance Preservation
In addition to readonly property detection, assignGingerly automatically preserves class instances when merging. This is particularly useful when working with enhancement instances:
import 'assign-gingerly/object-extension.js';
// Define an enhancement class
class MyEnhancement {
constructor(element, ctx, initVals) {
this.element = element;
this.instanceId = Math.random(); // Track instance identity
if (initVals) {
Object.assign(this, initVals);
}
}
prop1 = null;
prop2 = null;
}
const element = document.createElement('div');
element.enh = {
myEnh: new MyEnhancement(element, {}, {})
};
const originalId = element.enh.myEnh.instanceId;
// Clean syntax - no need for ?.myEnh?.prop1 notation
assignGingerly(element, {
enh: {
myEnh: {
prop1: 'value1',
prop2: 'value2'
}
}
});
console.log(element.enh.myEnh.instanceId === originalId); // true - instance preserved!
console.log(element.enh.myEnh.prop1); // 'value1'
console.log(element.enh.myEnh.prop2); // 'value2'How it works:
When assignGingerly encounters an object value being assigned to an existing property, it checks if the current value is a class instance (not a plain object):
- Class instances are detected by checking if their prototype is something other than
Object.prototypeornull - Plain objects
{}haveObject.prototypeas their prototype - Class instances have their class's prototype
If the existing value is a class instance, assignGingerly merges into it instead of replacing it.
What counts as a class instance:
- Custom class instances:
new MyClass() - Built-in class instances:
new Date(),new Map(),new Set(), etc. - Enhancement instances on the
enhproperty - Any object whose prototype is not
Object.prototypeornull
What doesn't count:
- Plain objects:
{},{ a: 1 } - Arrays:
[],[1, 2, 3](arrays are replaced, not merged) - Primitives: strings, numbers, booleans
Benefits:
This feature enables clean, framework-friendly syntax for updating enhancements:
// Before: Verbose nested path syntax
assignGingerly(element, {
'?.enh?.mellowYellow?.madAboutFourteen': true
});
// After: Clean object syntax
assignGingerly(element, {
enh: {
mellowYellow: {
madAboutFourteen: true
}
}
});Additional examples:
// Multiple enhancements at once
assignGingerly(element, {
enh: {
enhancement1: { prop: 'value1' },
enhancement2: { prop: 'value2' }
}
});
// Works with built-in classes too
const obj = {
timestamp: new Date('2024-01-01')
};
assignGingerly(obj, {
timestamp: {
customProp: 'metadata'
}
});
console.log(obj.timestamp instanceof Date); // true - Date instance preserved
console.log(obj.timestamp.customProp); // 'metadata'Combined with readonly detection:
Both readonly properties and class instances are preserved:
const div = document.createElement('div');
div.enh = {
myEnh: new MyEnhancement(div, {}, {})
};
assignGingerly(div, {
style: { height: '100px' }, // Readonly - merged
enh: {
myEnh: { prop: 'value' } // Class instance - merged
},
dataset: { userId: '123' } // Readonly - merged
});
// All instances and readonly objects preservedWhile we are in the business of passing values of object A into object B, we might as well add some extremely common behavior that allows updating properties of object B based on the current values of object B -- things like incrementing, toggling, and deleting. Deleting is critical for assignTentatively, but is included with both functions.
Example 4 - Incrementing values with += command
The += command allows us to increment numeric values and concatenate string values:
const obj = {
a: {
b: {
c: 2
}
}
};
assignGingerly(obj, {
'?.a?.b?.c +=': 3,
'?.a?.d?.e +=': -2
});
console.log(obj);
// {
// a: {
// b: { c: 5 }, // 2 + 3 = 5
// d: { e: -2 } // non-existent path created with value -2
// }
// }The += command syntax is <path> += where the path can use the ?. nested notation. The right-hand side value is added to the existing value using +=. If the path doesn't exist, it's created and set directly to the value. If the expression is a string, string concatenation is used. If the expression can't be "added to", it allows JavaScript to throw its natural error.
Example 5 - Toggling boolean values and negating
The =! command allows us to toggle boolean values:
const obj = {
a: {
b: {
c: true
}
}
};
assignGingerly(obj, {
'?.a?.b?.c =!': '.', // Toggle itself
// Negates another property.
// The RHS doesn't spawn new objects
// and evaluates to true if it doesn't exist
'?.a?.d?.c =!': '?.a?.d?.e'
});
console.log(obj);
// {
// a: {
// b: { c: false } // Toggled immediately
// // d doesn't exist yet
// }
// }The =! command syntax is <path> =! where the path can use the ?. nested notation.
For existing values, the toggle is performed using JavaScript's logical NOT operator (!value), regardless of what type it is.
Example 6 - Deleting properties with -= command
The -= command allows us to delete properties from objects:
const obj = {
a: {
b: {
c: true,
d: 'hello'
}
}
};
assignGingerly(obj, {
//deletes obj.a.b.c if it exists
'?.a?.b -=': 'c',
});
console.log(obj);
// {
// a: {
// b: { d: 'hello' } // c deleted
// }
// }The -= command syntax is <path> -= where the path points to the parent object. The right-hand side value specifies what to delete:
- String: Delete a single property
- Array: Delete multiple properties
const obj = {
data: {
keep: 'this',
remove1: 'delete',
remove2: 'delete',
remove3: 'delete'
}
};
// Delete single property
assignGingerly(obj, { '?.data -=': 'remove1' });
// Delete multiple properties
assignGingerly(obj, { '?.data -=': ['remove2', 'remove3'] });
console.log(obj);
// {
// data: { keep: 'this' }
// }Important notes:
- The path specifies the parent object, not the property to delete
- Non-existent properties are silently skipped
- If the parent path doesn't exist, the command is silently skipped
- For root-level deletion, use
-=(space before -=)
Example 7 - Reversible assignments with assignTentatively
The assignTentatively function works like assignGingerly but with a powerful addition: reversibility. It tracks changes and generates a reversal object that can undo all modifications:
import assignTentatively from 'assign-gingerly/assignTentatively';
const obj = { f: { g: 'hello' } };
const reversal = {};
assignTentatively(obj, {
'?.style?.height': '15px',
'?.a?.b?.c': {
d: 'hello',
e: 'world'
},
'?.f?.g': 'bye'
}, { reversal });
console.log(obj);
// {
// f: { g: 'bye' },
// style: { height: '15px' },
// a: { b: { c: { d: 'hello', e: 'world' } } }
// }
console.log(reversal);
// {
// ' -=': 'a',
// ' -=': 'style',
// '?.f?.g': 'hello'
// }
// Later, restore to original state:
assignTentatively(obj, reversal);
console.log(obj);
// {
// f: { g: 'hello' }
// }Key differences from assignGingerly:
- No registry/DI support: Dependency injection features are not available (pass it in and it will be ignored). Dependency injection is discussed below.
- Reversal tracking: Maintains a reversal object that records:
- Original values of modified existing properties
- -= commands for newly created top-level paths (e.g.,
-=: 'a'for paths created undera) - Original values for deleted properties
Reversal guarantee:
const reversal = {};
const obj = {...originalObj};
const string1 = JSON.stringify(obj);
assignTentatively(obj, sourceChanges, { reversal });
assignTentatively(obj, reversal);
const string2 = JSON.stringify(obj);
console.log(string1 === string2); // trueThis guarantees that applying the reversal object restores the object to its exact original state.
Dependency injection based on a registry object and a Symbolic reference mapping
interface IEnhancementRegistryItem<T = any, TObjToExtend = any> {
spawn: {new(objToExtend: TObjToExtend, ctx: SpawnContext, initVals: Partial<T>): T}
symlinks?: {[key: symbol]: keyof T}
// Optional: for element enhancement access
enhKey?: string | symbol
// Optional: automatic attribute parsing
withAttrs?: AttrPatterns<T>
}
export const isHappy = Symbol.for('TFWsx0YH5E6eSfhE7zfLxA');
class MyEnhancement{
//optional
constructor(augmentedObj?: Object){}
get isHappy(){}
set isHappy(nv){}
}
export const isMellow = Symbol.for('BqnnTPWRHkWdVGWcGQoAiw');
class YourEnhancement{
get isMellow(){}
set isMellow(nv){}
get madAboutFourteen(){}
set madAboutFourteen(nv){}
}
class EnhancementRegistry{
push(IEnhancementRegistryItem | IEnhancementRegistryItem[]){
...
}
}
//Here's where the dependency injection mapping takes place
const EnhancementRegistry = new EnhancementRegistry;
EnhancementRegistry.push([
{
symlinks: {
[isHappy]: 'isHappy'
},
spawn: MyEnhancement,
},{
enhKey: 'mellowYellow',
symlinks: {
[isMellow]: 'isMellow'
},
spawn: YourEnhancement,
}
]);
//end of dependency injection
const result = assignGingerly({}, {
[isHappy]: true,
[isMellow]: true,
style:{
height: '40px',
},
enh: {
'?.mellowYellow?.madAboutFourteen': true
}
}, {
registry: EnhancementRegistry
});
//result.set[isMellow] = false;The assignGingerly function searches the registry for any items that has a mapping with a matching symbol of isHappy and isMellow, and if found, sees if it already has an instance of the spawn class associated with the first passed in parameter. If no such instance is found, it instantiates one, associates the instance with the first parameter, then sets the property value.
It also adds a lazy property to the first passed in parameter, "set", which returns a proxy, and that proxy watches for symbol references passed in a value, and sets the value from that spawned instance. Again, if the spawned instance is not found, it re-spawns it.
The suggestion to use Symbol.for with a guid, as opposed to just Symbol(), is based on some negative experiences I've had with multiple versions of the same library being referenced, but is not required. Regular symbols could also be used when that risk can be avoided.
To ensure instance uniqueness even when multiple versions of this package are loaded, spawned instances are stored in a global WeakMap at globalThis['HDBhTPLuIUyooMxK88m68Q']. This guarantees that:
- Same instance across versions: Different versions of the package will share the same instance map
- Memory safety: Using WeakMap allows garbage collection when objects are no longer referenced
- No conflicts: The GUID-based key prevents collisions with other libraries
- Registry item keying: Instances are keyed by registry item (not by symbol), ensuring that multiple symbols mapped to the same registry item share the same spawned instance
- Shared between assignGingerly and enh.set: Both
assignGingerly()andelement.enh.setuse the same global instance map, ensuring only one instance per registry item per element
This is particularly important in complex applications where different dependencies might bundle different versions of assign-gingerly.
Example of shared instances:
const symbol1 = Symbol.for('prop1');
const symbol2 = Symbol.for('prop2');
class MyEnhancement {
element;
ctx;
prop1 = null;
prop2 = null;
instanceId = Math.random();
constructor(oElement, ctx, initVals) {
this.element = oElement;
this.ctx = ctx;
if (initVals) {
Object.assign(this, initVals);
}
}
}
const registryItem = {
spawn: MyEnhancement,
symlinks: {
[symbol1]: 'prop1',
[symbol2]: 'prop2'
},
enhKey: 'myEnh'
};
const registry = new EnhancementRegistry();
registry.push(registryItem);
const element = document.createElement('div');
element.customElementRegistry.enhancementRegistry.push(registryItem);
// Use assignGingerly first
assignGingerly(element, { [symbol1]: 'value1' }, { registry });
const id1 = element.enh.myEnh.instanceId;
// Use enh.set - gets the SAME instance
element.enh.set.myEnh.prop2 = 'value2';
const id2 = element.enh.myEnh.instanceId;
console.log(id1 === id2); // true - same instance!
console.log(element.enh.myEnh.prop1); // 'value1'
console.log(element.enh.myEnh.prop2); // 'value2'Example of registry item keying:
const symbol1 = Symbol.for('prop1');
const symbol2 = Symbol.for('prop2');
class MyEnhancement {
prop1 = null;
prop2 = null;
}
const registryItem = {
spawn: MyEnhancement,
symlinks: {
[symbol1]: 'prop1',
[symbol2]: 'prop2'
}
};
const registry = new EnhancementRegistry();
registry.push(registryItem);
const target = {};
// Both symbols use the SAME instance because they're in the same registry item
assignGingerly(target, { [symbol1]: 'value1' }, { registry });
assignGingerly(target, { [symbol2]: 'value2' }, { registry });
// Both properties are set on the same instance
console.log(target.set[symbol1] === target.set[symbol2]); // true
console.log(target.set[symbol1].prop1); // 'value1'
console.log(target.set[symbol1].prop2); // 'value2'const result = assignGingerly({}, {
"[Symbol.for('TFWsx0YH5E6eSfhE7zfLxA')]": true,
"[Symbol.for('BqnnTPWRHkWdVGWcGQoAiw')]": true,
style: {
height: '40px'
}
enh: {
mellowYellow?.madAboutFourteen': true
}
}, {
registry: EnhancementRegistry
});Enhancement Registry Addendum to the Custom Element Registry
This package polyfill adds an "enhancementRegistry" registry on the CustomElementRegistry prototype.
In this way, we achieve dependency injection in harmony with scoped custom elements DOM registry scopes.
[!NOTE] Safari/WebKit played a critical role in pushing scoped custom element registries forward, and announced with little fanfare or documentation that Safari 26 supports it. However, the Playwright test machinery's cross platform Safari test browser doesn't yet support it. For now, only Chrome 146+ has been tested / vetted for this functionality.
For more information about scoped custom element registries, see Chrome's announcement and guide.
When assignGingerly or assignTentatively is called on an Element instance without providing an explicit registry option, it automatically uses the registry from element.customElementRegistry.enhancementRegistry:
import 'assign-gingerly/object-extension.js';
// Set up a registry on the custom element registry
const myElement = document.createElement('div');
const registry = myElement.customElementRegistry.enhancementRegistry;
const mySymbol = Symbol.for('myProperty');
class MyEnhancement {
value = null;
}
registry.push({
spawn: MyEnhancement,
symlinks: { [mySymbol]: 'value' }
});
// No need to pass registry option - it's automatically used!
myElement.assignGingerly({
[mySymbol]: 'hello world'
});Each CustomElementRegistry instance gets its own enhancementRegistry property via a lazy getter. The EnhancementRegistry instance is created on first access and cached for subsequent uses:
const element1 = document.createElement('div');
const element2 = document.createElement('span');
// Each element's customElementRegistry gets its own registry
const registry1 = element1.customElementRegistry.enhancementRegistry;
const registry2 = element2.customElementRegistry.enhancementRegistry;
// Multiple accesses return the same instance
console.log(registry1 === element1.customElementRegistry.enhancementRegistry); // trueYou can still provide an explicit registry option to override the automatic behavior:
const customRegistry = new EnhancementRegistry();
// ... configure customRegistry ...
myElement.assignGingerly({
[mySymbol]: 'value'
}, { registry: customRegistry }); // Uses customRegistry instead of customElementRegistry.enhancementRegistryBrowser Support: This feature requires Chrome 146+ with scoped custom element registry support. The implementation is designed as a polyfill for the web standards proposal and does not include fallback behavior for older browsers.
Enhanced Element Property Assignment with enh.set Proxy (Chrome 146+)
Building on the Custom Element Registry integration, this package provides a powerful enh.set proxy on all Element instances that enables automatic enhancement spawning and simplified property assignment syntax. The enh property serves as a dedicated namespace for enhancements, preventing conflicts with future platform properties.
Basic Usage
The enh.set proxy allows us to assign properties to enhancements using a clean, chainable syntax:
import 'assign-gingerly/object-extension.js';
//import { EnhancementRegistry } from 'assign-gingerly';
// Define an enhancement class
class MyEnhancement {
element;
ctx;
myProp = null;
anotherProp = null;
constructor(oElement, ctx, initVals) {
this.element = oElement;
this.ctx = ctx;
if (initVals) {
Object.assign(this, initVals);
}
}
}
// Register the enhancement with an enhKey
const myElement = document.createElement('div');
const registry = myElement.customElementRegistry.enhancementRegistry;
registry.push({
spawn: MyEnhancement,
enhKey: 'myEnh' // Key identifier for this enhancement
});
// Use the enh.set proxy to automatically spawn and assign properties
myElement.enh.set.myEnh.myProp = 'hello';
myElement.enh.set.myEnh.anotherProp = 'world';
console.log(myElement.enh.myEnh instanceof MyEnhancement); // true
console.log(myElement.enh.myEnh.myProp); // 'hello'
console.log(myElement.enh.myEnh.element === myElement); // trueWhen you access element.enh.set.enhKey.property, the proxy:
- Checks the registry: Looks for a registry item with
enhKeymatching the property name - Spawns if needed: If found and the enhancement doesn't exist or is the wrong type:
- Creates a
SpawnContextwith{ config: registryItem } - Calls the constructor with
(element, ctx, initVals) - If a non-matching object already exists at
element.enh[enhKey], it's passed asinitVals - Stores the spawned instance at
element.enh[enhKey]
- Creates a
- Reuses existing instances: If the enhancement already exists and is the correct type, it reuses it
- Falls back to plain objects: If no registry item is found, creates a plain object at
element.enh[enhKey]
Why the enh Namespace?
The enh property provides a dedicated namespace for enhancements, similar to how dataset provides a namespace for data attributes. This prevents conflicts with:
- Future platform properties that might be added to Element
- Existing element properties and methods
- Other libraries that might extend HTMLElement
This approach is part of a proposal to WHATWG for standardizing element enhancements.
Constructor Signature
Element enhancement classes should follow this constructor signature:
interface SpawnContext<T, TMountContext = any> {
config: IEnhancementRegistryItem<T>;
mountCtx?: TMountContext; // Optional custom context passed by caller
}
class Enhancement<T> {
constructor(
oElement?: Element, // The element being enhanced
ctx?: SpawnContext, // Context with registry item info and optional mountCtx
initVals?: Partial<T> // Initial values if property existed
) {
// Your initialization logic
// Access custom context via ctx.mountCtx if provided
}
}All parameters are optional for backward compatibility with existing code.
Note that the class need not extend any base class or leverage any mixins. In fact, ES5 prototype functions can be used, and in both cases are instantiated using new .... Arrow functions cannot be used.
You can pass custom context when calling enh.get() or enh.whenResolved() (discussed in detail below):
// Pass custom context to the spawned instance
const myContext = { userId: 123, permissions: ['read', 'write'] };
const instance = element.enh.get(registryItem, myContext);
// The constructor receives it via ctx.mountCtx
class MyEnhancement {
constructor(oElement, ctx, initVals) {
console.log(ctx.mountCtx.userId); // 123
console.log(ctx.mountCtx.permissions); // ['read', 'write']
}
}This is useful for:
- Passing authentication/authorization context
- Providing configuration that varies per invocation
- Sharing state between caller and enhancement
- Dependency injection of services or utilities
Note: The mountCtx is only available when explicitly calling enh.get() or enh.whenResolved(). It's not available when accessing via the enh.set proxy (since that's a property getter with no way to pass parameters).
Registry Item with enhKey
In addition to spawn and symlinks, registry items support optional properties enhKey, withAttrs, canSpawn, and lifecycleKeys:
interface IEnhancementRegistryItem<T, TObj = Element> {
spawn: {
new (obj?: TObj, ctx?: SpawnContext<T>, initVals?: Partial<T>): T;
canSpawn?: (obj: TObj, ctx?: SpawnContext<T>) => boolean; // Optional spawn guard
};
symlinks?: { [key: string | symbol]: keyof T };
enhKey?: string; // String identifier for set proxy access
withAttrs?: AttrPatterns<T>; // Automatic attribute parsing during spawn
lifecycleKeys?:
| true // Use standard names: "dispose" method, "resolved" property/event
| {
dispose?: string | symbol; // Method name to call on disposal
resolved?: string | symbol; // Property name and event name for async resolution
};
}The withAttrs property enables automatic attribute parsing when the enhancement is spawned. See the Parsing Attributes with parseWithAttrs section for details.
It also tips off extending polyfills / libraries, in particular mount-observer, to be on te lookout for the attributes specified by withAttrs. But assign-gingerly, by itself, performs no DOM observing to automatically spawn the class instance. It expects consumers of the polyfill to programmatically attach such behavior/enhancements, and/or rely on alternative, higher level packages to be vigilant for enhancement opportunities.
The canSpawn static method allows enhancement classes to conditionally block spawning based on the target object. See the Conditional Spawning with canSpawn section for details.
The lifecycleKeys property configures lifecycle integration without requiring base classes. See the Lifecycle Keys: Configuration vs Convention section for details.
Multiple Enhancements:
class StyleEnhancement {
constructor(oElement, ctx, initVals) {
this.element = oElement;
}
height = null;
width = null;
}
class DataEnhancement {
constructor(oElement, ctx, initVals) {
this.element = oElement;
}
value = null;
}
const element = document.createElement('div');
const registry = element.customElementRegistry.enhancementRegistry;
registry.push([
{ spawn: StyleEnhancement, enhKey: 'styles' },
{ spawn: DataEnhancement, enhKey: 'data' }
]);
element.enh.set.styles.height = '100px';
element.enh.set.data.value = 'test';
console.log(element.enh.styles instanceof StyleEnhancement); // true
console.log(element.enh.data instanceof DataEnhancement); // truePreserving Existing Data with initVals:
const element = document.createElement('div');
const registry = element.customElementRegistry.enhancementRegistry;
registry.push({
spawn: MyEnhancement,
enhKey: 'config'
});
// Set a plain object first
element.config = { existingProp: 'preserved', anotherProp: 'also preserved' };
// Access via enh.set proxy - spawns enhancement with initVals
element.enh.set.config.newProp = 'added';
console.log(element.enh.config instanceof MyEnhancement); // true
console.log(element.enh.config.existingProp); // 'preserved'
console.log(element.enh.config.newProp); // 'added'Plain Objects Without Registry:
const element = document.createElement('div');
// No registry item for 'plainData' - creates plain object
element.enh.set.plainData.prop1 = 'value1';
element.enh.set.plainData.prop2 = 'value2';
console.log(element.enh.plainData); // { prop1: 'value1', prop2: 'value2' }The EnhancementRegistry class includes a findByEnhKey method:
const registry = new EnhancementRegistry();
registry.push({
spawn: MyEnhancement,
enhKey: 'myEnh'
});
const item = registry.findByEnhKey('myEnh');
console.log(item.enhKey); // 'myEnh'Programmatic Instance Spawning with enh.get()
The enh.get(registryItem) method provides a programmatic way to spawn or retrieve previously instantiated enhancement instances:
const registryItem = {
spawn: MyEnhancement,
enhKey: 'myEnh'
};
// Get or spawn the instance
const instance = element.enh.get(registryItem);
console.log(instance instanceof MyEnhancement); // true
console.log(element.enh.myEnh === instance); // trueHow enh.get() works:
- Adds to registry: If the registry item isn't already in
element.customElementRegistry.enhancementRegistry, it's automatically added - Spawns if needed: If no instance exists for this registry item, it spawns one (passing element, context, and initVals if applicable)
- Stores on enh: If the registry item has an
enhKey, the instance is stored atelement.enh[enhKey] - Returns instance: Returns the spawned or existing instance
Benefits:
- Explicit control: Spawn instances programmatically without needing to use symbols or property assignment
- Shared instances: Uses the same global instance map as
assignGingerlyandenh.set, ensuring only one instance per registry item - Auto-registration: Automatically adds registry items to the element's registry if not present
const registryItem = {
spawn: MyEnhancement,
symlinks: { [mySymbol]: 'value' },
enhKey: 'myEnh'
};
// Get instance programmatically
const instance1 = element.enh.get(registryItem);
instance1.prop1 = 'from get()';
// Use enh.set - gets the SAME instance
element.enh.set.myEnh.prop2 = 'from set';
// Use assignGingerly - still the SAME instance
assignGingerly(element, { [mySymbol]: 'from assign' }, { registry });
console.log(element.enh.myEnh.prop1); // 'from get()'
console.log(element.enh.myEnh.prop2); // 'from set'
console.log(element.enh.myEnh.value); // 'from assign'Enhancement classes can integrate with the lifecycle system through configurable method/property names, avoiding the need for base classes or mixins.
Why configurable lifecycle keys?
- Zero coupling: Enhancement classes remain plain classes with no framework dependencies
- Framework agnostic: Works with classes from any source - your own, third-party libraries, generated code, legacy code
- Naming freedom: Avoids debates over standard names. One team's
dispose()is another'scleanup(),destroy(), orteardown() - Multiple patterns: Different enhancement libraries can coexist with different conventions
- Gradual adoption: Integrate with existing classes without refactoring
- Testability: Enhancement classes remain simple POJOs (Plain Old JavaScript Objects) that are easy to test in isolation
The shortcut: lifecycleKeys: true
For convenience, you can use lifecycleKeys: true to adopt standard naming conventions:
const registryItem = {
spawn: MyEnhancement,
enhKey: 'myEnh',
lifecycleKeys: true // Uses standard names: "dispose" and "resolved"
};This is equivalent to:
lifecycleKeys: {
dispose: 'dispose',
resolved: 'resolved'
}Custom lifecycle keys:
When you need different names (for legacy code, team conventions, or avoiding conflicts):
lifecycleKeys: {
dispose: 'cleanup', // Call cleanup() method on disposal
resolved: 'isReady' // Watch isReady property and dispatch "isReady" event
}Symbol support:
Lifecycle keys can be symbols to avoid naming collisions:
const DISPOSE = Symbol('dispose');
const RESOLVED = Symbol('resolved');
class MyEnhancement {
[DISPOSE]() {
// Cleanup code
}
[RESOLVED] = false;
}
const registryItem = {
spawn: MyEnhancement,
enhKey: 'myEnh',
lifecycleKeys: {
dispose: DISPOSE,
resolved: RESOLVED
}
};Note: Symbol event names are not yet supported by the platform but have been requested. When supported, the resolved key will work as both property name and event name.
Disposing Enhancement Instances with enh.dispose(regItem)
The enh.dispose(regItem) method provides a way to clean up and remove enhancement instances:
class MyEnhancement {
element;
ctx;
constructor(oElement, ctx, initVals) {
this.element = oElement;
this.ctx = ctx;
// Setup code...
}
cleanup(registryItem) {
// Cleanup code - remove event listeners, clear timers, etc.
console.log('Disposing enhancement');
}
}
const registryItem = {
spawn: MyEnhancement,
enhKey: 'myEnh',
lifecycleKeys: true // Standard: calls dispose() method
};
// Or with custom name:
const customRegistryItem = {
spawn: MyEnhancement,
enhKey: 'myEnh',
lifecycleKeys: {
dispose: 'cleanup' // Custom: calls cleanup() method
}
};
// Get instance
const instance = element.enh.get(registryItem);
// Later, dispose of it
element.enh.dispose(registryItem);How enh.dispose(regItem) works:
- Retrieves instance: Gets the spawned instance from the global instance map
- Calls lifecycle method: If
lifecycleKeys.disposeis specified, calls that method on the instance (passing the registry item) - Removes from map: Removes the instance from the global instance map
- Removes from enh: If the registry item has an
enhKey, removes the property from the enh container
Benefits:
- Proper cleanup: Allows enhancements to clean up resources (event listeners, timers, etc.)
- Memory management: Removes references to allow garbage collection
- Safe: Safely handles non-existent instances without errors
- Isolated: Only affects the specified instance, leaving others intact
Example with lifecycle cleanup:
class TimerEnhancement {
element;
timerId = null;
constructor(oElement, ctx) {
this.element = oElement;
this.timerId = setInterval(() => {
console.log('Timer tick');
}, 1000);
}
dispose() {
if (this.timerId) {
clearInterval(this.timerId);
this.timerId = null;
console.log('Timer cleaned up');
}
}
}
const registryItem = {
spawn: TimerEnhancement,
enhKey: 'timer',
lifecycleKeys: true // Standard: calls dispose() method
};
element.enh.get(registryItem); // Starts timer
// Later...
element.enh.dispose(registryItem); // Stops timer and cleans upAfter disposal:
- The instance is removed from the global instance map
- Calling
enh.get()again will create a new instance - The enhancement property is removed from the enh container
Waiting for Async Initialization with enh.whenResolved(regItem)
The enh.whenResolved(regItem) method provides a way to wait for asynchronous enhancement initialization:
class AsyncEnhancement extends EventTarget {
element;
ctx;
isResolved = false;
data = null;
constructor(oElement, ctx) {
super();
this.element = oElement;
this.ctx = ctx;
this.initialize();
}
async initialize() {
// Simulate async operation (fetch data, load resources, etc.)
const response = await fetch('/api/data');
this.data = await response.json();
// Mark as resolved and dispatch event
this.resolved = true;
this.dispatchEvent(new Event('resolved'));
}
}
const registryItem = {
spawn: AsyncEnhancement,
enhKey: 'asyncEnh',
lifecycleKeys: true // Standard: watches "resolved" property and event
};
// Or with custom name:
const customRegistryItem = {
spawn: AsyncEnhancement,
enhKey: 'asyncEnh',
lifecycleKeys: {
resolved: 'isReady' // Custom: watches "isReady" property and event
}
};
// Wait for the enhancement to be fully initialized
const instance = await element.enh.whenResolved(registryItem);
console.log(instance.data); // Data is loaded and ready
// With custom context
const authContext = { token: 'abc123', userId: 456 };
const instanceWithContext = await element.enh.whenResolved(registryItem, authContext);
// The constructor receives authContext via ctx.mountCtx- Validates configuration: Throws error if
lifecycleKeys.resolvedis not specified - Gets instance: Calls
enh.get()to get or spawn the instance - Checks if resolved: If the resolved property is already true, returns immediately
- Validates EventTarget: Throws error if instance is not an EventTarget
- Waits for event: Lazy loads the
waitForEventmodule and waits for the resolved event (using the same name as the property) - Returns or rejects: Returns the instance if resolved flag is set, otherwise throws
Requirements:
- Enhancement class must extend
EventTarget - Must specify
lifecycleKeys.resolvedproperty name (or uselifecycleKeys: truefor standard "resolved") - Instance must dispatch an event with the same name as the resolved property when ready
- Instance must set the resolved property to a truthy value
Note: The resolved key serves dual purpose - it's both the property name to check AND the event name to listen for. When lifecycleKeys: true, both use "resolved".
Benefits:
- Async-aware: Properly handles asynchronous initialization
- Lazy loading: The waitForEvent module is only loaded when needed
- Early return: Returns immediately if already resolved (no waiting)
- Type safety: Validates that instance can dispatch events
- Clean API: Simple promise-based interface
Example with multiple async operations:
class DataEnhancement extends EventTarget {
element;
resolved = false;
users = null;
settings = null;
constructor(oElement, ctx) {
super();
this.element = oElement;
this.loadData();
}
async loadData() {
try {
// Load multiple resources in parallel
const [usersRes, settingsRes] = await Promise.all([
fetch('/api/users'),
fetch('/api/settings')
]);
this.users = await usersRes.json();
this.settings = await settingsRes.json();
// Mark as resolved
this.resolved = true;
this.dispatchEvent(new Event('resolved'));
} catch (error) {
console.error('Failed to load data:', error);
// Could dispatch a 'rejected' event here
}
}
}
const registryItem = {
spawn: DataEnhancement,
enhKey: 'data',
lifecycleKeys: true // Standard: watches "resolved" property and event
};
// Wait for all data to be loaded
try {
const dataEnh = await element.enh.whenResolved(registryItem);
console.log('Users:', dataEnh.users);
console.log('Settings:', dataEnh.settings);
} catch (error) {
console.error('Enhancement failed to resolve:', error);
}Calling multiple times:
// Multiple calls to whenResolved all wait for the same instance
const promise1 = element.enh.whenResolved(registryItem);
const promise2 = element.enh.whenResolved(registryItem);
const [instance1, instance2] = await Promise.all([promise1, promise2]);
console.log(instance1 === instance2); // true - same instanceBrowser Support: This feature requires Chrome 146+ with scoped custom element registry support.
Conditional Spawning with canSpawn
Enhancement classes can implement a static canSpawn method to conditionally block spawning based on the target object. This is useful for:
- Restricting enhancements to specific element types
- Checking object compatibility before spawning
- Implementing version-based feature gates
- Validating object state before enhancement
Basic Usage
class DivOnlyEnhancement {
element;
ctx;
constructor(oElement, ctx, initVals) {
this.element = oElement;
this.ctx = ctx;
if (initVals) {
Object.assign(this, initVals);
}
}
// Static method to control spawning
static canSpawn(obj, ctx) {
// Only spawn for div elements
return obj.tagName && obj.tagName.toLowerCase() === 'div';
}
}
const registry = new EnhancementRegistry();
registry.push({
spawn: DivOnlyEnhancement,
enhKey: 'divOnly'
});
const div = document.createElement('div');
const span = document.createElement('span');
// Will spawn - div is allowed
const divInstance = div.enh.get(registry.getItems()[0]);
console.log(divInstance instanceof DivOnlyEnhancement); // true
// Will NOT spawn - span is blocked
const spanInstance = span.enh.get(registry.getItems()[0]);
console.log(spanInstance); // undefined- Called before spawning: When an enhancement is about to be spawned (via
assignGingerly,enh.get(), orenh.set), thecanSpawnmethod is called first - Receives context: The method receives the target object and spawn context with registry item information
- Returns boolean: Return
trueto allow spawning,falseto block it - Applies everywhere: Works consistently across all spawning methods (dependency injection,
enh.get(),enh.set) - Optional: If not defined, spawning proceeds normally
Parameters
static canSpawn(obj: any, ctx?: SpawnContext<T>): booleanobj: The target object being enhanced (element, plain object, etc.)ctx: Optional spawn context containing{ config: IEnhancementRegistryItem<T> }- Returns:
trueto allow spawning,falseto block
Use Cases
Element Type Checking:
class ButtonEnhancement {
static canSpawn(obj, ctx) {
return obj.tagName && obj.tagName.toLowerCase() === 'button';
}
}Version Gating:
class ModernFeature {
static canSpawn(obj, ctx) {
// Only spawn for objects with version 2+
return obj.version && obj.version >= 2;
}
}Custom Type Checking:
class CustomTypeEnhancement {
static canSpawn(obj, ctx) {
return obj instanceof MyCustomClass;
}
}Attribute-Based Conditions:
class OptInEnhancement {
static canSpawn(obj, ctx) {
// Only spawn if element has opt-in attribute
return obj.hasAttribute && obj.hasAttribute('data-enhanced');
}
}Complex Validation:
class ValidatedEnhancement {
static canSpawn(obj, ctx) {
// Multiple conditions
if (!obj.id) return false;
if (obj.disabled) return false;
if (!obj.dataset?.ready) return false;
return true;
}
}Behavior Notes
- No spawning: When
canSpawnreturnsfalse, no instance is created and no constructor is called - Returns undefined: Methods like
enh.get()returnundefinedwhen spawning is blocked - Silent blocking: No errors are thrown - spawning is simply skipped
- Reuse unaffected: If an instance already exists,
canSpawnis not called again - Performance:
canSpawnis only called once per spawn attempt, not on every access
Example with Dependency Injection
import assignGingerly, { EnhancementRegistry } from 'assign-gingerly';
class ElementOnlyEnhancement {
value = null;
static canSpawn(obj, ctx) {
return typeof Element !== 'undefined' && obj instanceof Element;
}
}
const registry = new EnhancementRegistry();
const enhSymbol = Symbol.for('myEnhancement');
registry.push({
spawn: ElementOnlyEnhancement,
symlinks: { [enhSymbol]: 'value' }
});
// Plain object - will not spawn
const plainObj = {};
assignGingerly(plainObj, { [enhSymbol]: 'test' }, { registry });
// No enhancement created
// Element - will spawn
const element = document.createElement('div');
assignGingerly(element, { [enhSymbol]: 'test' }, { registry });
// Enhancement created and value setParsing Attributes with parseWithAttrs
The parseWithAttrs function provides a declarative way to read and parse HTML attributes and pass the parsed values into the spawned enhancement constructor.
Automatic Integration with Enhancement Spawning
Important: When using the enh.get(), enh.set, or assignGingerly() methods with registry items, you typically do not need to call parseWithAttrs() manually. The attribute parsing happens automatically during enhancement spawning when you include a withAttrs property in your registry item configuration.
<my-element my-enhancement-count="42" my-enhancement-theme="dark"></my-element>import 'assign-gingerly/object-extension.js';
class MyEnhancement {
elementRef;
ctx;
count = 0;
theme = 'light';
constructor(oElement, ctx, initVals) {
this.element = new WeakRef(oElement);
this.ctx = ctx;
// initVals automatically contains parsed attributes!
if (initVals) {
Object.assign(this, initVals);
}
}
}
const element = document.querySelector('my-element');
const enhancementConfig = {
spawn: MyEnhancement,
enhKey: 'myEnh',
withAttrs: {
base: 'my-enhancement',
count: '${base}-count',
_count: { instanceOf: 'Number' },
theme: '${base}-theme'
}
};
// Spawn the enhancement - attributes are automatically parsed!
const instance = element.enh.get(enhancementConfig);
console.log(instance.count); // 42 (parsed from attribute)
console.log(instance.theme); // 'dark' (parsed from attribute)// withAttrs works even without enhKey
class SimpleEnhancement {
element;
ctx;
value = null;
constructor(oElement, ctx, initVals) {
this.element = oElement;
this.ctx = ctx;
if (initVals) {
Object.assign(this, initVals);
}
}
}
const element = document.createElement('div');
element.setAttribute('data-value', 'test123');
const config = {
spawn: SimpleEnhancement,
// No enhKey - attributes still parsed!
withAttrs: {
base: 'data-',
value: '${base}value'
}
};
const instance = element.enh.get(config);
console.log(instance.value); // 'test123' (parsed from attribute)- When an enhancement is spawned via
enh.get(),enh.set, orassignGingerly() - If the registry item has a
withAttrsproperty defined parseWithAttrs(element, registryItem.withAttrs)is automatically called- The parsed attributes are passed to the enhancement constructor as
initVals - If the registry item also has an
enhKey, the parsed attributes are merged with any existing values fromelement.enh[enhKey](existing values take precedence)
![NOTE]
withAttrsworks with or withoutenhKey. When there's noenhKey, the parsed attributes are passed directly to the constructor. When there is anenhKey, they're merged with any pre-existing values on the enh container.
The enh- Prefix for Attribute Isolation
The parseWithAttrs function supports an enh- prefix for attributes to provide better isolation and avoid conflicts, especially for custom elements and SVG elements.
Behavior by Element Type:
Built-in HTML elements (div, span, etc.): The
enh-prefix acts as an alias. The function triesenh-prefixed attributes first, then falls back to unprefixed attributes.<!-- Both work for built-in elements --> <div data-count="42"></div> <div enh-data-count="42"></div> <!-- enh- prefix takes precedence --> <div data-count="10" enh-data-count="42"></div> <!-- Uses 42 -->Custom elements and SVG elements: The
enh-prefix is strictly enforced by default. Onlyenh-prefixed attributes are read.<!-- Only enh- prefixed attributes work --> <my-element data-count="42"></my-element> <!-- Ignored --> <my-element enh-data-count="42"></my-element> <!-- Works --> <svg enh-data-theme="dark"></svg> <!-- Works --> <svg data-theme="dark"></svg> <!-- Ignored -->
Overriding with allowUnprefixed:
For custom elements and SVG, you can opt-in to reading unprefixed attributes by specifying a pattern (string or RegExp) that the element's tag name must match:
// Allow unprefixed for elements matching pattern
registry.push({
spawn: MyEnhancement,
enhKey: 'myEnh',
allowUnprefixed: '^my-', // Only for elements starting with "my-"
withAttrs: {
base: 'data-',
count: '${base}count',
_count: { instanceOf: 'Number' }
}
});
// Or use RegExp for more complex patterns
registry.push({
spawn: MyEnhancement,
enhKey: 'myEnh',
allowUnprefixed: /^(my-|app-)/, // For "my-*" or "app-*" elements
withAttrs: {
base: 'data-',
count: '${base}count',
_count: { instanceOf: 'Number' }
}
});- Avoid conflicts: Custom elements may use unprefixed attributes for their own purposes
- Clear intent: Makes it obvious which attributes are for enhancements
- Future-proof: Protects against future attribute additions to custom elements
- Consistency: Provides a standard convention across all enhanced elements
- Selective override: Pattern-based
allowUnprefixedlets you opt-in specific element families while maintaining strict isolation for others
While automatic parsing is the recommended approach, you can also call parseWithAttrs() manually when needed.
When calling parseWithAttrs() manually, pass the pattern as the third (optional) parameter:
// Allow unprefixed only for elements matching pattern
const result = parseWithAttrs(element, attrPatterns, '^my-');
// Or with RegExp
const result = parseWithAttrs(element, attrPatterns, /^(my-|app-)/);Pattern Matching:
- The pattern is tested against the element's lowercase tag name
- String patterns are automatically converted to RegExp
- If the tag name matches, unprefixed attributes are allowed (but
enh-still takes precedence) - If the tag name doesn't match, only
enh-prefixed attributes are read
Example:
<my-widget data-count="42"></my-widget>
<other-widget data-count="42"></other-widget>// Pattern: '^my-' (only matches "my-widget")
const result1 = parseWithAttrs(
document.querySelector('my-widget'),
{ base: 'data-', count: '${base}count', _count: { instanceOf: 'Number' } },
'^my-'
);
// result1.count = 42 (unprefixed allowed because tag matches)
const result2 = parseWithAttrs(
document.querySelector('other-widget'),
{ base: 'data-', count: '${base}count', _count: { instanceOf: 'Number' } },
'^my-'
);
// result2.count = undefined (unprefixed ignored because tag doesn't match)Basic Usage
import { parseWithAttrs } from 'assign-gingerly/parseWithAttrs';
const element = document.querySelector('#myElement');
const config = parseWithAttrs(element, {
base: 'data-',
count: '${base}count',
_count: {
instanceOf: 'Number',
mapsTo: 'itemCount'
}
});Error Handling
The function throws descriptive errors for common issues:
// Circular reference
parseWithAttrs(element, {
a: '${b}',
b: '${a}' // Error: Circular reference detected
});
// Undefined variable
parseWithAttrs(element, {
name: '${missing}' // Error: Undefined template variable: missing
});
// Invalid JSON
// HTML: <div data-obj='{invalid}'></div>
parseWithAttrs(element, {
base: 'data-',
obj: '${base}obj',
_obj: { instanceOf: 'Object' }
// Error: Failed to parse JSON: "{invalid}"
});
// Invalid number
// HTML: <div data-count="abc"></div>
parseWithAttrs(element, {
base: 'data-',
count: '${base}count',
_count: { instanceOf: 'Number' }
// Error: Failed to parse number: "abc"
});Base Attribute Validation:
The base attribute must contain either a dash (-) or a non-ASCII character to prevent conflicts with native attributes:
// Valid base attributes
const enhConfig1 = { base: 'data-config' }; // Has dash
const enhConfig2 = { base: '🎨-theme' }); // Has non-ASCII (and dash)
// Invalid - throws error
const enhConig3 = { base: 'config' }; // No dash or non-ASCIIThe parseWithAttrs function accepts an AttrPatterns object that defines:
- Attribute name templates: String values with
${variable}placeholders - Configuration objects: Properties prefixed with
_that specify parsing behavior
interface AttrPatterns<T> {
base?: string; // Base attribute name prefix
_base?: AttrConfig<T>; // Configuration for base attribute
[key: string]: string | AttrConfig<T>; // Other attributes and configs
}
interface AttrConfig<T> {
mapsTo?: keyof T | '.'; // Target property name (or '.' to spread)
instanceOf?: string | Function; // Type for default parser
parser?:
| ((v: string | null) => any) // Inline parser function
| string // Named parser from globalParserRegistry
| [string, string]; // [CustomElementName, StaticMethodName]
}Template Variables
Attribute names support template variables using ${varName} syntax:
// HTML: <div data-user-name="Alice" data-user-age="30"></div>
const result = parseWithAttrs(element, {
base: 'data-',
user: '${base}user',
name: '${user}-name',
age: '${user}-age'
});
// Result: { name: 'Alice', age: '30' }Template variables are resolved recursively and cached for performance. Circular references are detected and throw an error.
Type Parsing with instanceOf
The instanceOf property determines how attribute values are parsed:
// HTML: <div data-count="42" data-active data-tags='["a","b"]'></div>
const result = parseWithAttrs(element, {
base: 'data-',
count: '${base}count',
_count: { instanceOf: 'Number' },
active: '${base}active',
_active: { instanceOf: 'Boolean' }, // Presence check
tags: '${base}-tags',
_tags: { instanceOf: 'Array' }
});
// Result: { count: 42, active: true, tags: ['a', 'b'] }Built-in type parsers:
String: Identity (default)Number: Parses numeric values, throws on invalid numbersBoolean: Presence check (attribute exists = true)Object: Parses JSON objectsArray: Parses JSON arrays
Custom Parsers
Provide a custom parser function for specialized parsing:
// HTML: <div data-timestamp="2024-01-15T10:30:00Z"></div>
const result = parseWithAttrs(element, {
base: 'data-',
timestamp: '${base}timestamp',
_timestamp: {
mapsTo: 'createdAt',
parser: (v) => v ? new Date(v).getTime() : null
}
});
// Result: { createdAt: 1705315800000 }Named Parsers for Reusability and JSON Serialization
Instead of inline functions, you can reference parsers by name, making configs JSON serializable and parsers reusable:
import { globalParserRegistry, parseWithAttrs } from 'assign-gingerly';
// Register parsers once (typically in app initialization)
globalParserRegistry.register('timestamp', (v) =>
v ? new Date(v).getTime() : null
);
globalParserRegistry.register('csv', (v) =>
v ? v.split(',').map(s => s.trim()) : []
);
// Use by name - config is now JSON serializable!
const config = {
base: 'data-',
created: '${base}created',
_created: {
parser: 'timestamp' // String reference instead of function
},
tags: '${base}tags',
_tags: {
parser: 'csv'
}
