Skip to content

Commit 0a69891

Browse files
committed
Removes identity() and simplifies uniq().
1 parent e9b951e commit 0a69891

File tree

3 files changed

+12
-42
lines changed

3 files changed

+12
-42
lines changed

test/every.test.js

-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ const _ = require('../underbar');
22

33
describe('every()', () => {
44
describe('processing an array of numbers', () => {
5-
it('will return true if no callback is supplied', () => {
6-
const nums = [1, 3, 5, 7];
7-
expect(_.every(nums)).toBe(true);
8-
});
9-
105
it('returns true if all numbers in an array are odd and we test for odd numbers', () => {
116
const nums = [1, 3, 5, 7];
127
expect(_.every(nums, num => num % 2 === 1)).toBe(true);

test/identity.test.js

-23
This file was deleted.

underbar.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
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-
61
// Returns the first n elements of the given array.
72
const first = function(array, n = 1) {
83
return n === 1 ? array[0] : array.slice(0, n);
@@ -33,7 +28,7 @@ const isArrayLike = function(obj) {
3328

3429
// The cornerstone of a functional library -- iterate all elements, pass each to a callback function.
3530
// 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) {
3732
if (isArrayLike(obj)) {
3833
for (let index = 0; index < obj.length; index++) {
3934
callback(obj[index], index, obj);
@@ -47,7 +42,7 @@ const each = function(obj, callback=identity) {
4742

4843
// Return the results of applying the callback to each element.
4944
// 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) {
5146
const results = [];
5247
each(obj, (currentValue, currentIndexOrKey, obj) => {
5348
results.push(callback(currentValue, currentIndexOrKey, obj));
@@ -67,7 +62,7 @@ const pluck = function(obj, key) {
6762
// is not given, the first element of the collection is used as the initial
6863
// value. The callback is invoked with four arguments:
6964
// (accumulator, value, index|key, collection).
70-
const reduce = function(obj, callback=identity, initialValue) {
65+
const reduce = function(obj, callback, initialValue) {
7166
let accumulator = initialValue;
7267
let initializing = accumulator === undefined;
7368
each(obj, (currentValue, currentIndexOrKey, iteratedObj) => {
@@ -89,21 +84,21 @@ const contains = function(obj, target) {
8984
};
9085

9186
// 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) {
9388
return reduce(obj, (allPassed, item) => {
9489
return allPassed && !!callback(item);
9590
}, true);
9691
};
9792

9893
// 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) {
10095
return reduce(obj, (anyPassed, item) => {
10196
return anyPassed || !!callback(item);
10297
}, false);
10398
};
10499

105100
// 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) {
107102
const result = [];
108103
each(obj, item => {
109104
if (callback(item)) {
@@ -114,15 +109,19 @@ const filter = function(obj, callback=identity) {
114109
};
115110

116111
// 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) {
118113
return filter(obj, item => !callback(item));
119114
};
120115

121116
// De-duplicates (de-dups) the elements / object values.
122117
const uniq = function(obj) {
123118
const foundItems = {};
124119
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;
126125
});
127126
};
128127

@@ -133,7 +132,6 @@ module.exports = {
133132
every: every,
134133
filter: filter,
135134
first: first,
136-
identity: identity,
137135
indexOf: indexOf,
138136
isArrayLike,
139137
last: last,

0 commit comments

Comments
 (0)