Global

Methods

addOneTimeListener(target, event, λ)

Utility function to emulate jQuery's one() on targets that don't have it. Basically, this adds an event listener on `target` that will be executed once, then removed. Careful! `this` is not bound! Also, for DOM events, the event is not automatically transformed into an Event, so you have to take care of this yourself.
Parameters:
Name Type Description
target EventEmitterType Any object that has on/off, addListener/removeListener, addEventListener/removeEventListener or once methods.
event Event The event to listen for.
λ function The event handler
Source:
See:

assert(truthy, message) → {Any}

If `truthy` is a truthy, returns that truthy without doing anything else. If it is a falsy, returns it and displays the assertion message.
Parameters:
Name Type Description
truthy Truthy | Falsy The truthy to test.
message String The message to display with console.error() when `truthy` is a falsy.
Source:
Returns:
Returns truthy.
Type
Any

compact(object) → {Object}

Deletes null/false/undefined values from the passed object.
Parameters:
Name Type Description
object Object The object to compact in-place.
Source:
Returns:
The modified `object`, for convenience
Type
Object

createErrorType(initialize, ErrorClass, prototype) → {function}

Return a constructor for a new error type.
Parameters:
Name Type Description
initialize function A function that gets passed the constructed error and the passed message and runs during the construction of new instances.
ErrorClass function An error class you wish to subclass. Defaults to Error.
prototype Object Additional properties and methods for the new error type.
Source:
Returns:
The constructor for the new error type.
Type
function

curriedDemethodify(methodName, method) → {function}

Takes a `method` as input and transforms it into a curried function taking an instance as its last parameter.
Parameters:
Name Type Description
methodName String The name of the method on the prototype.
method function The method to demethodify. It must have no rest parameters and all arguments must be named. The arguments cannot have default values.
Source:
See:
Returns:
The demethodified function
Type
function
Example
const { curriedDemethodify } = require("helpbox");

function Person(age) {
    this.age = age;
}

Person.prototype.ageInNYears = function (n) {
    return n + this.age;
}

const ageInNYears = curriedDemethodify(Person.prototype.ageInNYears);
const person      = new Person(3);

const ageIn10Years = ageInNYears(10);

// Prints 13.
console.log(ageIn10Years(person));

delay(λ, delay, …otherArguments) → {Promise}

Calls a function after a given delay.
Parameters:
Name Type Attributes Description
λ function The function to execute.
delay Number The delay, in ms.
otherArguments Any <repeatable>
Additional arguments to pass to λ.
Source:
See:
Returns:
A promise that resolves with the return value of λ.
Type
Promise

delayed(λ, delay, …otherArguments) → {function}

Return a version of λ that runs it after a given delay when called.
Parameters:
Name Type Attributes Description
λ function The function to transform.
delay Number The delay, in ms.
otherArguments Any <repeatable>
Additional arguments to pass to λ.
Source:
See:
Returns:
A function that returns a promise that resolves with the return value of λ.
Type
function

demethodify(λ) → {function}

Takes a method and transforms it into a function by binding this to the first argument passed to the resulting function. `this` gets bound to null when the method is called.
Parameters:
Name Type Description
λ function The function to demethodify.
Source:
See:
Returns:
The function in its demethodified form.
Type
function
Example
const { demethodify } = require("helpbox");

function Person(age) {
    this.age = age;
}

Person.prototype.tellMyAge = function () {
    console.log(this.age);
}

const person        = new Person(3);
const tellPersonAge = demethodify(Person.prototype.tellMyAge);

// Prints 3.
tellPersonAge(person);

demethodifyPrototype(prototype, definition) → {Object}

Takes a prototype, a `definition` object and defines all prototype methods as curried instance-last functions on the definition object. The prototype method must follow the rules of `curriedDemethodify`.
Parameters:
Name Type Description
prototype Object An object containing all the methods.
definition Object (Optional) The object on which the functions will be defined. Defaults to `{}`.
Source:
See:
Returns:
The transformed definition object.
Type
Object

looselyMatches(haystack, needle) → {Boolean}

