Skip to content

Instantly share code, notes, and snippets.

🙂
Writing code

Yair Even Or yairEO

🙂
Writing code
View GitHub Profile
@yairEO
yairEO / useDidMountEffect.js
Created May 23, 2020
React useEffect - disable execution on first load
View useDidMountEffect.js
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 Feb 25, 2020
mock async AJAX request
View mockAjaxPromise.js
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 Feb 22, 2020
Check if an Array includes a specific Object
View arrayIncludesObj.js
function arrayIncludesObj(arr, obj){
return arr.some(arrObj => JSON.stringify(arrObj) == JSON.stringify(obj))
}
@yairEO
yairEO / delay.js
Created Dec 26, 2019
promise javascript delay
View delay.js
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
@yairEO
yairEO / minifyHTML.js
Last active Dec 16, 2019
HTML string minify / compress to single-line
View minifyHTML.js
/**
* @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 Dec 9, 2019
Replace text in complex DOM with a node
View replaceTextWithNode.js
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 Dec 6, 2019
Get caret character offset at selection
View getCaretCharOffset.js
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
}
View gist:88c71d61d14f4bad39a4c8f2f945e08e
var opts = process.argv.reduce((result, item) => {
if( item.indexOf('--') == 0 )
result[item.replace('--','')] = 1
return result;
}, {})
@yairEO
yairEO / randomStringsArray.js
Created Nov 26, 2019
Generate javascript array with random strings
View randomStringsArray.js
[...Array(10)].map(() => {
const randomStr = "abcdefghijklmnopqrstuvwxyz".split('').sort(() => .5-Math.random()).join('');
return randomStr.slice(0, Math.random()*26 + 2)
})
@yairEO
yairEO / stringInterpolator.js
Last active Aug 21, 2019
custom string interpolation
View stringInterpolator.js
var str = "aaa {{xxx}} bb {{x}} ccc {{vv}}";
// split by "start" pattern
str.split('{{').map(s1 => {
// for each splited part, split again by "end" pattern
var s2 = s1.split('}}');
// if this split resulted in an array with more than 1 item, it means the first item was between the patterns
if( s2.length > 1 ){
s2[0] = "foo" // replace the array item with whatever
}
You can’t perform that action at this time.