Last active
April 28, 2021 15:20
-
-
Save typable/ce0a3de96ed308d98c3873e6c12c26f4 to your computer and use it in GitHub Desktop.
JavaScript Cheat Sheet
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
// sigmoid function | |
(x) => 1 / (1 + Math.pow(Math.E, -k * (x - b))); | |
// parable function | |
(x) => a * Math.pow(x - b, 2) + c; | |
// range function | |
// returns array with numbers from 0 to n | |
(n) => [...Array(n).keys()]; | |
// dot function | |
// returns dot product from two arrays | |
(a, b) => a.length == b.length ? a.reduce((n, m, i) => a[i] * b[i]) : null; | |
// returns array with length three each containing a random number between -1 and 1 | |
Array.from({ length: 3 }, () => Math.random() * (1 + 1) + -1); | |
// merges two arrays | |
a.concat(b); | |
// is primitive type | |
(obj) => obj !== Object(obj); | |
// is function | |
(obj) => obj && {}.toString.call(obj) === '[object Function]'; | |
// get digits of number | |
(x) => Math.floor(Math.log10(x)) + 1; | |
// remove duplicate values of an array | |
(arr) => [...new Set(arr)]; | |
// event listener that's called only once | |
element.addEventListener('click', callback, { once: true }); | |
// create image from array buffer | |
(obj) => { | |
let image = new Image(); | |
let blob = new Blob([new Uint8Array(obj).buffer]); | |
let reader = new FileReader(); | |
reader.onload = (event) => image.src = event.target.result; | |
reader.readAsDataURL(blob); | |
return image; | |
}; | |
// convert string into a function | |
(string) => new Function(string); | |
// extract fallback | |
(args) => { | |
let { obj } = args || {}; | |
let [ key, value ] = args || []; | |
}; | |
// module export for node and browser | |
if(typeof module === 'object' && module && typeof module.exports === 'object') { | |
module.exports = obj; | |
} | |
else { | |
window.obj = obj; | |
} | |
// add property to an object | |
Object.defineProperty(HTMLElement.prototype, 'hide', { | |
value: function(bool) { | |
this.style.display = Boolean(bool) ? 'none' : ''; | |
}, | |
writable: false, | |
enumerable: false, | |
configurable: false | |
}); | |
// copy range from array | |
(arr, start, end) => arr.slice(start, end); | |
// check if element contains another element | |
(parent, child) => parent.contains(child); | |
// quote replacement | |
(string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | |
// bind the outer this to the function scope | |
function() { ... }.bind(this); | |
// change url | |
window.history.pushState(null, null, 'http://localhost/test'); | |
// convert x, y to angle | |
(x, y) => Math.atan2(x, y); | |
// rename extracted property | |
let { pointerType: type } = event; | |
// get computed style properties | |
// { value: 14, unit: 'px' } | |
node.computedStyleMap().get('font-size'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment