UNPKG

@ldrick/trade-indicators

Version:
38 lines (37 loc) 1.43 kB
import { either as E, function as F, readonlyArray as RA, readonlyNonEmptyArray as RNEA, } from 'fp-ts'; import { EmptyArrayError } from '../errors/EmptyArrayError.js'; import { NotEnoughDataError } from '../errors/NotEnoughDataError.js'; import * as big from './big.js'; import * as num from './number.js'; /** * Safely convert `RNEA.ReadonlyNonEmptyArray<number>` to `RNEA.ReadonlyNonEmptyArray<Big>`. * * @internal */ export const toBig = RNEA.traverse(E.Applicative)(num.toBig); /** * Convert `RNEA.ReadonlyNonEmptyArray<Big>` to `RNEA.ReadonlyNonEmptyArray<number>`. * * @internal */ export const toNumber = (values) => RNEA.map(big.toNumber)(values); /** * Create new Array from given and fill left with value up to given size. * * @internal */ export const fillLeftW = (size, value) => (tail) => F.pipe(size > tail.length ? size - tail.length : 0, (times) => RA.replicate(times, value), RNEA.concatW(tail)); /** * Get all but the first of an `ReadonlyNonEmptyArray` as `ReadonlyNonEmptyArray` * * @internal */ export const tail = (array) => F.pipe(array, RNEA.tail, (rest) => RA.isNonEmpty(rest) ? E.right(rest) : E.left(new EmptyArrayError())); /** * Validates if an Array has the required size. * * @internal */ export const validateRequiredSize = (required) => (array) => RA.isNonEmpty(array) && array.length >= required ? E.right(array) : E.left(new NotEnoughDataError(array.length, required));