Skip to content

Instantly share code, notes, and snippets.

@buymed-hoangpham
Created October 13, 2022 02:34
Show Gist options
  • Save buymed-hoangpham/af13b98de6084dc3bae35f1d3c8f7b2d to your computer and use it in GitHub Desktop.
Save buymed-hoangpham/af13b98de6084dc3bae35f1d3c8f7b2d to your computer and use it in GitHub Desktop.
lodash
const _ = {
clamp(number, lower, upper) {
const lowerClampedValue = Math.max(number, lower);
const clampedValue = Math.min(lowerClampedValue, upper);
return clampedValue;
},
inRange(number, start, end) {
if (!end) {
end = start;
start = 0;
}
return number >= start && number <= end;
},
words(string) {},
pad(string, length) {},
has(object, key) {},
invert(object) {},
findKey(object, predicate) {
for (let key in object) {
let value = object[key];
let predicateReturnValue = predicate(value);
if (predicateReturnValue) {
return key;
}
}
},
drop(array, n) {
return array.slice(n);
},
dropWhile(array, predicate) {
let idx = 0;
while (predicate(array[idx], idx)) {
idx += 1;
}
return array.slice(n);
},
chunk(array, size) {
let arrayChunks = [];
for (let i = 0; i < array.length; i += size) {
let arrayChunk = array.slice(i, i + size);
arrayChunks.push(arrayChunk);
}
return arrayChunks;
},
};
// Do not write or modify code below this line.
module.exports = _;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment