pax_global_header00006660000000000000000000000064126611404520014513gustar00rootroot0000000000000052 comment=abdad539338536e57d2f3317200de944c04f5ae8 object-key-0.2.0/000077500000000000000000000000001266114045200135465ustar00rootroot00000000000000object-key-0.2.0/.gitignore000066400000000000000000000010211266114045200155300ustar00rootroot00000000000000# Logs logs *.log npm-debug.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 # node-waf configuration .lock-wscript # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory node_modules # Optional npm cache directory .npm # Optional REPL history .node_repl_history object-key-0.2.0/LICENSE000066400000000000000000000021101266114045200145450ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Fabrício Tavares de Oliveira 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. object-key-0.2.0/README.md000066400000000000000000000013041266114045200150230ustar00rootroot00000000000000# object-key Assing value to an object property using path string separated by dots and lookup values using the same dot separated string paths. ## Usage ### ok.assign(key, value, hash[, stringCase]) `stringCase` might be: - camel - kebab - snake ``` const ok = require('ok'); var path = 'foo.bar.baz'; var path2 = 'foo.barBar.baz'; var obj = {}; ok.assign(obj, path, 'some value'); // { foo: { bar: { baz: 'some value' }}} ok.assign(obj, path2, 'some value', 'kebab'); // { foo: { 'bar-bar': { baz: 'some value' }}} ``` ### ok.lookup(key, hash) ``` const ok = require('ok'); var path = 'foo.bar.baz'; var obj = { foo: { bar: { baz: 'some value' }}}; ok.lookup(path, obj); // 'some value' ``` object-key-0.2.0/index.js000066400000000000000000000031461266114045200152170ustar00rootroot00000000000000'use strict'; if (typeof module === 'object' && module.exports) { var _ = require('lodash'); } !function() { var ok = { version: '0.1.0' }; /** * Assign value to object property. * * @param {string} key * @param {(string|number|object|array)} value * @param {object} hash * @param {string} stringCase * * @return {object} */ ok.assign = function assign(object, path, value, stringCase) { var convertCase; switch(stringCase) { case 'camel': convertCase = _.camelCase; break; case 'kebab': convertCase = _.kebabCase; break; case 'snake': convertCase = _.snakeCase; break; default: convertCase = function convertCase(d) { return d; }; break; } let keys = path.split('.'); keys = _.map(keys, convertCase); keys = keys.join('.'); return _.set(object, keys, value); } /** * Lookup key value * * @param {string} key * @param {object} hash * * @return {(string|number|object|array)} */ ok.lookup = function lookup(key, hash) { let firstKey; let idx; let remainingKeys; if (hash[key] !== undefined) { return hash[key]; } if ((idx = key.indexOf('.')) !== -1) { firstKey = key.substr(0, idx); remainingKeys = key.substr(idx + 1); hash = hash[firstKey]; if (hash) { return lookup(remainingKeys, hash); } } } if (typeof define === 'function' && define.amd) { define(ok); } else if (typeof module === 'object' && module.exports) { module.exports = ok; } else { this.ok = ok; } }(); object-key-0.2.0/package.json000066400000000000000000000012321266114045200160320ustar00rootroot00000000000000{ "name": "object-key", "version": "0.2.0", "description": "Object key helpers", "main": "index.js", "scripts": { "test": "node_modules/.bin/mocha test --timeout 15000 --recursive --reporter list" }, "repository": { "type": "git", "url": "https://github.com/fabriciotav/object-key.git" }, "keywords": [ "object", "key", "property" ], "author": "Fabrício Tavares", "license": "MIT", "bugs": { "url": "https://github.com/fabriciotav/object-key/issues" }, "homepage": "https://github.com/fabriciotav/object-key", "dependencies": { "lodash": "4.2.1" }, "devDependencies": { "mocha": "2.4.5" } } object-key-0.2.0/test/000077500000000000000000000000001266114045200145255ustar00rootroot00000000000000object-key-0.2.0/test/ok-test.js000066400000000000000000000031711266114045200164530ustar00rootroot00000000000000const assert = require('assert'); const ok = require('../index'); describe('assign', function() { it('single', function() { assert.deepEqual({ random: 'value' }, ok.assign({}, 'random', 'value')); }); it('single', function() { assert.deepEqual({ randomValue: 'value' }, ok.assign({}, 'randomValue', 'value')); }); it('nested', function() { assert.deepEqual({ foo: { bar: { baz: 'value' }}}, ok.assign({}, 'foo.bar.baz', 'value', 'kebab')); }); it('kebabCase', function() { assert.deepEqual({ 'random-value': 'value' }, ok.assign({}, 'randomValue', 'value', 'kebab')); }); it('camelCase', function() { assert.deepEqual({ randomValue: 'value' }, ok.assign({}, 'random_value', 'value', 'camel')); }); it('snakeCase', function() { assert.deepEqual({ random_value: 'value' }, ok.assign({}, 'random_value', 'value', 'snake')); }); }); describe('lookup', function() { it('single', function() { assert.deepEqual('value', ok.lookup('random', { random: 'value' })); }); it('single', function() { assert.deepEqual('value', ok.lookup('randomValue', { randomValue: 'value' })); }); it('nested', function() { assert.deepEqual('value', ok.lookup('random-value.nested', { 'random-value': { nested: 'value' }})); }); it('kebabCase', function() { assert.deepEqual('value', ok.lookup('random-value', { 'random-value': 'value' })); }); it('camelCase', function() { assert.deepEqual('value', ok.lookup('random_value', { random_value: 'value' })); }); it('snakeCase', function() { assert.deepEqual('value', ok.lookup('random_value', { random_value: 'value' })); }); });