insertBefore(document.getElementById('myId'), '<p>before</p>'); // <p>before</p> <div id="myId">...</div>
Returns true if the browser tab of the page is focused, false otherwise.
Use the Document.hidden property, introduced by the Page Visibility API to check if the browser tab of the page is visible or hidden.
const isBrowserTabFocused = () => !document.hidden;
isBrowserTabFocused(); // true -
Converts a NodeList to an array.
Use Array.prototype.slice() and Function.prototype.call() to convert a NodeList to an array.
const nodeListToArray = nodeList => Array.prototype.slice.call(nodeList); +
Converts a NodeList to an array.
Use spread operator inside new array to convert a NodeList to an array.
const nodeListToArray = nodeList => [...nodeList];
nodeListToArray(document.childNodes); // [ <!DOCTYPE html>, html ]
Returns a new MutationObserver and runs the provided callback for each mutation on the specified element.
Use a MutationObserver to observe mutations on the given element. Use Array.forEach() to run the callback for each mutation that is observed. Omit the third argument, options, to use the default options (all true).
const observeMutations = (element, callback, options) => { const observer = new MutationObserver(mutations => mutations.forEach(m => callback(m))); diff --git a/docs/index.html b/docs/index.html index 093437826..261548c19 100644 --- a/docs/index.html +++ b/docs/index.html @@ -218,7 +218,7 @@ };
intersection([1, 2, 3], [4, 3, 2]); // [2,3]
Returns a list of elements that exist in both arrays, after applying the provided function to each array element of both.
Create a Set by applying fn to all elements in b, then use Array.filter() on a to only keep elements, which produce values contained in b when fn is applied to them.
const intersectionBy = (a, b, fn) => { - const s = new Set(b.map(x => fn(x))); + 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] diff --git a/docs/math.html b/docs/math.html index edb086d33..ad8614262 100644 --- a/docs/math.html +++ b/docs/math.html @@ -232,7 +232,7 @@ own individual rating by supplying it as the third argument.
randomIntegerInRange(0, 5); // 2
Returns a random number in the specified range.
Use Math.random() to generate a random value, map it to the desired range using multiplication.
const randomNumberInRange = (min, max) => Math.random() * (max - min) + min;
randomNumberInRange(2, 10); // 6.0211363285087005 -
Rounds a number to a specified amount of digits.
Use Math.round() and template literals to round the number to the specified number of digits. Omit the second argument, decimals to round to an integer.
const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`); +
Rounds a number to a specified amount of digits.
Use Math.round() and template literals to round the number to the specified number of digits. Omit the second argument, decimals to round to an integer.
const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);
round(1.005, 2); // 1.01
Hashes the input string into a whole number.
Use String.split('') and Array.reduce() to create a hash of the input string, utilizing bit shifting.
const sdbm = str => { let arr = str.split('');