Comparing version 0.0.7 to 0.0.8
@@ -5,3 +5,3 @@ { | ||
"description": "Work with objects of different cased keys", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"keywords": [], | ||
@@ -8,0 +8,0 @@ "dependencies": { |
0.0.8 / 2014-08-29 | ||
================== | ||
* add performance test, getting 100000/1800 = 55ops/ms | ||
* refactor to iterate in reverse, so we can handle dots in key names | ||
0.0.8 / 2014-08-29 | ||
================== | ||
* Merge pull request #4 from segmentio/support/nested-dots | ||
* move comment to normalize | ||
* move out normalize method | ||
* add performance test, getting 100000/1800 = 55ops/ms | ||
* add performance test, getting 100000/1120 = 90ops/ms = 90000ops/sec | ||
* refactor to iterate in reverse, so we can handle dots in key names | ||
0.0.7 / 2014-06-12 | ||
@@ -3,0 +19,0 @@ ================== |
65
index.js
@@ -56,14 +56,48 @@ | ||
function multiple (fn) { | ||
return function (obj, key, val) { | ||
var keys = key.split('.'); | ||
if (keys.length === 0) return; | ||
return function (obj, path, val) { | ||
path = normalize(path); | ||
while (keys.length > 1) { | ||
key = keys.shift(); | ||
obj = find(obj, key); | ||
var key; | ||
var finished = false; | ||
if (obj === null || obj === undefined) return; | ||
while (!finished) loop(); | ||
function loop() { | ||
for (key in obj) { | ||
var normalizedKey = normalize(key); | ||
if (0 === path.indexOf(normalizedKey)) { | ||
path = path.substr(normalizedKey.length + 1); | ||
var child = obj[key]; | ||
// we're at the end and there is nothing. | ||
if (null == child) { | ||
finished = true; | ||
obj = null; | ||
return; | ||
} | ||
// we're at the end and there is something. | ||
if (!path.length) { | ||
finished = true; | ||
return; | ||
} | ||
// step into child | ||
obj = child; | ||
// but we're done here | ||
return; | ||
} | ||
} | ||
// if we found no matching properties | ||
// on the current object, there's no match. | ||
finished = true; | ||
} | ||
key = keys.shift(); | ||
// the `obj` and `key` is one above the leaf object and key, so | ||
// start object: { a: { 'b.c': 10 } } | ||
// end object: { 'b.c': 10 } | ||
// end key: 'b.c' | ||
// this way, you can do `obj[key]` and get `10`. | ||
return fn(obj, key, val); | ||
@@ -116,1 +150,16 @@ }; | ||
} | ||
/** | ||
* Normalize a `dot.separated.path`. | ||
* | ||
* A.HELLO_WORLD.bar => a.hello_world.bar | ||
* | ||
* @param {String} path | ||
* @return {String} | ||
*/ | ||
function normalize(path) { | ||
return path.split('.').map(function(part){ | ||
return Case.camel(part); | ||
}).join('.'); | ||
} |
{ | ||
"name": "obj-case", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Work with objects of different cased keys", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -27,5 +27,36 @@ | ||
}); | ||
it('should find dot-separated paths as object key', function () { | ||
var obj = { 'a.b': 10 }; | ||
expect(objCase(obj, 'a.b')).to.eql(10); | ||
}); | ||
it('should find dot-separated paths in a nested object', function () { | ||
var obj = { a: { 'b.c': 10 } }; | ||
expect(objCase(obj, 'a.b.c')).to.eql(10); | ||
}); | ||
describe('casing', function(){ | ||
it('should find crazy looking paths', function () { | ||
var obj = { a: { 'HelloWorld.BAR': 10 } }; | ||
expect(objCase(obj, 'A.HELLO_WORLD.bar')).to.eql(10); | ||
}); | ||
it('should find crazy looking paths 2', function () { | ||
var obj = { 'HELLO_WORLD.a.B': 10 }; | ||
expect(objCase(obj, 'helloWorld.a.B')).to.eql(10); | ||
}); | ||
it('should find crazy looking paths 3', function () { | ||
var obj = { 'some-crazy.PROBABLY_POSSIBLE.NestedProperty': 10 }; | ||
expect(objCase(obj, 'SOME_CRAZY.ProbablyPossible.nested_property')).to.eql(10); | ||
}); | ||
it('should work without finding a match', function () { | ||
var obj = { 'some-crazy.PROBABLY_MISSPELLED.NestedProperty': 10 }; | ||
expect(objCase(obj, 'SOME_CRAZY.ProbablyMssplld.nested_property')).to.eql(10); | ||
}); | ||
}); | ||
}); | ||
describe('.del()', function () { | ||
@@ -59,2 +90,11 @@ it('should delete simple keys', function () { | ||
}); | ||
describe('performance', function(){ | ||
it('should be performant', function(){ | ||
var obj = { 'A bird': { flew_under: { theTrain: 4 } } }; | ||
for (var i = 0, n = 100000; i < n; i++) { | ||
objCase(obj, 'a bird.flew_under.the_train'); | ||
} | ||
}); | ||
}); | ||
}); |
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
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
10620
227
1