A lightweight javascript tool library for various needs.
List of tools: isObject, isEmptyOrOnlySpacesString, getTimeZone, doesObjectHaveEmptyProps, isSorted, shuffleArray, generateRandomIntList, randomInt.
$ npm install js-tools-baseconst { ExampleTool } = require("js-tools-base");isObject(object)
Check is that object in argument of function or not.
console.log(isObject({ "id": 123, "message": "hello Iphone" })); //true
console.log(isObject('some string')); //falseisEmptyOrOnlySpacesString(string)
Checks is string empty or has only spaces.
console.log(isEmptyOrOnlySpacesString('Apple')); //false
console.log(isEmptyOrOnlySpacesString('')); //true
console.log(isEmptyOrOnlySpacesString(' ')); //true
console.log(isEmptyOrOnlySpacesString('Ap p le')); //falsegetTimeZone()
Gets the requester's time zone.
console.log(getTimeZone()); //europe/kievdoesObjectHaveEmptyProperties(object)
checks does object have blank properties.
const obj = {
name: "John",
dataOfBirth: "",
externalId: 2548,
email: "john@email.com",
mobile: ""
}
const data = {
name: "Sal",
dataOfBirth: "12.12.2012",
externalId: 83838,
email: "sal@email.com",
mobile: "122344566"
}
console.log(doesObjectHaveEmptyProps(obj)); //dataOfBirth is empty, mobile is empty, other keys are filled
console.log(doesObjectHaveEmptyProps(data)); //other keys are filledIsSorted(array)
Checks is sorted array in argument of function or not.
console.log(isSorted([1,3,4,9,13])); //true
console.log(isSorted([5, 7, 1, 67])); //falseshuffleArray(array)
Shuffles the values of an array, returning a new array.
let arr = [4, 6, 7, 9];
console.log(shuffleArray(arr)); //[7, 4, 9, 6]generateRandomIntList(length, max)
Generates an array with random int values. First argument is the length of array, second is the max possible integer.
console.log(generateRandomIntList(8, 100)); //[ 41, 4, 52, 9, 82, 50, 19, 60 ]
console.log(generateRandomIntList(5, 5)); //[ 1, 0, 2, 0, 4 ]randomInt(min, max)
Generates an integer between the minimum given value and the maximum given value.
console.log(randomInt(3, 576)); // 207
console.log(randomInt(50, 60)); // 58capitalizeFirstLetter(string)
Capitalizes the first letter of a string.
console.log(capitalizeFirstLetter('hello')); // Hello
console.log(capitalizeFirstLetter('')); // ''debounce(func, wait)
Debounces a function to limit its execution rate.
const log = debounce(() => console.log('Debounced!'), 1000);
log();
log();
log(); // Only this call will log 'Debounced!' after 1 secondgetQueryParams(url)
Parses URL query parameters into an object.
console.log(getQueryParams('https://example.com?page=1&sort=asc')); // { page: '1', sort: 'asc' }flattenArray(arr)
Flattens a nested array.
console.log(flattenArray([1, [2, [3, [4]], 5]])); // [1, 2, 3, 4, 5]at least if one of the tools you like, please star the repo. if you have free time feel free to contribute to this library
add to do