Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args);
+ }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args);
Promise.resolve([1, 2, 3])
.then(call('map', x => 2 * x))
.then(console.log); //[ 2, 4, 6 ]
@@ -357,6 +357,12 @@ elementIsVisibleInViewport(el, true); // true // (partially visible)
setStyle(document.querySelector('p'), 'font-size', '20px'); // The first <p> element on the page will have a font-size of 20px
show
Shows all the elements specified.
Use the spread operator (...) and Array.forEach() to clear the display property for each element specified.
const show = (...el) => [...el].forEach(e => (e.style.display = ''));
show(document.querySelectorAll('img')); // Shows all <img> elements on the page
+
speechSynthesis
Performs speech synthesis (experimental).
Use SpeechSynthesisUtterance.voice and window.speechSynthesis.getVoices() to convert a message to speech. Use window.speechSynthesis.speak() to play the message.
Learn more about the SpeechSynthesisUtterance interface of the Web Speech API.
const speechSynthesis = message => {
+ const msg = new SpeechSynthesisUtterance(message);
+ msg.voice = window.speechSynthesis.getVoices()[0];
+ window.speechSynthesis.speak(msg);
+};
+
speechSynthesis('Hello, World'); // // plays the message
toggleClass
Toggle a class for an element.
Use element.classList.toggle() to toggle the specified class for the element.
const toggleClass = (el, className) => el.classList.toggle(className);
toggleClass(document.querySelector('p.special'), 'special'); // The paragraph will not have the 'special' class anymore
UUIDGeneratorBrowser
Generates a UUID in a browser.
Use crypto API to generate a UUID, compliant with RFC4122 version 4.
const UUIDGeneratorBrowser = () =>
@@ -549,12 +555,6 @@ median([0, 10, -2, 7]); // 3.5
standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (population)
sum
Returns the sum of an of two or more numbers/arrays.
Use Array.reduce() to add each value to an accumulator, initialized with a value of 0.
const sum = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0);
sum([1, 2, 3, 4]); // 10
-
Media
speechSynthesis
Performs speech synthesis (experimental).
Use SpeechSynthesisUtterance.voice and window.speechSynthesis.getVoices() to convert a message to speech. Use window.speechSynthesis.speak() to play the message.
Learn more about the SpeechSynthesisUtterance interface of the Web Speech API.
const speechSynthesis = message => {
- const msg = new SpeechSynthesisUtterance(message);
- msg.voice = window.speechSynthesis.getVoices()[0];
- window.speechSynthesis.speak(msg);
-};
-
speechSynthesis('Hello, World'); // // plays the message
Node
JSONToFile
Writes a JSON object to a file.
Use fs.writeFile(), template literals and JSON.stringify() to write a json object to a .json file.
const fs = require('fs');
const JSONToFile = (obj, filename) =>
fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2));