I am a newbie js "developer" and for practice, I created a tiny (8 methods) JavaScript library. I need you to give me feedback "should I keep expanding my library or switch to something else" and how do I improve the code quality?
full code:
function isObject(val) {
if (val === null) { return false;}
return ( (typeof val === 'function') || (typeof val === 'object') );
}
function isDate(value) {
switch (typeof value) {
case 'number':
return true;
case 'string':
return !isNaN(Date.parse(value));
case 'object':
if (value instanceof Date) {
return !isNaN(value.getTime());
}
default:
return false;
}
}
function isEmptyOrOnlySpacesString(str) {
return (!str || /^\s*$/.test(str));
}
function getTimeZone() {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
}
function doesObjectHaveEmptyProperties(obj) {
let emptyKeys = [];
for(let key in obj) {
if(obj[key] === "") {
emptyKeys.push(key + " is empty.\n");
}
}
return emptyKeys.join('') + ' others keys are filled';
}
function isSorted (arr){
return arr.every((a,b,c) => !b || c[b-1] <= a);
}
function shuffleArray (arr){
let len = arr.length;
while (len) {
const i = Math.floor(Math.random() * len--);
[arr[len], arr[i]] = [arr[i], arr[len]];
}
return arr;
}
function generateRandomIntArr(len, upTo){
return Array.from({ length: len }, () => Math.floor(Math.random() * upTo));
}
Also you can visit my github repo
thanks in advance
1 Answer 1
From a short review;
You may want to check out the lodash library, the implementation of isObject there is
function isObject(value) { var type = typeof value; return value != null && (type == 'object' || type == 'function'); }
- It does spell out completely
value
, which is better for readability - It does not repeat typeof value, but stores it in a temp value (would have gone for
const
here though) - It has one exit statement which seems cleaner
- It does spell out completely
isDate returns true for
37
, it feels wrong. I would try to parse numbers as well. Furthermore the functional name lies a bit, it does not check whether value is a date, but whether it can be converted to a date.I prefer
list
more thanarr
, it reads better as a whole wordarr.every((a,b,c)
-> those variable names are not good.every
is not that common. I would if you insist on 1 letter variables go for v,i,l/a (value, index, list/array)I would expect
shuffleArray
to create a new shuffled list, not to shuffle in place.let emptyKeys
could beconst
-
\$\begingroup\$ Also see the ramda library stackoverflow.com/a/71403954/1614973 \$\endgroup\$Dmitri Zaitsev– Dmitri Zaitsev2022年08月24日 03:27:11 +00:00Commented Aug 24, 2022 at 3:27