1.2.0 • Published 9 years ago
derived-property v1.2.0
derived-property
Create a derived property for an object
Install
Install with npm
$ npm install derived-property --save
Usage
var derivedProperty = require('derived-property');
var obj = {
first: 'Gilad',
last: 'Peleg'
}
var displayName = derivedProperty({
dependencies: ['first', 'last'],
getter: function (first, last) {
return first + ' ' + last;
}
});
// apply the derived property
Object.defineProperty(obj, 'displayName', displayName);
console.log(obj.displayName);
// => 'Gilad Peleg'
// later on..
obj.first = 'John';
console.log(obj.displayName);
// => 'John Peleg'
API
derivedProperty
derivedProperty(options)
Create a derived property. Returns a response
that should be applied using Object.defineProperty(obj, 'property', response)
options
getter
{Function}: Getter function to do the calculation. Gets the values of dependencies as arguments.dependencies
{Array}: Optional list of properties to depend on.cache
{Boolean}: Whether to use the cached result if dependencies haven't changed. Defaults totrue
. Set off for non-pure derived properties (i.e - relies onDate.now()
).getMethod
{Function}: Optional getter method to access the dependencies on the object. Defaults tolodash.result
.compareMethod
{Function}: Optional compare method to check if the dependency has changed. Defaults to===
.
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Run tests
$ npm test
Related
License
MIT ©Gilad Peleg