Skip to content

Instantly share code, notes, and snippets.

@nclsndr
Created December 19, 2017 22:48
Show Gist options
  • Save nclsndr/992518c45c6516f106f4af20f00ff585 to your computer and use it in GitHub Desktop.
Save nclsndr/992518c45c6516f106f4af20f00ff585 to your computer and use it in GitHub Desktop.
Pick in object
/* ------------------------------------------
Pick in object
--------------------------------------------- */
const peek = (...keep) => iter => keep.indexOf(iter) > -1;
const split = c => c.split('.');
const tail = array => {
const [head, ...result] = array; // eslint-disable-line
return result;
};
/* eslint-disable no-confusing-arrow */
const pickInObject = (...fields) => object => {
if (!object) { return object; }
const levels = fields.map(split);
const currentLevel = levels
.filter(c => c.length === 1)
.map(c => c[0]);
const rec = levels
.filter(c => c.length > 1)
.filter(c => Object.prototype.hasOwnProperty.call(object, c[0]))
.reduce((acc, c, i, xs) => {
const next = {
...acc,
[c[0]]: [
...acc[c[0]] || {},
tail(c).join('.'),
],
};
return (i === xs.length - 1)
? Object.keys(next)
.map(d => [d, next[d]])
: next;
}, [])
.reduce((acc, c) => ({
...acc,
[c[0]]: {
...acc[c[0]],
...pickInObject(...c[1])(object[c[0]]),
},
}), {});
const res = Object.keys(object)
.filter(peek(...currentLevel))
.reduce((acc, k) => ({
...acc,
[k]: object[k],
}), {});
return Object.assign({}, res, rec);
};
export default pickInObject;
import pickkInObject from './pickInObject';
describe('Peek in object helper on chanceProfile module', () => {
describe('pickInObject', () => {
it('should peek id, name from the provided object', () => {
const res = pickInObject('id', 'name')({
id: 'id',
name: 'name',
});
expect(Object.keys(res).join('')).toBe('idname');
});
it('should peek nested information from the provided object', () => {
const res = pickInObject('nested.test')({
nested: {
test: 'test',
},
});
expect(res.nested.test).toBe('test');
});
it('should peek very nested information from the provided object', () => {
const res = pickInObject('properties.nested.veryNested.veryVeryNested.value')({
properties: {
nested: {
veryNested: {
veryVeryNested: {
value: true,
},
},
},
},
});
expect(res.properties.nested.veryNested.veryVeryNested.value)
.toBe(true);
});
it('should return empty object if peek is empty from the provided object', () => {
const res = pickInObject()({
id: 'id',
name: 'name',
});
expect(res).toEqual({});
});
it('should return empty object if peek is empty from an empty object', () => {
const res = pickInObject()({});
expect(res).toEqual({});
});
it('should return null if object is null', () => {
const res = pickInObject()(null);
expect(res).toBeNull();
});
it('should return empty object if object is undefined', () => {
const res = pickInObject()();
expect(res).toBe(undefined);
});
});
});
import pickkInObject from './pickInObject';
const res = peekInObject(
'id', 'properties.birth', 'devices.mobile', 'properties.nested.very', 'properties.nested.veryNested.re', 'properties.location'
)({
id: 'id',
name: 'name',
properties: {
birth: 'birth value',
location: 'rio',
nested: {
very: true,
veryNested: {
re: true,
},
},
},
devices: {
mobile: true,
desktop: false,
},
});
console.log(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment