22 lines
1.3 KiB
JavaScript
22 lines
1.3 KiB
JavaScript
// Array Helpers
|
|
const countOccurrences = arr => arr.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {});
|
|
const isEmpty = arr => !(Array.isArray(arr) && arr.length > 0 && arr.filter(el => el === undefined).length > 0)
|
|
const isEqual = (a, b) => JSON.stringify(a.sort()) === JSON.stringify(b.sort());
|
|
const countBy = (arr, prop) => arr.reduce((prev, curr) => (prev[curr[prop]] = ++prev[curr[prop]] || 1, prev), {});
|
|
const transpose = matrix => matrix[0].map((col, i) => matrix.map(row => row[i]));
|
|
|
|
// Dom Helpers
|
|
const insertAfter = (ele, anotherEle) => anotherEle.parentNode.insertBefore(ele, anotherEle.nextSibling);
|
|
const insertBefore = (ele, anotherEle) => anotherEle.parentNode.insertBefore(ele, anotherEle);
|
|
const insertHtmlAfter = (html, ele) => ele.insertAdjacentHTML('afterend', html);
|
|
const insertHtmlBefore = (html, ele) => ele.insertAdjacentHTML('beforebegin', html);
|
|
|
|
const goTo = url => location.href = url;
|
|
const replace = (ele, newEle) => ele.parentNode.replaceChild(newEle, ele);
|
|
const toNumbers = arr => arr.map(Number);
|
|
const wait = async (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
|
|
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
|
|
const slugify = string => string.toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]+/g, '');
|
|
const isNumeric = str => !/[^0-9]/.test(str);
|