dottie
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -41,2 +41,6 @@ (function(undefined) { | ||
// Dottie memoization flag | ||
Dottie.memoizePath = true; | ||
var memoized = {}; | ||
// Traverse object according to path, return value if found - Return undefined if destination is unreachable | ||
@@ -49,3 +53,12 @@ Dottie.get = function(object, path, defaultVal) { | ||
if (typeof path === "string") { | ||
names = path.split('.').reverse(); | ||
if (Dottie.memoizePath) { | ||
if (memoized[path]) { | ||
names = memoized[path].slice(0); | ||
} else { | ||
names = path.split('.').reverse(); | ||
memoized[path] = names.slice(0); | ||
} | ||
} else { | ||
names = path.split('.').reverse(); | ||
} | ||
} else if (Array.isArray(path)) { | ||
@@ -52,0 +65,0 @@ names = path.reverse(); |
{ | ||
"name": "dottie", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"devDependencies": { | ||
"benchmark": "^1.0.0", | ||
"expect.js": "~0.2.0", | ||
"microtime": "^1.2.0", | ||
"mocha": "~1.14.0" | ||
@@ -7,0 +9,0 @@ }, |
@@ -83,4 +83,8 @@ [![Build Status](https://travis-ci.org/mickhansen/dottie.js.png)](https://travis-ci.org/mickhansen/dottie.js.png) | ||
## Performance | ||
`0.3.1` and up ships with `dottie.memoizePath: true` by default, if this causes any bugs, please try setting it to false | ||
## License | ||
[MIT](https://github.com/mickhansen/dottie.js/blob/master/LICENSE) |
@@ -23,2 +23,6 @@ var expect = require("expect.js"), | ||
describe("dottie.get", function () { | ||
var flags = { | ||
memoizePath: [false, true] | ||
}; | ||
var data = { | ||
@@ -41,42 +45,61 @@ 'foo': { | ||
it('should return undefined if value is undefined', function () { | ||
expect(dottie.get(undefined, 'foo')).to.equal(undefined); | ||
}); | ||
Object.keys(flags).forEach(function (flag) { | ||
flags[flag].forEach(function (value) { | ||
describe(flag+': '+value, function () { | ||
beforeEach(function () { | ||
dottie[flag] = value; | ||
}); | ||
it("should get first-level values", function () { | ||
expect(dottie.get(data, 'zoo')).to.equal('lander'); | ||
}); | ||
it('should return undefined if value is undefined', function () { | ||
expect(dottie.get(undefined, 'foo')).to.equal(undefined); | ||
expect(dottie.get(undefined, 'foo')).to.equal(undefined); | ||
}); | ||
it("should get nested-level values", function () { | ||
expect(dottie.get(data, 'foo.bar')).to.equal('baz'); | ||
}); | ||
it("should get first-level values", function () { | ||
expect(dottie.get(data, 'zoo')).to.equal('lander'); | ||
expect(dottie.get(data, 'zoo')).to.equal('lander'); | ||
}); | ||
it("should return undefined if not found", function () { | ||
expect(dottie.get(data, 'foo.zoo.lander')).to.equal(undefined); | ||
}); | ||
it("should get nested-level values", function () { | ||
expect(dottie.get(data, 'foo.bar')).to.equal('baz'); | ||
}); | ||
it("should return false values properly", function () { | ||
expect(dottie.get(data, 'false.value')).to.equal(false); | ||
}); | ||
it("should get nested-level values multiple times", function () { | ||
expect(dottie.get(data, 'foo.bar')).to.equal('baz'); | ||
expect(dottie.get(data, 'foo.bar')).to.equal('baz'); | ||
expect(dottie.get(data, 'foo.bar')).to.equal('baz'); | ||
expect(dottie.get(data, 'foo.bar')).to.equal('baz'); | ||
}); | ||
it("should return the default value passed in if not found", function() { | ||
expect(dottie.get(data, 'foo.zoo.lander', 'novalue')).to.equal('novalue'); | ||
}); | ||
it("should return undefined if not found", function () { | ||
expect(dottie.get(data, 'foo.zoo.lander')).to.equal(undefined); | ||
}); | ||
it("should return null of the value is null and not undefined", function() { | ||
expect(dottie.get(data, 'null.value')).to.equal(null); | ||
}); | ||
it("should return false values properly", function () { | ||
expect(dottie.get(data, 'false.value')).to.equal(false); | ||
}); | ||
it("should return undefined if accessing a child property of a null value", function () { | ||
expect(dottie.get(data, 'nullvalue.childProp')).to.equal(undefined); | ||
expect(dottie.get(data, 'null.value.childProp')).to.equal(undefined); | ||
}); | ||
it("should return the default value passed in if not found", function() { | ||
expect(dottie.get(data, 'foo.zoo.lander', 'novalue')).to.equal('novalue'); | ||
}); | ||
it("should return undefined if accessing a child property of a string value", function () { | ||
expect(dottie.get(data, 'foo.bar.baz.yapa')).to.equal(undefined); | ||
}); | ||
it("should return null of the value is null and not undefined", function() { | ||
expect(dottie.get(data, 'null.value')).to.equal(null); | ||
}); | ||
it('should get nested values with keys that have dots', function () { | ||
expect(dottie.get(data, ['nested.dot', 'key'])).to.equal('value'); | ||
it("should return undefined if accessing a child property of a null value", function () { | ||
expect(dottie.get(data, 'nullvalue.childProp')).to.equal(undefined); | ||
expect(dottie.get(data, 'null.value.childProp')).to.equal(undefined); | ||
}); | ||
it("should return undefined if accessing a child property of a string value", function () { | ||
expect(dottie.get(data, 'foo.bar.baz.yapa')).to.equal(undefined); | ||
}); | ||
it('should get nested values with keys that have dots', function () { | ||
expect(dottie.get(data, ['nested.dot', 'key'])).to.equal('value'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
19659
12
495
90
4