This file contains hidden or 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
module.exports = function deepFreeze (o) { | |
Object.freeze(o); | |
Object.getOwnPropertyNames(o).forEach(function (prop) { | |
if (o.hasOwnProperty(prop) | |
&& o[prop] !== null | |
&& (typeof o[prop] === "object" || typeof o[prop] === "function") | |
&& !Object.isFrozen(o[prop])) { | |
deepFreeze(o[prop]); | |
} |
This file contains hidden or 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
const group = (obj, field) => obj[field] | |
const addValue = (result, obj, field) => | |
result[obj[field]] || [] | |
const addToGroup = (result, obj, field) => | |
[...(addValue(result, obj, field)), obj] | |
const toNewGroupUsing = (field) => (result, obj) => ({ | |
...result, |
This file contains hidden or 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
const toNewGroup = (field) => (result, obj) => ({ | |
...result, | |
[obj[field]]: [...(result[obj[field]] || []), obj] | |
}) | |
const group = (list) => (field) => | |
list.reduce(toNewGroup(field), {}) | |
const groupBy = group(list) |
This file contains hidden or 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
const toNewGroup = (field) => (result, obj) => ({ | |
...result, | |
[obj[field]]: [...(result[obj[field]] || []), obj] | |
}) | |
const groupBy = (list, field) => | |
list.reduce(toNewGroup(field), {}) | |
console.log(groupBy(list, "level")) |
This file contains hidden or 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
const toNewGroup = (field) => (result, obj) => ({ | |
...result, | |
[obj[field]]: [...(result[obj[field]] || []), obj] | |
}) | |
const groups = list.reduce(toNewGroup("level"), {}) |
This file contains hidden or 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
const toNewGroup = (field) => (result, obj) => { | |
result[obj[field]] = (!result[obj[field]]) | |
? [obj] | |
: [...result[obj[field]], obj] | |
return result | |
} | |
const groups = list.reduce(toNewGroup("level"), {}) | |
console.log(groups) |
This file contains hidden or 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
const toNewGroup = (field) => (result, obj) => { | |
result[field] = (!result[field]) | |
? [obj] | |
: [...result[field], obj] | |
return result | |
} | |
const groups = list.reduce(toNewGroup("level"), {}) | |
console.log(groups) |
This file contains hidden or 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
{ senior: | |
[ { id: 1, | |
name: 'Suissa', | |
active: true, | |
level: 'senior', | |
tags: [Array] }, | |
{ id: 2, | |
name: 'Bella', | |
active: true, | |
level: 'senior', |
This file contains hidden or 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
const plenos = list.filter(toGroup("level", "pleno")) | |
console.log({plenos}) | |
/** | |
{ plenos: | |
[ { id: 4, | |
name: 'John', | |
level: 'pleno', | |
active: false, | |
tags: [Array] } ] } |
This file contains hidden or 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
const toGroup = (field, value) => (obj) => | |
obj[field] === value | |
const filtered = list.filter(toGroup("level", "senior")) | |
console.log({filtered}) | |
/** | |
{ filtered: | |
[ { id: 1, | |
name: 'Suissa', |