Members
(inner, constant) nanoid
- Description:
Generates am ID using the UUID method, but makes it shorter Also allows passing some custom options like a prefix for the generated ID
- Source:
Generates am ID using the UUID method, but makes it shorter Also allows passing some custom options like a prefix for the generated ID
Methods
(inner) applyToFunc(item, expression)
- Description:
Helper for pipeline. Passes 'item' into 'expression' as its first argument.
Expression may be a function or an array of form: [function, ...remainingArguments].
- Source:
Parameters:
Name | Type | Description |
---|---|---|
item |
* | |
expression |
* |
(inner) cloneFunc(func) → {Object}
- Description:
Clones a function using the Function constructor and calling toString on the passed in function
- Source:
Example
const func = () => { console.log('test') }
const clone = cloneFunc(func)
// clone !== func
Parameters:
Name | Type | Description |
---|---|---|
func |
function | function to clone |
Returns:
cloned function
- Type
- Object
(inner) compareTo(x, y) → {number|null}
- Description:
Generic compare to method that works for strings, numbers, and booleans
- Source:
Parameters:
Name | Type | Description |
---|---|---|
x |
string | number | boolean | |
y |
string | number | boolean |
Returns:
- returns a value < 0 if x is less than y, 0 if they are equal, and a value greater than 0 if x is greater than y. Returns null if the args are not comparable.
- Type
- number | null
(inner) complement(predicate) → (nullable) {function}
- Description:
Returns a new function that is the complement of predicate function
predicate
- Source:
Example
const isNegative = x => (x < 0)
const isNonNegative = complement(isNegative)
isNonNegative(1) // true
Parameters:
Name | Type | Description |
---|---|---|
predicate |
function |
Returns:
the complement of predicate
, if it's a function, otherwise null
- Type
- function
(inner) debounce(func, wait, immediate) → {void}
- Description:
Limits the amount of calls to a function over time
- Source:
Examples
debounce(myFunction)
// Calls myFunction after the default 250 ms
debounce(myFunction, 500)
// Calls myFunction after 500 ms
debounce(myFunction, 500, true)
// Calls myFunction immediately
Parameters:
Name | Type | Description |
---|---|---|
func |
function | function to call |
wait |
Number | how long to wait between function calls |
immediate |
Boolean | should call immediately |
Returns:
- Type
- void
(inner) eitherFunc(func1, func2) → {function}
- Description:
Returns the first param if it's a function.
If first param is not a function, returns second param.
- Source:
Examples
eitherFunc(() => {}, 'bar')
// Returns first param because it's a function.
eitherFunc('foo', 'bar')
// Returns 'bar'
Parameters:
Name | Type | Description |
---|---|---|
func1 |
function | return if is func |
func2 |
function | use if first is not an object |
Returns:
- Type
- function
(inner) identity(x) → {*}
- Description:
A function that simply returns its input
- Source:
Parameters:
Name | Type | Description |
---|---|---|
x |
* |
Returns:
the input
- Type
- *
(inner) isFunc(test) → {Boolean}
- Description:
Check if the passed in item is a function.
- Source:
Examples
isFunc(() => {})
// Returns true
isFunc('bar')
// Returns false
Parameters:
Name | Type | Description |
---|---|---|
test |
* |
Returns:
is a function
- Type
- Boolean
(inner) isOrderable(x) → {bool}
- Description:
Checks if param is an orderable primitive
- Source:
Parameters:
Name | Type | Description |
---|---|---|
x |
* |
Returns:
- true if x is a comparable primitive
- Type
- bool
(inner) limboify(cb, args) → {Promise|*}
- Description:
Converts a method with a callback as the last argument into a promise
- Source:
Examples
limboify(fs.rename, 'my/file.txt', 'my/renamed-file.txt')
limboify(fs.mkdir, 'my/new/directory', { recursive: true })
Parameters:
Name | Type | Description |
---|---|---|
cb |
* | method to wrap in a promise |
args |
* | Arguments to pass to the callback method |
Returns:
- Success response of fs.rename method
- Type
- Promise | *
(inner) match(matchArg, entries) → {*}
- Description:
Pattern matching function. Iterates through the entries,
which have the form [ check value or predicate, return value ], and
when it encounters an entry whose check value matches the matchArg
(or the predicate returns true when passed the matchArg), it returns
the return value of that entry.For the default case: use [ match.default,
]
- Source:
Examples
const value = 1
match(value,
[ 1, "hello" ],
[ x => x > 2, "greater" ]
[ match.default, "defaulted"]
)
=> returns "hello"
const value = 3
match(value,
[ 1, "hello" ],
[ x => x > 2, "greater" ]
)
=> returns "greater"
// react reducer:
function todoReducer(state, action) {
const reducer = match(action.type,
[ 'ADD-TODO', addTodo ],
[ 'REMOVE-TODO', removeTodo ],
[ 'UPDATE-TODO', updateTodo ],
[ match.default, state ]
)
return reducer(state, action)
}
Parameters:
Name | Type | Description |
---|---|---|
matchArg |
* | the argument to match against the cases |
entries |
Array | the cases to match against the matchArg |
Returns:
- the return value of the first entry with a matching check value, else null
- Type
- *
(inner) memorize(func, getCacheKey) → {function}
- Description:
Creates a method to memorize passed in methods output
- Source:
Examples
memorize(myFunction, cacheKeyFunction)
memorize(myFunction, cacheKeyFunction, 100)
Parameters:
Name | Type | Description |
---|---|---|
func |
function | method to memorize output of |
getCacheKey |
function | gets the key to save cached output |
Returns:
memorized function with cache
- Type
- function
(inner) noOp() → {void}
- Description:
Reusable empty function that is a no-op
- Source:
Returns:
- Type
- void
(inner) not(func) → {function}
- Description:
When called, it calls original function, then returns inverse of the functions result
Should be used with functions that return a boolean
- Source:
Parameters:
Name | Type | Description |
---|---|---|
func |
function | Function call and invert its response |
Returns:
- Calls the passed in function then returns True if the passed in function returns falsy, otherwise false
- Type
- function
(inner) parseErrorMessage(exception) → (nullable) {string}
- Description:
Extracts the message from the exception, whether string or object
- Source:
Example
try {
throwSomeException()
}
catch (err) {
const message = parseErrorMessage(err) || 'Error'
}
Parameters:
Name | Type | Description |
---|---|---|
exception |
* | Error to be extracted |
Returns:
- The message or null if no message is present
- Type
- string
(inner) pipeline(item, …functions) → {*}
- Description:
Function for making repeated nested function calls (the 'pipeline') succinct. Passes "item" into
the first function (as its first argument), takes its result and passes that into the next function, and repeats.
Continues until no functions remain, at which point it returns the value returned by the last function.
- you can also pass in an array in place of a function to specify a function to be called with some arguments. E.g.: [foo, 2, 3] would return foo(item, 2, 3)
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
item |
* | the starting input. If it is a function, it will be executed immediately and the result will be piped into the remaining functions. |
|
functions |
function |
<repeatable> |
Functions to be iterated over one after the other |
Returns:
- the final result of calling the pipeline of functions , starting with item as input
- Type
- *
(inner) stackTracePaths(filter) → {Array.<string>}
- Description:
Gets the paths from a stacktrace as CallSites and returns them
- Source:
Parameters:
Name | Type | Description |
---|---|---|
filter |
Array | function | List of paths to ignore, or function that returns truthy to ignore |
Returns:
- List of paths from the stackTrace
- Type
- Array.<string>
(inner) TAsyncFuns(asyncFns) → {Promise.<Array.<*>>}
- Description:
Calls each promise-returning function in array
asyncFns
, but awaits each before calling the next. Will pass the index and resolved values of complete functions to each subsequent function, in case any need them.
- Source:
Examples
const results = await runSeq(asyncFunctions)
const results = await runSeq(asyncFunctions, { cloneResults: true, returnOriginal: false })
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
asyncFns |
Array.<function()> | array of functions to call in sequence. Each will be passed (currentIndex, resultsSoFar) |
||
options.cloneResults |
boolean |
<optional> <nullable> |
false
|
if true, each function will be passed a deep clone of the results array, rather than the reference to it. |
options.returnOriginal |
boolean |
<optional> <nullable> |
true
|
if true, any member of asyncFns that is not a function will have its corresponding value in the return array be itself. If this is false, that value will be undefined. |
Returns:
- returns a promise that resolves to an array of all the asyncFns' return values
- Type
- Promise.<Array.<*>>
(inner) throttle(func, waitopt) → {function}
- Description:
Throttle function calls to only execute once over a wait period
- Source:
Example
throttle(() => console.log('throttled'), 50)()
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
func |
* | method to call after wait |
||
wait |
Number |
<optional> |
100
|
time to wait between calls |
Returns:
throttled function
- Type
- function
(inner) throwError(error)
- Description:
Throws an Error from the passed in error
- Source:
Parameters:
Name | Type | Description |
---|---|---|
error |
Object | string | The Error message or Object to throw |
Throws:
Error
(inner) timedRun(fn, …argsnullable) → {Promise.<Array.<*, number>>}
- Description:
Executes and times the function
fn
.
- Source:
Examples
const [ result, executionTime ] = timedRun(() => http.get(url)))
const [ result, executionTime ] = timedRun(http.get, url)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fn |
function | ||
args |
* |
<nullable> <repeatable> |
any number of arguments to pass to fn when it is called |
Returns:
[ fn output, execution time in ms ]
- Type
- Promise.<Array.<*, number>>
(inner) TWaitArgs() → {Promise.<any>}
- Description:
Waits for check method to return true before calling onFinish
Will call the passed in checkMethod x (amount) number of times before failing
Will wait x ( wait ) milliseconds between calls to the check method
- Source:
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
args.check |
function | checks if onFinished should be called ( returns true ) |
||
args.onFinish |
function | called when check methods returns true |
||
args.amount |
Number |
<optional> |
4
|
Amount of times to call the check before failing |
args.wait |
Number |
<optional> |
1000
|
Time to wait between each check |
...args |
Array |
<optional> |
Arguments to pass to the check, and onFinish methods |
Returns:
- Resolves to the response from onFinish
- Type
- Promise.<any>
(inner) uuid(startopt) → {String}
- Description:
Creates a uuid, unique up to around 20 million iterations.
- Source:
Example
uuid()
// New uuid as a string
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
start |
Number |
<optional> |
of the uuid |
Returns:
- build uuid
- Type
- String