1
0
Fork 0
js-utils/docs/api/js-utils.md

7.6 KiB

Home > @apextoaster/js-utils

js-utils package

Classes

Class Description
ArrayMapper Map an array of items into a map of arrays using the specified keys, able to skip initial items and gather remaining items into a rest key.
AsyncTracker Async resource tracker using node's internal hooks.This probably won't work in a browser. It does not hold references to the resource, to avoid leaks. Adapted from https://gist.github.com/boneskull/7fe75b63d613fa940db7ec990a5f5843#file-async-dump-js
Checklist Check whether items are included or not (blacklist or whitelist, depending on mode).
ChildProcessError Error indicating that a child process exited with an error status.
InvalidArgumentError Error indicating that an invalid argument was passed to a function call.
MissingKeyError Error indicating that a required key did not exist in a map-like.
NotFoundError Error indicating that some value was not found.
NotImplementedError Error indicating that a function has not been implemented yet.
TimeoutError Error indicating that a promise timed out.

Enumerations

Enumeration Description
ChecklistMode

Functions

Function Description
concat(chunks) Concatenate a list of buffers.
constructorName(val) Get the constructor name from an instance.
countOf(val) Calculate the "length" of an array or value.Arrays return their length, single values return 1, and nil values return 0. This counts the number of elements that setOrPush would add.
defaultWhen(condition, items) Return the first element when condition is true and the second element when condition is false.
defer(ms, val) Resolve after a set amount of time.
doesExist(val) Check if a variable is not nil.
encode(chunks, encoding) Concatenate then encode a list of buffers.
ensureArray(val)
entriesOf(map) Get entries of a map-like.
filterNil(list) Remove any null or undefined items from the list.
getConstructor(val) Get the constructor from an instance.
getHead(map, key) Get the first element from the specified key within a map of lists.
getHeadOrDefault(map, key, defaultValue) Get the first element from the specified key, within a map of lists, or a default value when the key does not exist or is nil.
getMethods(value) Get the methods from an instance and its prototypes.
getOrDefault(map, key, defaultValue) Get a map key or default value when the key does not exist or is nil.
getTestLogger(verbose) Get a test logger. Returns a null logger unless verbose is true or run under debug mode.
hasItems(val) Test if a value is an array with some items (length > 0).This is not a general replacement for .length > 0, since it is also a typeguard: if (hasItems(val)) else { val } will complain that val is never in the else branch, since it was proven not to be an array by this function, even if val is simply empty.
hasItems(val)
isDebug() Test if DEBUG mode is set.TODO: check variable value as well
isNil(val) Check if a value is nil.
leftPad(val, min, fill)
makeDict(map) Turns a map or dict into a dict
makeMap(val) Clone a map or map-like object into a new map.
mergeList(parts) Merge arguments, which may or may not be arrays, into one return that is definitely an array.
mergeMap(target, source) Merge the source map into the target map, replacing keys that already exist.
mustCoalesce(values) Return the first value that is not nil.@todo: rename to mustDefault
mustExist(val) Assert that a variable is not nil and return the value.
mustFind(list, predicate) Find a value matching the given predicate or throw.
mustGet(map, key) Get an element from a Map and guard against nil values.
normalizeMap(map) (BETA) Normalize a map-like of values into a dict of lists of strings.
pairsToMap(pairs) Turns a list of name-value pairs into a map.
pushMergeMap(args) Merge the provided maps into a new map, merging keys that already exist by pushing new items.
removePid(path) Remove the file at the given path.
setOrPush(map, key, val) Set a map key to a new array or push to the existing value.
signal(signals)
spyLogger(spies) Create a spy logger using the provided methods, which returns itself as a child.
timeout(ms, oper) Reject after a set amount of time if the original promise has not yet resolved.
trim(val, max, tail)
waitForChild(child) Wait for a child process to exit, collecting output, errors, and exit status.
writePid(path) Write the current process ID to a file at the given path.
writeValue(stream, value)

Interfaces

Interface Description
ArrayMapperOptions
ChecklistOptions
ChildOptions
ChildResult
Dict
ExternalModule

Variables

Variable Description
SIGNAL_RELOAD
SIGNAL_RESET
SIGNAL_STOP

Type Aliases

Type Alias Description
ChildSpawner
MapLike A Map or dictionary object with string keys and TVal values.
ModuleCtor
Nil Unset value.
Optional Value that may be nil.