Last active
September 20, 2022 01:33
-
-
Save jsonberry/c1f43140304a7aea529427568bcf1cb2 to your computer and use it in GitHub Desktop.
Helpful JavaScript Utilities
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
// zip arrays together | |
// Catalin Dumitru @colin_dumitru | |
// Jason Awbrey @jsawbrey | |
const zip = (a, b) => a.map((n, i) => [n, b[i]]) | |
// merge objects in an array together | |
// caution: properties are overridden if duplicated | |
// @a [{a},{b}] | |
// @returns [{a, b}] | |
// Jason Awbrey @jsawbrey | |
const mergeObjsInArray = a => a.reduce((acc, c) => ({...acc, ...c})) | |
// Partition | |
// http://colin-dumitru.github.io/functional-programming/javascript/tutorial/2014/12/28/functional_operations_in_es6.html | |
// Catalin Dumitru @colin_dumitru | |
var input = [1, 2, "John", 3, "Mike", "Colin"], | |
// This predicate separates strings from numbers. | |
p = x => x.length; | |
// This statement | |
input.reduce( | |
(l, r) => ( (p(r) ? l[0] : l[1]).push(r), l ), | |
[[],[]] | |
); | |
// Outputs [["John","Mike","Colin"],[1,2,3]] | |
// Sort array alphabetically | |
// David Wells @DavidWells | |
[].sort((a, b) => { | |
if (a < b) return -1 | |
else if (a > b) return 1 | |
return 0 | |
}) | |
// Sort array reverse alphabetically | |
// David Wells @DavidWells | |
[].sort((a, b) => { | |
if (a > b) return -1; | |
else if (a < b) return 1; | |
return 0; | |
}) | |
// unique array values | |
// Addy Osmani @addyosmani | |
const uniqueArray = arr => [ ...new Set(arr) ] | |
// Immutable Array Operations | |
// Luke Jackson @lukejacksonn | |
// Vincent Billey @Fenntasy | |
const clone = x => [ ...x ] | |
const push = y => x => [ ...x, y ] | |
const pop = x => [...x].pop() | |
const unshift = y => x => [ y, ...x ] | |
const shift = x => x.slice(1) | |
const sort = f => x => [...x].sort(f) | |
const del = i => x => [ ...x.slice(0,i), ...x.slice(i+1) ] | |
const splice = (s, c, ...y) => x => [ ...x.slice(0,s), ...y, ...x.slice(s+c) ] | |
const reverse = x => [ ...x ].reverse() | |
// Filter array by it's truthy values | |
// arr = [0, '', false, 1].filter(Boolean) // [1] | |
// Alex Jover Morales @alexjoverm | |
[].filter(Boolean) | |
// Make a hash from an array | |
// @a [{id: uniqueIdA...}, {id: uniqueIdB...}] | |
// @returns {uniqueIdA: {...}, uniqueIdB: {...}} | |
// Jason Awbrey @jsawbrey // and probably almost every other dev haha | |
export const makeHashFromArray = a => a.reduce((acc, curr) => ({...acc, [curr.id]: curr}), {}) | |
// Make array from a hash | |
// @a {uniqueIdA: {...}, uniqueIdB: {...}} | |
// @returns [{id: uniqueIdA...}, {id: uniqueIdB...}] | |
// Jason Awbrey @jsawbrey // and probably almost every other dev haha | |
export const makeArrayFromHash = a => Object.keys(a).map(x => a[x]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment