Check if input is of number type
// Check if input is of number type
/*
* Input: Any Type
* Output: Boolean
*/
function isOfNumberType(input){
return typeof input == 'number' ||
input instanceof Number;
}
// Tests
isOfNumberType(10) //true
isOfNumberType(Number(10)) //true
isOfNumberType(new Number(10)) //true
isOfNumberType("10") //false
Check if input from a form
is of number type
is of number type
// Check if input from a form is of number type
/*
* Input: Any Type
* Output: Boolean
*/
function isOfNumberTypeFromForm(input){
return !isNaN(input);
}
Check if input value
contains a number
contains a number
// Check if input value contains a number
/*
* Input: Any Type
* Output: Boolean
*/
function isNumber(input){
return isOfNumberType(input) ||
!isNaN(Number(input))
}
// Tests
isNumber(10) //true
isNumber(Number(10)) //true
isNumber(new Number(10)) //true
isNumber("10") //true
isNumber("10COPY") //false
isNumber("10.01") //true
isNumber("10e+14") //true
Check if input value is
null or undefined
null or undefined
// Check if input value is null or undefined
/*
* Input: Any Type
* Output: Boolean
*/
function usNullish(input){
return input == undefined;
}
// Tests
usNullish(undefined) //true
usNullish(null) //true
usNullish(false) //false
usNullish("") //false
Check if input value is an
null or empty string
null or empty string
// Check if input value is an null or empty string
/*
* Input: Any Type
* Output: Boolean
*/
function isEmptyString(input){
return input === null ||
input === "";
}
// Tests
isEmptyString(undefined) //false
isEmptyString(null) //true
isEmptyString(" ") //false
isEmptyString("") //true
Check if input value is falsy
// Check if input value is falsy
/*
* Input: Any Type
* Output: Boolean
*/
function isFalsy(input){
return !input;
}
// Tests
isFalsy(undefined) //true
isFalsy(null) //true
isFalsy(" ") //false
isFalsy("") //true
isFalsy(false) //true
Check if input is an Array
// Check if input is an Array
/*
* Input: Any Type
* Output: Boolean
*/
function isArray(input){
return Array.isArray(input);
}
// Tests
isArray(null) //false
isArray([]) //true
isArray([1, 2, 3]) //true
isArray({}) //false
Check if input is an Empty object
// Check if input is an Empty object
/*
* Input: Object Type
* Output: Boolean
*/
function isEmptyObject(input){
for(const key in input){
if(input.hasOwnProperty(key))
return false;
}
return true;
}
// Tests
isEmptyObject(null) //true
isEmptyObject({}) //true
isEmptyObject([]) //true
isEmptyObject({x: 10}) //false
Convert first character of each sentence to uppercase
// Convert first character of each sentence to uppercase
// Sentence Demiliter is new line of '.\s+'
/*
* Input: String Type
* Output: String Type
*/
function sentenceCase(input){
return input?.toLowerCase().replace(/(^\w)|\.\s+(\w)/gm,
s => s.toUpperCase());
}
// Tests
sentenceCase("") //""
sentenceCase("i am bored") //I am bored
sentenceCase("i am bored. let's play") //I am bored. Let's play
Convert first character of
each word to uppercase
each word to uppercase
// Convert first character of each word to uppercase
/*
* Input: String Type
* Output: String Type
*/
function properCase(input){
return input?.toLowerCase().replace(/^\w|\s\w/g,
s => s.toUpperCase());
}
// Tests
properCase("") //""
properCase("bruno leo") //I am bored
properCase("brUNO lEo") //I am bored. Let's play
Reverse a string
// Reverse a string
/*
* Input: String Type
* Output: String Type
*/
function reverseString(input){
return input?.split('').reverse().join('');
}
// Tests
reverseString("") //""
reverseString("Hello") // olleH
reverseString("Hello, World!") // !dlroW ,olleH
Convert a number from one
base to another base
base to another base
// Convert a number from one base to another base
/*
* Input: String Type
* Output: String Type
*/
function convertBase(input, base1, base2){
return parseInt(input, base1).toString(base2);
}
// Tests
convertBase("15", 10, 2) // "1111"
convertBase("F076", 16, 8) // "170166"
Create and initialize array
// Create and initialize array
/*
* Input: Length of Array (Number)
* Input: Default Value (Any Type)
* Output: Array
*/
function initialize(length, value){
return Array(length).fill(value)
}
// Tests
initialize(5, 0) // [0,0,0,0,0]
initialize(0, 5) // []
Empty an array
// Empty an array
/*
* Input: Array
* Output: Array
*/
function empty(input){
input.length = 0;
return input;
}
// Tests
empty([]) // []
empty([1,2,3,4,5]) // []
Remove falsy values from an array
// Remove falsy values from an array
/*
* Input: Array
* Output: Array
*/
function removeBlanks(input){
return input.filter(Boolean);
}
// Tests
removeBlanks([]) // []
removeBlanks([1,2,false,4,undefined]) // [1,2,4]
Remove duplicates from array
// Remove duplicates from array
/*
* Input: Array
* Output: Array
*/
function removeDuplicates(input){
return [...new Set(input)];
}
// Tests
removeDuplicates([]) // []
removeDuplicates([1,2,1,3,2,4]) // [1,2,3,4]
Casting an array to array of numbers
// Casting an array to array of numbers
/*
* Input: Array
* Output: Array
*/
function castToNumbers(input){
return input.map(Number);
}
// Tests
castToNumbers([]) // []
castToNumbers(["1",2,false,"3a",undefined,]) // [1,2,0,Nan,Nan]
Return a series of integers
starting from 0
starting from 0
// Return a series of integers starting from 0
/*
* Input: Number (excluding NaN, Negative, Infinity, -Infinity)
* Output: Array
*/
function series(limit){
return [...Array(limit).keys()];
}
// Tests
series(0) // []
series(1) // [0]
series(10) // [0,1,2,3,4,5,6,7,8,9]