一些非常有用的snippets

求多重数组的最小值

const ary = (fn, n) => (...args) => fn(...args.slice(0, n)); const getMax = ary(Math.min); [[2, 3, 4], [5, 67, 3], [20, 3]].map(x => getMax(...x)); // [2, 3, 3]

setTimeout的第三个参数
const sum = (...args) => console.log(args.reduce((x, y) => x + y, 0)); setTimeout(sum, 1000, 1, 2, 4, 5, 6, 7); // 25 0 // 相当于一秒钟之后打印出1 + 2 + 4 + 5 + 7的总和

取一堆字符串中的最小和最大值
const over = (...fns) => (...args) => fns.map(fn => fn.apply(null, args)); const minMax = over(Math.min, Math.max); minMax(1, 2, 3, 4, 5); // [1, 5]

转译标签
const unescapeHTML = str => str.replace(/& |< |> |' |" /g, tag => ({ '& ': '$', '< ': '<', '> ': '>', ''': "'", '" ': '"' }[tag] || tag)); unescapeHTML('1< 2, 3> 2'); // 1<2, 3>2

every some
  • 都只返回true或者false
const arr = [2, 2, 3, 4]; const all = (arr, fn = Boolean) => arr.every(fn); const any = (arr, fn = Boolean) => arr.some(fn); all(arr, x => x > 1); // true any(arr, x => x > 3); // true

是否为数字
const isNumber = val => typeof val === 'number'; isNumber('1'); // false isNumber(1); true

是否整除
const isDivisible = (dividend, divisor) => dividend % divisor === 0; isDivisible(6, 3); // true

类数组
const isArrayLike = obj => obj !== null && typeof obj[Symbol.iterator] === 'function'; isArrayLike(document.querySelectorAll('.className')); // true isArrayLike(null); // false isArrayLike('abc'); // true

判断是否为空
const isEmpty = val => val === null || val === undefined || !(Object.keys(val) || val).length; isEmpty({}); // true isEmpty([]); // true isEmpty(''); // true isEmpty(new Set()); // true isEmpty(undefined); // true isEmpty(); // true

是否为浏览器
const isBrowser = () => ![typeof window, typeof document].includes('undefined');

是否为素数 【一些非常有用的snippets】素数:也称为质数,意为在大于1的自然数中,除了1和它本身以外不再有其他[因数]
const isPrime = num => { const boundary = Math.floor(Math.sqrt(num)); for (let i = 2; i < boundary; i += 1) if (num % i === 0) return false; return num >= 2; }

斐波那契
const factorial = n => n <= 1 ? 1 : n * factorial(n - 1); factorail(6); // 720

chunk
const chunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size)); chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]];

深拷贝
const deepClone = obj => { let clone = Object.assign({}, obj); Object.keys(clone).forEach(key => ( clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key] )); return Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : clone; }; const a = {foo: 'bar', obj: {a: 1, b: 2}}; const b = deepClone(a); // {foo: 'bar', obj: {a: 1, b: 2}};

forOwn
const forOwn = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key, obj)); forOwn({foo: 'bar', a: 1}, v => console.log(v)); // 'bar', 1

mapValues
const mapValues = (obj, fn) => Object.keys(obj).reduce((acc, k) => { acc[k] = fn(obj[k], k, obj); return acc; }, {}); const users = { fred: { user: 'fred', age: 40 }, pebbles: { user: 'pebbles', age: 1 } }; mapValues(users, u => u.age); // {fred: 40, pebbles: 1}

merge
const object = {a: 3, b: 4: c: 2}; const other = {a: 4, b: 4}; {...object, ...other} // {a: 4, b: 4, c: 2}

    推荐阅读