1.8.16 • Published 1 year ago

ejz-helpers v1.8.16

Weekly downloads
173
License
-
Repository
-
Last release
1 year ago

ejz-helpers

Misc Function Helpers

Contents


rand

Get random number from min to max. min can be array of elements or string.

Example:

rand(0, 1000)
// 392
rand('abc')
// 'c'
ParameterTypeDefault
minnumber\|Array<any>\|string\|null0
maxnumber\|null2 ** 32 - 1

Returns number|any|string|null.

top ↑


shuffle

Shuffle array. Array itself is not changed. Also can be invoked with string.

ParameterType
arrayArray<any>\|string

Returns Array<any>|string.

top ↑


md5

Return MD5 digest of string/Buffer.

ParameterType
strstring\|Buffer

Returns Buffer.

top ↑


sha256

Return SHA-256 digest of string/Buffer.

ParameterType
strstring\|Buffer

Returns Buffer.

top ↑


sha1

Return SHA-1 digest of string/Buffer.

ParameterType
strstring\|Buffer

Returns Buffer.

top ↑


seeded

Return seeded version of functions rand(), shuffle(), randomBytes(), randomUInt() and randomInt().

ParameterType
seedstring\|Buffer

Returns {rand, shuffle, randomBytes, randomUInt, randomInt}.

top ↑


clone

Deep copy of object or array.

ParameterType
objObject\|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]]
ParameterType
aArray<any>
bArray<any>
compareFunction((any,any)=>boolean)\|null

Returns [Array<any>,Array<any>,Array<any>].

top ↑


generatePassword

Generate password.

ParameterType
lennumber\|null
dictstring\|null

Returns string.

top ↑


isPrimeNumber

Check if num is prime number.

Example:

isPrimeNumber(19)
// true
isPrimeNumber(6)
// false
ParameterType
numnumber

Returns boolean.

top ↑


primeNumbers

Get all prime numbers of number.

Example:

primeNumbers(80)
// [2, 2, 2, 2, 5]
primeNumbers(11)
// [11]
ParameterType
numbernumber

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
ParameterType
numnumber
posnumber\|null
pownumber\|null

Returns number.

top ↑


wordwrap

Wraps a string to a given number of characters.

ParameterType
strstring
limit(line)=>number\|number\|null
nlstring\|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
ParameterType
numnumber
posnumber\|null
pownumber\|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
ParameterType
numnumber
posnumber\|null
pownumber\|null

Returns number.

top ↑


randomUInt

Get random unsigned integer. If bytes more than 6, returns BigInt.

Example:

randomUInt(1)
// 107
randomUInt(8)
// 10725390500255046932n
ParameterType
bytesnumber\|null

Returns number|BigInt.

top ↑


randomInt

Get random integer. If bytes more than 6, returns BigInt.

Example:

randomInt(2)
// -1019
ParameterType
bytesnumber\|null

Returns number|BigInt.

top ↑


sleep

Sleep in asynchronous code.

Example:

sleep(100).then(() => {
    console.log('We slept for a while..');
});
ParameterType
msnumber

Returns Promise.

top ↑


toArray

Cast obj to Array. If obj is already Array, it is returned as is.

ParameterType
objany

Returns Array.

top ↑


toObject

Cast obj to with key key. If obj is already Object, it is returned as is.

ParameterType
objany
keystring

Returns Object.

top ↑


isObject

Checks if obj is Object.

Example:

isObject([])
// false
isObject({})
// true
ParameterType
objany

Returns boolean.

top ↑


isRegExp

Checks if regex is RegExp instance.

Example:

isRegExp(/foo/i)
// true
ParameterType
regexany

Returns boolean.

top ↑


isRegExpString

Checks if string represents RegExp.

Example:

isRegExpString('/foo/i')
// true
ParameterType
stringany

Returns boolean.

top ↑


isError

Checks if error is Error instance.

Example:

