From 7c25859b53db6cdfa0d00afa2501977aa8cb5515 Mon Sep 17 00:00:00 2001 From: gcanti Date: Wed, 5 Jun 2019 17:34:26 +0200 Subject: [PATCH] add isNonEmpty --- docs/modules/Array.ts.md | 15 ++++++++++++++- src/Array.ts | 13 +++++++++++-- test/Array.ts | 8 +++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index 9f3c26490..6e6fb1e15 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -45,6 +45,7 @@ Adapted from https://github.com/purescript/purescript-arrays - [insertAt (function)](#insertat-function) - [intersection (function)](#intersection-function) - [isEmpty (function)](#isempty-function) +- [isNonEmpty (function)](#isnonempty-function) - [isOutOfBound (function)](#isoutofbound-function) - [last (function)](#last-function) - [lefts (function)](#lefts-function) @@ -138,7 +139,7 @@ value and the rest of the array. **Signature** ```ts -export function chop(f: (as: Array) => [B, Array]): (as: Array) => Array { ... } +export function chop(f: (as: NonEmptyArray) => [B, Array]): (as: Array) => Array { ... } ``` **Example** @@ -790,6 +791,18 @@ assert.strictEqual(isEmpty([]), true) Added in v2.0.0 +# isNonEmpty (function) + +Test whether an array is non empty narrowing down the type to `NonEmptyArray` + +**Signature** + +```ts +export function isNonEmpty(as: Array): as is NonEmptyArray { ... } +``` + +Added in v2.0.0 + # isOutOfBound (function) Test whether an array contains a particular index diff --git a/src/Array.ts b/src/Array.ts index 52d72e941..a7707a692 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -301,6 +301,15 @@ export function isEmpty(as: Array): boolean { return as.length === 0 } +/** + * Test whether an array is non empty narrowing down the type to `NonEmptyArray` + * + * @since 2.0.0 + */ +export function isNonEmpty(as: Array): as is NonEmptyArray { + return as.length > 0 +} + /** * Test whether an array contains a particular index * @@ -1113,11 +1122,11 @@ export function sortBy(ords: Array>): (as: Array) => Array { * * @since 2.0.0 */ -export function chop(f: (as: Array) => [B, Array]): (as: Array) => Array { +export function chop(f: (as: NonEmptyArray) => [B, Array]): (as: Array) => Array { return as => { const result: Array = [] let cs: Array = as - while (cs.length > 0) { + while (isNonEmpty(cs)) { const [b, c] = f(cs) result.push(b) cs = c diff --git a/test/Array.ts b/test/Array.ts index 93f065d65..739bff51f 100644 --- a/test/Array.ts +++ b/test/Array.ts @@ -56,7 +56,8 @@ import { findLastMap, getShow, reverse, - getEq + getEq, + isNonEmpty } from '../src/Array' import { left, right } from '../src/Either' import { fold as foldMonoid, monoidSum, monoidString } from '../src/Monoid' @@ -152,6 +153,11 @@ describe('Array', () => { assert.strictEqual(isEmpty([]), true) }) + it('isNotEmpty', () => { + assert.strictEqual(isNonEmpty(as), true) + assert.strictEqual(isNonEmpty([]), false) + }) + it('cons', () => { assert.deepStrictEqual(cons(0, as), [0, 1, 2, 3]) assert.deepStrictEqual(cons([1], [[2]]), [[1], [2]])