Comparing version 0.2.4 to 0.2.5
@@ -42,20 +42,13 @@ (function(undefined) { | ||
// Traverse object according to path, return value if found - Return undefined if destination is unreachable | ||
Dottie.get = function(object, path) { | ||
var pieces = path.split('.'), current = object, piece; | ||
Dottie.get = function(object, path, defaultVal) { | ||
if (object === null) return defaultVal; | ||
if (current) { | ||
for (var index = 0, length = pieces.length; index < length; index++) { | ||
piece = pieces[index]; | ||
if (!hasOwnProp.call(current, piece)) { | ||
return undefined; | ||
} | ||
current = current[piece]; | ||
var names = path.split('.').reverse(); | ||
if (current === undefined) { | ||
return undefined; | ||
} | ||
} | ||
return current; | ||
} | ||
return undefined; | ||
while (names.length && (object = object[names.pop()]) !== undefined && object !== null); | ||
// Handle cases where accessing a childprop of a null value | ||
if (object === null && names.length) object = undefined; | ||
return (object === undefined ? defaultVal : object); | ||
}; | ||
@@ -153,3 +146,3 @@ | ||
current = object[key]; | ||
if (current === Object(current)) { | ||
if (Object.prototype.toString.call(current) === "[object Object]") { | ||
nested = Dottie.flatten(current, seperator); | ||
@@ -167,3 +160,3 @@ | ||
return flattened; | ||
} | ||
}; | ||
@@ -170,0 +163,0 @@ if (typeof module !== 'undefined' && module.exports) { |
{ | ||
"name": "dottie", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"devDependencies": { | ||
@@ -12,3 +12,3 @@ "expect.js": "~0.2.0", | ||
"type": "git", | ||
"url": "git://github.com/greenwoodio/dottie.js.git" | ||
"url": "git://github.com/mickhansen/dottie.js.git" | ||
}, | ||
@@ -15,0 +15,0 @@ "main": "dottie.js", |
[![Build Status](https://travis-ci.org/mickhansen/dottie.js.png)](https://travis-ci.org/mickhansen/dottie.js.png) | ||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/mickhansen/dottie.js/trend.png)](https://bitdeli.com/free "Bitdeli Badge") | ||
@@ -11,3 +10,3 @@ ## Install | ||
### Get value | ||
Gets nested value, or undefined if unreachable. | ||
Gets nested value, or undefined if unreachable, or a default value if passed. | ||
@@ -25,2 +24,3 @@ ```js | ||
dottie.get(values, 'some.undefined.key'); // returns undefined | ||
dottie.get(values, 'some.undefined.key', 'defaultval'); // returns 'defaultval' | ||
``` | ||
@@ -40,6 +40,6 @@ | ||
var values = { | ||
'user.name': 'Mick Hansen', | ||
'user.email': 'maker@mhansen.io', | ||
'user.professional.title': 'Developer', | ||
'user.professional.employer': 'Coshopr' | ||
'user.name': 'Gummy Bear', | ||
'user.email': 'gummybear@candymountain.com', | ||
'user.professional.title': 'King', | ||
'user.professional.employer': 'Candy Mountain' | ||
}; | ||
@@ -51,7 +51,7 @@ var transformed = dottie.transform(values); | ||
user: { | ||
name: 'Mick Hansen', | ||
email: 'maker@mhansen.io', | ||
name: 'Gummy Bear', | ||
email: 'gummybear@candymountain.com', | ||
professional: { | ||
title: 'Developer', | ||
employer: 'Coshopr' | ||
title: 'King', | ||
employer: 'Candy Mountain' | ||
} | ||
@@ -58,0 +58,0 @@ } |
@@ -6,16 +6,16 @@ var expect = require("expect.js"), | ||
Array.prototype.getByName = function(name) { | ||
for (var i = 0, len = this.length; i < len; i++) { | ||
if (typeof this[i] != "object") continue; | ||
if (this[i].name === name) return this[i]; | ||
for (var i = 0, len = this.length; i < len; i++) { | ||
if (typeof this[i] != "object") continue; | ||
if (this[i].name === name) return this[i]; | ||
} | ||
}; | ||
} | ||
}; | ||
Array.prototype.getByType = function(type) { | ||
var newvalues = []; | ||
for (var i = 0, len = this.length; i < len; i++) { | ||
if (typeof this[i] != "object") continue; | ||
if (this[i].type === type) newvalues.push(this[i]); | ||
} | ||
if (newvalues.length <= 0) newvalues = undefined; | ||
return newvalues; | ||
var newvalues = []; | ||
for (var i = 0, len = this.length; i < len; i++) { | ||
if (typeof this[i] != "object") continue; | ||
if (this[i].type === type) newvalues.push(this[i]); | ||
} | ||
if (newvalues.length <= 0) newvalues = undefined; | ||
return newvalues; | ||
}; | ||
@@ -31,3 +31,7 @@ | ||
'value': false | ||
} | ||
}, | ||
'null': { | ||
'value': null | ||
}, | ||
'nullvalue': null | ||
}; | ||
@@ -50,2 +54,19 @@ | ||
}); | ||
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 null of the value is null and not undefined", function() { | ||
expect(dottie.get(data, 'null.value')).to.equal(null); | ||
}); | ||
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); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
16184
395
11