https://github.com/pgilad/derived-property
Create a derived property for an object
https://github.com/pgilad/derived-property
Last synced: about 1 year ago
JSON representation
Create a derived property for an object
- Host: GitHub
- URL: https://github.com/pgilad/derived-property
- Owner: pgilad
- License: mit
- Created: 2015-07-13T03:18:38.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-07-13T19:45:46.000Z (almost 11 years ago)
- Last Synced: 2025-03-16T18:19:09.044Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 129 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# derived-property
[](https://travis-ci.org/pgilad/derived-property)
[](https://coveralls.io/github/pgilad/derived-property?branch=master)
> Create a derived property for an object
* [Install](#install)
* [Usage](#usage)
* [API](#api)
* [Contributing](#contributing)
* [Run tests](#run-tests)
* [Related](#related)
* [License](#license)
## Install
Install with [npm](https://www.npmjs.com/)
```sh
$ npm install derived-property --save
```
## Usage
```js
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](index.js#L44)
`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](https://github.com/pgilad/derived-property/issues)
## Run tests
```sh
$ npm test
```
## Related
- [computed-property](https://github.com/doowb/computed-property)
## License
MIT ©[Gilad Peleg](http://giladpeleg.com)