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 cx = (...list) => list.filter(Boolean).join(' ') |
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
import React, { useEffect, useRef } from 'react'; | |
const useDidMountEffect = (func, deps) => { | |
const didMount = useRef(false); | |
useEffect(() => { | |
if (didMount.current) func(); | |
else didMount.current = true; | |
}, deps); | |
} |
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 mockAjax = (timeout => (RES, duration) => { | |
clearTimeout(timeout); // abort last request | |
return new Promise((resolve, reject) => timeout = setTimeout(resolve, duration || 0, RES)) | |
})() |
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
function arrayIncludesObj(arr, obj){ | |
return arr.some(arrObj => JSON.stringify(arrObj) == JSON.stringify(obj)) | |
} |
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 delay = ms => new Promise(resolve => setTimeout(resolve, ms)) |
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
/** | |
* @param {string} s [HTML string] | |
*/ | |
function minify( s ){ | |
return s ? s | |
.replace(/\>[\r\n ]+\</g, "><") // Removes new lines and irrelevant spaces which might affect layout, and are better gone | |
.replace(/(<.*?>)|\s+/g, (m, $1) => $1 ? $1 : ' ') | |
.trim() | |
: "" | |
} |
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
function replaceTextWithNode( elm, text, replacerNode ){ | |
var iter = document.createNodeIterator(elm, NodeFilter.SHOW_TEXT), | |
textnode, | |
idx, | |
maxLoops = 100, | |
replacedNode; | |
while( textnode = iter.nextNode() ){ | |
if( !maxLoops-- ) break; | |
// get the index of which the tag (string) is within the textNode (if at all) |
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
function getCaretCharOffset(elm) { | |
var caretOffset = 0; | |
if (window.getSelection) { | |
var range = window.getSelection().getRangeAt(0), | |
preCaretRange = range.cloneRange(); | |
preCaretRange.selectNodeContents(elm) | |
preCaretRange.setEnd(range.endContainer, range.endOffset) | |
caretOffset = preCaretRange.toString().length | |
} |
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
var opts = process.argv.reduce((result, item) => { | |
if( item.indexOf('--') == 0 ) | |
result[item.replace('--','')] = 1 | |
return result; | |
}, {}) |
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
[...Array(10)].map(() => { | |
const randomStr = "abcdefghijklmnopqrstuvwxyz".split('').sort(() => .5-Math.random()).join(''); | |
return randomStr.slice(0, Math.random()*26 + 2) | |
}) |