set-getter NPM version NPM downloads Build Status

Create nested getter properties and any intermediary dot notation ('a.b.c') paths

Install

Install with npm:

  1. $ npm install set-getter --save

Usage

  1. var getter = require('set-getter');

set-getter works like set-value by adding a property to an object or an object hierarchy using dot notation. The main difference is that the property is added using Object.defineProperty and is expected to be a getter function that returns a value.

Example

  1. var obj = {};
  2. // root level property
  3. getter(obj, 'foo', function() {
  4. return 'bar';
  5. });
  6. console.log(obj.foo);
  7. //=> 'bar'
  8. // property dot notation
  9. getter(obj, 'bar.baz', function() {
  10. return 'qux';
  11. });
  12. console.log(obj.bar.baz);
  13. //=> 'qux'
  14. // property array notation
  15. getter(obj, ['beep', 'boop'], function() {
  16. return 'bop';
  17. });
  18. console.log(obj.beep.boop);
  19. //=> 'bop'

API

setGetter

Defines a getter function on an object using property path notation.

Params

  • obj {Object}: Object to add property to.
  • prop {String|Array}: Property string or array to add.
  • getter {Function}: Getter function to add as a property.

Example

  1. var obj = {};
  2. getter(obj, 'foo', function() {
  3. return 'bar';
  4. });

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

Generate readme and API documentation with verb:

  1. $ npm install verb && npm run docs

Or, if verb is installed globally:

  1. $ verb

Running tests

Install dev dependencies:

  1. $ npm install -d && npm test

Author

Brian Woodward

License

Copyright © 2016, Brian Woodward. Released under the MIT license.


This file was generated by verb, v0.9.0, on April 29, 2016.