From b99375d2957724359abe2bd3c91085e171de3a25 Mon Sep 17 00:00:00 2001 From: marleyrae Date: Tue, 27 Jun 2023 18:12:38 -0700 Subject: [PATCH] Start lodash --- lodash/_.js | 2 ++ lodash/test/assert.js | 76 +++++++++++++++++++++++++++++++++++++++ lodash/test/chunk.js | 25 +++++++++++++ lodash/test/clamp.js | 19 ++++++++++ lodash/test/drop-while.js | 26 ++++++++++++++ lodash/test/drop.js | 25 +++++++++++++ lodash/test/find-key.js | 19 ++++++++++ lodash/test/has.js | 18 ++++++++++ lodash/test/in-range.js | 23 ++++++++++++ lodash/test/invert.js | 18 ++++++++++ lodash/test/lodash.js | 17 +++++++++ lodash/test/pad.js | 19 ++++++++++ lodash/test/test-all.js | 10 ++++++ lodash/test/words.js | 26 ++++++++++++++ 14 files changed, 323 insertions(+) create mode 100644 lodash/_.js create mode 100644 lodash/test/assert.js create mode 100644 lodash/test/chunk.js create mode 100644 lodash/test/clamp.js create mode 100644 lodash/test/drop-while.js create mode 100644 lodash/test/drop.js create mode 100644 lodash/test/find-key.js create mode 100644 lodash/test/has.js create mode 100644 lodash/test/in-range.js create mode 100644 lodash/test/invert.js create mode 100644 lodash/test/lodash.js create mode 100644 lodash/test/pad.js create mode 100644 lodash/test/test-all.js create mode 100644 lodash/test/words.js diff --git a/lodash/_.js b/lodash/_.js new file mode 100644 index 0000000..a79693a --- /dev/null +++ b/lodash/_.js @@ -0,0 +1,2 @@ +// Do not write or modify code below this line. +module.exports = _ diff --git a/lodash/test/assert.js b/lodash/test/assert.js new file mode 100644 index 0000000..08d9e5d --- /dev/null +++ b/lodash/test/assert.js @@ -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; \ No newline at end of file diff --git a/lodash/test/chunk.js b/lodash/test/chunk.js new file mode 100644 index 0000000..71bd63c --- /dev/null +++ b/lodash/test/chunk.js @@ -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(); \ No newline at end of file diff --git a/lodash/test/clamp.js b/lodash/test/clamp.js new file mode 100644 index 0000000..3e8b8e4 --- /dev/null +++ b/lodash/test/clamp.js @@ -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(); \ No newline at end of file diff --git a/lodash/test/drop-while.js b/lodash/test/drop-while.js new file mode 100644 index 0000000..ab9ba90 --- /dev/null +++ b/lodash/test/drop-while.js @@ -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(); \ No newline at end of file diff --git a/lodash/test/drop.js b/lodash/test/drop.js new file mode 100644 index 0000000..6f4ceb1 --- /dev/null +++ b/lodash/test/drop.js @@ -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(); \ No newline at end of file diff --git a/lodash/test/find-key.js b/lodash/test/find-key.js new file mode 100644 index 0000000..26ab1f7 --- /dev/null +++ b/lodash/test/find-key.js @@ -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(); \ No newline at end of file diff --git a/lodash/test/has.js b/lodash/test/has.js new file mode 100644 index 0000000..59b2339 --- /dev/null +++ b/lodash/test/has.js @@ -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(); \ No newline at end of file diff --git a/lodash/test/in-range.js b/lodash/test/in-range.js new file mode 100644 index 0000000..b1926a1 --- /dev/null +++ b/lodash/test/in-range.js @@ -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(); \ No newline at end of file diff --git a/lodash/test/invert.js b/lodash/test/invert.js new file mode 100644 index 0000000..4af6f81 --- /dev/null +++ b/lodash/test/invert.js @@ -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(); \ No newline at end of file diff --git a/lodash/test/lodash.js b/lodash/test/lodash.js new file mode 100644 index 0000000..549f465 --- /dev/null +++ b/lodash/test/lodash.js @@ -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(); \ No newline at end of file diff --git a/lodash/test/pad.js b/lodash/test/pad.js new file mode 100644 index 0000000..83b2fc9 --- /dev/null +++ b/lodash/test/pad.js @@ -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(); \ No newline at end of file diff --git a/lodash/test/test-all.js b/lodash/test/test-all.js new file mode 100644 index 0000000..7166132 --- /dev/null +++ b/lodash/test/test-all.js @@ -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'); \ No newline at end of file diff --git a/lodash/test/words.js b/lodash/test/words.js new file mode 100644 index 0000000..bdd737a --- /dev/null +++ b/lodash/test/words.js @@ -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(); \ No newline at end of file