Skip to content

Commit

Permalink
feat: Type Context as NonEmptyArray (gcanti#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
devkat committed Nov 2, 2022
1 parent f584c79 commit 5c4eed1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/modules/index.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 5 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5c4eed1

Please sign in to comment.