pax_global_header00006660000000000000000000000064130325014420014504gustar00rootroot0000000000000052 comment=194bfa11d43f2850c7d7a18ba4b1fada7377d793 propget-1.1.0/000077500000000000000000000000001303250144200131635ustar00rootroot00000000000000propget-1.1.0/.gitignore000066400000000000000000000000151303250144200151470ustar00rootroot00000000000000node_modules propget-1.1.0/LICENSE.md000066400000000000000000000021371303250144200145720ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Arnout Kazemier, Martijn Swaagman, the Contributors. 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. propget-1.1.0/README.md000066400000000000000000000036111303250144200144430ustar00rootroot00000000000000# propget Propget is a small helper utility for finding values/keys in deeply nested objects without having to worry about undefined properties and what not. It uses a human readable dot based notation to find items in your object or array. ## Installation Just install it through npm, like you do with the all your code: ``` npm install --save propget ``` It doesn't use any fancy node.js magic or ES6 so it should be good to go for browser usage as well using something as browserify. ## Usage Using the module is super simple. We export the `propget` method as default function: ```js var propget = require('propget'); ``` The function accepts the following arguments: - `object`, data structure that we need to walk. - `string`, dot notated string for deeply nested object access. - `..`, rest arguments that will be used for optional function calls. So accessing a complex data structure can be as easy as this: ```js 'use strict'; var prop = require('propget') , data = { hello: 'world', yo: { deeply: ['nested', 'arrays'] } }; prop(data, 'hello'); // world prop(data, 'yo.deeply.1'); // arrays prop(data, 'yo.deeply.nested.unknown.keys'); // undefined ``` #### Function execution Of one the unique functions of this module is that it allows you to execute functions that are inside data structure. We can then re-use the result of the function and walk it further. ```js data = { hello: { world: function () { return { hi: 'hello' }; } } }; prop(data, 'hello.world().hi') // hello ``` But in addition to simple function execution we can also call these functions with arguments. We automatically use the additionally supplied arguments to the propget method for this. ```js data = { hello: { world: function (arg) { return { hi: arg }; } } }; prop(data, 'hello.world(what).hi', 'additional') // additional ``` ## License MIT propget-1.1.0/index.js000066400000000000000000000022021303250144200146240ustar00rootroot00000000000000'use strict'; /** * Small helper function that allows you get a key from an object by * specifying it's depth using dot notations. * * Example: * * - path.to.0.keys * - key.depth * * @param {Object|Array} data Data structure that we need search. * @param {String} prop Property structure. * @param {Args} args Possible arguments for function calls. * @returns {Mixed} * @api private */ module.exports = function find(data, prop) { data = data || {}; prop = prop || ''; // // Fastest match, direct match against a key in the data. // if (prop in data) return data[prop]; var args = Array.prototype.slice.call(arguments, 2) , paths = prop.split('.') , length = paths.length , structure = data , result = prop , i = 0; for (; i < length && structure; i++) { var fn = /(.+)\(([^\)]+?)?\)$/g.exec(paths[i]); if (fn) { if (!fn[2]) { result = structure[fn[1]](); } else { result = structure[fn[1]].apply(null, args.splice(0, fn[2].split(/[\s,]+/).length)); } } else { result = structure[paths[i]]; } structure = result; } return result; }; propget-1.1.0/package.json000066400000000000000000000012211303250144200154450ustar00rootroot00000000000000{ "name": "propget", "version": "1.1.0", "description": "Use dot notation to get properties from deeply nested object and array structures.", "main": "index.js", "scripts": { "test": "mocha test.js" }, "repository": { "type": "git", "url": "git+https://github.com/bigpipe/propget.git" }, "keywords": [ "prop", "property", "get" ], "author": "Arnout Kazemier", "license": "MIT", "bugs": { "url": "https://github.com/bigpipe/propget/issues" }, "homepage": "https://github.com/bigpipe/propget#readme", "devDependencies": { "assume": "1.4.x", "mocha": "3.2.x", "pre-commit": "1.2.x" } } propget-1.1.0/test.js000066400000000000000000000037141303250144200145050ustar00rootroot00000000000000describe('propget', function () { 'use strict'; var propget = require('./') , assume = require('assume'); it('is exported as function', function () { assume(propget).is.a('function'); }); it('can find item in array using dot notation', function () { var data = { foo: ['bar', 'baz'] }; assume(propget(data, 'foo.1')).equals('baz'); assume(propget(data, 'foo.3')).equals(undefined); }); it('can find deeply nested object', function () { var data = { foo: {'bar': 'baz'} }; assume(propget(data, 'foo.bar')).equals('baz'); assume(propget(data, 'foo.lol')).equals(undefined); }); it('does not worry about undefined keys', function () { var data = {}; assume(propget(data, 'foo.bar.l1.4.0.afa')).equals(undefined); }); it('can find keys that have dot notation', function () { var data = { 'foo.bar': 'lol' }; assume(propget(data, 'foo.bar')).equals('lol'); }); describe('function execution', function () { it('can execute a function using dot notation', function () { var data = { foo: { bar: function () { return 'baz'; } } }; assume(propget(data, 'foo.bar()')).equals('baz'); }); it('can execute a function with arguments using dot notation', function (next) { next = assume.plan(3, next); var data = { foo: { bar: function (arg1, arg2) { assume(arg1).equals('he'); assume(arg2).equals('hoo'); return 'baz'; } } }; assume(propget(data, 'foo.bar(first, second)', 'he', 'hoo')).equals('baz'); next(); }); it('can continue searching the returned object from function call', function () { var data = { foo: { bar: function () { return { what: 'lol' } } } }; assume(propget(data, 'foo.bar().what')).equals('lol'); }); }); });