Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ unflatten({
### delimiter

Use a custom delimiter for (un)flattening your objects, instead of `.`.
To use bracket notation set the delimiter option to `[]`,`{}`, or `()`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps mention any two characters can be used. I do worry a bit about the inability to escape any delimited character, though this isn't necessarily specifically a problem for your proposed change.


### safe

Expand Down
36 changes: 34 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function flatten(target, opts) {
)

var newKey = prev
? prev + delimiter + key
? prev + delimit(delimiter, key)
: key

if (!opts.maxDepth) {
Expand Down Expand Up @@ -68,7 +68,7 @@ function unflatten(target, opts) {
}

Object.keys(target).forEach(function(key) {
var split = key.split(delimiter)
var split = splitkey(key, delimiter)
var key1 = getkey(split.shift())
var key2 = getkey(split[0])
var recipient = result
Expand Down Expand Up @@ -105,3 +105,35 @@ function isBuffer(value) {
if (typeof Buffer === 'undefined') return false
return Buffer.isBuffer(value)
}

// add delimiters to a key
// 1-2 characters
// 2 characters will be treated as brackets
// ex. delimit('[]', 'hi') = '[hi]'
function delimit(delimiter, key) {
if (delimiter.length == 1) {
return delimiter + key
}
else {
var delimiters = delimiter.split('')
return delimiters[0] + key + delimiters[1]
}
}

// split a key by a delimiter(s)
// 1 or 2 characters
// 2 characters treated as brackets
function splitkey(key, delimiter){
if (delimiter.length == 1) {
return key.split(delimiter)
}
else if (delimiter.length == 2) {
var delimiters = delimiter.split('')
var regex = new RegExp('\\'+delimiters[1] , 'g')
//remove trailing bracket from string
var result = key.replace(regex, '')
//split on leading bracket
return (result.split(delimiters[0]))
}
}

30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ suite('Flatten', function() {
})
})

test('Custom Two Character Delimiter', function() {
assert.deepEqual(flatten({
hello: {
world: {
again: 'good morning'
}
}
}, {
delimiter: '()'
}), {
'hello(world)(again)': 'good morning'
})
})



test('Empty Objects', function() {
assert.deepEqual(flatten({
hello: {
Expand Down Expand Up @@ -215,6 +231,20 @@ suite('Unflatten', function() {
}))
})

test('Custom Two Character Delimiter', function() {
assert.deepEqual({
hello: {
world: {
again: 'good morning'
}
}
}, unflatten({
'hello[world][again]': 'good morning'
}, {
delimiter: '[]'
}))
})

test('Overwrite', function() {
assert.deepEqual({
travis: {
Expand Down