Functions
The Functions module provides a collection of utility functions for functional programming in TypeScript. It includes common functional programming primitives, function composition utilities, and helper functions for common operations.
Function Evaluation
invoke
Immediately invokes a function and returns its return value.
import { invoke } from 'funkcia/functions';
declare const shape: Shape;
const humanReadableShape = invoke(() => {
switch (shape.kind) {
case 'CIRCLE':
return 'Circle';
case 'SQUARE':
return 'Square';
default:
const invalidKind: never = shape.kind;
throw new Error(`Invalid shape: ${invalidKind}`);
}
});lazyCompute
Lazily computes a value by invoking a function. The value is computed only once when first accessed.
Functional Primitives
identity
Returns the provided value unchanged.
noop
A function that does nothing and returns nothing.
Constant Functions
always
Returns a function that will always return the provided value.
alwaysNull
Returns null.
alwaysUndefined
Returns undefined.
alwaysVoid
Returns void.
ignore
Returns never.
alwaysTrue
Returns true.
alwaysFalse
Returns false.
Type Utilities
coerce
This operation is unsafe and can be misleading if misused. Make sure you know what you're doing and use it wisely.
Ideally, you should only use this function when you have a better understanding of your code than TypeScript, which may be unable to narrow down the type of a value.
Returns the provided value coerced to the desired type.
compose
Composes two or more functions into a single function, from left to right.
pipe
Pipes a value through a series of functions, from left to right.
Last updated
Was this helpful?