Skip to content

Commit c9bbfd2

Browse files
committed
Completed Task
1 parent 7dcf0ac commit c9bbfd2

File tree

14 files changed

+451
-0
lines changed

14 files changed

+451
-0
lines changed

lodash-task/_.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//This file is my code
2+
3+
const _ = {
4+
5+
clamp(number,lower,upper){
6+
7+
/*
8+
if(number < lower){
9+
return lower;
10+
}else if(number > upper){
11+
return upper;
12+
}else{
13+
return number;
14+
}
15+
*/
16+
//return Math.min(Math.max(number,lower),upper);
17+
},
18+
19+
inRange(number,start,end){
20+
21+
if(end===undefined){
22+
end = start;
23+
start = 0;
24+
}
25+
if(start > end){
26+
let temp = start;
27+
start = end;
28+
end = temp;
29+
}
30+
//console.log(`is ${number} between ${start} and ${end} inclusive?`)
31+
return (number >= start && number < end) ? true : false;
32+
},
33+
34+
words(string){
35+
36+
return string.split(' ');
37+
38+
},
39+
40+
pad(string,length){
41+
42+
while(string.length<length){
43+
44+
let dif = length - string.length;
45+
46+
if(dif >= 2){
47+
string = ` ${string} `
48+
}
49+
else if (dif = 1)
50+
{
51+
string = `${string} `
52+
}
53+
54+
}
55+
return string;
56+
},
57+
58+
has(obj,key){
59+
60+
let val = obj[key]
61+
62+
return (val === undefined) ? false : true;
63+
64+
},
65+
66+
invert(obj){
67+
68+
const newObj = {}
69+
70+
for(const [key,value] of Object.entries(obj)){
71+
72+
newObj[value] = key
73+
}
74+
75+
return newObj;
76+
},
77+
78+
findKey(obj,func){
79+
80+
for(const[key,value] of Object.entries(obj)){
81+
82+
if(func(value)){
83+
return key;
84+
}
85+
}
86+
return undefined;
87+
88+
},
89+
90+
drop(arr,num=1){
91+
92+
for(let i = num;i>0;i--){
93+
arr.shift()
94+
}
95+
return arr;
96+
97+
},
98+
99+
dropWhile(arr,func){
100+
101+
for(let i = 0;i<arr.length;i++){
102+
103+
if(!func(arr[i],i,arr))
104+
{
105+
return this.drop(arr,i);
106+
}
107+
108+
109+
}
110+
},
111+
112+
chunk(arr,size=1){
113+
114+
let newArr = []
115+
116+
for(let i = 0;i < arr.length;i+=size){
117+
118+
newArr.push(arr.slice(i,i+size))
119+
120+
}
121+
122+
return newArr;
123+
}
124+
};
125+
126+
127+
128+
129+
// Do not write or modify code below this line.
130+
module.exports = _;

lodash-task/test/assert.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const assert = {
2+
testBlockTestCounts: {},
3+
4+
beginTestBlock(testName) {
5+
this.testBlockTestCounts[testName] = 0;
6+
console.log(`${testName} Tests:`);
7+
// console.group(); - Not currently supported by LE Node version
8+
},
9+
10+
endTestBlock() {
11+
// console.groupEnd(); - Not currently supported by LE Node version
12+
},
13+
14+
terminateTestBlock() {
15+
console.log('Terminating tests...');
16+
this.endTestBlock();
17+
},
18+
19+
incrementTestNumber(testName) {
20+
this.testBlockTestCounts[testName] += 1;
21+
return this.testBlockTestCounts[testName];
22+
},
23+
24+
exists(testName, functionString, value) {
25+
const testNumber = this.incrementTestNumber(testName);
26+
27+
if (value) {
28+
console.log("\x1b[32m%s\x1b[0m", `${testNumber} - ${functionString} is defined - Passed!`);
29+
} else {
30+
console.log("\x1b[31m%s\x1b[0m", `${testNumber} - ${functionString} is defined - Failed: ${functionString} was not properly defined.`);
31+
}
32+
},
33+
34+
equals(testName, description, functionString, actualValue, expectedValue) {
35+
const testNumber = this.incrementTestNumber(testName);
36+
37+
if (actualValue === expectedValue) {
38+
console.log("\x1b[32m%s\x1b[0m", `${testNumber} - ${description} - Passed!`);
39+
} else {
40+
console.log("\x1b[31m%s\x1b[0m", `${testNumber} - ${description} - Failed: ${functionString} returned ${actualValue} instead of ${expectedValue}.`);
41+
}
42+
},
43+
44+
arrayEquals(testName, description, functionString, actualValue, expectedValue) {
45+
const testNumber = this.incrementTestNumber(testName);
46+
47+
if (arraysAreEqual(actualValue, expectedValue)) {
48+
console.log("\x1b[32m%s\x1b[0m", `${testNumber} - ${description} - Passed!`);
49+
} else {
50+
console.log("\x1b[31m%s\x1b[0m", `${testNumber} - ${description} - Failed: ${functionString} returned ${arrayToString(actualValue)} instead of ${arrayToString(expectedValue)}.`);
51+
}
52+
},
53+
};
54+
55+
function arrayToString(array) {
56+
return `[ ${array.join(', ')} ]`;
57+
}
58+
59+
function arraysAreEqual(array1, array2) {
60+
const sortedArray2 = array2.sort();
61+
return array1.length === array2.length &&
62+
array1.sort().every((array1value, index) => {
63+
array2Value = sortedArray2[index];
64+
if (isArray(array1value) && isArray(array2Value)) {
65+
return arraysAreEqual(array1value, array2Value);
66+
} else {
67+
return array1value === array2Value;
68+
}
69+
});
70+
}
71+
72+
function isArray(array) {
73+
return Object.prototype.toString.call(array) === '[object Array]';
74+
}
75+
76+
module.exports = assert;

