Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove identity and simplify uniq #2

Merged
merged 2 commits into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions test/every.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ const _ = require('../underbar');

describe('every()', () => {
describe('processing an array of numbers', () => {
it('will return true if no callback is supplied', () => {
const nums = [1, 3, 5, 7];
expect(_.every(nums)).toBe(true);
});

it('returns true if all numbers in an array are odd and we test for odd numbers', () => {
const nums = [1, 3, 5, 7];
expect(_.every(nums, num => num % 2 === 1)).toBe(true);
Expand Down
23 changes: 0 additions & 23 deletions test/identity.test.js

This file was deleted.

28 changes: 13 additions & 15 deletions underbar.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
// Returns the given value. Seems pointless perhaps but see its use below for providing a default, no-op callback.
const identity = function(val) {
return val;
};

// Returns the first n elements of the given array.
const first = function(array, n = 1) {
return n === 1 ? array[0] : array.slice(0, n);
Expand Down Expand Up @@ -33,7 +28,7 @@ const isArrayLike = function(obj) {

// The cornerstone of a functional library -- iterate all elements, pass each to a callback function.
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
const each = function(obj, callback=identity) {
const each = function(obj, callback) {
if (isArrayLike(obj)) {
for (let index = 0; index < obj.length; index++) {
callback(obj[index], index, obj);
Expand All @@ -47,15 +42,15 @@ const each = function(obj, callback=identity) {

// Return the results of applying the callback to each element.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
const map = function(obj, callback=identity) {
const map = function(obj, callback) {
const results = [];
each(obj, (currentValue, currentIndexOrKey, obj) => {
results.push(callback(currentValue, currentIndexOrKey, obj));
});
return results;
};

// Return an array of the values o a certain property in the collection.
// Return an array of the values of a certain property in the collection.
// E.g. given an array of people objects, return an array of just their ages.
const pluck = function(obj, key) {
return map(obj, item => item[key]);
Expand All @@ -67,7 +62,7 @@ const pluck = function(obj, key) {
// is not given, the first element of the collection is used as the initial
// value. The callback is invoked with four arguments:
// (accumulator, value, index|key, collection).
const reduce = function(obj, callback=identity, initialValue) {
const reduce = function(obj, callback, initialValue) {
let accumulator = initialValue;
let initializing = accumulator === undefined;
each(obj, (currentValue, currentIndexOrKey, iteratedObj) => {
Expand All @@ -89,21 +84,21 @@ const contains = function(obj, target) {
};

// Return true if all the elements / object values are accepted by the callback.
const every = function(obj, callback=identity) {
const every = function(obj, callback) {
return reduce(obj, (allPassed, item) => {
return allPassed && !!callback(item);
}, true);
};

// Return true if even 1 element / object value is accepted by the callback.
const some = function(obj, callback=identity) {
const some = function(obj, callback) {
return reduce(obj, (anyPassed, item) => {
return anyPassed || !!callback(item);
}, false);
};

// Return an array with all elements / object values that are accepted by the callback.
const filter = function(obj, callback=identity) {
const filter = function(obj, callback) {
const result = [];
each(obj, item => {
if (callback(item)) {
Expand All @@ -114,15 +109,19 @@ const filter = function(obj, callback=identity) {
};

// Return object without the elements / object valuesthat were rejected by the callback.
const reject = function(obj, callback=identity) {
const reject = function(obj, callback) {
return filter(obj, item => !callback(item));
};

// De-duplicates (de-dups) the elements / object values.
const uniq = function(obj) {
const foundItems = {};
return filter(obj, item => {
return !(item in foundItems) && (foundItems[item] = true);
if (item in foundItems) {
return false;
}
foundItems[item] = true;
return true;
});
};

Expand All @@ -133,7 +132,6 @@ module.exports = {
every: every,
filter: filter,
first: first,
identity: identity,
indexOf: indexOf,
isArrayLike,
last: last,
Expand Down