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

ak-opath

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ak-opath - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

40

lib/opath.js

@@ -6,4 +6,4 @@ 'use strict';

/**
* @param {String|Array} path
* @param {Object} obj
* @param {Array} path
* @return {Mixed}

@@ -32,8 +32,36 @@ */

/**
* @param {Object} obj
* @param {String} childKey
* @param {Array} path
* @return {Mixed}
*/
var opathChildKey = function (obj, childKey, path) {
var key = '';
if (! Array.isArray(path)) {
throw new Error('Invalid path.');
}
for (var i = 0, len = path.length; i < len; i += 1) {
key = path[i];
obj = obj[childKey];
if (! has(obj, key)) {
return;
}
obj = obj[key];
}
return obj;
};
/**
* Export `opath`
*
* @param {Object} obj
* @param {String} childKey (optional)
* @return {Function}
*/
module.exports = function (obj) {
module.exports = function (obj, childKey) {
if (! obj) {

@@ -43,4 +71,10 @@ throw new Error('Invalid argument.');

var fn = opath.bind(null, obj);
var fn;
if (childKey) {
fn = opathChildKey.bind(null, obj, childKey);
} else {
fn = opath.bind(null, obj);
}
Object.defineProperty(fn, 'reference', {

@@ -47,0 +81,0 @@ 'get': function () {

2

package.json
{
"name": "ak-opath",
"version": "0.0.1",
"version": "0.0.2",
"description": "access nested Object key with array of keys",

@@ -5,0 +5,0 @@ "keywords": [

@@ -25,2 +25,32 @@ /*global describe, it*/

};
var objChildKey = {
'children': {
'home': {
'children': {
'avetis': {
'children': {
'my projects': {
'children': {
'file1.js': 1,
'file2.js': false,
'\\': 4,
'falsePositive': undefined
}
},
'\\\\\\': {
'children': {
'good': 'evil',
}
}
}
},
'home': {
'children': {
'avetis': 'nested'
}
}
}
}
}
};

@@ -67,1 +97,34 @@ describe('opath = new Opath()', function () {

});
describe('opath = new Opath(obj, childKey)', function () {
it('should initialize', function () {
assert(new Opath(objChildKey, 'children') instanceof Function);
});
it('should load paths', function () {
var opath = new Opath(objChildKey, 'children');
assert.throws(function () {
opath('/home');
}, 'Invalid path.');
assert.strictEqual(opath([]), objChildKey);
assert.strictEqual(opath(['home']), objChildKey['children']['home']);
assert.strictEqual(opath(['home', 'home', 'avetis']), objChildKey['children']['home']['children']['home']['children']['avetis']);
assert.strictEqual(opath(['home', 'avetis', 'my projects']), objChildKey['children']['home']['children']['avetis']['children']['my projects']);
assert.strictEqual(opath(['home', 'avetis', 'my projects', 'file1.js']), 1);
assert.strictEqual(opath(['home', 'avetis', 'my projects', 'file2.js']), false);
assert.strictEqual(opath(['home', 'avetis', '\\\\\\']), objChildKey['children']['home']['children']['avetis']['children']['\\\\\\']);
assert.strictEqual(opath(['home', 'avetis', '\\\\\\', 'good']), objChildKey['children']['home']['children']['avetis']['children']['\\\\\\']['children']['good']);
assert.strictEqual(opath(['home', 'avetis', 'my projects', 'falsePositive']), undefined);
assert.strictEqual(opath(['home', 'avetis', 'do-not-exist', 'failMePlease']), undefined);
});
it('should return reference objChildKey', function () {
var opath = new Opath(objChildKey, 'children');
assert(opath.reference === objChildKey);
assert.throws(function () {
opath.reference = {};
});
});
});
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