Created
February 28, 2017 00:03
-
-
Save cayasso/c8637a1cb6fe4111df7c756b5534034d to your computer and use it in GitHub Desktop.
flatten
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
/** | |
* Takes a deeply nested object, `source`, and returns an object with | |
* dot-separated paths pointing to all primitive values from `source`. | |
*/ | |
const keys = Object.keys | |
const isArray = Array.isArray | |
function flatten(source) { | |
let total = {} | |
return keys(source).reduce((acc, key) => { | |
// If its an array then convert it to an object | |
if (isArray(source[key])) { | |
source[key] = source[key].reduce((arr, val, i) => { | |
arr[i] = val | |
return arr | |
}, {}) | |
} | |
if (_.isPlainObject(source[key])) { | |
const res = flatten(source[key]) | |
// Iterate over the response to get the final key/val | |
total = keys(res).reduce((t, k) => { | |
// This account for only promitives | |
if (!_.isPlainObject(res[k])) { | |
t[`${key}.${k}`] = res[k] | |
} | |
return t | |
}, total) | |
} else { | |
acc[key] = source[key] | |
} | |
return acc | |
}, total) | |
} |
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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<title>Elastic Coding Exercise</title> | |
<link href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.css" rel="stylesheet" /> | |
</head> | |
<body> | |
<div id="mocha"></div> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/2.3.0/chai.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script> | |
<script src="flatten.js"></script> | |
<script src="test.flatten.js"></script> | |
</body> | |
</html> |
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
mocha.setup('bdd'); | |
const expect = chai.expect; | |
describe('flatten()', function () { | |
it('should flatten a simple nested object', function () { | |
const source = { | |
foo: { | |
bar: 1 | |
} | |
}; | |
const expected = { | |
'foo.bar': 1 | |
}; | |
expect(flatten(source)).to.deep.equal(expected); | |
}); | |
it('should flatten an object with nested arrays', function () { | |
const source = { | |
foo: [{ | |
bar: 1 | |
}, { | |
bar: 2 | |
}] | |
}; | |
const expected = { | |
'foo.0.bar': 1, | |
'foo.1.bar': 2 | |
}; | |
expect(flatten(source)).to.deep.equal(expected); | |
}); | |
it('should flatten a complex nested object', function () { | |
const source = { | |
a: 1, | |
b: { | |
c: true, | |
d: { | |
e: 'foo' | |
} | |
}, | |
f: false, | |
g: ['red', 'green', 'blue'], | |
h: [{ | |
i: 2, | |
j: 3 | |
}] | |
}; | |
const expected = { | |
'a': 1, | |
'b.c': true, | |
'b.d.e': 'foo', | |
'f': false, | |
'g.0': 'red', | |
'g.1': 'green', | |
'g.2': 'blue', | |
'h.0.i': 2, | |
'h.0.j': 3 | |
}; | |
expect(flatten(source)).to.deep.equal(expected); | |
}); | |
}); | |
mocha.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment