Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
set-getter/index.js /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
83 lines (70 sloc)
1.86 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*! | |
| * set-getter (https://github.com/doowb/set-getter) | |
| * | |
| * Copyright (c) 2016, Brian Woodward. | |
| * Licensed under the MIT License. | |
| */ | |
| 'use strict'; | |
| var toPath = require('to-object-path'); | |
| /** | |
| * Defines a getter function on an object using property path notation. | |
| * | |
| * ```js | |
| * var obj = {}; | |
| * getter(obj, 'foo', function() { | |
| * return 'bar'; | |
| * }); | |
| * ``` | |
| * @param {Object} `obj` Object to add property to. | |
| * @param {String|Array} `prop` Property string or array to add. | |
| * @param {Function} `getter` Getter function to add as a property. | |
| * @api public | |
| */ | |
| function setGetter(obj, prop, getter) { | |
| var key = toPath(arguments); | |
| return define(obj, key, getter); | |
| } | |
| /** | |
| * Define getter function on object or object hierarchy using dot notation. | |
| * | |
| * @param {Object} `obj` Object to define getter property on. | |
| * @param {String} `prop` Property string to define. | |
| * @param {Function} `getter` Getter function to define. | |
| * @return {Object} Returns original object. | |
| */ | |
| function define(obj, prop, getter) { | |
| if (!~prop.indexOf('.')) { | |
| defineProperty(obj, prop, getter); | |
| return obj; | |
| } | |
| var keys = prop.split('.'); | |
| var last = keys.pop(); | |
| var target = obj; | |
| var key; | |
| while ((key = keys.shift())) { | |
| while (key.slice(-1) === '\\') { | |
| key = key.slice(0, -1) + '.' + keys.shift(); | |
| } | |
| target = target[key] || (target[key] = {}); | |
| } | |
| defineProperty(target, last, getter); | |
| return obj; | |
| } | |
| /** | |
| * Define getter function on object as a configurable and enumerable property. | |
| * | |
| * @param {Object} `obj` Object to define property on. | |
| * @param {String} `prop` Property to define. | |
| * @param {Function} `getter` Getter function to define. | |
| */ | |
| function defineProperty(obj, prop, getter) { | |
| Object.defineProperty(obj, prop, { | |
| configurable: true, | |
| enumerable: true, | |
| get: getter | |
| }); | |
| } | |
| /** | |
| * Expose `setGetter` | |
| */ | |
| module.exports = setGetter; |