@dongskyler/helpers.js
Homepage: https://github.com/dongskyler/helpers.js
Table of contents
Description
This is a collections of JavaScript and TypeScript helper functions.
Functions
inputOutput
-
listFilesInDirSync()
listFilesInDirSync(dir
[, ext, removeExt]
)
- Parameters
-
dir
: string
(Positional, required)- Path to directory.
-
ext
: string | null
(Positional, optional)- Filter by file extension.
-
removeExt
: boolean
(Positional, optional)- Remove the file extension from the filename string.
-
- Return value
: string[] | null
- An array of filenames in a directory.
- Parameters
-
loadFile()
-
readPipeWrite()
-
writeToFile()
others
-
runFuncsWithArgs()
runFuncsWithArgs(funcs
[, args, spread]
)
- Run an array of functions with the same set of argument(s)
- Parameters
-
funcs
: ((...args: any | any[] | null) => any)[]
(Positional, required)- An array of functions.
-
args
: any | any[] | null = null
(Positional, optional)- An array of arguments. Each element of the array will be presented to each function.
-
spread
: boolean
(Positional, optional)- If
args
is an array of arrays, you can choose to expand each sub-array with the spread operator. - Default is false.
- If
-
- Return value
: any[]
- An array of return values from functions
-
valOfKeysAndIndices()
valOfKeysAndIndices(obj
[, key0[, key1[, ...keyN]]]
)
- Returns the value in an array or an object (or you call it JSON, a hash map or a dictionary) given the series of indices and/or keys pointing to the value.
- Parameters
-
obj
: any
(Positional, required)- An array, an object (or you call it JSON, a hash map or a dictionary) or a combination.
- The first index or key pointing to the value is required.
-
keyN
: string | number | null
(Positional, optional)- An arbitrary number of indices and/or keys pointing to the value.
- For example, if
x = {name: 'Tom'}
, to fetch value'Tom'
, usevalOfKeysAndIndicies(x, 'name')
. Ify = { key0: [{ key1: [3.142, 6.626,] },] }
, to fetch6.626
, usevalOfKeysAndIndicies(y, 'key0', 0, 'key', 1)
.
-
- Return value
: any
- The value from the given indices/keys.
sort
-
comparatorLexic()
comparatorLexic(
[{key = [], ignoreCase = false, descending = false}
)
- Compare function for comparing lexicographic order.
- Parameters
-
ignoreCase
: boolean
(Named, optional)- Ignore the case of the given string.
-
Default is
false
.
-
descending
: boolean
(Named, optional)- Ascending or descending order.
-
Default is
false
, that is, ascending order.
-
keyN
: string | number | null
(Positional, optional)- An arbitrary number of indices and/or keys pointing to the value for comparison.
- For example, if
x = {name: 'Tom'}
, usecomparatorLexic('name')
to fetch value'Tom'
for comparison. Ifx = { key0: [{ key1: [Earth, Mars,] },] }
, to fetchMars
for comparison, usecomparatorLexic('key0', 0, 'key', 1)
. - See
keyN
invalOfKeysAndIndices
for more details. -
Default is
null
.
-
- Return value
: function
- Function
(x: any, y: any) => number
- Return value
: number
-
-1
ifa
should placed beforeb
. -
0
ifa
andb
are equal. -
1
ifa
should be placed afterb
.
-
- Return value
- Function
-
comparatorNumeric()
comparatorNumeric(
[{key = [], ignoreSign = false, descending = false}
)
- Compare function for comparing numerical order.
- Parameters
-
keyN
: string | number | null
(Positional, optional)- An arbitrary number of indices and/or keys pointing to the value for comparison.
- If
x = { key0: [{ key1: [3.142, 6.626,] },] }
, to fetch6.626
for comparison, usecomparatorLexic('key0', 0, 'key', 1)
. - See
keyN
invalOfKeysAndIndices
for more details. -
Default is
null
.
-
ignoreSign
: boolean
(Named, optional)- Ignore the sign of the number, i.e. use the absolute value for comparison.
-
Default is
false
.
-
descending
: boolean
(Named, optional)- Ascending or descending order.
-
Default is
false
, that is, ascending order.
-
- Return value
: function
- Function
(a: any, b: any) => number
- Return value
: number
-
-1
ifa
should placed beforeb
. -
0
ifa
andb
are equal. -
1
ifa
should be placed afterb
.
-
- Return value
- Function
-
bubbleSort()
bubbleSort(arr
[, compareFunction, k]
)
- Bubble sort algorithm. Stable.
- Parameters
-
arr
: any[]
(Positional, required)- An array to be sorted.
-
compareFunction
: (a: any, b: any) => number
(Positional, optional)- Compare function.
- Default is comparatorLexic().
-
k
: number
(Positional, optional)- Partially sort the array to get the first k sorted elements
-
- Return value
: any[]
- A sorted array.
-
mergeSort()
mergeSort(arr
[, compareFunction]
)
- Merge sort algorithm. Stable.
- Parameters
-
arr
: any[]
(Positional, required)- An array to be sorted.
-
compareFunction
: (a: any, b: any) => number
(Positional, optional)- Compare function.
- Default is comparatorLexic().
-
- Return value
: any[]
- A sorted array.
-
partialSort()
partialSort(arr
[, compareFunction, k]
)
- Partially sort the array to get the first k sorted elements, given the compare function.