总计95,持续新增!
感谢大佬愚人码头 的分享http://www.css88.com/archives/8748,下面都已经验证,做个收藏,后面有新会继续添加,有补充的请在下面评论去留code,感谢!
目的旨在减少外部引入underscore、lodash之类的
Array concatenation (数组拼接)
Array difference (数组比较)
Array includes (数组包含)
Array intersection (数组交集)
…这里太多就不列出来鸟。
Code
Array concatenation 数组拼接
描述:使用Array.concat(),并通过…args来合并数组 和/或值,完成拼接
const ArrayConcat = (arr, ...args) => [].concat(arr, ...args);
// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 2, 3, [4]]
Array didderence 数组比较
描述:
根据数组b创建一个set对象,然后再数组a上使用Array.filter()方法,过滤出数组b里不包含的数组a中的值,返回一个数组,顺序为这些数在数组a里的顺序
const difference = (a, b) => { const s = new Set(b); return a.filter( x => !s.has(x) ); } };
// difference([1,2,3], [1,2]) -> [3]
Array includes 数组包含
描述: 使用 slice() 来取得需要查找的范围的数组 /或字符串,并使用 indexOf() 来检查是否包含待检测值。如果省略最后一个参数 fromIndex ,则会检查整个数组/字符串,返回true或false。
const includes = (collection, val, fromIndex = 0) => collection.slice(fromIndex).indexOf(val) != -1;
// includes('30-seconds-of-code', 'code');
// includes([1, 2, 3, 4], [1, 2], 1) -> false
Array intersection 数组交集
描述:据数组 b 创建一个 Set 对象,然后在数组 a 上使用 Array.filter() 方法,返回一个新数组,数组 b 和a中都有的值,顺序为a中出现的顺序。
const intersection = (a, b) => { const s = new Set(b); return a.filter( x => s.has(x); ); }
// intersection([1,2,3], [4,3,2]) -> [2,3]
Array remove 数组移除(感觉直接用Array.filter() 也能实现该功能)
描述:使用 Array.filter() 和 Array.reduce() 来查找返回真值的数组元素,使用 Array.splice() 来移除元素。 func 有三个参数(value, index, array),返回被移除的数组成的数组,而非被移除之后的数组,顺序为arr中被筛选出来的数的顺序
const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { arr.splice(arr.indexOf(val), 1); return acc.concat(val); }, []) : [];
//remove([1, 2, 3, 4], n => n % 2 == 0) -> [2, 4]
Array sample 数组/字符串 中随机取样一个数(一个字符)
描述:使用 Math.random() 生成一个随机数,乘以 length,并使用 Math.floor() 舍去小数获得到最接近的整数。这个方法也适用于字符串。
const sample = arr => arr[Math.floor(Math.random() * arr.length)];
// sample([3, 7, 9, 11]) -> 9
Array union 数组合集(去重合并)
描述:用数组 a 和 b 的所有值创建一个 Set 对象,并转换成一个数组,IE暂不支持from()。
const union = (a, b) => Array.from(new Set([...a, ...b]));
// union([1,2,3], [4,3,2]) -> [1,2,3,4]
官方文档给的方法二:
function combine(){
let arr = [].concat.apply([], arguments); //没有去重复的新数组
return Array.from(new Set(arr));
}
var m = [1, 2, 2], n = [2,3,3];
// console.log(combine(m,n));
// console.log(combine([...m, ...n]));
Array without 排除数组中给定值
描述:使用 Array.filter() 创建一个排除所有给定值的数组。
const without = (arr, ...args) => arr.filter( v => args.indexOf(v) === -1 );
// without([2, 1, 2, 3], 1, 2) -> [3]
// without([2, 1, 2, 3, 4, 5, 5, 5, 3, 2, 7, 7], 3, 1, 5, 2) -> [ 4, 7, 7 ]
Array zip (创建一个分组元素数组)
描述: 使用 Math.max.apply() 获取参数中最长的数组。 创建一个长度为返回值的数组,并使用 Array.from() 和 map-function 来创建一个分组元素数组。 如果参数数组的长度不同,则在未找到值的情况下使用 undefined 。这个暂时没想到应用场景
const zip = (...arrays) => {
const maxLength = Math.max.apply(null, arrays.map(a => a.length));
return Array.from({length: maxLength}).map((_, i) => {
return Array.from({length: arrays.length}, (_, k) => arrays[k][i]);
})
}
//zip(['a', 'b'], [1, 2], [true, false]); -> [['a', 1, true], ['b', 2, false]]
//zip(['a'], [1, 2], [true, false]); -> [['a', 1, true], [undefined, 2, false]]
Array’s average (求数字数组的平均数)
描述: 使用 Array.reduce() 将数组中的每个值添加到一个累加器,使用 0 初始化,除以数组的 length (长度)。
const average3 = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
//average3([1,2,3,6,8]);
Array Chunk 数组分片
描述: 使用 Array.from() 创建一个新的数组,它的长度与将要生成的 chunk(块) 数量相匹配。 使用 Array.slice() 将新数组的每个元素映射到长度为 size 的 chunk 中。 如果原始数组不能均匀分割,最后的 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]]
compact (过滤数组里所有假值)
描述: 使用 Array.filter() 过滤掉数组中所有 假值元素(false, null, 0, “”, undefined, and NaN)。
const compact = (arr) => arr.filter( v => v );
// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ]
Count occurrences of a value in array (计数某数在数组中出现的次数)
描述: 每次遇到数组中的指定值时,使用 Array.reduce() 来递增计数器。
const countOccurrences2 = (arr, val) => arr.reduce((a, v) => v === val ? a + 1 : a + 0, 0 );
// countOccurrences2([1,1,2,1,2,3], 1) -> 3
Deep flatten array (深度平铺数组)
描述: 通过空数组([]) 使用 Array.concat() ,结合 展开运算符( … ) 来平铺数组。 递归平铺每个数组元素。
const deepFlatten = arr => [].concat(...arr.map(v => Array.isArray(v) ? deepFlatten(v) : v));
// deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
Drop elements in array (删除数组中的元素)
描述:这个方法有问题, 当有数值不满足的在最后的时候有问题,
const deleteEle = (arr, func) => { while(arr.length > 0 && !func(arr[0])) arr.shift(); return arr };
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
// dropElements([1, 2, 3, 4, 1, 2], n => n >= 3) -> [3,4]
Array fill (数组填充)
描述: 使用 Array.map() 将指定值映射到 start(包含)和 end (排除)之间。省略 start 将从第一个元素开始,省略 end 将在最后一个元素完成。
const fillArray = (arr, val, start = 0, end = arr.length) => arr.map((v, i) => i >= start && i < end ? val : v);
// fillArray([1,2,3,4],'8',1,3) -> [1,'8','8',4]
Filter out non-unique values in an array (过滤掉数组中的非唯一值)
描述: 使用 Array.filter() 滤除掉非唯一值,使数组仅包含唯一值。
const filterNonUnique = arr => arr.filter(v => arr.indexOf(v) === arr.lastIndexOf(v));
// filterNonUnique([1,2,2,3,4,4,5]) -> [1,3,5]
Flatten array up to depth (根据depth平铺数组)
描述:每次递归,使 depth 减 1 。使用 Array.reduce() 和 Array.concat() 来合并元素或数组。默认情况下, depth 等于 1 时停递归。省略第二个参数 depth ,只能平铺1层的深度 (单层平铺)。
const flattenDepth1 = (arr, depth = 1) =>
depth != 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), [])
: arr.reduce((a, v) => a.concat(v), []);
// flattenDepth1([1,[2],[[[3],4],5]], 2) -> [1,2,[3],4,5]
Flatten Array 单层平铺数组
描述: 使用 Array.reduce() 获取数组中的所有元素,并使用 concat() 将其平铺。
const flatten = arr => arr.reduce((a, v) => a.concat(v), []);
// flatten([1,[2],3,4]) -> [1,2,3,4]
Get max value from array (获取数组中的最大值)
描述: 结合使用 Math.max() 与 展开运算符( … ),获取数组中的最大值。就是学习了下(…)展开运算符
const arrayMax = arr => Math.max(...arr);
// arrayMax([10, 1, 5]) -> 10
Get min value from array (获取数组中的最小值)
描述: 结合使用 Math.max() 与 展开运算符( … ),获取数组中的最小值。
const arrayMin = arr => Math.min(...arr);
// arrayMin([10, 1, 5]) -> 1
Group by (数组分组)
描述: 使用 Array.map() 将数组的值映射到函数或属性名称。使用 Array.reduce() 来创建一个对象,其中的 key 是从映射结果中产生。 先写在这里,后面来理解使用场景
const groupBy = (arr, func) =>
arr.map(typeof func === 'function' ? func : val => val[func])
.reduce((acc, val, i) => { acc[val] = (acc[val] || []).concat(arr[i]); return acc; }, {});
// groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]}
// groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']}
Initial of list (排除数组中最后一个元素)
描述:使用 arr.slice(0,-1) 返回排除了最后一个元素的数组。
const initial = arr => arr.slice(0, -1);
// initial([1,2,3]) -> [1,2]
Last of list (获取数组的最后一个元素)
描述: 使用 arr.slice(-1)[0] 来获取给定数组的最后一个元素。
const last = arr => arr.slice(-1)[0];
// last([1,2,3]) -> 3
Initialize array with range (初始化特定范围的数组)
描述: 使用 Array(end-start) 创建所需长度的数组,使用 Array.map() 在一个范围内填充所需的值。您可以省略 start ,默认值 0。
const initializeArrayRange = (end, start = 0) =>
Array.apply(null, Array(end - start)).map((v, i) => i + start);
// initializeArrayRange(5) -> [0,1,2,3,4]
Initialize array with values (初始化特定范围和值的数组)
描述:使用 Array(n) 创建所需长度的数组,使用 fill(v) 以填充所需的值。您可以忽略 value ,使用默认值 0 。
const initializeArray = (n, value = 0) => Array(n).fill(value);
// initializeArray(5, 2) -> [2,2,2,2,2]
Median of Array ()
描述: 找到数字数组的中间值,使用 Array.sort() 对值进行排序。如果 length 是奇数,则返回中间值数字,否则返回两个中间值数值的平均值。
const median = arr => {
const mid = Math.floor(arr.length / 2), nums = arr.sort((a, b) => a - b);
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
};
// median([5,6,50,1,-5]) -> 5
// median([0,10,-2,7]) -> 3.5
Nth element of array (获取数组的第N个元素)
描述:使用 Array.slice() 获取数组的第 n 个元素。如果索引超出范围,则返回 [] 。省略第二个参数 n ,将得到数组的第一个元素。
const nth = (arr, n = 0) => (n > 0 ? arr.slice(n, n+1) : arr.slice(n))[0];
// nth(['a','b','c'],1) -> 'b'
// nth(['a','b','b'],-2) -> 'a'
Pick(提取)
描述: 使用 Array.reduce() 只 过滤/萃取 出 arr 参数指定 key (如果 key 存在于 obj 中)的属性值,。
const pick = (obj, arr) =>
arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
// pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) -> { 'a': 1, 'c': 3 }
// pick(object, ['a', 'c'])['a'] -> 1 这个有问题
Shuffle array (随机排列数组)
描述:使用 Array.sort() 来重新排序元素,比较器中使用 Math.random() 。
const shuffle = arr => arr.sort(() => Math.random() - 0.5);
// shuffle([1,2,3]) -> [2,3,1]
Similarity between arrays (获取数组交集)
描述: 使用 filter() 移除不在 values 中的值,使用 includes() 确定。
const similarity = (arr, values) => arr.filter((v) => values.includes(v));
// similarity([1,2,3], [1,2,4]) -> [1,2]
Sum of array of numbers (数字数组求和)
描述: 使用 Array.reduce() 将每个值添加到累加器,并使用0值初始化。
const sum = arr => arr.reduce((a, v) => a + v, 0);
// sum([1,2,3,4]) -> 10
Tail of list (返回剔除第一个元素后的数组)
描述:如果数组的 length 大于 1 ,则返回 arr.slice(1),否则返回整个数组。
const tail = arr => arr.length > 1 ? arr.slice(1) : arr;
// tail([1,2,3]) -> [2,3]
// tail([1]) -> [1]
Take right(从一个给定的数组中创建一个后N个元素的数组)
描述:使用 Array.slice() 来创建一个从第 n 个元素开始从末尾的数组。
const takeRight = (arr, n = 1) => arr.slice(arr.length - n, arr.length);
// takeRight([1, 2, 3], 2) -> [ 2, 3 ]
// takeRight([1, 2, 3]) -> [3]
Take(从一个给定的数组中创建一个前N个元素的数组)
描述: 使用 Array.slice() 创建一个数组包含第一个元素开始,到 n 个元素结束的数组。
const take = (arr, n = 1) => arr.slice(0, n);
// take([1, 2, 3], 5) -> [1, 2, 3]
// take([1, 2, 3], 0) -> []
Unique values of array (数组去重)
描述:使用 ES6 的 Set 和 …rest 操作符剔除重复的值
const unique = arr => [...new Set(arr)];
// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
呼,写了这么多数组的,其他的继续
重要的浏览器相关代码出场,
Bottom visibility (浏览器底部是否可见)
描述:
const bottomVisible = _ => (document.documentElement.clientHeight + window.srcollY) >= (document.documentElement.scrollHeight || document.documentElement.clientHeight);
//bottomVisible(); -> true
Element is visible in viewport (元素是否在可视窗口内)
描述: 使用 Element.getBoundingClientRect() 和 window.inner(Width|Height) 值来确定给定元素是否在可视窗口中可见。 省略第二个参数来判断元素是否完全可见,或者指定 true 来判断它是否部分可见。
const isVisibleInViewport = (el, partiallyVisible = true) => {
const { top, left, bottom, right } = el.getBoundingClientRect();
return partiallyVisible ? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) && ((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)) : top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
};
// 举个例子,有一个 100x100 可视窗口, 和一个 10x10px 元素定位在 {top: -1, left: 0, bottom: 9, right: 10}
// elementIsVisibleInViewport(el) -> false (not fully visible)
// elementIsVisibleInViewport(el, true) -> true (partially visible)
Get scroll position (获取滚动条位置)
描述:如果浏览器支持 pageXOffset 和 pageYOffset ,那么请使用 pageXOffset 和 pageYOffset ,否则请使用 scrollLeft 和 scrollTop 。 你可以省略 el 参数,默认值为 window。
const getScrollPosition = (el = window) => ({
x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollRight
})
// getScrollPosition() -> {x: 0, y: 200}
Redirect of url (URL重定向)
描述:
const redirect = (url, asLink) => asLink ? window.location.href = url : window.location.replace(url);
// redirect('https://google.com')
Scroll to top (回到顶部)
描述: 使用 document.documentElement.scrollTop 或 document.body.scrollTop 获取到顶部距离。从顶部滚动一小部分距离。使用window.requestAnimationFrame() 来实现滚动动画。
const scrollToTop = _ => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if(c > 0){
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c/8);
}
};
// scrollToTop()
哎,浏览器的就完了,完了? 不不,还有很多,未完待续,持续更新
日期,各种加密什么鬼的都会用到,
Get days difference between dates (获取两个日期之间相差天数)
描述: 计算 Date 对象之间的差异(以天为单位)。
const getDaysBetweenDifferenceDates = (datesInitial, datesFinal) => (datesFinal - datesInitial)/(1000*3600*24);
// getDaysDiffBetweenDates(new Date("2017-12-13"), new Date("2017-12-22")) -> 9
Function, 重头戏
Chain asynchronous functions (链式调用异步函数)
描述: 循环遍历包含异步事件的函数数组,每次异步事件完成后调用 next 。
const chainAsync = fns => {
let curr = 0;
const next = () => fns[curr++](next);
next();
}
/*
chainAsync([
next => { console.log('0 seconds'); setTimeout(next, 1000); },
next => { console.log('1 second'); setTimeout(next, 1000); },
next => { console.log('2 seconds'); }
])
*/
Curry (函数式编程术语: 柯里化)
描述: 使用递归。 如果提供的参数(args)数量足够(是否就是 > 0呢),调用传递函数 fn 。否则返回一个柯里化后的函数 fn ,期望剩下的参数。如果你想柯里化一个接受可变参数数量的函数(可变参数数量的函数,例如 Math.min() ),你可以选择将参数个数传递给第二个参数 arity。
这个需要多理解下, 这里给个参考多谢鑫老师的精彩分析(就是能复用之前的值)。但对于此处的es6的写法确实还差些理解
http://www.zhangxinxu.com/wordpress/2013/02/js-currying/
这篇文章,配合上面的,基本能理解
http://blog.youkuaiyun.com/neweastsun/article/details/75947785
const curry = (fn, arity = fn.length, ...args) => arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args);
// curry(Math.pow)(2)(10) -> 1024
// curry(Math.min, 3)(10)(50)(2) -> 2
Pipe (管道或者疏导)
描述: 使用 Array.reduce() 来执行从左到右的函数组合。第一个(最左边的)函数可以接受一个或多个参数;其余的函数必须是一元函数。
const pipe = (...fns) => fns.reduce((f, g) => (...args) => (g(f(...args))));
/*
const add5 = x => x + 5
const multiply = (x, y) => x * y
const multiplyAndAdd5 = pipe(multiply, add5)
multiplyAndAdd5(5, 2) -> 15
*/
Promisify (柯里化一个Promise)
描述: 使用柯里化返回一个函数,这个函数返回一个调用原始函数的 Promise 。 使用 …rest 运算符传入所有参数。
在 Node 8+ 中,你可以使用 util.promisify
const promisify = func => (...args) => new Promise((resolve, reject) => func(...args, (err, result) => err ? reject(err) : resolve(result)));
// const delay = promisify((d, cb) => setTimeout(cb, d))
// delay(2000).then(() => console.log('Hi!')) -> Promise resolves after 2s
Run promises in series (运行连续的Promise)
描述: 使用 Array.reduce() 通过创建 promise 链来运行连续的 promises,其中每个 promise 在 resolved 时返回下一个 promise 。
const series = ps => ps.reduce((p, next) => p.then(next), Promise.resolve());
// const delay = (d) => new Promise(r => setTimeout(r, d))
// series([() => delay(1000), () => delay(2000)]) -> executes each promise sequentially, taking a total of 3 seconds to complete
Sleep (休眠)
描述:
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
/*
async function sleepyWork() {
console.log('I\'m going to sleep for 1 second.');
await sleep(1000);
console.log('I woke up after 1 second.');
}
sleepyWork();
*/
Collatz algorithm (考拉兹算法)
描述: 如果 n 是偶数,则返回 n/2 。否则返回 3n+1 .
愚人码头注:考拉兹猜想(英语:Collatz conjecture),又称为奇偶归一猜想、3n+1猜想、冰雹猜想、角谷猜想、哈塞猜想、乌拉姆猜想或叙拉古猜想,是指对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1。 – 维基百科。
const collatz = n => (n % 2 === 0) ? (n / 2) : (3*n + 1);
// collatz(8);
// collatz(5);
Distance between two points(两点的欧氏距离)
描述:使用 Math.hypot() 计算两点之间的欧氏距离( Euclidean distance)。
愚人码头注: 欧氏距离( Euclidean distance)是一个通常采用的距离定义,它是在m维空间中两个点之间的真实距离。
const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
// distance(1,1, 2,3) -> 2.23606797749979
Factorial (阶乘)
描述: 使用递归。如果 n 小于或等于 1 ,则返回 1 。否则返回 n 和 n - 1 的阶乘。
const factorial = n => n <= 1 ? 1 : n * factorial(n - 1);
// factorial(6) -> 720
Fibonacci array generator (斐波那契数列发生器)
描述: 创建一个指定长度的空数组,初始化前两个值( 0 和 1 )。使用 Array.reduce() 向数组中添加值,使用最的值是两个值的和(前两个除外)。
const fibonacci = n => Array(n).fill(0).reduce((a, v, i) => a.concat(i > 1 ? (a[i - 1] + a[i - 2]) : i), []);
//fibonacci(6);
Greatest common divisor (最大公约数)
描述: 使用递归。当 y 等于 0 的情况下,返回 x 。否则,返回 y 和 x/y 余数最大公约数。
const gcd = (x, y) => !y ? x : gcd(y, x%y);
//gcd(8, 36);
Hamming distance (汉明距离)
描述:使用XOR运算符( ^ )查找这两个数字之间的位差,使用 toString(2) 转换为二进制字符串。使用 match(/1/g) 计算并返回字符串中 1 的数量。
这个感觉还不够,很多时候是字符串的比较,还需要转化
愚人码头注:在信息论中,两个等长字符串之间的汉明距离(英语:Hamming distance)是两个字符串对应位置的不同字符的个数。换句话说,它就是将一个字符串变换成另外一个字符串所需要替换的字符个数。- 维基百科
const hammingDistance = (num1, num2) => ((num1 ^ num2).toString(2).match(/1/g) || '').length;
//hammingDistance(2, 3);
Percentile (百分比)
描述:使用 Array.reduce() 来计算有多少数字小于等于该值,并用百分比表示。
const percentile = (arr, val) => 100 * arr.reduce((a, v, i) => a + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length;
// percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55
Powerset (幂集)
描述: 使用 Array.reduce() 与 Array.map() 结合来遍历元素,并将其组合成一个包含所有排列组合的数组。
const powerset = arr => arr.reduce((a, v) => a.concat(a.map(r => [v].concat(r))), [[]]);
// powerset([1,2]) -> [[], [1], [2], [2,1]]
Round number to n digits (精确到几位小数)
描述:
const round = (n, decimals = 0) => Number('${Math.round('${n}e${decimals}')e-${decimals}}')
// round(1.005, 2) -> 1.01
Standard deviation (标准偏差)
描述: 使用 Array.reduce() 来计算均值,方差已经值的方差之和,方差的值,然后确定标准偏差。 您可以省略第二个参数来获取样本标准偏差,或将其设置为 true 以获得总体标准偏差。
主要应用是数据采集
const standardDeviation = (arr, usePopulation = false) => {
const mean = arr.reduce((a, v) => a + v, 0) / arr.length;
return Math.sqrt(arr.reduce((a, v) => a.concat(Math.pow(v - mean, 2)), []).reduce((a, v) => a + v, 0) / (arr.length - (usePopulation ? 0 : 1)));
}
// standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample)
// standardDeviation([10,2,38,23,38,23,21], true) -> 12.29899614287479 (population)
Media (媒体)
Speech synthesis (语音合成,试验阶段)
描述: 使用 SpeechSynthesisUtterance.voice 和 indow.speechSynthesis.getVoices() 将消息转换为语音。使用 window.speechSynthesis.speak() 播放消息。
了解有关Web Speech API的SpeechSynthesisUtterance的更多信息。
const speak = message => {
const msg = new SpeechSynthesisUtterance(message);
msg.voice = window.speechSynthesis.getVoices()[0];
window.speechSynthesis.speak(msg);
}
// speak('Hello, World') -> plays the message
Node
Write JSON to file (将JSON写到文件)
描述: 使用 fs.writeFile(),模板字面量 和 JSON.stringify() 将 json 对象写入到 .json 文件中。
const fs = require('fs');
const jsonToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2))
// jsonToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json'
Object
Object from key-value pairs (根据键值对创建对象)
描述: 使用 Array.reduce() 来创建和组合键值对。
const objectFromPairs = arr => arr.reduce((a, v) => (a[v[0]] = v[1], a), {});
// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
Object to key-value pairs (对象转化为键值对 )
描述:使用 Object.keys() 和 Array.map() 遍历对象的键并生成一个包含键值对的数组。
const objectToPairs = obj => Object.keys(obj).map(k => [k , obj[k]]);
// objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]])
String
Anagrams of string (with duplicates) (字符串的排列组合,带有重复项)
描述: 使用递归。 对于给定字符串中的每个字母,为其余字母创建所有部分字母。 使用 Array.map() 将字母与每个部分字母组合在一起,然后使用 Array.reduce() 将所有字母组合到一个数组中。 基本情况是字符串 length 等于 2 或 1 。
const anagrams = str => {
if(str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
return str.split('').reduce((acc, letter, i) => acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)), []);
}
// anagrams('abc') -> ['abc','acb','bac','bca','cab','cba']
Capitalize of every word (大写每个单词的首字母)
描述: 使用 replace() 来匹配每个单词的第一个字符,并使用 toUpperCase() 来将其大写。
const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());
// capitalizeEveryWord('hello world!') -> 'Hello World!'
Capitalize first letter (大写首字母)
描述:
const capitalize = ([first, ...rest], lowerRest = false) => first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));
// capitalize('myName') -> 'MyName'
// capitalize('myName', true) -> 'Myname'
Check for palindrome (检查回文)
描述: 使用 toLowerCase() 转换字符串,并使用 replace() 从中删除非字母数字字符。 然后,在将其转换为 tolowerCase() 之后,将 split(”) 为单独的字符,reverse() ,join(”)并与原始非反转字符串进行比较。
const palindrome = str => {
const s = str.toLowerCase().replace(/[\W]/g, '');
return s === s.split('').reverse().join('');
}
// palindrome('taco cat') -> true
Reverse string (反转一个字符串)
描述:使用数组解构和 Array.reverse() 来反转字符串中字符的顺序。使用 join(”)合并字符串。
const reverseString = str => [...str].reverse().join('');
// reverseString('foobar') -> 'raboof'
Sort string (按照字母顺序排序)
描述: 使用 split(”) 分割字符串,通过 localeCompare() 排序字符串 Array.sort() ,使用 join(”) 进行重组。
const sortStringAToZ = str => str.split('').sort((a, b) => a.localeCompare(b)).join('');
// sortStringAToZ('cabbage') -> 'aabbceg'
Truncate a string (截断一个字符串)
描述: 确定字符串的 length 是否大于 num。 返回截断所需长度的字符串,用 … 附加到结尾或原始字符串。
const truncate = (str, num) => str.length > num ? str.slice(0, num > 3 ? (num - 3) : num) + '...' : str;
// truncate('boomerang', 7) -> 'boom...'
Utility 实用函数
Escape regular expression (转义正则表达式)
描述: 使用 replace() 来转义特殊字符。
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// escapeRegExp('(test)') -> \(test\)
Get native type of value (获取原生类型的值)
描述:返回值小写的构造函数名称,如果值为 undefined 或 null ,则返回 “undefined” 或 “null”。
const getType = v => v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
// getType(new Set([1,2,3])) -> "set"
Hexcode to Rgb (Hex转rgb)
描述: 使用Array.slice() , Array.map() 和 match() 将十六进制颜色代码(前缀为#)转换为RGB值的字符串。
下面的”怎么打出来运行有问题
const hexToRgb = hex => `rgb(${hex.slice(1).match(/.{2}/g).map(x => parseInt(x, 16)).join()})`
// hexToRgb('#27ae60') -> 'rgb(39,174,96)'
Is array(是否为数组)
描述:使用 Array.isArray() 来检查一个值是否为一个数组。
const isArray = val => !!val && Array.isArray(val);
// isArray(null) -> false
// isArray([1]) -> true
Measure time taken by function (计算函数执行花费的时间)
描述: 使用 console.time() 和 console.timeEnd() 来测量开始和结束时间之间的差,以确定回调执行的时间。
const timeTaken = (callback) => {
console.time('timeTaken');
const r = callack();
console.timeEnd('timeTaken');
return r;
}
// timeTaken(() => Math.pow(2, 10)) -> 1024
// (logged): timeTaken: 0.02099609375ms
Number to array of digits (把数字转化为整数数组)
描述: 将数字转换为字符串,使用 split() 来转换构建一个数组。 使用 Array.map() 和 parseInt() 将每个值转换为整数。
此处转化为string 类型,官方推荐的做法是String(带转化的值)
const digitize = n => String(n).split('').map((k, i) => parseInt(k));
// digitize(2334) -> [2, 3, 3, 4]
Ordinal suffix of number (数字序号的后缀)
描述: 使用模运算符(%)来查找各位和十位的值。查找哪些序号模式数字匹配。如果数字在十位模式中找到,请使用十位的序数。
这个使用场景,还不明了
const toOrdinalSuffix = num => {
const int = parseInt(num), digits = [(int % 10), (int % 100)],
ordinals = ['st', 'nd', 'rd', 'th'], oPattern = [1, 2, 3, 4],
tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19];
return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0] - 1] : int + ordinals[3];
};
// toOrdinalSuffix("123") -> "123rd"
Random integer in range (在指定的范围内生成一个随机整数)
描述: 使用 Math.random() 生成一个随机数并将其映射到所需的范围,使用 Math.floor() 使其成为一个整数。
const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min)) + min;
// randomIntegerInRange(0, 5) -> 2
RGB to hexadecimal (RGB转hex)
描述: 使用按位左移运算符(<<)和 toString(16) 将给定的RGB参数转换为十六进制字符串,然后使用 padStart(6,’0’) 得到一个6位的十六进制值。
const rgbToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
// rgbToHex(255, 165, 1) -> 'ffa501'
Swap values of two variables (交换两个变量的值)
描述:
[valueA, valueB] = [valueB, valueA];
URL parameters (网址参数)
描述: 通过适当的正则表达式,使用 match() 来获得所有的键值对, Array.reduce() 来映射和组合成一个单一的对象。将 location.search 作为参数传递给当前 url。
const getUrlParameters = url => url.match(/([^?=&]+)(=([^&]*))/g).reduce((a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {});
// getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'}
UUID generator (UUID生成器)
描述: 使用 crypto API 生成符一个 UUID,符合 RFC4122版本 4 。
const uuid = _ =>
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
// uuid() -> '7982fcfe-5721-4632-bede-6000885be57d'
validate email (验证邮箱)
描述:使用正则表达式来检查电子邮件是否有效。如果电子邮件有效,则返回 true ,否则返回false 。
const validateEmail = str => /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str);
// validateEmail('mymail@gmail.com') -> true
Validate number (验证数字)
描述: 使用 !isNaN 和 parseFloat() 来检查参数是否是一个数字。使用 isFinite() 来检查数字是否是有限数。使用 Number() 来检查强制转换是否成立。
感觉这里用验证类型就可以了哦,
由于 JavaScript 中一切都是对象,任何都不例外,对所有值类型应用 Object.prototype.toString.call() 方法结果如下:
;如下是js所有类型的验证
console.log(Object.prototype.toString.call(123)) //[object Number]
console.log(Object.prototype.toString.call('123')) //[object String]
console.log(Object.prototype.toString.call(undefined)) //[object Undefined]
console.log(Object.prototype.toString.call(true)) //[object Boolean]
console.log(Object.prototype.toString.call({})) //[object Object]
console.log(Object.prototype.toString.call([])) //[object Array]
console.log(Object.prototype.toString.call(function(){})) //[object Function]
const validateNumber = n => !isNaN(parseFloat(n)) && isFinite(n) && Number(n) == n;
// validateNumber('10') -> true