package/package.json000644 000766 000024 0000001135 12636365703013031 0ustar00000000 000000 { "name": "obj-util", "version": "2.0.0", "description": "A simple helper to set/get keys from objects using a string path like 'some.key.here'", "main": "index.js", "scripts": { "test": "test" }, "repository": { "type": "git", "url": "https://github.com/royriojas/obj-util.git" }, "keywords": [ "obj", "util", "setKey", "getKey", "set", "key", "using", "path" ], "author": "Roy Riojas", "license": "MIT", "bugs": { "url": "https://github.com/royriojas/obj-util/issues" }, "homepage": "https://github.com/royriojas/obj-util" } package/.npmignore000644 000766 000024 0000001113 12477236643012541 0ustar00000000 000000 # Logs logs *.log # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory # Commenting this out is preferred by some people, see # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- node_modules # Users Environment Variables .lock-wscript package/README.md000644 000766 000024 0000002302 12477237506012021 0ustar00000000 000000 # obj-util > A simple helper to set/get keys from objects using a string path like 'some.key.here' ## Install ```bash npm i --save obj-util ``` ## Usage ### obj.getKeyValue Returns the value from the given object which matches the passed key - **param** obj {Object} The object to get the values from - **param** key {String} a string representing a key in the object. Subkeys are supported separating them with dots. i.e. `key1.subkey1.subsubkey1` - **returns** {Mixed} the value of the given key in the passed obj ```javascript var objUtil = require('obj-util'); var obj = { some: { key: 'some value' } }; // getKeyValue objUtil.getKeyValue(obj, 'some.key'); // 'some value' ``` ### obj.setKeyValue Sets a value in an object if a matching key is found inside the given object - **param** obj {Object} the object where to set the value using if the key is found - **param** key {String} a string that represents the key. Subkeys are supported by separating them with dots. - **param** val the value to be set in the object ```javascript var objUtil = require('obj-util'); var obj = { }; objUtil.setKeyValue(obj, 'some.key', 'some value'); obj.some.key === 'some value' //==> true ``` That's it!package/LICENSE000644 000766 000024 0000002066 12477236643011557 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2015 Roy Riojas Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/index.js000644 000766 000024 0000004443 12636365535012220 0ustar00000000 000000 /** * * Module with utilities to set and retrieve values on an object using a `path` * @class objUtil * @static */ var objUtil = { /** * Returns the value from the given object which matches the passed key * * @method getKeyValue * @param obj {Object} The object to get the values from * @param key {String} a string representing a key in the object. Subkeys are supported * separating them with dots. i.e. `key1.subkey1.subsubkey1` * @returns {Mixed} the value of the given key in the passed obj * @example * ```javascript * var objUtil = require('obj-util'); * * var obj = { * some: { * key: 'some value' * } * }; * * objUtil.getKeyValue(obj, 'some.key'); // 'some value' * * ``` */ getKeyValue: function ( obj, key ) { if ( !obj ) { return null; } var temp = obj, keys = key.split( '.' ); for (var ix = 0; ix < keys.length; ix++) { var theKey = keys[ ix ]; temp = temp[ theKey ]; if (typeof temp === 'undefined') { return undefined; } if ( temp === null ) { return null; } } return temp; }, /** * Sets a value in an object if a matching key is found inside the given object * * @method setKeyValue * @param obj {Object} the object where to set the value using if the key is found * @param key {String} a string that represents the key. Subkeys are supported by separating them with dots. * @param val the value to be set in the object * @example * ```javascript * var objUtil = require('obj-util'); * * var obj = { * }; * objUtil.setKeyValue(obj, 'some.key', 'some value'); * obj.some.key === 'some value' //==> true * ``` */ setKeyValue: function ( obj, key, val ) { if ( !obj ) { return; } var temp = obj, keys = key.split( '.' ); for (var ix = 0, len = keys.length; ix < len; ix++) { var theKey = keys[ ix ], t = temp[ theKey ]; if ( typeof t === 'undefined' || t === null ) { var isPrevLast = (ix < len - 1); temp[ theKey ] = isPrevLast ? {} : val; if ( isPrevLast ) { temp = temp[ theKey ]; } } else { if ( ix < len ) { temp = t; } } } } }; module.exports = objUtil;