Skip to content

Instantly share code, notes, and snippets.

View frankiehayward's full-sized avatar

Frankie Hayward frankiehayward

View GitHub Profile
@frankiehayward
frankiehayward / intersection_by.js
Created December 5, 2019 22:59
This snippet can be used to return a list of elements that exist in both arrays, after a particular function has been executed to each element of both arrays.
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]
@frankiehayward
frankiehayward / intersection.js
Created December 5, 2019 22:58
This snippet can be used to get an array with elements that are included in two other arrays.
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]
@frankiehayward
frankiehayward / insert_before.js
Created December 5, 2019 22:56
This snippet can be used to insert an HTML string before a particular element.
const insertBefore = (el, htmlString) => el.insertAdjacentHTML('beforebegin', htmlString);
insertBefore(document.getElementById('myId'), '<p>before</p>'); // <p>before</p> <div id="myId">...</div>
@frankiehayward
frankiehayward / insert_after.js
Created December 5, 2019 22:56
This snippet can be used to insert an HTML string after the end of a particular element.
const insertAfter = (el, htmlString) => el.insertAdjacentHTML('afterend', htmlString);
insertAfter(document.getElementById('myId'), '<p>after</p>'); // <div id="myId">...</div> <p>after</p>
@frankiehayward
frankiehayward / initial.js
Created December 5, 2019 22:55
This snippet returns all elements of an array except the last one.
const initial = arr => arr.slice(0, -1);
initial([1, 2, 3]); // [1,2]
@frankiehayward
frankiehayward / index_of_all.js
Created December 5, 2019 22:54
This snippet can be used to get all indexes of a value in an array, which returns an empty array, in case this value is not included in it.
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); // []
@frankiehayward
frankiehayward / https_redirect.js
Created December 5, 2019 22:52
This snippet can be used to redirect from HTTP to HTTPS in a particular domain.
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
@frankiehayward
frankiehayward / hide.js
Created December 5, 2019 22:43
This snippet can be used to hide all elements specified.
const hide = (...el) => [...el].forEach(e => (e.style.display = 'none'));
hide(document.querySelectorAll('img')); // Hides all <img> elements on the page
@frankiehayward
frankiehayward / head.js
Created December 5, 2019 22:41
This snippet returns the `head` of a list.
const head = arr => arr[0];
head([1, 2, 3]); // 1
@frankiehayward
frankiehayward / get_type.js
Created December 5, 2019 22:38
This snippet can be used to get the type of a value.
const getType = v =>
v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
getType(new Set([1, 2, 3])); // 'set'