Skip to content

Commit 5c4eed1

Browse files
committed
feat: Type Context as NonEmptyArray (gcanti#670)
1 parent f584c79 commit 5c4eed1

2 files changed

Lines changed: 7 additions & 12 deletions

File tree

‎docs/modules/index.ts.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ Added in v1.0.0
374374
**Signature**
375375
376376
```ts
377-
export interface Context extends ReadonlyArray<ContextEntry> {}
377+
export interface Context extends ReadonlyNonEmptyArray<ContextEntry> {}
378378
```
379379
380380
Added in v1.0.0
@@ -2123,7 +2123,7 @@ Added in v1.0.0
21232123
**Signature**
21242124
21252125
```ts
2126-
export declare function appendContext(c: Context, key: string, decoder: Decoder<any, any>, actual?: unknown): Context
2126+
export declare const appendContext: (c: Context, key: string, decoder: Decoder<any, any>, actual?: unknown) => Context
21272127
```
21282128
21292129
Added in v1.0.0

‎src/index.ts‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*/
44
import { Either, isLeft, left, right } from 'fp-ts/lib/Either'
55
import { Predicate, Refinement } from 'fp-ts/lib/function'
6+
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray'
7+
import { readonlyNonEmptyArray } from 'fp-ts'
68

79
// -------------------------------------------------------------------------------------
810
// Decode error
@@ -23,7 +25,7 @@ export interface ContextEntry {
2325
* @category Decode error
2426
* @since 1.0.0
2527
*/
26-
export interface Context extends ReadonlyArray<ContextEntry> {}
28+
export interface Context extends ReadonlyNonEmptyArray<ContextEntry> {}
2729

2830
/**
2931
* @category Decode error
@@ -237,15 +239,8 @@ export function getContextEntry(key: string, decoder: Decoder<any, any>): Contex
237239
/**
238240
* @since 1.0.0
239241
*/
240-
export function appendContext(c: Context, key: string, decoder: Decoder<any, any>, actual?: unknown): Context {
241-
const len = c.length
242-
const r = Array(len + 1)
243-
for (let i = 0; i < len; i++) {
244-
r[i] = c[i]
245-
}
246-
r[len] = { key, type: decoder, actual }
247-
return r
248-
}
242+
export const appendContext = (c: Context, key: string, decoder: Decoder<any, any>, actual?: unknown): Context =>
243+
readonlyNonEmptyArray.snoc(c, { key, type: decoder, actual })
249244

250245
function pushAll<A>(xs: Array<A>, ys: Array<A>): void {
251246
const l = ys.length

0 commit comments

Comments
 (0)