This file contains 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 intersectionBy = (a, b, fn) => { | |
const s = new Set(b.map(fn)); | |
return a.filter(x => s.has(fn(x))); | |
}; | |
intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [2.1] |
This file contains 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 intersection = (a, b) => { | |
const s = new Set(b); | |
return a.filter(x => s.has(x)); | |
}; | |
intersection([1, 2, 3], [4, 3, 2]); // [2, 3] |
This file contains 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 insertBefore = (el, htmlString) => el.insertAdjacentHTML('beforebegin', htmlString); | |
insertBefore(document.getElementById('myId'), '<p>before</p>'); // <p>before</p> <div id="myId">...</div> |
This file contains 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 insertAfter = (el, htmlString) => el.insertAdjacentHTML('afterend', htmlString); | |
insertAfter(document.getElementById('myId'), '<p>after</p>'); // <div id="myId">...</div> <p>after</p> |
This file contains 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 initial = arr => arr.slice(0, -1); | |
initial([1, 2, 3]); // [1,2] |
This file contains 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 indexOfAll = (arr, val) => arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), []); | |
indexOfAll([1, 2, 3, 1, 2, 3], 1); // [0,3] | |
indexOfAll([1, 2, 3], 4); // [] |
This file contains 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 httpsRedirect = () => { | |
if (location.protocol !== 'https:') location.replace('https://' + location.href.split('//')[1]); | |
}; | |
httpsRedirect(); // If you are on http://mydomain.com, you are redirected to https://mydomain.com |
This file contains 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 hide = (...el) => [...el].forEach(e => (e.style.display = 'none')); | |
hide(document.querySelectorAll('img')); // Hides all <img> elements on the page |
This file contains 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 head = arr => arr[0]; | |
head([1, 2, 3]); // 1 |
This file contains 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 getType = v => | |
v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase(); | |
getType(new Set([1, 2, 3])); // 'set' |