Comparing version 0.2.2 to 0.2.3
@@ -93,4 +93,9 @@ (function(undefined) { | ||
Dottie.transform = function(object, options) { | ||
options = options || {}; | ||
options.delimiter = options.delimiter || '.'; | ||
if (Array.isArray(object)) { | ||
return object.map(Dottie.transform); | ||
return object.map(function(o) { | ||
return Dottie.transform(o, options); | ||
}); | ||
} | ||
@@ -105,4 +110,4 @@ | ||
for (var key in object) { | ||
if (key.indexOf('.') !== -1) { | ||
pieces = key.split('.'); | ||
if (key.indexOf(options.delimiter) !== -1) { | ||
pieces = key.split(options.delimiter); | ||
piecesLength = pieces.length; | ||
@@ -109,0 +114,0 @@ current = transformed; |
{ | ||
"name": "dottie", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"devDependencies": { | ||
@@ -5,0 +5,0 @@ "expect.js": "~0.2.0", |
@@ -40,3 +40,4 @@ [![Build Status](https://travis-ci.org/mickhansen/dottie.js.png)](https://travis-ci.org/mickhansen/dottie.js.png) | ||
transforms is now equal to = | ||
// transforms is now equal to = | ||
{ | ||
@@ -52,1 +53,18 @@ user: { | ||
} | ||
#### With a custom delimiter | ||
var values = { | ||
'user_name': 'Mick Hansen', | ||
'user_email': 'mh@innofluence.com' | ||
}; | ||
var transformed = dottie.transform(values, { delimiter: '_' }); | ||
// transforms is now equal to = | ||
{ | ||
user: { | ||
name: 'Mick Hansen', | ||
email: 'mh@innofluence.com' | ||
} | ||
} |
@@ -122,2 +122,28 @@ var expect = require("expect.js"), | ||
}); | ||
}); | ||
it("supports custom delimiters", function () { | ||
var values = { | ||
user: { | ||
name: 'John Doe' | ||
}, | ||
'user_email': 'jd@foobar.com', | ||
'user_location_country': 'Zanzibar', | ||
'user_location_city': 'Zanzibar City', | ||
'project_title': 'dottie' | ||
}; | ||
var transformed = dottie.transform(values, { delimiter: '_' }); | ||
expect(transformed.user).not.to.be(undefined); | ||
expect(transformed.user.location).not.to.be(undefined); | ||
expect(transformed.project).not.to.be(undefined); | ||
expect(transformed.user).to.be.an('object'); | ||
expect(transformed.user.location).to.be.an('object'); | ||
expect(transformed.project).to.be.an('object'); | ||
expect(transformed.user.email).to.equal('jd@foobar.com'); | ||
expect(transformed.user.location.city).to.equal('Zanzibar City'); | ||
expect(transformed.project.title).to.equal('dottie'); | ||
}); | ||
}); |
14436
383
69