Skip to content

Instantly share code, notes, and snippets.

@VoloshinS
VoloshinS / moveInArray.ts
Last active April 24, 2020 15:41
Immutable move element in array
export const moveInArray = <T>(arr: T[], fromIndex: number, toIndex: number): T[] => {
if (toIndex === fromIndex || toIndex >= arr.length) return arr;
const toMove = arr[fromIndex];
const movedForward = fromIndex < toIndex;
return arr.reduce((res, next, index) => {
if (index === fromIndex) return res;
if (index === toIndex) return res.concat(movedForward ? [next, toMove] : [toMove, next]);
@VoloshinS
VoloshinS / index.js
Created September 7, 2018 18:39
Serial Promise
export const promiseSerial = funcs =>
funcs.reduce((promise, func) =>
promise.then(result => func().then(Array.prototype.concat.bind(result))),
Promise.resolve([]));
@VoloshinS
VoloshinS / declarativeQuickSort.js
Created March 16, 2018 14:02
Declarative implementation of quicksort algorithm
const lessThen = (p) => (a) => a < p;
const greaterThen = (p) => (a) => a > p;
const quicksort = ([p, ...xs]) =>
(p === undefined) && !xs.length ? [] : [ ...quicksort(xs.filter(lessThen(p))), p, ...quicksort(xs.filter(greaterThen(p))) ]
@VoloshinS
VoloshinS / example.js
Created March 16, 2018 13:54
Check if props accidentally updated
componentWillReceiveProps(nextProps) {
const log = key => {
this.props[key] !== nextProps[key] && console.log(key, this.props[key], nextProps[key]);
};
console.log('===================');
Object.keys(this.props).map((i) => log(i));
console.log('===================');
}