Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

propget

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

propget - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

25

index.js

@@ -14,2 +14,3 @@ 'use strict';

* @param {String} prop Property structure.
* @param {Args} args Possible arguments for function calls.
* @returns {Mixed}

@@ -19,5 +20,12 @@ * @api private

module.exports = function find(data, prop) {
if (!prop || !~prop.indexOf('.') || prop in data) return data[prop];
data = data || {};
prop = prop || '';
var paths = prop.split('.')
//
// 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

@@ -29,3 +37,14 @@ , structure = data

for (; i < length && structure; i++) {
result = structure[paths[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;

@@ -32,0 +51,0 @@ }

8

package.json
{
"name": "propget",
"version": "1.0.0",
"version": "1.1.0",
"description": "Use dot notation to get properties from deeply nested object and array structures.",

@@ -25,6 +25,6 @@ "main": "index.js",

"devDependencies": {
"assume": "1.2.x",
"mocha": "2.2.x",
"pre-commit": "1.0.x"
"assume": "1.4.x",
"mocha": "3.2.x",
"pre-commit": "1.2.x"
}
}

@@ -20,8 +20,17 @@ # propget

Using this module is super simple. It exports a single function which accepts
2 arguments;
Using the module is super simple. We export the `propget` method as default
function:
1. The object that it needs to extract the data from
2. A dot notated string for deeply nested object access.
```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

@@ -38,4 +47,42 @@ 'use strict';

#### 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

@@ -31,3 +31,3 @@ describe('propget', function () {

it('can find keys that have dot noatiation', function () {
it('can find keys that have dot notation', function () {
var data = { 'foo.bar': 'lol' };

@@ -37,2 +37,48 @@

});
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');
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc