Another way to combine outline and content into an object.
This is a fork of object-fusion and used as a component for [doubleshot][doubleshot] to combine specification with content.
The reason this is a fork, is object-fusion supports a simpler outline but lacks order. This fork requires arrays and therefore has order at each level.
Getting Started
Install the module with: npm install object-fusion2
// Load in objectFusionvar objectFusion2 = ; // An outline acts as the shell that keys will line up tovar outline = 'One': 'is equal to one'; // Content acts as the values to match up to the keys of valuevar content = { thisone = 1; } { assert; }; // Match up the strings to the valuesvar fusedObject = ; // The result looks like 'nodeName': 'One' { thisone = 1; } 'childNodes': 'nodeName': 'is equal to one' { assert; }
Documentation
objectFusion
exposes a single function for you to use.
;/** * Fuse outline and content objects together * @param * @param * @param * @param * @param * @param * @returns */
There is the Fuser
, available as objectFusion.Fuser
, which does the heavy lifting. If you provide a proxy, it will be invoked in a Fuser
context.
- Constructor for fusing mechanismFuser - Bind settings to Fuser - Retrieve settings from Fuser - Add value for translationFuser - Add object of values for translationFuser - Look value by Fuser - Look child from obj by nameFuser - Walk looking up value and child of each node // Fuser inherits from node's EventEmitter so feel free to use those methods// During looping, we expose: // `this.get('value key');` - the key used for accessing the value // `this.get('child key');` - the key used for accessing the child // `this.get('node');` - the node being returned // Emits `this.emit('value key used', valueKey);` when a value key is used// Emits `this.emit('value key not found', valueKey);` when a value key's value is not found// Emits `this.emit('child key used', childKey);` when a child key is used// Emits `this.emit('child key not found', childKey);` when a child key's value is not found
Lastly, there are proxies available which allow for key aliasing and array expansion.
objectFusion.aliasProxy
- Allows for aliasing of content values (e.g.{'uno': 'one'}
)- Emits
this.emit('content aliased', key, val);
when aliasing occurs
- Emits
objectFusion.expandProxy
- Allows for expansion of content values (e.g.{'two': ['one', 'plusOne']}
- Emits
this.emit('content expanded', key, val);
when expansion occurs
- Emits
objectFusion.aliasAndExpandProxy
- Allows for both aliasing and expansion of content values
Examples
Here is an advanced example using the aliasAndExpandProxy
// Create an outline and content to fusevar outline = '1 + 2': '= 3'; var content = '1 + 2': '1' '+ 2' '1': 'One' { thissum = 1; } '+ 2': 'plus two' { thissum += 2; } '= 3': 'is equal to three' { assert; }; // Fuse them togethervar fusedObject = ; // The result looks like 'nodeName': '1 + 2' 'value': { thissum = 1; } { thissum += 2; } 'childNodes': 'nodeName': '= 3' { assert; }
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint your code using grunt and test via npm test
.
License
Copyright (c) 2013 Todd Wolfson
Licensed under the MIT license.