Comparing version 1.7.5 to 1.8.0
47
index.js
@@ -102,9 +102,13 @@ var keypather = module.exports = function (opts) { | ||
var split = []; | ||
var open = false; | ||
var buffer, preParen; | ||
var openParen = false; | ||
var openBracket = false; | ||
var parenBuffer, bracketBuffer, preParen, preBracket; | ||
dotSplit.forEach(function groupParens (part) { | ||
var parenSplit, leftover; | ||
if (!open && ~part.indexOf('(')) { | ||
open = true; | ||
buffer = []; | ||
var parenSplit, leftover, bracketSplit; | ||
if (part.length === 0) { | ||
return; | ||
} | ||
else if (!openParen && ~part.indexOf('(')) { | ||
openParen = true; | ||
parenBuffer = []; | ||
parenSplit = part.split('('); | ||
@@ -115,8 +119,8 @@ preParen = parenSplit.shift() || ''; | ||
} | ||
else if (open) { | ||
else if (openParen) { | ||
if (~part.indexOf(')')) { | ||
open = false; | ||
openParen = false; | ||
parenSplit = part.split(')'); | ||
buffer.push(parenSplit.shift()); | ||
split.push(preParen+'('+buffer.join('.')+')'); | ||
parenBuffer.push(parenSplit.shift()); | ||
split.push(preParen+'('+parenBuffer.join('.')+')'); | ||
leftover = parenSplit.join(')'); | ||
@@ -126,5 +130,26 @@ if (leftover.length) groupParens(leftover); | ||
else { | ||
buffer.push(part); | ||
parenBuffer.push(part); | ||
} | ||
} | ||
else if (!openBracket && ~part.indexOf('[')) { | ||
openBracket = true; | ||
bracketBuffer = []; | ||
bracketSplit = part.split('['); | ||
preBracket = bracketSplit.shift() || ''; | ||
leftover = bracketSplit.join('['); | ||
if (leftover.length) groupParens(leftover); | ||
} | ||
else if (openBracket) { | ||
if (~part.indexOf(']')) { | ||
openBracket = false; | ||
bracketSplit = part.split(']'); | ||
bracketBuffer.push(bracketSplit.shift()); | ||
split.push(preBracket+'['+bracketBuffer.join('.')+']'); | ||
leftover = bracketSplit.join(']'); | ||
if (leftover.length) groupParens(leftover); | ||
} | ||
else { | ||
bracketBuffer.push(part); | ||
} | ||
} | ||
else { | ||
@@ -131,0 +156,0 @@ split.push(part); |
{ | ||
"name": "keypather", | ||
"version": "1.7.5", | ||
"version": "1.8.0", | ||
"description": "Get or set a object values from a keypath string. Supports bracket notation, dot notation, and functions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# keypather [![Build Status](https://travis-ci.org/tjmehta/keypather.png?branch=master)](https://travis-ci.org/tjmehta/keypather) | ||
Get or set a object values from a keypath string. Supports bracket notation, dot notation, and functions | ||
Get or set a object values from a keypath string. Supports bracket notation, dot notation, and functions. | ||
Ignores errors for deep-keypaths by default. | ||
@@ -209,2 +210,2 @@ # installation | ||
# License | ||
### MIT | ||
### MIT |
@@ -30,2 +30,48 @@ var keypather = require('../index')(); | ||
}); | ||
}); | ||
describe("keypather.get(obj, '['foo.bar.baz']')", function () { | ||
before(function () { | ||
this.obj = {foo:{bar:{baz:'foo.bar.bas'}}}; | ||
this.obj['foo.bar.baz'] = Math.random(); | ||
}); | ||
it('should get the value', function () { | ||
keypather.get(this.obj, "['foo.bar.baz']").should.eql(this.obj['foo.bar.baz']); | ||
}); | ||
}); | ||
describe("keypather.get(obj, 'some['foo.bar.baz']')", function () { | ||
before(function () { | ||
this.obj = {foo:{bar:{baz:'foo.bar.bas'}}}; | ||
this.obj['foo.bar.baz'] = Math.random(); | ||
this.obj.some = { 'foo.bar.baz': Math.random() }; | ||
}); | ||
it('should get the value', function () { | ||
keypather.get(this.obj, "some['foo.bar.baz']").should.eql(this.obj.some['foo.bar.baz']); | ||
}); | ||
}); | ||
describe("keypather.get(obj, '['foo.bar.baz'].some')", function () { | ||
before(function () { | ||
this.obj = {foo:{bar:{baz:'foo.bar.bas'}}}; | ||
this.obj['foo.bar.baz'] = { | ||
one: Math.random(), | ||
some: Math.random() | ||
}; | ||
this.obj.some = { 'foo.bar.baz': Math.random() }; | ||
}); | ||
it('should get the value', function () { | ||
keypather.get(this.obj, "['foo.bar.baz'].some").should.eql(this.obj['foo.bar.baz'].some); | ||
}); | ||
}); | ||
describe("keypather.get(obj, '['foo.bar.baz']['his.hers']')", function () { | ||
before(function () { | ||
this.obj = {foo:{bar:{baz:'foo.bar.bas'}}}; | ||
this.obj['foo.bar.baz'] = { | ||
one: Math.random(), | ||
some: Math.random(), | ||
'his.hers': Math.random() | ||
}; | ||
this.obj.some = { 'foo.bar.baz': Math.random() }; | ||
}); | ||
it('should get the value', function () { | ||
keypather.get(this.obj, "['foo.bar.baz']['his.hers']").should.eql(this.obj['foo.bar.baz']['his.hers']); | ||
}); | ||
}); | ||
}); |
@@ -30,2 +30,14 @@ var keypather = require('../index')(); | ||
}); | ||
describe("keypather.get(obj, '()()')", function () { | ||
before(function () { | ||
this.obj = function () { | ||
return function () { | ||
return 'val'; | ||
}; | ||
}; | ||
}); | ||
it('should get the value', function () { | ||
keypather.get(this.obj, '()()').should.eql(this.obj()()); | ||
}); | ||
}); | ||
describe("keypather.get(obj, 'foo.bar()')", function () { | ||
@@ -32,0 +44,0 @@ before(function () { |
@@ -24,2 +24,4 @@ var keypather = require('../index')(); | ||
describe("keypather.has(obj, '['foo']['no']['no']')", keyInObject("['foo']['no']['no']", false)); | ||
describe("keypather.has(obj, '['foo.bar']')", keyInObject("['foo.bar']", false)); | ||
describe("keypather.has(obj, 'foo['dot.path']')", keyInObject("foo['dot.path']", true)); | ||
}); | ||
@@ -33,3 +35,4 @@ | ||
bar: 'value', | ||
qux: 1 | ||
qux: 1, | ||
'dot.path': 1 | ||
} | ||
@@ -36,0 +39,0 @@ }; |
@@ -16,10 +16,12 @@ var keypather = require('../index')(); | ||
describe("keypather.in(obj, '['foo']['deep']')", keyInObject("['foo']['deep']", true)); | ||
describe("keypather.has(obj, 'foo.no')", keyInObject("foo.no", false)); | ||
describe("keypather.has(obj, '['foo'].no')", keyInObject("['foo'].no", false)); | ||
describe("keypather.has(obj, 'foo['no']')", keyInObject("foo['no']", false)); | ||
describe("keypather.has(obj, '['foo']['no']')", keyInObject("['foo']['no']", false)); | ||
describe("keypather.has(obj, 'foo.no.no')", keyInObject("foo.no.no", false)); | ||
describe("keypather.has(obj, '['foo'].no.no')", keyInObject("['foo'].no.no", false)); | ||
describe("keypather.has(obj, 'foo['no']['no']')", keyInObject("foo['no']['no']", false)); | ||
describe("keypather.has(obj, '['foo']['no']['no']')", keyInObject("['foo']['no']['no']", false)); | ||
describe("keypather.in(obj, 'foo.no')", keyInObject("foo.no", false)); | ||
describe("keypather.in(obj, '['foo'].no')", keyInObject("['foo'].no", false)); | ||
describe("keypather.in(obj, 'foo['no']')", keyInObject("foo['no']", false)); | ||
describe("keypather.in(obj, '['foo']['no']')", keyInObject("['foo']['no']", false)); | ||
describe("keypather.in(obj, 'foo.no.no')", keyInObject("foo.no.no", false)); | ||
describe("keypather.in(obj, '['foo'].no.no')", keyInObject("['foo'].no.no", false)); | ||
describe("keypather.in(obj, 'foo['no']['no']')", keyInObject("foo['no']['no']", false)); | ||
describe("keypather.in(obj, '['foo']['no']['no']')", keyInObject("['foo']['no']['no']", false)); | ||
describe("keypather.in(obj, '['foo.bar']')", keyInObject("['foo.bar']", false)); | ||
describe("keypather.in(obj, 'foo['dot.path']')", keyInObject("foo['dot.path']", true)); | ||
}); | ||
@@ -33,3 +35,4 @@ | ||
bar: 'value', | ||
qux: 1 | ||
qux: 1, | ||
'dot.path': 1 | ||
} | ||
@@ -36,0 +39,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
38745
1011
211
0