Skip to content

Commit

Permalink
feat: 🎸 add reverse option to [OBJFlatten]
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Oct 19, 2020
1 parent f3ef1b0 commit e275607
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
1 change: 0 additions & 1 deletion packages/basics/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default {
// aliases
bufferify: BUFObject.BUFObject,
jsonify: JSONString.JSONString,
flatten: OBJFlatten.OBJFlatten,
concat: TXTConcat.TXTConcat,
standardize: OBJStandardize.OBJStandardize,
split: TXTParse.TXTParse,
Expand Down
29 changes: 13 additions & 16 deletions packages/basics/src/obj-flatten.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import flatten from 'flat';

function OBJFlatten(data, feed) {
const opts = {
delimiter: this.getParam('separator', '/'),
safe: this.getParam('safe', true),
};
if (this.isLast()) {
feed.close();
} else {
feed.send(flatten(data, opts));
}
}

/**
* Take `Object` and flat it with delimited character.
*
* @name OBJFlatten
* @alias flatten
* @param {String} [separator=/] choose a character for flatten keys
* @param {Boolean} [safe=false] preserve arrays and their contents,
* @returns {Object}
* @see https://www.npmjs.com/package/flat
*/
export default {
OBJFlatten,
};
export default function OBJFlatten(data, feed) {
if (this.isLast()) {
return feed.close();
}
const opts = {
delimiter: String(this.getParam('separator', '/')),
safe: Boolean(this.getParam('safe', true)),
};
if (this.getParam('reverse', false)) {
return feed.send(unflatten(data, opts));
}
return feed.send(flatten(data, opts));
}
12 changes: 12 additions & 0 deletions packages/basics/test/obj-flatten.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ describe('OBJFlatten', () => {
done();
});
});
it('should unflatten the nested objects', (done) => {
from([{ a: { b: 1, c: 2 } }])
.pipe(ezs('OBJFlatten'))
.pipe(ezs('OBJFlatten', { reverse: true }))
.on('data', (data) => {
expect(data).toStrictEqual({ a: { b: 1, c: 2 } });
})
.on('end', () => {
done();
});
});


it('should use the given separator', (done) => {
from([{ a: { b: 1, c: 2 } }])
Expand Down

0 comments on commit e275607

Please sign in to comment.