-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new methods flattenObject and expandObject
- Loading branch information
Showing
6 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const isObject = require('./isObject') | ||
const setValue = require('./setValue') | ||
|
||
/** | ||
* Expand a flattened object. This method unpacks values flattened with | ||
* flattenObject | ||
* | ||
* Example: | ||
* | ||
* ``` | ||
* expandObject({ | ||
* { | ||
* 'foo.bar.value': true | ||
* 'foo.array': ['foo', 'bar'] | ||
* } | ||
* }) | ||
* ``` | ||
* | ||
* will outpout | ||
* | ||
* ``` | ||
* { | ||
* foo: { | ||
* bar: { | ||
* value: true | ||
* }, | ||
* array: ['foo', 'bar'], | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @param { object } input Object to expand | ||
*/ | ||
module.exports = function expandObject (input) { | ||
if (!isObject(input)) { | ||
return input | ||
} | ||
|
||
const value = {} | ||
for (const key in input) { | ||
setValue(value, key, input[key]) | ||
} | ||
|
||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const isObject = require('./isObject') | ||
const getValue = require('./getValue') | ||
|
||
/** | ||
* Flatten an object. This is a serialization method that takes an object and | ||
* flattens it into a single-level object with deep keys joined with a dot. | ||
* | ||
* Example: | ||
* | ||
* ``` | ||
* flattenObject({ | ||
* foo: { | ||
* bar: { | ||
* value: true | ||
* }, | ||
* array: ['foo', 'bar'], | ||
* } | ||
* }) | ||
* ``` | ||
* | ||
* will outpout | ||
* | ||
* ``` | ||
* { | ||
* 'foo.bar.value': true | ||
* 'foo.array': ['foo', 'bar'] | ||
* } | ||
* ``` | ||
* | ||
* @param { object } input Object tree to flatten | ||
*/ | ||
module.exports = function flattenObject (input) { | ||
if (!isObject(input)) { | ||
return input | ||
} | ||
|
||
const values = {} | ||
|
||
const traversePath = (path) => { | ||
const p = path.join('.') | ||
const v = getValue(input, p) | ||
|
||
if (isObject(v)) { | ||
for (const k in v) { | ||
traversePath([...path, k]) | ||
} | ||
return | ||
} | ||
|
||
values[p] = v | ||
} | ||
|
||
for (const k in input) { | ||
traversePath([k]) | ||
} | ||
|
||
return values | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const expect = require('expect.js') | ||
const expandObject = require('../lib/expandObject') | ||
const flattenObject = require('../lib/flattenObject') | ||
|
||
describe('lib/expandObject', () => { | ||
it('should return non-objects as they were', (done) => { | ||
expect(expandObject(null)).to.eql(null) | ||
expect(expandObject(undefined)).to.eql(undefined) | ||
expect(expandObject(1.23)).to.eql(1.23) | ||
expect(expandObject('foo')).to.eql('foo') | ||
expect(expandObject(['foo', 'bar'])).to.eql(['foo', 'bar']) | ||
done() | ||
}) | ||
|
||
it('should expand an object', (done) => { | ||
const source = { | ||
'foo.bar': 'foo', | ||
'foo.array': ['foo', 'bar'], | ||
'foo.nested.deep.object': true | ||
} | ||
|
||
const value = expandObject(source) | ||
expect(value).to.eql({ | ||
foo: { | ||
bar: 'foo', | ||
array: [ | ||
'foo', | ||
'bar' | ||
], | ||
nested: { | ||
deep: { | ||
object: true | ||
} | ||
} | ||
} | ||
}) | ||
done() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const expect = require('expect.js') | ||
const flattenObject = require('../lib/flattenObject') | ||
|
||
describe('lib/flattenObject', () => { | ||
it('should return non-objects as they were', (done) => { | ||
expect(flattenObject(null)).to.eql(null) | ||
expect(flattenObject(undefined)).to.eql(undefined) | ||
expect(flattenObject(1.23)).to.eql(1.23) | ||
expect(flattenObject('foo')).to.eql('foo') | ||
expect(flattenObject(['foo', 'bar'])).to.eql(['foo', 'bar']) | ||
done() | ||
}) | ||
|
||
it('should flatten a deep object', (done) => { | ||
const source = { | ||
foo: { | ||
bar: 'foo', | ||
array: [ | ||
'foo', | ||
'bar' | ||
], | ||
nested: { | ||
deep: { | ||
object: true | ||
} | ||
} | ||
} | ||
} | ||
|
||
const value = flattenObject(source) | ||
expect(value['foo.bar']).to.eql(source.foo.bar) | ||
expect(value['foo.array']).to.eql(source.foo.array) | ||
expect(value['foo.nested.deep.object']).to.eql(true) | ||
done() | ||
}) | ||
}) |