@sekiju/utilities
v1.0.2
Published
Common Node.js utilities library
Downloads
4
Readme
Table of Contents
Description
This is a library with common functions to help with programming. I got tired of moving utilities from one project to another, so I made this library.
Utilities
isClass
Verifies if the input is a class constructor.
class A {}
isClass(A) // true
isClass(function () {}) // falseisFunction
Verifies if the input is a function.
isFunction(function () {}) // true
isFunction('foo') // falseisUrl
Verifies if the input string is a valid url
isUrl('https://example.com') // true
isUrl('100PercentNotUrl') // falseisNumber
Verifies if a number is a finite number.
isNumber(10) // true
isNumber('10') // falseisObject
Verify if the input is an object literal (or class).
isObject({}) // true
isObject([]) // true
isObject('foo') // falseisNullOrUndefined
Checks whether a value is null or undefined.
isNullOrUndefined(null) // true
isNullOrUndefined(undefined) // true
isNullOrUndefined(1) // falseisNullOrUndefinedOrEmpty
Checks whether or not a value is null, undefined or '', []
isNullOrUndefinedOrEmpty('') // true
isNullishOrEmpty('') // trueisNullOrUndefinedOrZero
Checks whether or not a value is null, undefined or 0
isNullOrUndefinedOrZero(0) // true
isNullishOrZero(0) // truefetchFiles
Fetches file paths from a directory
.
└── root/
└── images/
├── jpeg/
│ └── 01.jpeg
└── 01.png/**
* @param dir Directory with files
* @param includeSubdirectories Include subdirectories
*/
fetchFiles('/root/images') // ['/root/images/01.png']
isNullOrUndefined('/root/images', true) // ['/root/images/01.png', '/root/images/jpeg/01.jpeg']clamp
Limiting number by min and max values
clamp(15, 1, 13) // 13
clamp(3, 5, 10) // 5
clamp(7, 0, 100) // 7pickRandom
Picks a random element from an array
pickRandom([0, 1, 2, 3, 4, 5]) // 3
pickRandom([0, 1, 2, 3, 4, 5], 2) // [3, 0]chunk
Splits an array into smaller arrays of a specified size
chunk([1000, 7, 993, 7], 2) // [[1000, 7], [993, 7]]flatten
Flattens a nested array (i.e., an array of arrays) into a single-level array
flatten([[1, 2, 3], [9, 9, 3]]) // [1, 2, 3, 9, 9, 3]generateString
Generates string from latin chars with numbers
generateString(8) // BbLzLcN3cleanObject
Clean undefined and null values from object
cleanObject({ foo: 'bar', nullish: null }) // { foo: 'bar' }sleep
await sleep(1000) // Sleep 1 second