Start lodash

This commit is contained in:
marleyrae 2023-06-27 18:12:38 -07:00
parent 35ea836a65
commit b99375d295
14 changed files with 323 additions and 0 deletions

2
lodash/_.js Normal file
View file

@ -0,0 +1,2 @@
// Do not write or modify code below this line.
module.exports = _

76
lodash/test/assert.js Normal file
View file

@ -0,0 +1,76 @@
const assert = {
testBlockTestCounts: {},
beginTestBlock(testName) {
this.testBlockTestCounts[testName] = 0;
console.log(`${testName} Tests:`);
// console.group(); - Not currently supported by LE Node version
},
endTestBlock() {
// console.groupEnd(); - Not currently supported by LE Node version
},
terminateTestBlock() {
console.log('Terminating tests...');
this.endTestBlock();
},
incrementTestNumber(testName) {
this.testBlockTestCounts[testName] += 1;
return this.testBlockTestCounts[testName];
},
exists(testName, functionString, value) {
const testNumber = this.incrementTestNumber(testName);
if (value) {
console.log("\x1b[32m%s\x1b[0m", `${testNumber} - ${functionString} is defined - Passed!`);
} else {
console.log("\x1b[31m%s\x1b[0m", `${testNumber} - ${functionString} is defined - Failed: ${functionString} was not properly defined.`);
}
},
equals(testName, description, functionString, actualValue, expectedValue) {
const testNumber = this.incrementTestNumber(testName);
if (actualValue === expectedValue) {
console.log("\x1b[32m%s\x1b[0m", `${testNumber} - ${description} - Passed!`);
} else {
console.log("\x1b[31m%s\x1b[0m", `${testNumber} - ${description} - Failed: ${functionString} returned ${actualValue} instead of ${expectedValue}.`);
}
},
arrayEquals(testName, description, functionString, actualValue, expectedValue) {
const testNumber = this.incrementTestNumber(testName);
if (arraysAreEqual(actualValue, expectedValue)) {
console.log("\x1b[32m%s\x1b[0m", `${testNumber} - ${description} - Passed!`);
} else {
console.log("\x1b[31m%s\x1b[0m", `${testNumber} - ${description} - Failed: ${functionString} returned ${arrayToString(actualValue)} instead of ${arrayToString(expectedValue)}.`);
}
},
};
function arrayToString(array) {
return `[ ${array.join(', ')} ]`;
}
function arraysAreEqual(array1, array2) {
const sortedArray2 = array2.sort();
return array1.length === array2.length &&
array1.sort().every((array1value, index) => {
array2Value = sortedArray2[index];
if (isArray(array1value) && isArray(array2Value)) {
return arraysAreEqual(array1value, array2Value);
} else {
return array1value === array2Value;
}
});
}
function isArray(array) {
return Object.prototype.toString.call(array) === '[object Array]';
}
module.exports = assert;

25
lodash/test/chunk.js Normal file
View file

@ -0,0 +1,25 @@
const _ = require('../_.js');
const assert = require('./assert.js');
const TEST_NAME = '_.chunk()';
assert.beginTestBlock(TEST_NAME);
assert.exists(TEST_NAME, '_.chunk()', _.chunk);
if (!_.chunk) {
assert.terminateTestBlock();
return;
}
assert.equals(TEST_NAME, 'Returns an array', '_.chunk([1, 2, 3, 4], 2)', Object.prototype.toString.call(_.chunk([1, 2, 3, 4], 2)), '[object Array]');
if (Object.prototype.toString.call(_.chunk([1, 2, 3, 4], 2)) !== '[object Array]') {
assert.terminateTestBlock();
return;
}
assert.arrayEquals(TEST_NAME, 'Chunks evenly-divided arrays', '_.chunk([1, 2, 3, 4], 2)', _.chunk([1, 2, 3, 4], 2), [[1, 2], [3, 4]]);
assert.arrayEquals(TEST_NAME, 'Chunks unevenly-divided arrays', '_.chunk([1, 2, 3, 4, 5], 2)', _.chunk([1, 2, 3, 4, 5], 2), [[1, 2], [3, 4], [5]]);
assert.endTestBlock();

19
lodash/test/clamp.js Normal file
View file

@ -0,0 +1,19 @@
const _ = require('../_.js');
const assert = require('./assert.js');
const TEST_NAME = '_.clamp()';
assert.beginTestBlock(TEST_NAME);
assert.exists(TEST_NAME, '_.clamp()', _.clamp);
if (!_.clamp) {
assert.terminateTestBlock();
return;
}
assert.equals(TEST_NAME, 'Returns in-range values unmodified', '_.clamp(2, 1, 3)', _.clamp(2, 1, 3), 2);
assert.equals(TEST_NAME, 'Clamps values by lower bound', '_.clamp(0, 1, 3)', _.clamp(0, 1, 3), 1);
assert.equals(TEST_NAME, 'Clamps values by upper bound', '_.clamp(5, 1, 3)', _.clamp(5, 1, 3), 3);
assert.endTestBlock();

26
lodash/test/drop-while.js Normal file
View file

@ -0,0 +1,26 @@
const _ = require('../_.js');
const assert = require('./assert.js');
const TEST_NAME = '_.dropWhile()';
assert.beginTestBlock(TEST_NAME);
assert.exists(TEST_NAME, '_.dropWhile()', _.dropWhile);
if (!_.dropWhile) {
assert.terminateTestBlock();
return;
}
const indexIsSmallerThanElement = (element, index) => index < element;
assert.equals(TEST_NAME, 'Returns an array', '_.dropWhile([1, 2, 0, 4], indexIsSmallerThanElement)', Object.prototype.toString.call(_.dropWhile([1, 2, 0, 4], indexIsSmallerThanElement)), '[object Array]');
if (Object.prototype.toString.call(_.dropWhile([1, 2, 0, 4], indexIsSmallerThanElement)) !== '[object Array]') {
assert.terminateTestBlock();
return;
}
assert.arrayEquals(TEST_NAME, 'Drops elements until predicate function returns falsy', "_.dropWhile([1, 2, 0, 4], indexIsSmallerThanElement", _.dropWhile([1, 2, 0, 4], indexIsSmallerThanElement), [0, 4]);
assert.endTestBlock();

25
lodash/test/drop.js Normal file
View file

@ -0,0 +1,25 @@
const _ = require('../_.js');
const assert = require('./assert.js');
const TEST_NAME = '_.drop()';
assert.beginTestBlock(TEST_NAME);
assert.exists(TEST_NAME, '_.drop()', _.drop);
if (!_.drop) {
assert.terminateTestBlock();
return;
}
assert.equals(TEST_NAME, 'Returns an array', '_.drop(["hi", "bye"])', Object.prototype.toString.call(_.drop(["hi", "bye"])), '[object Array]');
if (Object.prototype.toString.call(_.drop(["hi", "bye"])) !== '[object Array]') {
assert.terminateTestBlock();
return;
}
assert.arrayEquals(TEST_NAME, 'Drops one element if no number is specified', '_.drop(["hi", "bye"])', _.drop(["hi", "bye"]), ["bye"]);
assert.arrayEquals(TEST_NAME, 'Drops the specified number of elements from the beginning of an array', '_.drop(["hi", "okay", "yes", "bye"], 2)', _.drop(["hi", "okay", "yes", "bye"], 2), ["yes", "bye"]);
assert.endTestBlock();

19
lodash/test/find-key.js Normal file
View file

@ -0,0 +1,19 @@
const _ = require('../_.js');
const assert = require('./assert.js');
const TEST_NAME = '_.findKey()';
assert.beginTestBlock(TEST_NAME);
assert.exists(TEST_NAME, '_.findKey()', _.findKey);
if (!_.findKey) {
assert.terminateTestBlock();
return;
}
const startsWithV = string => string.startsWith('v');
assert.equals(TEST_NAME, 'Returns the corresponding key of a value that returns truthy from the predicate function', '_.findKey({"key": "value"}, startsWithV)', _.findKey({"key": "value"}, startsWithV), "key");
assert.equals(TEST_NAME, 'Returns undefined if an object has no values that return truthy from the predicate function', '_.findKey({"key": "notValue"}, startsWithV)', _.findKey({"key": "notValue"}, startsWithV), undefined);
assert.endTestBlock();

18
lodash/test/has.js Normal file
View file

@ -0,0 +1,18 @@
const _ = require('../_.js');
const assert = require('./assert.js');
const TEST_NAME = '_.has()';
assert.beginTestBlock(TEST_NAME);
assert.exists(TEST_NAME, '_.has()', _.has);
if (!_.has) {
assert.terminateTestBlock();
return;
}
assert.equals(TEST_NAME, 'Returns true if an object has a value at a specified key', '_.has({"key": "value"}, "key")', _.has({"key": "value"}, "key"), true);
assert.equals(TEST_NAME, 'Returns false if an object does not have a value at a specified key', '_.has({"key": "value"}, "notKey")', _.has({"key": "value"}, "notKey"), false);
assert.endTestBlock();

23
lodash/test/in-range.js Normal file
View file

@ -0,0 +1,23 @@
const _ = require('../_.js');
const assert = require('./assert.js');
const TEST_NAME = '_.inRange()';
assert.beginTestBlock(TEST_NAME);
assert.exists(TEST_NAME, '_.inRange()', _.inRange);
if (!_.inRange) {
assert.terminateTestBlock();
return;
}
assert.equals(TEST_NAME, 'Uses end value as start value and start value as 0 if end value is not defined', '_.inRange(1, 2)', _.inRange(1, 2), true);
assert.equals(TEST_NAME, 'Reverses start and end values if start is bigger than end', '_.inRange(3, 4, 2)', _.inRange(3, 4, 2), true);
assert.equals(TEST_NAME, 'Returns true if an in-range value is in range', '_.inRange(2, 1, 3)', _.inRange(2, 1, 3), true);
assert.equals(TEST_NAME, 'Returns false if a too small value is out of range', '_.inRange(0, 1, 3)', _.inRange(0, 1, 3), false);
assert.equals(TEST_NAME, 'Returns false if a too large value is out of range', '_.inRange(4, 1, 3)', _.inRange(4, 1, 3), false);
assert.equals(TEST_NAME, 'Returns true if provided value is same as start value', '_.inRange(1, 1, 3)', _.inRange(1, 1, 3), true);
assert.equals(TEST_NAME, 'Returns false if provided value is same as end value', '_.inRange(3, 1, 3)', _.inRange(3, 1, 3), false);
assert.endTestBlock();

18
lodash/test/invert.js Normal file
View file

@ -0,0 +1,18 @@
const _ = require('../_.js');
const assert = require('./assert.js');
const TEST_NAME = '_.invert()';
assert.beginTestBlock(TEST_NAME);
assert.exists(TEST_NAME, '_.invert()', _.invert);
if (!_.invert) {
assert.terminateTestBlock();
return;
}
assert.equals(TEST_NAME, 'Returns an object with all keys and values inverted', '_.invert({originalKey: "originalValue"})["originalValue"])', _.invert({originalKey: "originalValue"})['originalValue'], 'originalKey');
assert.equals(TEST_NAME, 'Returns an object with all keys and values inverted', '_.invert({originalKey: "originalValue"})["originalKey"])', _.invert({originalKey: "originalValue"})['originalKey'], undefined);
assert.endTestBlock();

17
lodash/test/lodash.js Normal file
View file

@ -0,0 +1,17 @@
const _ = require('../_.js');
const assert = require('./assert.js');
const TEST_NAME = 'Lodash Object';
assert.beginTestBlock(TEST_NAME);
assert.exists(TEST_NAME, '_', _);
if (!_) {
assert.terminateTestBlock();
return;
}
assert.equals(TEST_NAME, 'The value of _ is an object', '_', Object.prototype.toString.call(_), '[object Object]');
assert.endTestBlock();

19
lodash/test/pad.js Normal file
View file

@ -0,0 +1,19 @@
const _ = require('../_.js');
const assert = require('./assert.js');
const TEST_NAME = '_.pad()';
assert.beginTestBlock(TEST_NAME);
assert.exists(TEST_NAME, '_.pad()', _.pad);
if (!_.pad) {
assert.terminateTestBlock();
return;
}
assert.equals(TEST_NAME, 'Returns strings longer than provided length un-padded', '_.pad("hello", 4)', _.pad("hello", 4), "hello");
assert.equals(TEST_NAME, 'Returns evenly-padded strings', '_.pad("hi", 6)', _.pad("hi", 6), " hi ");
assert.equals(TEST_NAME, 'Returns oddly-padded strings with extra padding on right side', '_.pad("hi", 5)', _.pad("hi", 5), " hi ");
assert.endTestBlock();

10
lodash/test/test-all.js Normal file
View file

@ -0,0 +1,10 @@
require('./chunk.js');
require('./clamp.js');
require('./drop-while.js');
require('./drop.js');
require('./find-key.js');
require('./has.js');
require('./in-range.js');
require('./invert.js');
require('./pad.js');
require('./words.js');

26
lodash/test/words.js Normal file
View file

@ -0,0 +1,26 @@
const _ = require('../_.js');
const assert = require('./assert.js');
const TEST_NAME = '_.words()';
assert.beginTestBlock(TEST_NAME);
assert.exists(TEST_NAME, '_.words()', _.words);
if (!_.words) {
assert.terminateTestBlock();
return;
}
assert.equals(TEST_NAME, 'Returns an array', '_.words("hi ho")', Object.prototype.toString.call(_.words("hi ho")), '[object Array]');
if (Object.prototype.toString.call(_.words("hi ho")) !== '[object Array]') {
assert.terminateTestBlock();
return;
}
assert.arrayEquals(TEST_NAME, 'Returns an array of words from a string with one word', '_.words("hi")', _.words("hi"), ["hi"]);
assert.arrayEquals(TEST_NAME, 'Returns an array of words from a string with two words', '_.words("hi there")', _.words("hi there"), ["hi", "there"]);
assert.arrayEquals(TEST_NAME, 'Returns an array of words from a string with three words', '_.words("hi there you")', _.words("hi there you"), ["hi", "there", "you"]);
assert.endTestBlock();