内容来源: https://github.com/parro-it/awesome-micro-npm-packages
1.is-sorted
检查数组是否排序
var sorted = require('is-sorted')
console.log(sorted([1, 2, 3]))
// => true
console.log(sorted([3, 1, 2]))
// => false
// supports custom comparators
console.log(sorted([3, 2, 1], function (a, b) { return b - a })
// => true
2.array-first
获取数组的第一个元素或前n个元素。
$ npm i -d && npm test
var first = require('array-first');
first(['a', 'b', 'c', 'd', 'e', 'f']);
//=> 'a'
first(['a', 'b', 'c', 'd', 'e', 'f'], 1);
//=> 'a'
first(['a', 'b', 'c', 'd', 'e', 'f'], 3);
//=> ['a', 'b', 'c']
3.array-last
返回数组中的最后一个元素。
$ npm install --save array-last
var last = require(' array-last ');
last([ ' a ',' b ',' c ',' d ',' e ',' f ' ]);
// =>'f'
last([ ' a ',' b ',' c ',' d ',' e ',' f ' ],1);
// =>'f'
last([ ' a ',' b ',' c ',' d ',' e ',' f ' ],3);
// => ['d','e','f']
4.arr-flatten
递归的展开一个或多个数组。
$ npm install --save arr-flatten
var flatten = require(' arr-flatten ');
flatten([ ' a ',[ ' b ',[ ' c ' ]],' d ',[ ' e ' ]]);
// => ['a','b','c','d','e']
5. dedupe
从队列中删除重复项
$ npm install dedupe
var dedupe = require('dedupe')
var a = [1, 2, 2, 3]
var b = dedupe(a)
console.log(b)
//result: [1, 2, 3]
var dedupe = require('dedupe')
var aa = [{a: 2}, {a: 1}, {a: 1}, {a: 1}]
var bb = dedupe(aa)
console.log(bb)
//result: [{a: 2}, {a: 1}]
var dedupe = require('dedupe')
var aaa = [{a: 2, b: 1}, {a: 1, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]
var bbb = dedupe(aaa, value => value.a)
console.log(bbb)
//result: [{a: 2, b: 1}, {a: 1,b: 2}]
6.array-range
创建一个给定范围的新数组。
npm install array-range -save
// 创建一个指定了范围的数组
var range = require('array-range')
range(3) // -> [ 0, 1, 2 ]
range(1, 4) // -> [ 1, 2, 3 ]
// 主要用于函数式编程
var array = require('array-range')
array(5).map( x => x*x )
// -> [ 0, 1, 4, 9, 16 ]
array(2, 10).filter( x => x%2===0 )
// -> [ 2, 4, 6, 8 ]
// 创建固定大小的数组,不会创建中间数组。
array(5)
//vs.
Array.apply(null, new Array(5))
7.arr-diff
通过使用严格的相等性进行比较,排除第一个数组中存在的其他数组中有的值,返回排除后的第一个数组。
$ npm install --save arr-diff
var diff = require('arr-diff');
var a = ['a', 'b', 'c', 'd'];
var b = ['b', 'c'];
console.log(diff(a, b))
//=> ['a', 'd']
8.filled-array
返回填充指定输入的数组
$ npm install --save filled-array
const filledArray = require('filled-array');
filledArray('x', 3);
//=> ['x', 'x', 'x']
filledArray(0, 3);
//=> [0, 0, 0]
filledArray(i => {
return (++i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz') || i;
}, 15);
//=> [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz']
9.map-array
将对象键和值映射到数组中
npm install --save map-array
const mapArray = require('map-array');
const obj = {
giorgio: 'Bianchi',
gino: 'Rossi'
};
console.log(mapArray(obj, (key, value) => key + ' ' + value));
10.in-array
如果数组中存在任何传递的值,则返回true --> 比使用indexOf快。
$ npm install in-array --save
var inArray = require('in-array');
console.log(inArray(['a', 'b', 'c'], 'a'));
//=> true
console.log(inArray(null, 'a'));
//=> false
console.log(inArray(null));
//=> false
11. unordered-array-remove
在无需拼接的情况下从无序数组中有效地移除元素
npm install unordered-array-remove
var remove = require('unordered-array-remove')
var list = ['a', 'b', 'c', 'd', 'e']
remove(list, 2) // remove 'c'
console.log(list) // returns ['a', 'b', 'e', 'd'] (no 'c')
12.array-swap
交换数组中两个元素的位置。
$ npm install swap-array --save
import SwapArray from 'swap-array';
var SomeArray = ['thats','cool','dude'];
SwapArray(SomeArray, 0, 2);
// ['dude','thats','cool'];
14.group-array
将对象数组分组到列表中。
yarn add mirrarray
var arr = [
{tag: 'one', content: 'A'},
{tag: 'one', content: 'B'},
{tag: 'two', content: 'C'},
{tag: 'two', content: 'D'},
{tag: 'three', content: 'E'},
{tag: 'three', content: 'F'}
];
// group by the `tag` property
groupArray(arr, 'tag');
结果是:
{
one: [
{tag: 'one', content: 'A'},
{tag: 'one', content: 'B'}
],
two: [
{tag: 'two', content: 'C'},
{tag: 'two', content: 'D'}
],
three: [
{tag: 'three', content: 'E'},
{tag: 'three', content: 'F'}
]
}
// given an array of object, like blog posts...
var arr = [
{ data: { year: '2016', tag: 'one', month: 'jan', day: '01'}, content: '...'},
{ data: { year: '2016', tag: 'one', month: 'jan', day: '01'}, content: '...'},
{ data: { year: '2016', tag: 'one', month: 'jan', day: '02'}, content: '...'},
{ data: { year: '2016', tag: 'one', month: 'jan', day: '02'}, content: '...'},
{ data: { year: '2016', tag: 'one', month: 'feb', day: '10'}, content: '...'},
{ data: { year: '2016', tag: 'one', month: 'feb', day: '10'}, content: '...'},
{ data: { year: '2016', tag: 'one', month: 'feb', day: '12'}, content: '...'},
{ data: { year: '2016', tag: 'one', month: 'feb', day: '12'}, content: '...'},
{ data: { year: '2016', tag: 'two', month: 'jan', day: '14'}, content: '...'},
{ data: { year: '2016', tag: 'two', month: 'jan', day: '14'}, content: '...'},
{ data: { year: '2016', tag: 'two', month: 'jan', day: '16'}, content: '...'},
{ data: { year: '2016', tag: 'two', month: 'jan', day: '16'}, content: '...'},
{ data: { year: '2016', tag: 'two', month: 'feb', day: '18'}, content: '...'},
{ data: { year: '2017', tag: 'two', month: 'feb', day: '18'}, content: '...'},
{ data: { year: '2017', tag: 'two', month: 'feb', day: '10'}, content: '...'},
{ data: { year: '2017', tag: 'two', month: 'feb', day: '10'}, content: '...'},
{ data: { year: '2017', tag: 'three', month: 'jan', day: '01'}, content: '...'},
{ data: { year: '2017', tag: 'three', month: 'jan', day: '01'}, content: '...'},
{ data: { year: '2017', tag: 'three', month: 'jan', day: '02'}, content: '...'},
{ data: { year: '2017', tag: 'three', month: 'jan', day: '02'}, content: '...'},
{ data: { year: '2017', tag: 'three', month: 'feb', day: '01'}, content: '...'},
{ data: { year: '2017', tag: 'three', month: 'feb', day: '01'}, content: '...'},
{ data: { year: '2017', tag: 'three', month: 'feb', day: '02'}, content: '...'},
{ data: { year: '2017', tag: 'three', month: 'feb', day: '02'}, content: '...'}
]
groupArray(arr, 'data.year', 'data.tag', 'data.month', 'data.day');
结果是:
{ '2016':
{ one:
{ jan:
{ '01':
[ { data: { year: '2016', tag: 'one', month: 'jan', day: '01' },
content: '...' },
{ data: { year: '2016', tag: 'one', month: 'jan', day: '01' },
content: '...' } ],
'02':
[ { data: { year: '2016', tag: 'one', month: 'jan', day: '02' },
content: '...' },
{ data: { year: '2016', tag: 'one', month: 'jan', day: '02' },
content: '...' } ] },
feb:
{ '10':
[ { data: { year: '2016', tag: 'one', month: 'feb', day: '10' },
content: '...' },
{ data: { year: '2016', tag: 'one', month: 'feb', day: '10' },
content: '...' } ],
'12':
[ { data: { year: '2016', tag: 'one', month: 'feb', day: '12' },
content: '...' },
{ data: { year: '2016', tag: 'one', month: 'feb', day: '12' },
content: '...' } ] } },
two:
{ jan:
{ '14':
[ { data: { year: '2016', tag: 'two', month: 'jan', day: '14' },
content: '...' },
{ data: { year: '2016', tag: 'two', month: 'jan', day: '14' },
content: '...' } ],
'16':
[ { data: { year: '2016', tag: 'two', month: 'jan', day: '16' },
content: '...' },
{ data: { year: '2016', tag: 'two', month: 'jan', day: '16' },
content: '...' } ] },
feb:
{ '18':
[ { data: { year: '2016', tag: 'two', month: 'feb', day: '18' },
content: '...' } ] } } },
'2017':
{ two:
{ feb:
{ '10':
[ { data: { year: '2017', tag: 'two', month: 'feb', day: '10' },
content: '...' },
{ data: { year: '2017', tag: 'two', month: 'feb', day: '10' },
content: '...' } ],
'18':
[ { data: { year: '2017', tag: 'two', month: 'feb', day: '18' },
content: '...' } ] } },
three:
{ jan:
{ '01':
[ { data: { year: '2017', tag: 'three', month: 'jan', day: '01' },
content: '...' },
{ data: { year: '2017', tag: 'three', month: 'jan', day: '01' },
content: '...' } ],
'02':
[ { data: { year: '2017', tag: 'three', month: 'jan', day: '02' },
content: '...' },
{ data: { year: '2017', tag: 'three', month: 'jan', day: '02' },
content: '...' } ] },
feb:
{ '01':
[ { data: { year: '2017', tag: 'three', month: 'feb', day: '01' },
content: '...' },
{ data: { year: '2017', tag: 'three', month: 'feb', day: '01' },
content: '...' } ],
'02':
[ { data: { year: '2017', tag: 'three', month: 'feb', day: '02' },
content: '...' },
{ data: { year: '2017', tag: 'three', month: 'feb', day: '02' },
content: '...' } ] } } } }
15.array.chunk
将array / TypedArray拆分为给定大小的块。
const chunks = require('array.chunk');
// [[1, 2], [3, 4], [5]]
console.log(chunks([1, 2, 3, 4, 5], 2));
网友评论