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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ unflatten({

Use a custom delimiter for (un)flattening your objects, instead of `.`.

### toUpperCase

Use a toUpperCase option for flattening your objects and upper case object keys at the same time.
This can be handy when working with constants, i.e. `API_KEY: 'some key'`

### toLowerCase

Use a toLowerCase option for flattening your objects and lower case object keys at the same time.

### safe

When enabled, both `flat` and `unflatten` will preserve arrays and their
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function flatten(target, opts) {
opts = opts || {}

var delimiter = opts.delimiter || '.'
var lowerCase = opts.toLowerCase || false
var upperCase = opts.toUpperCase || false
var maxDepth = opts.maxDepth
var currentDepth = 1
var output = {}
Expand Down Expand Up @@ -36,6 +38,12 @@ function flatten(target, opts) {
return step(value, newKey)
}

if (lowerCase) {
newKey = newKey.toLowerCase()
}
if (upperCase) {
newKey = newKey.toUpperCase()
}
output[newKey] = value
})
}
Expand Down
28 changes: 28 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,34 @@ suite('Flatten', function() {
})
})

test('To upper case', function() {
assert.deepEqual(flatten({
hello: {
world: {
again: 'good morning'
}
}
}, {
toUpperCase: true
}), {
'HELLO.WORLD.AGAIN': 'good morning'
})
})

test('To lower case', function() {
assert.deepEqual(flatten({
HELLO: {
WORLD: {
AGAIN: 'good morning'
}
}
}, {
toLowerCase: true
}), {
'hello.world.again': 'good morning'
})
})

test('Empty Objects', function() {
assert.deepEqual(flatten({
hello: {
Expand Down