Skip to content

Instantly share code, notes, and snippets.

View frentsel's full-sized avatar

Frentsel Alexandr frentsel

  • frentsel
  • Ukraine
View GitHub Profile
@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 / 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 / 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 / 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);
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 [$]