Skip to content

Instantly share code, notes, and snippets.

View yairEO's full-sized avatar
🙂
Writing code

Yair Even Or yairEO

🙂
Writing code
View GitHub Profile
@yairEO
yairEO / cx.js
Last active October 22, 2020 10:38
conditional multiple class names
const cx = (...list) => list.filter(Boolean).join(' ')
@yairEO
yairEO / useDidMountEffect.js
Created May 23, 2020 18:41
React useEffect - disable execution on first load
import React, { useEffect, useRef } from 'react';
const useDidMountEffect = (func, deps) => {
const didMount = useRef(false);
useEffect(() => {
if (didMount.current) func();
else didMount.current = true;
}, deps);
}
@yairEO
yairEO / mockAjaxPromise.js
Created February 25, 2020 19:19
mock async AJAX request
const mockAjax = (timeout => (RES, duration) => {
clearTimeout(timeout); // abort last request
return new Promise((resolve, reject) => timeout = setTimeout(resolve, duration || 0, RES))
})()
@yairEO
yairEO / arrayIncludesObj.js
Created February 22, 2020 19:55
Check if an Array includes a specific Object
function arrayIncludesObj(arr, obj){
return arr.some(arrObj => JSON.stringify(arrObj) == JSON.stringify(obj))
}
@yairEO
yairEO / delay.js
Last active January 14, 2021 14:58
promise javascript delay
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
@yairEO
yairEO / minifyHTML.js
Last active September 3, 2023 22:22
HTML string minify / compress to single-line
/**
* @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()
: ""
}
@yairEO
yairEO / replaceTextWithNode.js
Last active December 9, 2019 09:48
Replace text in complex DOM with a node
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)
@yairEO
yairEO / getCaretCharOffset.js
Last active September 13, 2023 08:26
Get caret character offset at selection
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
}
@yairEO
yairEO / gist:88c71d61d14f4bad39a4c8f2f945e08e
Created December 3, 2019 14:10
Get gulp env variable
var opts = process.argv.reduce((result, item) => {
if( item.indexOf('--') == 0 )
result[item.replace('--','')] = 1
return result;
}, {})
@yairEO
yairEO / randomStringsArray.js
Created November 26, 2019 13:21
Generate javascript array with random strings
[...Array(10)].map(() => {
const randomStr = "abcdefghijklmnopqrstuvwxyz".split('').sort(() => .5-Math.random()).join('');
return randomStr.slice(0, Math.random()*26 + 2)
})