1
- // Returns the given value. Seems pointless perhaps but see its use below for providing a default, no-op callback.
2
- const identity = function ( val ) {
3
- return val ;
4
- } ;
5
-
6
1
// Returns the first n elements of the given array.
7
2
const first = function ( array , n = 1 ) {
8
3
return n === 1 ? array [ 0 ] : array . slice ( 0 , n ) ;
@@ -33,7 +28,7 @@ const isArrayLike = function(obj) {
33
28
34
29
// The cornerstone of a functional library -- iterate all elements, pass each to a callback function.
35
30
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
36
- const each = function ( obj , callback = identity ) {
31
+ const each = function ( obj , callback ) {
37
32
if ( isArrayLike ( obj ) ) {
38
33
for ( let index = 0 ; index < obj . length ; index ++ ) {
39
34
callback ( obj [ index ] , index , obj ) ;
@@ -47,15 +42,15 @@ const each = function(obj, callback=identity) {
47
42
48
43
// Return the results of applying the callback to each element.
49
44
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
50
- const map = function ( obj , callback = identity ) {
45
+ const map = function ( obj , callback ) {
51
46
const results = [ ] ;
52
47
each ( obj , ( currentValue , currentIndexOrKey , obj ) => {
53
48
results . push ( callback ( currentValue , currentIndexOrKey , obj ) ) ;
54
49
} ) ;
55
50
return results ;
56
51
} ;
57
52
58
- // Return an array of the values o a certain property in the collection.
53
+ // Return an array of the values of a certain property in the collection.
59
54
// E.g. given an array of people objects, return an array of just their ages.
60
55
const pluck = function ( obj , key ) {
61
56
return map ( obj , item => item [ key ] ) ;
@@ -67,7 +62,7 @@ const pluck = function(obj, key) {
67
62
// is not given, the first element of the collection is used as the initial
68
63
// value. The callback is invoked with four arguments:
69
64
// (accumulator, value, index|key, collection).
70
- const reduce = function ( obj , callback = identity , initialValue ) {
65
+ const reduce = function ( obj , callback , initialValue ) {
71
66
let accumulator = initialValue ;
72
67
let initializing = accumulator === undefined ;
73
68
each ( obj , ( currentValue , currentIndexOrKey , iteratedObj ) => {
@@ -89,21 +84,21 @@ const contains = function(obj, target) {
89
84
} ;
90
85
91
86
// Return true if all the elements / object values are accepted by the callback.
92
- const every = function ( obj , callback = identity ) {
87
+ const every = function ( obj , callback ) {
93
88
return reduce ( obj , ( allPassed , item ) => {
94
89
return allPassed && ! ! callback ( item ) ;
95
90
} , true ) ;
96
91
} ;
97
92
98
93
// Return true if even 1 element / object value is accepted by the callback.
99
- const some = function ( obj , callback = identity ) {
94
+ const some = function ( obj , callback ) {
100
95
return reduce ( obj , ( anyPassed , item ) => {
101
96
return anyPassed || ! ! callback ( item ) ;
102
97
} , false ) ;
103
98
} ;
104
99
105
100
// Return an array with all elements / object values that are accepted by the callback.
106
- const filter = function ( obj , callback = identity ) {
101
+ const filter = function ( obj , callback ) {
107
102
const result = [ ] ;
108
103
each ( obj , item => {
109
104
if ( callback ( item ) ) {
@@ -114,15 +109,19 @@ const filter = function(obj, callback=identity) {
114
109
} ;
115
110
116
111
// Return object without the elements / object valuesthat were rejected by the callback.
117
- const reject = function ( obj , callback = identity ) {
112
+ const reject = function ( obj , callback ) {
118
113
return filter ( obj , item => ! callback ( item ) ) ;
119
114
} ;
120
115
121
116
// De-duplicates (de-dups) the elements / object values.
122
117
const uniq = function ( obj ) {
123
118
const foundItems = { } ;
124
119
return filter ( obj , item => {
125
- return ! ( item in foundItems ) && ( foundItems [ item ] = true ) ;
120
+ if ( item in foundItems ) {
121
+ return false ;
122
+ }
123
+ foundItems [ item ] = true ;
124
+ return true ;
126
125
} ) ;
127
126
} ;
128
127
@@ -133,7 +132,6 @@ module.exports = {
133
132
every : every ,
134
133
filter : filter ,
135
134
first : first ,
136
- identity : identity ,
137
135
indexOf : indexOf ,
138
136
isArrayLike,
139
137
last : last ,
0 commit comments