Comparing version 0.0.6 to 0.0.7-0
(function(undefined) { | ||
var root = this; | ||
// Object cloning function, uses jQuery/Underscore/Object.create depending on what's available | ||
var clone = function (object) { | ||
@@ -16,8 +16,11 @@ if (typeof Object.create !== 'undefined') { | ||
} | ||
}; | ||
// Weird IE shit, objects do not have hasOwn, but the prototype does... | ||
var hasOwnProp = Object.prototype.hasOwnProperty; | ||
var Dot = function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
if (args.length == 2) { | ||
@@ -28,3 +31,3 @@ return Dot.find.apply(this, args); | ||
}; | ||
// Legacy syntax, changed syntax to have get/set be similar in arg order | ||
@@ -38,7 +41,7 @@ Dot.find = function(path, object) { | ||
var pieces = path.split('.'), current = object, piece; | ||
if (current) { | ||
for (var index in pieces) { | ||
piece = pieces[index]; | ||
if (!current.hasOwnProperty(piece)) { | ||
if (!hasOwnProp.call(current, piece)) { | ||
return undefined; | ||
@@ -60,9 +63,11 @@ } | ||
var pieces = path.split('.'), current = object, piece, length = pieces.length; | ||
for (var index in pieces) { | ||
piece = pieces[index]; | ||
if (!current.hasOwnProperty(piece)) { | ||
if (!hasOwnProp.call(current, piece) || current[piece] === undefined) { | ||
current[piece] = {}; | ||
} | ||
if (typeof current !== 'object') throw new Error('Target key is not suitable for a nested value (it is either not undefined or not an object)'); | ||
if (index == (length - 1)) { | ||
@@ -84,3 +89,3 @@ current[piece] = value; | ||
}; | ||
// Transform unnested object with .-seperated keys into a nested object. | ||
@@ -94,3 +99,3 @@ Dot.transform = function(object) { | ||
current = transformed; | ||
for (var index in pieces) { | ||
@@ -101,3 +106,3 @@ piece = pieces[index]; | ||
} | ||
if (index == (piecesLength - 1)) { | ||
@@ -109,8 +114,10 @@ current[piece] = object[key]; | ||
} | ||
} else { | ||
transformed[key] = transformed[key]; // Ensure that properties exist on the object, not just the prototype | ||
} | ||
} | ||
return transformed; | ||
}; | ||
if (typeof module !== 'undefined' && module.exports) { | ||
@@ -120,3 +127,3 @@ exports = module.exports = Dot; | ||
root['Dot'] = Dot; | ||
if (typeof define === "function") { | ||
@@ -123,0 +130,0 @@ define([], function () { return Dot; }); |
{ | ||
"name" : "dottie", | ||
"version" : "0.0.6", | ||
"devDependencies": { | ||
"buster": "*" | ||
}, | ||
"description" : "Object traversing/manipulation util", | ||
"author" : "Mick Hansen <mh@innofluence.com>", | ||
"repository" : {"type": "git", "url": "git://github.com/innofluence/dottie.js.git"}, | ||
"main" : "dottie.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/buster-test" | ||
} | ||
} | ||
"name": "dottie", | ||
"version": "0.0.7-0", | ||
"devDependencies": { | ||
"buster": "*" | ||
}, | ||
"description": "Object traversing/manipulation util", | ||
"author": "Mick Hansen <mh@innofluence.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/innofluence/dottie.js.git" | ||
}, | ||
"main": "dottie.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/buster-test" | ||
} | ||
} |
@@ -23,2 +23,17 @@ var buster = require('buster'), | ||
}); | ||
it("should handle setting a nested value on an undefined value (should convert undefined to object)", function () { | ||
var data = { | ||
'values': undefined | ||
}; | ||
dottie.set(data, 'values.level1', 'foo'); | ||
expect(data.values.level1).toEqual('foo'); | ||
}); | ||
it("should throw error when setting a nested value on an existing key with a non-object value", function() { | ||
expect(function () { | ||
dottie.set(data, 'foo.bar.baz', 'someValue'); | ||
}).toThrow(); | ||
}); | ||
}); |
@@ -20,4 +20,4 @@ var buster = require('buster'), | ||
expect(typeof transformed.user).toEqual('object'); | ||
expect(typeof transformed.user.location).toEqual('object'); | ||
expect(transformed.user).toBeObject(); | ||
expect(transformed.user.location).toBeObject(); | ||
@@ -45,5 +45,5 @@ expect(transformed.user.email).toEqual('jd@foobar.com'); | ||
expect(typeof transformed.user).toEqual('object'); | ||
expect(typeof transformed.user.location).toEqual('object'); | ||
expect(typeof transformed.project).toEqual('object'); | ||
expect(transformed.user).toBeObject(); | ||
expect(transformed.user.location).toBeObject(); | ||
expect(transformed.project).toBeObject(); | ||
@@ -54,2 +54,22 @@ expect(transformed.user.email).toEqual('jd@foobar.com'); | ||
}); | ||
it("should be able to handle base level properties together with nested", function () { | ||
var values = { | ||
'customer.name': 'John Doe', | ||
'customer.age': 15, | ||
'client': 'Lolcat' | ||
}; | ||
var transformed = dottie.transform(values); | ||
expect(transformed.client).toBeDefined(); | ||
expect(transformed.hasOwnProperty('client')).toBeTruthy(); // Ensure that the property is actually on the object itself, not on the prototype. | ||
expect(transformed.customer).toBeDefined(); | ||
expect(transformed.customer).toBeObject(); | ||
expect(transformed.client).toEqual('Lolcat'); | ||
expect(transformed.customer.name).toEqual('John Doe'); | ||
expect(transformed.customer.age).toEqual(15); | ||
}); | ||
}); |
9419
236