lodash-task/test/chunk.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const _ = require('../_.js');
2+
const assert = require('./assert.js');
3+
4+
const TEST_NAME = '_.chunk()';
5+
6+
assert.beginTestBlock(TEST_NAME);
7+
8+
assert.exists(TEST_NAME, '_.chunk()', _.chunk);
9+
10+
if (!_.chunk) {
11+
assert.terminateTestBlock();
12+
return;
13+
}
14+
15+
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]');
16+
17+
if (Object.prototype.toString.call(_.chunk([1, 2, 3, 4], 2)) !== '[object Array]') {
18+
assert.terminateTestBlock();
19+
return;
20+
}
21+
22+
assert.arrayEquals(TEST_NAME, 'Chunks evenly-divided arrays', '_.chunk([1, 2, 3, 4], 2)', _.chunk([1, 2, 3, 4], 2), [[1, 2], [3, 4]]);
23+
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]]);
24+
25+
assert.endTestBlock();

lodash-task/test/clamp.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const _ = require('../_.js');
2+
const assert = require('./assert.js');
3+
4+
const TEST_NAME = '_.clamp()';
5+
6+
assert.beginTestBlock(TEST_NAME);
7+
8+
assert.exists(TEST_NAME, '_.clamp()', _.clamp);
9+
10+
if (!_.clamp) {
11+
assert.terminateTestBlock();
12+
return;
13+
}
14+
15+
assert.equals(TEST_NAME, 'Returns in-range values unmodified', '_.clamp(2, 1, 3)', _.clamp(2, 1, 3), 2);
16+
assert.equals(TEST_NAME, 'Clamps values by lower bound', '_.clamp(0, 1, 3)', _.clamp(0, 1, 3), 1);
17+
assert.equals(TEST_NAME, 'Clamps values by upper bound', '_.clamp(5, 1, 3)', _.clamp(5, 1, 3), 3);
18+
19+
assert.endTestBlock();

lodash-task/test/drop-while.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const _ = require('../_.js');
2+
const assert = require('./assert.js');
3+
4+
const TEST_NAME = '_.dropWhile()';
5+
6+
assert.beginTestBlock(TEST_NAME);
7+
8+
assert.exists(TEST_NAME, '_.dropWhile()', _.dropWhile);
9+
10+
if (!_.dropWhile) {
11+
assert.terminateTestBlock();
12+
return;
13+
}
14+
15+
const indexIsSmallerThanElement = (element, index) => index < element;
16+
17+
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]');
18+
19+
if (Object.prototype.toString.call(_.dropWhile([1, 2, 0, 4], indexIsSmallerThanElement)) !== '[object Array]') {
20+
assert.terminateTestBlock();
21+
return;
22+
}
23+
24+
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]);
25+
26+
assert.endTestBlock();

lodash-task/test/drop.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const _ = require('../_.js');
2+
const assert = require('./assert.js');
3+
4+
const TEST_NAME = '_.drop()';
5+
6+
assert.beginTestBlock(TEST_NAME);
7+
8+
assert.exists(TEST_NAME, '_.drop()', _.drop);
9+
10+
if (!_.drop) {
11+
assert.terminateTestBlock();
12+
return;
13+
}
14+
15+
assert.equals(TEST_NAME, 'Returns an array', '_.drop(["hi", "bye"])', Object.prototype.toString.call(_.drop(["hi", "bye"])), '[object Array]');
16+
17+
if (Object.prototype.toString.call(_.drop(["hi", "bye"])) !== '[object Array]') {
18+
assert.terminateTestBlock();
19+
return;
20+
}
21+
22+
assert.arrayEquals(TEST_NAME, 'Drops one element if no number is specified', '_.drop(["hi", "bye"])', _.drop(["hi", "bye"]), ["bye"]);
23+
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"]);
24+
25+
assert.endTestBlock();

lodash-task/test/find-key.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const _ = require('../_.js');
2+
const assert = require('./assert.js');
3+
4+
const TEST_NAME = '_.findKey()';
5+
6+
assert.beginTestBlock(TEST_NAME);
7+
8+
assert.exists(TEST_NAME, '_.findKey()', _.findKey);
9+
10+
if (!_.findKey) {
11+
assert.terminateTestBlock();
12+
return;
13+
}
14+
15+
const startsWithV = string => string.startsWith('v');
16+
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");
17+
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);
18+
19+
assert.endTestBlock();

lodash-task/test/has.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const _ = require('../_.js');
2+
const assert = require('./assert.js');
3+
4+
const TEST_NAME = '_.has()';
5+
6+
assert.beginTestBlock(TEST_NAME);
7+
8+
assert.exists(TEST_NAME, '_.has()', _.has);
9+
10+
if (!_.has) {
11+
assert.terminateTestBlock();
12+
return;
13+
}
14+
15+
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);
16+
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);
17+
18+
assert.endTestBlock();

lodash-task/test/in-range.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const _ = require('../_.js');
2+
const assert = require('./assert.js');
3+
4+
const TEST_NAME = '_.inRange()';
5+
6+
assert.beginTestBlock(TEST_NAME);
7+
8+
assert.exists(TEST_NAME, '_.inRange()', _.inRange);
9+
10+
if (!_.inRange) {
11+
assert.terminateTestBlock();
12+
return;
13+
}
14+
15+
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);
16+
assert.equals(TEST_NAME, 'Reverses start and end values if start is bigger than end', '_.inRange(3, 4, 2)', _.inRange(3, 4, 2), true);
17+
assert.equals(TEST_NAME, 'Returns true if an in-range value is in range', '_.inRange(2, 1, 3)', _.inRange(2, 1, 3), true);
18+
assert.equals(TEST_NAME, 'Returns false if a too small value is out of range', '_.inRange(0, 1, 3)', _.inRange(0, 1, 3), false);
19+
assert.equals(TEST_NAME, 'Returns false if a too large value is out of range', '_.inRange(4, 1, 3)', _.inRange(4, 1, 3), false);
20+
assert.equals(TEST_NAME, 'Returns true if provided value is same as start value', '_.inRange(1, 1, 3)', _.inRange(1, 1, 3), true);
21+
assert.equals(TEST_NAME, 'Returns false if provided value is same as end value', '_.inRange(3, 1, 3)', _.inRange(3, 1, 3), false);
22+
23+
assert.endTestBlock();

lodash-task/test/invert.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const _ = require('../_.js');
2+
const assert = require('./assert.js');
3+
4+
const TEST_NAME = '_.invert()';
5+
6+
assert.beginTestBlock(TEST_NAME);
7+
8+
assert.exists(TEST_NAME, '_.invert()', _.invert);
9+
10+
if (!_.invert) {
11+
assert.terminateTestBlock();
12+
return;
13+
}
14+
15+
assert.equals(TEST_NAME, 'Returns an object with all keys and values inverted', '_.invert({originalKey: "originalValue"})["originalValue"])', _.invert({originalKey: "originalValue"})['originalValue'], 'originalKey');
16+
assert.equals(TEST_NAME, 'Returns an object with all keys and values inverted', '_.invert({originalKey: "originalValue"})["originalKey"])', _.invert({originalKey: "originalValue"})['originalKey'], undefined);
17+
18+
assert.endTestBlock();

0 commit comments

Comments
 (0)