A collection of useful helper functions used internally in the Nyx interpreter
The package is not currently published to NPM, so you'll have to either clone the repo or download + unzip it into your dependencies directory.
Include the object in your project:
const helpers = require("helpers");
// You can also destructure your assignment to only get the functions you need
const { isArray, isNumber, handleNegativeIndex } = require("helpers");
(object: any, [mapFn: (item: any, index: number, array: any[]) -> void, thisArg: any]) -> any[]
Returns an array from any object that can be converted to an array (e.g. an array-like object). Optional mapFn
transforms every element of the array, and optional thisArg
sets the context for mapFn
.
(array: any[]) -> any
Returns the first item of an array.
(object: any) -> boolean
Determine if an object is an array.
(object: any) -> boolean
Determine if an object is array-like, e.g. has a length property and items stored in numeric indexes.
(array: any[]) -> any
Returns the last item of an array.
(iterable: object[Symbol.iterator]) -> any[]
Converts an iterable object (array-like object) to an array.
(value: any) -> boolean
Determine if a value is a boolean value.
(fn) -> fn
Auto-curry a function.
(length: number, fn) -> fn
Curry a function with specified arity of length
.
(args: any[], fn) -> fn
Transform a function that takes multiple arguments into one that takes an array.
(val: any, fns: function[]) -> any
Pipe a value through a series of functions
(fns: ...function[]) -> function
Compose a series of functions from left-to-right.
(name: string, fn) -> function
Add a name
property to a function.
(value: any) -> boolean
Determine if a value is null
or undefined
.
(value: any) -> boolean
Determine if a value is specifically null.
(value: any) -> boolean
Determine if a value is specifically undefined.
(value: any, default: any) -> any
If a value is null or undefined, specify a default value to return. Otherwise, return the value itself.
(index: number, seq: string|array|object) -> number
If second parameter is object, must be an array-like object or other iterable with numeric indexes.
Converts a negative index into its corresponding positive index relative to a sequence's length. Returns zero or positive index as-is.
(value: any) -> boolean
Determine if a value is a BigInteger.
(value: any) -> number
Determine if a value is a number.
(value: any) -> number
Convert a value into its numeric equivalent.
CAUTION: will return NaN
if the value has no direct numeric equivalent. Will return Infinity
or -Infinity
if the value is too large or small to be represented by a JavaScript number.
(objects: object[]) -> object
Shallowly copies properties of each successive object to the first object in the order they are passed into the function. Mutates the first object.
`(object: any) -> string
Calls Object.prototype.toString
with object
as the context.
(object: any) -> string
Gets the class or constructor name. Works with anonymous function expressions bound to variables, but the following returns an empty string:
let o = new (function () {})();
... so probably DON'T DO THAT.
(object: any) -> any
Make a clone of an object. If the object has a clone
method, it uses that. Otherwise, it recursively copies an object's own properties. Note that __proto__
is not an own property.
(a: any, b: any) -> boolean
Recursively checks all properties of both objects for strict equality (===
).
(a: object, b: object) -> object
Copies b
's properties shallowly onto a
. Mutates a
.
(object: any, fn: (property: any[, key: string, object: any]) -> void|boolean) -> void
Run fn
callback on all own properties of any object. Breaks out of the loop if the callback returns false.
(object: any, fn: (property: any[, key: string, object: any]) -> void|boolean) -> void
Run fn
callback on the enumerable own properties of any object. Breaks out of the loop if the callback returns false.
(object: any) -> function
Get the constructor function or class used to create an object.
(object: any, property: string) -> any
If an object has an own property, retrieve it. Otherwise returns false
.
(object: any) -> any
Get the prototype of an object.
(object: any, property: string) -> boolean
Determine if an object has an own property.
(value: any) -> boolean
Determine if a value is an instance of Object. Also detects objects with null
prototypes.
(value: any) -> boolean
Determine if value typeof "object"
is true.
(object: object, fn: (any) -> any) -> object
Returns a new object with the same keys as the source object and a callback function applied to each of the source object's properties.
(a: any, b: any) -> boolean
Checks if a and b are the exact same object. If primitive values and not NaN
, checks for strict equality. Also correctly checks if NaN
equals NaN
.
(object: any) -> string[]
Return an array of all an object's own string property names.
(target: object, key: string[, { getter: () -> any, setter: (...args: any) -> any }]) -> void
Sets a getter and setter (either or both optional) on target[key]
.
(target: object, name: string, value: any[, enumerable: boolean|undefined, writable: boolean|undefined]) -> void
Sets a property and its attributes on a target object.
(value: any) -> boolean
Determine if a value is a RegExp.
(value: any) -> string
If a value is not a string, coerce it to its string representation.
(value: any) -> boolean
Determine if a value is a string.
(string: string) -> string
Trims whitespace from both sides of a string.
(value: any) -> boolean
Determine if a value is a Symbol.
(value: any) -> boolean
Determine if a value is defined.
Use as a placeholder for curry
and curryN
.