derived-property

1.2.0 • Public • Published

derived-property

Build Status Coverage Status

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 to true. Set off for non-pure derived properties (i.e - relies on Date.now()).
  • getMethod {Function}: Optional getter method to access the dependencies on the object. Defaults to lodash.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

Package Sidebar

Install

npm i derived-property

Weekly Downloads

2

Version

1.2.0

License

MIT

Last publish

Collaborators

  • pgilad