isError(new Error)
// true
ParameterType
errorany

Returns boolean.

top ↑


isIterator

Checks if iter implements Iterator protocol.

Example:

let iter = function* () {
    yield 1;
};
isIterator(iter())
// true
ParameterType
iterany

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']
ParameterType
regexRegExp
strstring
idxnumber\|null

Returns Array<string|null>.

top ↑


__filename

Get current filename.

Example:

__filename(import.meta.url)
// '/home/user/ejz-helpers-test/index.js'
ParameterType
import_meta_urlURL\|string

Returns string.

top ↑


__dirname

Get current directory.

Example:

__dirname(import.meta.url)
// '/home/user/ejz-helpers-test'
ParameterType
import_meta_urlURL\|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.

ParameterType
streamany

Returns boolean.

top ↑


isWritableStream

Checks if stream is writable Node.js stream.

ParameterType
streamany

Returns boolean.

top ↑


isReadableStream

Checks if stream is readable Node.js stream.

ParameterType
streamany

Returns boolean.

top ↑


isDuplexStream

Checks if stream is duplex Node.js stream.

ParameterType
streamany

Returns boolean.

top ↑


isTransformStream

Checks if stream is transform Node.js stream.

ParameterType
streamany

Returns boolean.

top ↑


isFile

Checks if file is file (follows symlink).

ParameterType
fileany

Returns boolean.

top ↑


isRegularFile

Checks if file is regular file.

ParameterType
fileany

Returns boolean.

top ↑


isDirectory

Checks if dir is directory (follows symlink).

ParameterType
dirany

Returns boolean.

top ↑


isRegularDirectory

Checks if dir is regular directory.

ParameterType
dirany

Returns boolean.

top ↑


isSymbolicLink

Checks if link is symlink.

ParameterType
linkany

Returns boolean.

top ↑


isExecutable

Checks if file exists and has executable access flag.

ParameterType
fileany

Returns boolean.

top ↑


isWritable

Checks if file exists and has writable access flag.

ParameterType
fileany

Returns boolean.

top ↑


isReadable

Checks if file exists and has readable access flag.

ParameterType
fileany

Returns boolean.

top ↑


readFile

Read file and return its content. In case of error returns null.

ParameterType
filestring

Returns Buffer|null.

top ↑


readJson

Read file and pass its content to JSON parser. In case of error returns null.

ParameterType
filestring
defany\|null

Returns any|null.

top ↑


mkdir

Make directory dir and its parents. In case of error returns false.

ParameterType
dirstring

Returns boolean.

top ↑


writeFile

Write content to file. In case of error returns false.

ParameterType
filestring
contentstring\|Buffer
mkdirboolean\|null

Returns boolean.

top ↑


writeJson

Write obj in JSON to file.

ParameterType
filestring
objany
mkdirboolean\|null

Returns boolean.

top ↑


appendFile

Append content to file.

ParameterType
filestring
contentstring\|Buffer

Returns boolean.

top ↑


symlinkFile

Create symbolic link file what targets target.

ParameterTypeDefault
filestring
targetstring
relativeboolean\|nulltrue

Returns boolean.

top ↑


unlinkFile

Removes regular file or symlink.

ParameterType
filestring

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'}
ParameterType
argsArray<string>
varsObject<key,Object\|string>

Returns Object.

top ↑


tempFile

Create temporary file with content as content (if provided).

ParameterType
prefixstring\|null
extstring\|null
contentstring\|Buffer\|null

Returns string|null.

top ↑


tempDirectory

Create temporary directory with contents (if provided).

ParameterType
prefixstring\|null
contentsRecord<string,string\|Buffer>\|null

Returns string|null.

top ↑


listDirectory

List directory. Recursive listing is possible via recursive variable (can act as filter).

ParameterType
dirstring
recursive((string)=>boolean)\|boolean\|null

Returns Array<string>.

top ↑


removeDirectory

Remove directory and its content. dir is said to be regular directory.

ParameterType
dirstring

Returns boolean.

top ↑


isJestEnvironment

Returns true in case you are within Jest test environment.

Returns boolean.

top ↑


getStreamBuffer

Receives stream to buffer.

ParameterType
streamStream

Returns Promise<Buffer>.

top ↑


asyncExecute

Executes any synchronous (or asynchronous) function as asynchronous.

ParameterType
funcFunction

Returns Promise.

top ↑


asyncHandler

Asynchronous handler for Express routes.

ParameterType
funcFunction

Returns Function.

top ↑


onShutdownClosure

Closure that returns shutdown callbacks collector..

Returns Function.

top ↑


filter

Filter obj using function filter (can be array of keys).

ParameterType
objObject
filter((any,string,Object)=>boolean)\|Array<string>

Returns Object.

top ↑


map

Change obj values & return new object.

ParameterType
objObject
map(any,string,Object)=>any

Returns Object.

top ↑


each

Iterate obj values. For each value execute function each.

ParameterType
objObject
each(any,string,Object)=>void

top ↑


remap

Iterate over obj. Return new object with remapped keys & values.

ParameterType
objObject
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).

ParameterType
dateDate

Returns string.

top ↑


datetime

Get date & time in ISO format (YYYY-MM-DD hh:mm:ss).

ParameterType
dateTimeDate

Returns string.

top ↑


unix2date

Cast date in Unix time to ISO format (time component is ignored).

ParameterType
unixTimenumber

Returns string.

top ↑


unix2datetime

Cast date & time in Unix time to ISO format.

ParameterType
unixTimenumber

Returns string.

top ↑


date2unix

Cast date & time in ISO format to Unix time.

ParameterType
dateDate\|string

Returns number|null.

top ↑


now

Get current date & time in ISO format.

ParameterType
secondsnumber\|null

Returns string.

top ↑


curdate

Get current date in ISO format.

ParameterType
daysnumber\|null

Returns string.

top ↑


incrementDate

Increment to date date an interval.

ParameterType
dateDate\|string\|number
incrementRecord<string,number>
defstring\|null

Returns Date|string|number.

top ↑


range

Make array with numeric values from min to max.

ParameterType
minnumber
maxnumber

Returns Array<number>.

top ↑


count

Count occurrence of elements in array.

ParameterType
arrArray
mapperFunction\|null

Returns Object.

top ↑


sum

Get sum of array values.

ParameterType
arrArray
mapperFunction\|null

Returns number.

top ↑


nsplit

Split string by newlines. Each line is trimmed.

ParameterType
strstring

Returns Array<string>.

top ↑


split

Split array (or string) into two using filter function.

ParameterType
arrArray\|string

Returns [Array|string,Array|string].

top ↑


fromJson

Parses JSON.

ParameterType
textstring
defany\|null

Returns any.

top ↑


toJson

Stringify to JSON.

ParameterType
objany
spacesnumber\|null

Returns string.

top ↑


unique

Make array unique.

ParameterType
arrArray<any>
compareFunction((any,any)=>boolean)\|null

Returns Array<any>.

top ↑


combine

Make object from combination of keys and values.

ParameterType
keysArray<string>
valuesArray<any>\|any

Returns Object.

top ↑


flip

Flip keys and values.

ParameterType
objObject

Returns Object.

top ↑


chunk

Chunk array into smaller pieces.

ParameterType
arrArray
lengthnumber

Returns Array<Array>.

top ↑


indexOf

Find all occurrence of elem in array.

ParameterType
elemany
arrayArray

Returns Array<number>.

top ↑

1.8.2

1 year ago

1.8.1

1 year ago

1.8.9

1 year ago

1.8.10

1 year ago

1.8.8

1 year ago

1.8.11

1 year ago

1.8.7

1 year ago

1.8.12

1 year ago

1.8.6

1 year ago

1.8.13

1 year ago

1.8.5

