Skip to content

Instantly share code, notes, and snippets.

View frentsel's full-sized avatar

Frentsel Alexandr frentsel

  • frentsel
  • Ukraine
View GitHub Profile
const highlightText = (str, match) => {
if (!str) return null;
if (!match) return str;
const regex = new RegExp(match, 'mig');
return `>${str}<`.replace(/>[^<]+</gmi, (match) => match.replace(regex, '[$]')).slice(1, -1);
};
console.log(highlightText('lorem 0 erty o', 'o'));
console.log(highlightText('<p class="lorem">lorem ipsum dolor sit amet</p>', 'o'));
// l[$]rem 0 erty [$]
@frentsel
frentsel / es6-compose.md
Last active December 10, 2018 17:38 — forked from JamieMason/es6-compose.md
ES6 JavaScript compose function

ES6 JavaScript Compose Function

Definition

const compose = (...fns) =>
  fns.reduce((prevFn, nextFn) =>
    (...args) => prevFn(nextFn(...args)),
    (value) => value);
@frentsel
frentsel / memoization.js
Last active November 26, 2018 11:59
memoization.js
const memoize = function(fn) {
const cache = {};
return function(...args) {
const key = args.join();
if (cache[key]) {
console.log('from cache!');
return cache[key];
}
return cache[key] = fn.apply(this, args);
};
@frentsel
frentsel / newArray.js
Created November 23, 2018 12:57
Create a new array with keys in range from 0 to limit
const newArray = (size) => [...Array(size).keys()];
newArray(5); // [0, 1, 2, 3, 4]
@frentsel
frentsel / factorial.js
Last active September 11, 2018 05:47
Factorial (Javascript)
var factorial = function(num) {
return !num ? 1 : num *= factorial(num - 1);
}
// Shorter ES6 variant
// const factorial = num => !num ? 1 : num *= factorial(num-1);
alert(factorial(6)); // 720
// https://jsfiddle.net/2ary59t0/157/
@frentsel
frentsel / recursive.function.js
Created September 11, 2018 05:39
Recursive function javascript
const data = [1, [2, [3], 4], 5];
const sum = (data) => {
return data.reduce((res, el) => {
if (el.length) {
return res + sum(el);
}
return res + el;
}, 0);
}
@frentsel
frentsel / BinaryTree.js
Last active November 23, 2018 12:08
Binary Tree example (Javascript)
var a = [...Array(5000000).keys()];
var chunk = (data, center) => {
const chunks = [];
while (data.length)
chunks.push(data.splice(0, center));
return chunks;
}
var treeBuild = (array, tree = []) => {
if (!array.length) return tree;
@frentsel
frentsel / Queue.js
Last active September 7, 2018 15:25
Data structure - Queue (javascript)
function Queue() {
var collection = [];
this.dequeue = () => collection.shift();
this.enqueue = (el) => collection.push(el);
this.peek = () => collection[0];
this.size = () => collection.length;
this.get = () => collection.slice(0);
}
@frentsel
frentsel / Stack.js
Last active September 7, 2018 15:21
Data structure - Stack (javascript)
const Stack = new function() {
let data = [];
this.pop = () => data.pop();
this.push = (el) => data.push(el);
this.peek = () => data[data.length - 1];
};
Stack.push(1);
@frentsel
frentsel / delay.js
Last active November 22, 2018 15:31
// Promise
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// or pseudo Promise
const delay = (time) => ({
then: (cb) => setTimeout(cb, time)
});
// usage
delay(5000).then(…);