Function that returns true when `needle` is found in `haystack`. The main advantages of this function are that it removes accented characters and that it is case-insensitive. Powered by lodash's deburr.
Parameters:
Name Type Description
haystack String The string to inspect.
needle String The substring to look for.
Source:
Returns:
True when variations of `needle` can be found inside `haystack`.
Type
Boolean

makeRandomAlphanumericalString(length) → {String}

Generates a random string containing characters in range [A-Za-z0-9]. Unsafe for crypto!
Parameters:
Name Type Description
length Number The length of the random string to generate.
Source:
Returns:
A string containing `length` simple pseudo-random characters.
Type
String

methodify(λ) → {function}

Takes a function that expects an object as its first parameter and returns a version that passes `this` in lieu of this parameter, so that the function can be called like a method. `this` gets bound to null when the method is called.
Parameters:
Name Type Description
λ function The function to methodify.
Source:
See:
Returns:
The function in its methodified form.
Type
function
Example
const { methodify } = require("helpbox");

const tellAge = person => console.log(person.age);

function Person(age) {
    this.age = age;
}

Person.prototype.tellMyAge = methodify(tellAge);

// Prints 3.
(new Person(3)).tellMyAge();

noConcurrentCalls(λ) → {function}

Creates a version of λ that cannot be called concurrently.
Parameters:
Name Type Description
λ function The function to transform.
Source:
Returns:
A version of λ that cannot be called in parallel. If a call is performed while the other one is not done, it returns a promise that resolves with the results of λ when the first call finishes.
Type
function

numberOfDecimals(number) → {Number}

Returns the number of decimals in a given number. The function also works for strings containing valid numbers.
Parameters:
Name Type Description
number Number | String The number to check.
Source:
Returns:
A number of decimals (0 for integers).
Type
Number

regularly(λ, interval, errorHandler) → {function}

Returns a function that starts calling λ repeatedly with an `interval` time interval. That function has a stop() method to stop the loop. To resume the loop, make another call to that function.
Parameters:
Name Type Description
λ function The function to call repeatedly.
interval Number The repetition interval.
errorHandler function | Falsy A function to call when λ throws. By default, this is a noop.
Source:
Returns:
λ transformed into a loop control function that returns a promise resolved after the first λ run and requiring no argument.
Type
function

repeatAsyncUntil(λ, predicate) → {Promise}

Calls λ repeatedly until its results satisfy `predicate`.
Parameters:
Name Type Description
λ function The function to repeat. Can be asynchronous.
predicate function The predicate to check the results of λ. Can be asynchronous too.
Source:
Returns:
A promise that resolves with the first return value from λ that satisfies `predicate`.
Type
Promise

sequentially(λ, inputArray, defaultReturnValue) → {Promise}

Executes an async λ over inputArray sequentially instead of concurrently.
Parameters:
Name Type Description
λ function The async function to execute sequentially.
inputArray Array The array on which λ gets executed.
defaultReturnValue Any (Optional) If the inputArray, the returned promise will hold that value.
Source:
Returns:
A promise that's fullfilled when the inputArray element is processed.
Type
Promise
Example
const { sequentially } = require("helpbox");

const users = ["Alex", "Georges", "Fred"];

const createUser = user => {
    return (await User.persist(user)).id;
}

// Will contain Fred's ID.
const lastUserId = sequentially(createUser, users);

toAsync(λ) → {function}

Returns an asynchronous version of the passed λ.
Parameters:
Name Type Description
λ function The function to make asynchronous
Source:
Returns:
An asynchronous version of λ.
Type
function

tryOrCrash(λ, errorMessage, printException, crashFunction) → {Void}

Tries executing λ. If it throws, calls `crashFunction` and prints messages.
Parameters:
Name Type Description
λ function The function to execute.
errorMessage String | Falsy The message to print on error.
printException Booly Whether or not we should print the exception when λ throws.
crashFunction function A function to call when λ throws. Gets passed 1 as its sole argument. By default, this calls NodeJS' process.exit(1).
Source:
Returns:
Type
Void