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?
101/set.js /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
63 lines (59 sloc)
1.68 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
| /** | |
| * @module 101/set | |
| */ | |
| var isString = require('./is-string'); | |
| var isNumber = require('./is-number'); | |
| var isObject = require('./is-object'); | |
| var keypather = require('keypather')(); | |
| /** | |
| * Functional version of obj[key] = val. | |
| * When only a key and val are specified set returns a partial-function which accepts obj. | |
| * @function module:101/set | |
| * @param {*} [obj] - object on which the values will be set | |
| * @param {string} key - key of the value being set on obj | |
| * @param {*} val - value of the key being set on obj | |
| * @return {*|function} The same obj with new value set or Partial-function set (which accepts obj) and returns the same obj with val set | |
| */ | |
| module.exports = set; | |
| function set (obj, key, val) { | |
| var setObj; | |
| if (arguments.length === 1) { | |
| // (setObj) | |
| setObj = obj; | |
| return function (obj) { | |
| return setKeypaths(obj, setObj); // extends original | |
| }; | |
| } | |
| if (arguments.length === 2) { | |
| if (isString(obj) || isNumber(obj)) { | |
| // (key, val) | |
| val = key; | |
| key = obj; | |
| setObj = {}; | |
| keypather.set(setObj, key, val); | |
| return function (obj) { | |
| return setKeypaths(obj, setObj); // extends original | |
| }; | |
| } | |
| else if (isObject(key)) { | |
| // (obj, setObj) | |
| setObj = key; | |
| return setKeypaths(obj, setObj); // extends original | |
| } | |
| else { | |
| throw new TypeError('Invalid arguments: expected string, number, or object'); | |
| } | |
| } | |
| else { | |
| setObj = {}; | |
| setObj[key] = val | |
| return setKeypaths(obj, setObj); // extends original | |
| } | |
| } | |
| function setKeypaths (obj, setObj) { | |
| Object.keys(setObj).forEach(function (keypath) { | |
| var val = setObj[keypath]; | |
| keypather.set(obj, keypath, val); | |
| }); | |
| return obj; | |
| } |