Skip to content

Instantly share code, notes, and snippets.

🙂
Writing code

vsync yairEO

🙂
Writing code
Block or report user

Report or block yairEO

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@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
}
@yairEO
yairEO / consoleLogEmphasize.js
Last active May 28, 2019
console.log emphasize first arg
View consoleLogEmphasize.js
console.log = ((_c) => (...args) => args.length > 1 ? _c("%c "+ args[0], 'background:indianred; color:white; border-radius:3px; padding:1px;', ...args.slice(1)) : _c(...args))(console.log);
@yairEO
yairEO / string_parts.js
Created May 25, 2019
Get parts of string between certain characters
View string_parts.js
var s = "@cartman ,[[kyle]] do not know:[[homer simpson]]";
var collect = false;
var t;
for( let i = 0; i < s.length; i++ ){
if( s[i] == '[' && s[i] == s[i+1]){
collect = true;
i = i + 1;
continue;
}
@yairEO
yairEO / memo.js
Created Apr 21, 2019
simple memoize
View memo.js
var foo = (memo => ()=> {
// if the function was already computed, return the computed value
if(memo) return memo;
// continue with regular logic and set "memo" to the output value for later calls
memo = 1;
return memo;
})()
You can’t perform that action at this time.