ejz-helpers v1.8.16
ejz-helpers
Misc Function Helpers
Contents
- rand
- shuffle
- md5
- sha256
- sha1
- seeded
- clone
- euler
- generatePassword
- isPrimeNumber
- primeNumbers
- format
- round
- wordwrap
- floor
- ceil
- randomUInt
- randomInt
- sleep
- toArray
- toObject
- isObject
- isRegExp
- isRegExpString
- isError
- isIterator
- isArray
- isBuffer
- matchAll
- __filename
- __dirname
- randomBytes
- NOOP
- V2V
- KV2V
- V2V0
- V2V1
- INC
- KEY
- NN
- isStream
- isWritableStream
- isReadableStream
- isDuplexStream
- isTransformStream
- isFile
- isRegularFile
- isDirectory
- isRegularDirectory
- isSymbolicLink
- isExecutable
- isWritable
- isReadable
- readFile
- readJson
- mkdir
- writeFile
- writeJson
- appendFile
- symlinkFile
- unlinkFile
- argv
- tempFile
- tempDirectory
- listDirectory
- removeDirectory
- isJestEnvironment
- getStreamBuffer
- asyncExecute
- asyncHandler
- onShutdownClosure
- filter
- map
- each
- remap
- ms
- time
- date
- datetime
- unix2date
- unix2datetime
- date2unix
- now
- curdate
- incrementDate
- range
- count
- sum
- nsplit
- split
- fromJson
- toJson
- unique
- combine
- flip
- chunk
- indexOf
rand
Get random number from min
to max
. min
can be array of elements or string.
Example:
rand(0, 1000)
// 392
rand('abc')
// 'c'
Parameter | Type | Default |
---|---|---|
min | number\|Array<any>\|string\|null | 0 |
max | number\|null | 2 ** 32 - 1 |
Returns number|any|string|null
.
top ↑
shuffle
Shuffle array. Array itself is not changed. Also can be invoked with string.
Parameter | Type |
---|---|
array | Array<any>\|string |
Returns Array<any>|string
.
top ↑
md5
Return MD5 digest of string/Buffer.
Parameter | Type |
---|---|
str | string\|Buffer |
Returns Buffer
.
top ↑
sha256
Return SHA-256 digest of string/Buffer.
Parameter | Type |
---|---|
str | string\|Buffer |
Returns Buffer
.
top ↑
sha1
Return SHA-1 digest of string/Buffer.
Parameter | Type |
---|---|
str | string\|Buffer |
Returns Buffer
.
top ↑
seeded
Return seeded version of functions rand(), shuffle(), randomBytes(), randomUInt() and randomInt().
Parameter | Type |
---|---|
seed | string\|Buffer |
Returns {rand, shuffle, randomBytes, randomUInt, randomInt}
.
top ↑
clone
Deep copy of object or array.
Parameter | Type |
---|---|
obj | Object\|Array |
Returns Object|Array
.
top ↑
euler
Euler between 2 arrays (A/B, A&B, B/A).
Example:
euler([1, 2], [2, 3])
// [[1], [2], [3]]
Parameter | Type |
---|---|
a | Array<any> |
b | Array<any> |
compareFunction | ((any,any)=>boolean)\|null |
Returns [Array<any>,Array<any>,Array<any>]
.
top ↑
generatePassword
Generate password.
Parameter | Type |
---|---|
len | number\|null |
dict | string\|null |
Returns string
.
top ↑
isPrimeNumber
Check if num
is prime number.
Example:
isPrimeNumber(19)
// true
isPrimeNumber(6)
// false
Parameter | Type |
---|---|
num | number |
Returns boolean
.
top ↑
primeNumbers
Get all prime numbers of number
.
Example:
primeNumbers(80)
// [2, 2, 2, 2, 5]
primeNumbers(11)
// [11]
Parameter | Type |
---|---|
number | number |
Returns Array<number>
.
top ↑
format
Same as util.format
(refer here). Useful for string substitution.
Example:
format('%s -> %d for %j', 'foo', '2.0', {bar: 'baz'})
// 'foo -> 2 for {"bar":"baz"}'
top ↑
round
Round number
with custom precision.
Example:
round(1.6)
// 2
round(1.66, 1)
// 1.7
round(16, -1)
// 20
Parameter | Type |
---|---|
num | number |
pos | number\|null |
pow | number\|null |
Returns number
.
top ↑
wordwrap
Wraps a string to a given number of characters.
Parameter | Type |
---|---|
str | string |
limit | (line)=>number\|number\|null |
nl | string\|null |
Returns string
.
top ↑
floor
Floor number
with custom precision.
Example:
floor(1.6)
// 1
floor(1.66, 1)
// 1.6
floor(16, -1)
// 10
floor(270, -1, 50)
// 250
Parameter | Type |
---|---|
num | number |
pos | number\|null |
pow | number\|null |
Returns number
.
top ↑
ceil
Ceil number
with custom precision.
Example:
ceil(1.3)
// 2
ceil(1.33, 1)
// 1.4
ceil(13, -1)
// 20
Parameter | Type |
---|---|
num | number |
pos | number\|null |
pow | number\|null |
Returns number
.
top ↑
randomUInt
Get random unsigned integer. If bytes
more than 6, returns BigInt.
Example:
randomUInt(1)
// 107
randomUInt(8)
// 10725390500255046932n
Parameter | Type |
---|---|
bytes | number\|null |
Returns number|BigInt
.
top ↑
randomInt
Get random integer. If bytes
more than 6, returns BigInt.
Example:
randomInt(2)
// -1019
Parameter | Type |
---|---|
bytes | number\|null |
Returns number|BigInt
.
top ↑
sleep
Sleep in asynchronous code.
Example:
sleep(100).then(() => {
console.log('We slept for a while..');
});
Parameter | Type |
---|---|
ms | number |
Returns Promise
.
top ↑
toArray
Cast obj
to Array. If obj
is already Array, it is returned as is.
Parameter | Type |
---|---|
obj | any |
Returns Array
.
top ↑
toObject
Cast obj
to with key key
. If obj
is already Object, it is returned as is.
Parameter | Type |
---|---|
obj | any |
key | string |
Returns Object
.
top ↑
isObject
Checks if obj
is Object.
Example:
isObject([])
// false
isObject({})
// true
Parameter | Type |
---|---|
obj | any |
Returns boolean
.
top ↑
isRegExp
Checks if regex
is RegExp instance.
Example:
isRegExp(/foo/i)
// true
Parameter | Type |
---|---|
regex | any |
Returns boolean
.
top ↑
isRegExpString
Checks if string
represents RegExp.
Example:
isRegExpString('/foo/i')
// true
Parameter | Type |
---|---|
string | any |
Returns boolean
.
top ↑
isError
Checks if error
is Error instance.
Example:
isError(new Error)
// true
Parameter | Type |
---|---|
error | any |
Returns boolean
.
top ↑
isIterator
Checks if iter
implements Iterator protocol.
Example:
let iter = function* () {
yield 1;
};
isIterator(iter())
// true
Parameter | Type |
---|---|
iter | any |
Returns boolean
.
top ↑
isArray
Alias for Array.isArray
.
top ↑
isBuffer
Alias for Buffer.isBuffer
.
top ↑
matchAll
Use regular expression to match all occurrences.
Example:
matchAll(/DNS:\s+(\S+)/g, 'DNS: foo.com DNS: bar.com', 1)
// ['foo.com', 'bar.com']
Parameter | Type |
---|---|
regex | RegExp |
str | string |
idx | number\|null |
Returns Array<string|null>
.
top ↑
__filename
Get current filename.
Example:
__filename(import.meta.url)
// '/home/user/ejz-helpers-test/index.js'
Parameter | Type |
---|---|
import_meta_url | URL\|string |
Returns string
.
top ↑
__dirname
Get current directory.
Example:
__dirname(import.meta.url)
// '/home/user/ejz-helpers-test'
Parameter | Type |
---|---|
import_meta_url | URL\|string |
Returns string
.
top ↑
randomBytes
Alias for crypto.randomBytes
.
top ↑
NOOP
Alias for () => {}
.
top ↑
V2V
Alias for (v) => v
.
top ↑
KV2V
Alias for (k, v) => v
.
top ↑
V2V0
Alias for (v) => v[0]
.
top ↑
V2V1
Alias for (v) => v[1]
.
top ↑
INC
Closure that for array a
returns (v) => a.includes(v)
.
top ↑
KEY
Closure that for key k
returns (d) => d[k]
.
top ↑
NN
Alias for (v) => v != null
.
top ↑
isStream
Checks if stream
is Node.js stream.
Parameter | Type |
---|---|
stream | any |
Returns boolean
.
top ↑
isWritableStream
Checks if stream
is writable Node.js stream.
Parameter | Type |
---|---|
stream | any |
Returns boolean
.
top ↑
isReadableStream
Checks if stream
is readable Node.js stream.
Parameter | Type |
---|---|
stream | any |
Returns boolean
.
top ↑
isDuplexStream
Checks if stream
is duplex Node.js stream.
Parameter | Type |
---|---|
stream | any |
Returns boolean
.
top ↑
isTransformStream
Checks if stream
is transform Node.js stream.
Parameter | Type |
---|---|
stream | any |
Returns boolean
.
top ↑
isFile
Checks if file
is file (follows symlink).
Parameter | Type |
---|---|
file | any |
Returns boolean
.
top ↑
isRegularFile
Checks if file
is regular file.
Parameter | Type |
---|---|
file | any |
Returns boolean
.
top ↑
isDirectory
Checks if dir
is directory (follows symlink).
Parameter | Type |
---|---|
dir | any |
Returns boolean
.
top ↑
isRegularDirectory
Checks if dir
is regular directory.
Parameter | Type |
---|---|
dir | any |
Returns boolean
.
top ↑
isSymbolicLink
Checks if link
is symlink.
Parameter | Type |
---|---|
link | any |
Returns boolean
.
top ↑
isExecutable
Checks if file
exists and has executable access flag.
Parameter | Type |
---|---|
file | any |
Returns boolean
.
top ↑
isWritable
Checks if file
exists and has writable access flag.
Parameter | Type |
---|---|
file | any |
Returns boolean
.
top ↑
isReadable
Checks if file
exists and has readable access flag.
Parameter | Type |
---|---|
file | any |
Returns boolean
.
top ↑
readFile
Read file
and return its content. In case of error returns null.
Parameter | Type |
---|---|
file | string |
Returns Buffer|null
.
top ↑
readJson
Read file
and pass its content to JSON parser. In case of error returns null.
Parameter | Type |
---|---|
file | string |
def | any\|null |
Returns any|null
.
top ↑
mkdir
Make directory dir
and its parents. In case of error returns false.
Parameter | Type |
---|---|
dir | string |
Returns boolean
.
top ↑
writeFile
Write content
to file
. In case of error returns false.
Parameter | Type |
---|---|
file | string |
content | string\|Buffer |
mkdir | boolean\|null |
Returns boolean
.
top ↑
writeJson
Write obj
in JSON to file
.
Parameter | Type |
---|---|
file | string |
obj | any |
mkdir | boolean\|null |
Returns boolean
.
top ↑
appendFile
Append content
to file
.
Parameter | Type |
---|---|
file | string |
content | string\|Buffer |
Returns boolean
.
top ↑
symlinkFile
Create symbolic link file
what targets target
.
Parameter | Type | Default |
---|---|---|
file | string | |
target | string | |
relative | boolean\|null | true |
Returns boolean
.
top ↑
unlinkFile
Removes regular file or symlink.
Parameter | Type |
---|---|
file | string |
Returns boolean
.
top ↑
argv
Complex parsing of console arguments.
Example:
let vars = {
recursive: {type: 'boolean', alias: 'r'},
output: {alias: 'O'},
};
argv(['-rO', 'myfile.txt'], vars));
// {recursive: true, output: 'myfile.txt'}
Parameter | Type |
---|---|
args | Array<string> |
vars | Object<key,Object\|string> |
Returns Object
.
top ↑
tempFile
Create temporary file with content
as content (if provided).
Parameter | Type |
---|---|
prefix | string\|null |
ext | string\|null |
content | string\|Buffer\|null |
Returns string|null
.
top ↑
tempDirectory
Create temporary directory with contents
(if provided).
Parameter | Type |
---|---|
prefix | string\|null |
contents | Record<string,string\|Buffer>\|null |
Returns string|null
.
top ↑
listDirectory
List directory. Recursive listing is possible via recursive
variable (can act as filter).
Parameter | Type |
---|---|
dir | string |
recursive | ((string)=>boolean)\|boolean\|null |
Returns Array<string>
.
top ↑
removeDirectory
Remove directory and its content. dir
is said to be regular directory.
Parameter | Type |
---|---|
dir | string |
Returns boolean
.
top ↑
isJestEnvironment
Returns true in case you are within Jest test environment.
Returns boolean
.
top ↑
getStreamBuffer
Receives stream to buffer.
Parameter | Type |
---|---|
stream | Stream |
Returns Promise<Buffer>
.
top ↑
asyncExecute
Executes any synchronous (or asynchronous) function as asynchronous.
Parameter | Type |
---|---|
func | Function |
Returns Promise
.
top ↑
asyncHandler
Asynchronous handler for Express routes.
Parameter | Type |
---|---|
func | Function |
Returns Function
.
top ↑
onShutdownClosure
Closure that returns shutdown callbacks collector..
Returns Function
.
top ↑
filter
Filter obj
using function filter
(can be array of keys).
Parameter | Type |
---|---|
obj | Object |
filter | ((any,string,Object)=>boolean)\|Array<string> |
Returns Object
.
top ↑
map
Change obj
values & return new object.
Parameter | Type |
---|---|
obj | Object |
map | (any,string,Object)=>any |
Returns Object
.
top ↑
each
Iterate obj
values. For each value execute function each
.
Parameter | Type |
---|---|
obj | Object |
each | (any,string,Object)=>void |
top ↑
remap
Iterate over obj
. Return new object with remapped keys & values.
Parameter | Type |
---|---|
obj | Object |
call | (any,string,Object)=>([string,any]\|null) |
top ↑
ms
Get current time in milliseconds.
Returns number
.
top ↑
time
Get current time in seconds (aka Unix time).
Returns number
.
top ↑
date
Get date in ISO format (YYYY-MM-DD).
Parameter | Type |
---|---|
date | Date |
Returns string
.
top ↑
datetime
Get date & time in ISO format (YYYY-MM-DD hh:mm:ss).
Parameter | Type |
---|---|
dateTime | Date |
Returns string
.
top ↑
unix2date
Cast date in Unix time to ISO format (time component is ignored).
Parameter | Type |
---|---|
unixTime | number |
Returns string
.
top ↑
unix2datetime
Cast date & time in Unix time to ISO format.
Parameter | Type |
---|---|
unixTime | number |
Returns string
.
top ↑
date2unix
Cast date & time in ISO format to Unix time.
Parameter | Type |
---|---|
date | Date\|string |
Returns number|null
.
top ↑
now
Get current date & time in ISO format.
Parameter | Type |
---|---|
seconds | number\|null |
Returns string
.
top ↑
curdate
Get current date in ISO format.
Parameter | Type |
---|---|
days | number\|null |
Returns string
.
top ↑
incrementDate
Increment to date date
an interval.
Parameter | Type |
---|---|
date | Date\|string\|number |
increment | Record<string,number> |
def | string\|null |
Returns Date|string|number
.
top ↑
range
Make array with numeric values from min
to max
.
Parameter | Type |
---|---|
min | number |
max | number |
Returns Array<number>
.
top ↑
count
Count occurrence of elements in array.
Parameter | Type |
---|---|
arr | Array |
mapper | Function\|null |
Returns Object
.
top ↑
sum
Get sum of array values.
Parameter | Type |
---|---|
arr | Array |
mapper | Function\|null |
Returns number
.
top ↑
nsplit
Split string by newlines. Each line is trimmed.
Parameter | Type |
---|---|
str | string |
Returns Array<string>
.
top ↑
split
Split array (or string) into two using filter function.
Parameter | Type |
---|---|
arr | Array\|string |
Returns [Array|string,Array|string]
.
top ↑
fromJson
Parses JSON.
Parameter | Type |
---|---|
text | string |
def | any\|null |
Returns any
.
top ↑
toJson
Stringify to JSON.
Parameter | Type |
---|---|
obj | any |
spaces | number\|null |
Returns string
.
top ↑
unique
Make array unique.
Parameter | Type |
---|---|
arr | Array<any> |
compareFunction | ((any,any)=>boolean)\|null |
Returns Array<any>
.
top ↑
combine
Make object from combination of keys
and values
.
Parameter | Type |
---|---|
keys | Array<string> |
values | Array<any>\|any |
Returns Object
.
top ↑
flip
Flip keys and values.
Parameter | Type |
---|---|
obj | Object |
Returns Object
.
top ↑
chunk
Chunk array into smaller pieces.
Parameter | Type |
---|---|
arr | Array |
length | number |
Returns Array<Array>
.
top ↑
indexOf
Find all occurrence of elem
in array
.
Parameter | Type |
---|---|
elem | any |
array | Array |
Returns Array<number>
.
top ↑
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago