object-squish
Version:
Flatten nested objects to a single level
140 lines (99 loc) • 2.61 kB
Markdown
for flattening objects to a single level.
Install object-squish via npm:
```
npm install object-squish --save
```
Then require in your file
```javascript
var squish = require('object-squish');
```
Use object-squish to collapse pesky nested objects down to one level.
```javascript
var data = {
spiders: { are: 'ok' },
they: {
just: {
want: 'to',
be: 'friends'
}
}
};
var flattened = squish(data);
// === Outputs ->
{
'spiders.are': 'ok',
'they.just.want': 'to',
'they.just.be': 'friends'
}
```
arrays are skipped over by default but can be included by passing the `includeArrays` option.
```javascript
var data = { spiders: [1, 2, 3] };
var flattened = squish(data, { includeArrays: true });
// === Outputs ->
{
'spiders.0': 1,
'spiders.1': 2,
'spiders.2': 3,
}
```
See below for a comprehensive list of Options
Type: `Number`
Default value: `Infinity`
Set the depth at which to stop flattening.
Type: `Boolean`
Default value: `false`
Set whether to flatten arrays, keys will appear as array indexes `0, 1, 2, ...`.
Type: `String`
Default value: `'.'`
Set the seperating character/sequence in the derived path
Change the keys of the object, this can be one of:
`'lowercase'` -> ensure all keys are lowercase
`'uppercase'` -> ensure all keys are uppercase
`Function` -> a function which is passed the key and returns the modified key
A predicate, called with the object being processed, returning a boolean indicating whether to descend into the object or not.
```javascript
var data = {
spiders: { are: 'ok' },
they: {
just: {
want: 'to',
be: 'friends'
}
}
};
// we can use modifyKey to make all the keys uppercase
var uppercased = squish(data, { modifyKey: 'uppercase' });
// === Outputs ->
{
'SPIDERS.ARE': 'ok',
'THEY.JUST.WANT': 'to',
'THEY.JUST.BE': 'friends'
}
// or use a function to perform more complex processing
var mutate = function (key) {
return key.replace('are', 'arent');
};
var modified = squish(data, { modifyKey: mutate });
// === Outputs ->
{
'spiders.arent': 'ok',
'they.just.want': 'to',
'they.just.be': 'friends'
}
```
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.
* v 1.0.0 - Initial Release
> A node.js utility