@dancrumb/fpish
Version:
FP-friendly classes for Typescript
65 lines (64 loc) • 1.57 kB
JavaScript
import { Optional } from "./Optional.js";
/**
* This is the identity function - it returns whatever you pass in
*
* @category Utilities
*/
export const identity = (t) => t;
/**
* A synonym for {@link identity}
*
* @category Utilities
*/
export const asIs = identity;
/**
* This function returns undefined, whatever you pass in
*
* @category Utilities
*/
export const drop = (t) => void undefined;
/**
* This does the same as {@link drop}, but is typed to return undefined (rather than void)
*
* @category Utilities
*/
export const asUndefined = (t) => undefined;
/**
* Logs an error and moves on
*
* @category Utilities
*/
export const logError = (e) => console.error(e);
/**
* This is a handy utility function that just throws an error
* that exists in an Either. Useful for Either.apply
*
* @category Utilities
*/
export const throwError = (e) => {
throw e;
};
/**
* Sometimes, all you're looking to do is extract a single property from and object.
* That's what this function does
*
* @category Utilities
*/
export const extractProperty = (key) => (o) => o[key];
/**
* Sometimes, you want a subset of an object, with just a few properties
* That's what this function does
*
* @category Utilities
*/
export const pickProperties = (keys) => (o) => {
return keys.reduce((acc, key) => key in o ? ({ ...acc, [key]: o[key] }) : acc, {});
};
/**
* Take function that returns a value and make that return an Optional
*
* @category Utilities
*/
export const asOptional = (f) => {
return (...args) => Optional.of(f(...args));
};