property-expr
Advanced tools
Comparing version 1.3.1 to 1.3.2
117
index.js
/** | ||
* @license | ||
* expr 1.0.0 | ||
* Copyright 2014 Jason Quense | ||
* Based on Kendo UI Core expression code <https://github.com/telerik/kendo-ui-core#license-information> | ||
* Copyright :copyright: 2014 Telerik | ||
* Available under MIT license <https://github.com/theporchrat/expr/blob/master/LICENSE.txt> | ||
*/ | ||
'use strict'; | ||
var SPLIT_REGEX = /[^.^\]^[]+|(?=\[\]|\.\.)/g | ||
, DIGIT_REGEX = /^\d+$/; | ||
'use strict' | ||
var SPLIT_REGEX = /[^.^\]^[]+|(?=\[\]|\.\.)/g, | ||
DIGIT_REGEX = /^\d+$/, | ||
LEAD_DIGIT_REGEX = /^\d/, | ||
SPEC_CHAR_REGEX = /[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g | ||
var setCache = {} | ||
, getCache = {}; | ||
var setCache = {}, | ||
getCache = {} | ||
module.exports = { | ||
expr: expr, | ||
setter: function(path){ | ||
return setCache[path] || ( setCache[path] = new Function('data, value', 'return ' + expr(path, 'data') + ' = value, data;')) | ||
setter: function(path) { | ||
return ( | ||
setCache[path] || | ||
(setCache[path] = new Function( | ||
'data, value', | ||
expr(path, 'data') + ' = value' | ||
)) | ||
) | ||
}, | ||
@@ -26,14 +28,20 @@ | ||
var k = path + '_' + safe | ||
return getCache[k] || ( getCache[k] = new Function('data', 'return ' + expr(path, safe, 'data') )) | ||
return ( | ||
getCache[k] || | ||
(getCache[k] = new Function('data', 'return ' + expr(path, safe, 'data'))) | ||
) | ||
}, | ||
split: function(path){ | ||
split: function(path) { | ||
return path.match(SPLIT_REGEX) | ||
}, | ||
join: function(segments){ | ||
return segments.reduce(function(path, part){ | ||
return path + (isQuoted(part) || DIGIT_REGEX.test(part) | ||
? '['+ part + ']' | ||
: (path ? '.' : '') + part) | ||
join: function(segments) { | ||
return segments.reduce(function(path, part) { | ||
return ( | ||
path + | ||
(isQuoted(part) || DIGIT_REGEX.test(part) | ||
? '[' + part + ']' | ||
: (path ? '.' : '') + part) | ||
) | ||
}, '') | ||
@@ -47,9 +55,8 @@ }, | ||
function expr(expression, safe, param) { | ||
expression = expression || '' | ||
function expr(expression, safe, param){ | ||
expression = expression || '' | ||
if (typeof safe === 'string') { | ||
param = safe; | ||
safe = false; | ||
param = safe | ||
safe = false | ||
} | ||
@@ -59,18 +66,24 @@ | ||
if(expression && expression.charAt(0) !== '[') | ||
expression = '.' + expression | ||
if (expression && expression.charAt(0) !== '[') expression = '.' + expression | ||
return safe ? makeSafe(expression, param) : param + expression | ||
return safe ? makeSafe(expression, param) : param + expression | ||
} | ||
function forEach(parts, iter, thisArg){ | ||
var len = parts.length | ||
, part, idx, isArray, isBracket; | ||
function forEach(parts, iter, thisArg) { | ||
var len = parts.length, | ||
part, | ||
idx, | ||
isArray, | ||
isBracket | ||
for (idx = 0; idx < len; idx++){ | ||
for (idx = 0; idx < len; idx++) { | ||
part = parts[idx] | ||
if( part ) { | ||
if (part) { | ||
if (shouldBeQuoted(part)) { | ||
part = '"' + part + '"' | ||
} | ||
isBracket = isQuoted(part) | ||
isArray = !isBracket && /^\d+$/.test(part) | ||
isArray = !isBracket && /^\d+$/.test(part) | ||
@@ -82,17 +95,17 @@ iter.call(thisArg, part, isBracket, isArray, idx, parts) | ||
function isQuoted(str){ | ||
return typeof str === 'string' && str && ["'", '"'].indexOf(str.charAt(0)) !== -1 | ||
function isQuoted(str) { | ||
return ( | ||
typeof str === 'string' && str && ["'", '"'].indexOf(str.charAt(0)) !== -1 | ||
) | ||
} | ||
function makeSafe(path, param) { | ||
var result = param | ||
, parts = path.match(SPLIT_REGEX) | ||
, isLast; | ||
var result = param, | ||
parts = path.match(SPLIT_REGEX), | ||
isLast | ||
forEach(parts, function(part, isBracket, isArray, idx, parts){ | ||
isLast = idx === (parts.length - 1); | ||
forEach(parts, function(part, isBracket, isArray, idx, parts) { | ||
isLast = idx === parts.length - 1 | ||
part = ( isBracket || isArray) | ||
? '[' + part + ']' | ||
: '.' + part | ||
part = isBracket || isArray ? '[' + part + ']' : '.' + part | ||
@@ -102,3 +115,15 @@ result += part + (!isLast ? ' || {})' : ')') | ||
return new Array(parts.length + 1).join('(') + result; | ||
} | ||
return new Array(parts.length + 1).join('(') + result | ||
} | ||
function hasLeadingNumber(part) { | ||
return part.match(LEAD_DIGIT_REGEX) && !part.match(DIGIT_REGEX) | ||
} | ||
function hasSpecialChars(part) { | ||
return SPEC_CHAR_REGEX.test(part) | ||
} | ||
function shouldBeQuoted(part) { | ||
return !isQuoted(part) && (hasLeadingNumber(part) || hasSpecialChars(part)) | ||
} |
@@ -0,0 +0,0 @@ The MIT License (MIT) |
{ | ||
"name": "property-expr", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"description": "tiny util for getting and setting deep object props safely", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node ./test.js" | ||
"test": "node ./test.js", | ||
"debug": "node --inspect-brk ./test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/theporchrat/expr/" | ||
"url": "https://github.com/jquense/expr/" | ||
}, | ||
@@ -23,4 +24,8 @@ "keywords": [ | ||
], | ||
"author": "@theporchrat Jason Quense", | ||
"license": "MIT" | ||
"author": "@monasticpanic Jason Quense", | ||
"license": "MIT", | ||
"prettier": { | ||
"singleQuote": true, | ||
"semi": false | ||
} | ||
} |
@@ -0,0 +0,0 @@ expr |
118
test.js
@@ -1,55 +0,66 @@ | ||
var a = require('assert') | ||
, expression = require('./index.js') | ||
, getter = expression.getter | ||
, setter = expression.setter | ||
, obj = {} | ||
var a = require('assert'), | ||
expression = require('./index.js'), | ||
getter = expression.getter, | ||
setter = expression.setter, | ||
obj = {}; | ||
obj = { foo: { bar: ['baz', 'bux'], fux: 5 }} | ||
obj = { | ||
foo: { | ||
bar: ['baz', 'bux'], | ||
fux: 5, | ||
'00N40000002S5U0': 1, | ||
N40000002S5U0: 2, | ||
'FE43-D880-21AE': 3 | ||
} | ||
}; | ||
//--- Getters ----------------------------------------------- | ||
a.strictEqual(getter('foo.fux')(obj), 5) | ||
a.deepEqual(getter('foo.bar')(obj), ['baz', 'bux']) | ||
a.strictEqual(getter('foo.fux')(obj), 5); | ||
a.deepEqual(getter('foo.bar')(obj), ['baz', 'bux']); | ||
a.strictEqual(getter('foo.bar[1]')(obj), 'bux') | ||
a.strictEqual(getter('["foo"]["bar"][1]')(obj), 'bux') | ||
a.strictEqual(getter('[1]')([1, 'bux']), 'bux') | ||
a.strictEqual(getter('foo.bar[1]')(obj), 'bux'); | ||
a.strictEqual(getter('["foo"]["bar"][1]')(obj), 'bux'); | ||
a.strictEqual(getter('[1]')([1, 'bux']), 'bux'); | ||
//safe access | ||
a.strictEqual(getter('foo.fux', true)(obj), 5) | ||
a.deepEqual(getter('foo.bar', true)(obj), ['baz', 'bux']) | ||
a.strictEqual(getter('foo.fux', true)(obj), 5); | ||
a.deepEqual(getter('foo.bar', true)(obj), ['baz', 'bux']); | ||
a.strictEqual(getter('foo.bar[1]', true)(obj), 'bux') | ||
a.strictEqual(getter('["foo"]["bar"][1]', true)(obj), 'bux') | ||
a.strictEqual(getter('[1]', true)([1, 'bux']), 'bux') | ||
a.strictEqual(getter('foo.bar[1]', true)(obj), 'bux'); | ||
a.strictEqual(getter('["foo"]["bar"][1]', true)(obj), 'bux'); | ||
a.strictEqual(getter('[1]', true)([1, 'bux']), 'bux'); | ||
a.strictEqual(getter('foo.gih.df[0]', true)(obj), undefined) | ||
a.strictEqual(getter('["fr"]["bzr"][1]', true)(obj), undefined) | ||
a.strictEqual(getter('foo.gih.df[0]', true)(obj), undefined); | ||
a.strictEqual(getter('["fr"]["bzr"][1]', true)(obj), undefined); | ||
a.strictEqual(getter('foo["00N40000002S5U0"]', true)(obj), 1); | ||
a.strictEqual(getter('foo.00N40000002S5U0', true)(obj), 1); | ||
a.strictEqual(getter('foo["N40000002S5U0"]', true)(obj), 2); | ||
a.strictEqual(getter('foo.N40000002S5U0', true)(obj), 2); | ||
a.strictEqual(getter('foo["FE43-D880-21AE"]', true)(obj), 3); | ||
a.strictEqual(getter('foo.FE43-D880-21AE', true)(obj), 3); | ||
//--- Setters ----------------------------------------------- | ||
setter('foo.fux')(obj, 10) | ||
a.strictEqual(obj.foo.fux, 10) | ||
setter('foo.fux')(obj, 10); | ||
a.strictEqual(obj.foo.fux, 10); | ||
a.strictEqual( | ||
setter('foo.bar[1]')(obj, 'bot'), obj, 'setter must return the object') | ||
setter('foo.bar[1]')(obj, 'bot'); | ||
a.strictEqual(obj.foo.bar[1], 'bot'); | ||
a.strictEqual(obj.foo.bar[1], 'bot') | ||
setter('[\'foo\']["bar"][1]')(obj, 'baz'); | ||
a.strictEqual(obj.foo.bar[1], 'baz'); | ||
setter('[\'foo\']["bar"][1]')(obj, 'baz') | ||
a.strictEqual(obj.foo.bar[1], 'baz') | ||
// -- Split ------- | ||
var parts = expression.split('foo.baz["bar"][1]') | ||
var parts = expression.split('foo.baz["bar"][1]'); | ||
a.strictEqual(parts.length, 4) | ||
a.strictEqual(parts.length, 4); | ||
// -- JOIN ------- | ||
// -- JOIN ------- | ||
var parts = expression.split('foo.baz["bar"][1]') | ||
var parts = expression.split('foo.baz["bar"][1]'); | ||
a.strictEqual(expression.join(['0', 'baz', '"bar"', 1]), '[0].baz["bar"][1]') | ||
a.strictEqual(expression.join(['0', 'baz', '"bar"', 1]), '[0].baz["bar"][1]'); | ||
a.strictEqual(expression.join(parts), 'foo.baz["bar"][1]') | ||
a.strictEqual(expression.join(parts), 'foo.baz["bar"][1]'); | ||
@@ -60,30 +71,35 @@ // -- ForEach ------ | ||
expression.forEach('foo.baz["bar"][1]', function(part, isBracket, isArray, idx) { | ||
expression.forEach('foo.baz["bar"][1]', function( | ||
part, | ||
isBracket, | ||
isArray, | ||
idx | ||
) { | ||
count = idx; | ||
switch (idx){ | ||
switch (idx) { | ||
case 0: | ||
a.strictEqual(part, 'foo') | ||
a.strictEqual(isBracket, false) | ||
a.strictEqual(isArray, false) | ||
a.strictEqual(part, 'foo'); | ||
a.strictEqual(isBracket, false); | ||
a.strictEqual(isArray, false); | ||
break; | ||
case 1: | ||
a.strictEqual(part, 'baz') | ||
a.strictEqual(isBracket, false) | ||
a.strictEqual(isArray, false) | ||
a.strictEqual(part, 'baz'); | ||
a.strictEqual(isBracket, false); | ||
a.strictEqual(isArray, false); | ||
break; | ||
case 2: | ||
a.strictEqual(part, '"bar"') | ||
a.strictEqual(isBracket, true) | ||
a.strictEqual(isArray, false) | ||
a.strictEqual(part, '"bar"'); | ||
a.strictEqual(isBracket, true); | ||
a.strictEqual(isArray, false); | ||
break; | ||
case 3: | ||
a.strictEqual(part, '1') | ||
a.strictEqual(isBracket, false) | ||
a.strictEqual(isArray, true) | ||
a.strictEqual(part, '1'); | ||
a.strictEqual(isBracket, false); | ||
a.strictEqual(isArray, true); | ||
break; | ||
} | ||
}) | ||
}); | ||
a.strictEqual(count, 3) | ||
console.log('--- Tests Passed ---') | ||
a.strictEqual(count, 3); | ||
console.log('--- Tests Passed ---'); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
10344
204
1