-
Notifications
You must be signed in to change notification settings - Fork 0
/
you-might-not-need-underscore.js
105 lines (75 loc) · 1.83 KB
/
you-might-not-need-underscore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//使用js原生的API代替underscore中的方法
_.each(array, iteratee);
//ES5.1array.foreach(iteratee);
_.map(array, iteratee)
//ES5.1
array.map(iteratee)
_.reduce(array, iteratee, memo)
//ES5.1
array.reduce(iteratee, memo)
_.reduceRight(array, iteratee, memo)
//ES5.1
array.reduceRight(iteratee, memo)
_.every(array, predicate)
//ES5.1
array.every(predicate)
_.some(array, predicate)
//ES5.1
array.some(predicate)
_.find(array, predicate)
array.find(predicate)
_.pluck(array, propertyName)
array.map(value => value[propertyName])
_.includes(array, element)
array.includes(element)
_.toArray(arguments)
[...arguments]
_.object(array)
array.reduce((result, [key, val]) => Object.assign(result, {[key]: val}), {})
_.compact(array)
array.filter(Boolean)
_.uniq(array)
[...new Set(array)]
_.indexOf(array, value)
array.indexOf(value)
_.findIndex([4, 6, 7, 12], isPrime)
[4, 6, 7, 12].findIndex(isPrime)
_.range(x, x + n)
Array.from(Array(n), (_, i) => x + i)
_.keys(object)
Object.keys(object)
_.size(object)
Object.keys(object).length
_.allKeys(object)
[...Reflect.enumerate(object)]
_.values(object)
Object.keys(object).map(key => object[key])
_.create(proto, properties)
Object.assign(Object.create(proto), properties)
_.assign({}, source, { a: false })
Object.assign({}, source, { a: false })
_.extendOwn({}, object)
{ ...object }
_.isArray(object)
Array.isArray(object)
_.isNumber(object) && _.isFinite(object)
Number.isFinite(object)
foo(_.bind(function () { this.bar();}, this));
foo(_.bind(object.fun, object));
//ES6
foo(() => { this.bar();});
foo(() => object.fun());
_.identity
value => value
_.constant(value)
() => value
_.noop()
=> {}
_.now()
Date.now()
//templatevar
greeting = _.template("hello <%= name %>");
greeting({ name: 'moe' });
//ES2015
const greeting = ({ name }) => `hello ${name}`;
greeting({ name: 'moe' });