diff --git a/docs/modules/index.ts.md b/docs/modules/index.ts.md index acb51e28..a627325f 100644 --- a/docs/modules/index.ts.md +++ b/docs/modules/index.ts.md @@ -374,7 +374,7 @@ Added in v1.0.0 **Signature** ```ts -export interface Context extends ReadonlyArray<ContextEntry> {} +export interface Context extends ReadonlyNonEmptyArray<ContextEntry> {} ``` Added in v1.0.0 @@ -2123,7 +2123,7 @@ Added in v1.0.0 **Signature** ```ts -export declare function appendContext(c: Context, key: string, decoder: Decoder<any, any>, actual?: unknown): Context +export declare const appendContext: (c: Context, key: string, decoder: Decoder<any, any>, actual?: unknown) => Context ``` Added in v1.0.0 diff --git a/src/index.ts b/src/index.ts index 46d1bd93..f91b0e28 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,8 @@ */ import { Either, isLeft, left, right } from 'fp-ts/lib/Either' import { Predicate, Refinement } from 'fp-ts/lib/function' +import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray' +import { readonlyNonEmptyArray } from 'fp-ts' // ------------------------------------------------------------------------------------- // Decode error @@ -23,7 +25,7 @@ export interface ContextEntry { * @category Decode error * @since 1.0.0 */ -export interface Context extends ReadonlyArray<ContextEntry> {} +export interface Context extends ReadonlyNonEmptyArray<ContextEntry> {} /** * @category Decode error @@ -237,15 +239,8 @@ export function getContextEntry(key: string, decoder: Decoder<any, any>): Contex /** * @since 1.0.0 */ -export function appendContext(c: Context, key: string, decoder: Decoder<any, any>, actual?: unknown): Context { - const len = c.length - const r = Array(len + 1) - for (let i = 0; i < len; i++) { - r[i] = c[i] - } - r[len] = { key, type: decoder, actual } - return r -} +export const appendContext = (c: Context, key: string, decoder: Decoder<any, any>, actual?: unknown): Context => + readonlyNonEmptyArray.snoc(c, { key, type: decoder, actual }) function pushAll<A>(xs: Array<A>, ys: Array<A>): void { const l = ys.length