The @perfective/common/function package provides types and functions for the functional programming style.
As functions with more than three arguments are considered a code smell,
this package only declares the Nullary, Unary, Binary, and Ternary types.
The functions of higher arity is unlikely to be added to the package.
-
isFunction<T>(value: Function | T): value is Function— returnstrueif a givenvalueis aFunction. Otherwise, returnsfalse. -
isNotFunction<T>(value: Function | T): value is T— returnstrueif a givenvalueis not aFunction. Otherwise, returnsfalse.
-
Nullary<T>— a function without arguments:-
isNullary<F extends Function>(f: F): boolean— returnstrueif a given functionfhas length0(excluding a variadic argument). Otherwise, returnsfalse. -
constant<T>(value: T): Nullary<T>— creates a nullary function that returns a givenvalue.
-
-
Void— a procedure without arguments:-
naught(): void— an empty function to be passed as a callback when a no-op behavior is required.
-
-
Value<T> = T | Nullary<T>— a value itself or a nullary function that returns a value. (e.g. for lazy evaluation).-
valueOf<T>(value: Value<T>): T— When given a nullary function, evaluates the function and returns the result. Otherwise, returns the givenvalue.
-
-
Unary<X, V>— a function with one argument:-
isUnary<F extends Function>(f: F): boolean— returnstrueif a given functionfhas length1(excluding a variadic argument). -
same<T>(value: T): T— theidentityfunction. Returns a given value.
-
-
UnaryVoid<T> = (value: T) ⇒ void— a procedure with one argument.
-
Binary<X, Y, V>— a function with two arguments.-
isBinary<F extends Function>(f: F): boolean— Returnstrueif a given functionfhas length 2 (excluding a variadic argument). Otherwise, returnsfalse.
-
-
Ternary<X, Y, Z, V>— a function with three arguments.-
isTernary<F extends Function>(f: F): boolean— returnstrueif a given functionfhas length3(excluding a variadic argument). Otherwise, returnsfalse.
-
Length type defines a kind of objects that have "length" (arrays, strings, etc).
interface Length {
length: number;
}-
Functions
-
length<L extends Length>(value: L): number— returns thelengthproperty of a givenvalue.
-
-
Predicates
-
hasLength<L extends Length>(length: number): Predicate<L>— returns a predicate that checks if the input value has a givenlength. -
isNotEmpty<L extends Length>(value: L): boolean— returnstrueif a givenvaluehas non-positive length. -
isEmpty<L extends Length>(value: L): boolean— returnstrueif a givenvaluehaslengthgreater than 0.
-
-
Reducers
These functions can be used as a callback for
Array.prototype.reduce.-
toShortest<T extends Length>(shortest: T, value: T): T— returns the shortest of two given values. -
toLongest<T extends Length>(longest: T, array: T): T— returns the longest of two given values.
-