1 year ago

1.8.14

1 year ago

1.8.4

1 year ago

1.8.15

1 year ago

1.8.3

1 year ago

1.8.16

1 year ago

1.7.10

2 years ago

1.7.11

2 years ago

1.7.12

2 years ago

1.7.13

2 years ago

1.7.14

2 years ago

1.7.15

2 years ago

1.7.16

2 years ago

1.7.17

2 years ago

1.7.18

2 years ago

1.7.19

2 years ago

1.7.20

2 years ago

1.7.9

2 years ago

1.7.21

2 years ago

1.7.22

2 years ago

1.7.23

2 years ago

1.7.24

2 years ago

1.7.25

2 years ago

1.7.26

2 years ago

1.7.27

2 years ago

1.7.28

2 years ago

1.7.29

2 years ago

1.7.31

2 years ago

1.7.32

2 years ago

1.7.33

2 years ago

1.7.34

1 year ago

1.7.35

1 year ago

1.7.36

1 year ago

1.7.37

1 year ago

1.7.38

1 year ago

1.7.39

1 year ago

1.7.40

1 year ago

1.7.41

1 year ago

1.7.42

1 year ago

1.7.43

1 year ago

1.7.8

2 years ago

1.7.7

2 years ago

1.7.6

2 years ago

1.7.5

2 years ago

1.7.4

2 years ago

1.6.20

2 years ago

1.6.22

2 years ago

1.6.21

2 years ago

1.6.24

2 years ago

1.6.23

2 years ago

1.6.26

2 years ago

1.6.25

2 years ago

1.6.28

2 years ago

1.6.27

2 years ago

1.6.29

2 years ago

1.6.31

2 years ago

1.6.30

2 years ago

1.6.33

2 years ago

1.6.32

2 years ago

1.6.35

2 years ago

1.6.34

2 years ago

1.6.37

2 years ago

1.6.36

2 years ago

1.6.11

2 years ago

1.6.13

2 years ago

1.6.12

2 years ago

1.6.15

2 years ago

1.6.14

2 years ago

1.6.17

2 years ago

1.6.16

2 years ago

1.6.19

2 years ago

1.6.18

2 years ago

1.7.3

2 years ago

1.7.2

2 years ago

1.6.10

2 years ago

1.6.9

2 years ago

1.6.8

2 years ago

1.6.7

2 years ago

1.6.6

2 years ago

1.6.4

2 years ago

1.6.3

2 years ago

1.6.2

2 years ago

1.5.30

2 years ago

1.5.32

2 years ago

1.5.31

2 years ago

1.5.34

2 years ago

1.5.33

2 years ago

1.5.36

2 years ago

1.5.35

2 years ago

1.5.38

2 years ago

1.5.37

2 years ago

1.5.39

2 years ago

1.5.41

2 years ago

1.5.40

2 years ago

1.5.42

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.16

2 years ago

1.5.15

2 years ago

1.5.18

2 years ago

1.5.17

2 years ago

1.5.19

2 years ago

1.5.21

2 years ago

1.5.23

2 years ago

1.6.5

2 years ago

1.5.22

2 years ago

1.5.25

2 years ago

1.5.24

2 years ago

1.5.27

2 years ago

1.5.26

2 years ago

1.5.29

2 years ago

1.5.28

2 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.29

3 years ago

1.2.28

3 years ago

1.2.27

3 years ago

1.2.26

3 years ago

1.2.23

3 years ago

1.2.24

3 years ago

1.2.25

3 years ago

1.2.22

3 years ago

1.2.19

3 years ago

1.2.20

3 years ago

1.2.21

3 years ago

1.2.18

3 years ago

1.2.17

3 years ago

1.2.16

3 years ago

1.2.15

3 years ago

1.2.14

3 years ago

1.2.13

3 years ago

1.2.12

3 years ago

1.2.11

3 years ago

1.2.10

3 years ago

1.2.9

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago