2
\$\begingroup\$

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

konijn
34.2k5 gold badges70 silver badges267 bronze badges
asked Jul 4, 2022 at 11:15
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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
  • 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 than arr, it reads better as a whole word

  • arr.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 be const

answered Jul 4, 2022 at 12:54
\$\endgroup\$
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.