From deeaf1651ca28ea1c68d46de682dce280b62ed75 Mon Sep 17 00:00:00 2001 From: Deyan Totev Date: Thu, 22 Aug 2024 19:17:12 +0300 Subject: [PATCH] feat@is.not.empty --- files/NEXT_VERSION_CHECKLIST.md | 10 +++++++++- files/index.d.ts | 24 ++++++++++++++++++++++++ source/isNotEmpty-spec.ts | 19 +++++++++++++++++++ source/isNotEmpty.js | 5 +++++ source/isNotEmpty.spec.js | 16 ++++++++++++++++ source/lens-spec.ts | 2 +- 6 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 source/isNotEmpty-spec.ts create mode 100644 source/isNotEmpty.js create mode 100644 source/isNotEmpty.spec.js diff --git a/files/NEXT_VERSION_CHECKLIST.md b/files/NEXT_VERSION_CHECKLIST.md index d806994e..edd0ade6 100644 --- a/files/NEXT_VERSION_CHECKLIST.md +++ b/files/NEXT_VERSION_CHECKLIST.md @@ -1,11 +1,13 @@ throttle should accept 0 arguments, i.e. no need to force unary function https://github.com/ramda/types/pull/37/files + +https://github.com/ramda/types/pull/127/files + --- ABOVE IS DONE --- -https://github.com/ramda/types/pull/127/files --- ABOVE IS IN PROGRESS --- @@ -136,6 +138,12 @@ REFS: run immutable script === +https://github.com/ramda/types/pull/101/files + +export function last(list: readonly [...any[], T]): T; +export function last(list: ReadonlyNonEmptyArray): T; +export function last(list: readonly T[]): T | undefined; +=== from bookmarks: https://arethetypeswrong.github.io/?p=ramda%400.29.1 diff --git a/files/index.d.ts b/files/index.d.ts index 80c02cc4..7f62f523 100644 --- a/files/index.d.ts +++ b/files/index.d.ts @@ -1,5 +1,8 @@ export type RambdaTypes = "Object" | "Number" | "Boolean" | "String" | "Null" | "Array" | "RegExp" | "NaN" | "Function" | "Undefined" | "Async" | "Promise" | "Symbol" | "Set" | "Error" | "Map" | "WeakMap" | "Generator" | "GeneratorFunction" | "BigInt" | "ArrayBuffer" | "Date" +export type NonEmptyArray = [T, ...T[]]; +export type ReadonlyNonEmptyArray = readonly [T, ...T[]]; + type LastArrayElement = ValueType extends readonly [infer ElementType] ? ElementType @@ -1898,6 +1901,7 @@ export function last(list: readonly[]): undefined; export function last(list: never[]): undefined; export function last(array: T): LastArrayElement; export function last(array: T): LastArrayElement; +export function last(str: string): string | undefined; /* Method: lastIndexOf @@ -2680,6 +2684,26 @@ Notes: // @SINGLE_MARKER export function not(input: any): boolean; +/* +Method: notEmpty + +Explanation: + +Example: + +``` +``` + +Categories: Logic + +Notes: + +*/ +// @SINGLE_MARKER +export function isNotEmpty(value: T[]): value is NonEmptyArray; +export function isNotEmpty(value: readonly T[]): value is ReadonlyNonEmptyArray; +export function isNotEmpty(value: any): boolean; + /* Method: nth diff --git a/source/isNotEmpty-spec.ts b/source/isNotEmpty-spec.ts new file mode 100644 index 00000000..dd77b365 --- /dev/null +++ b/source/isNotEmpty-spec.ts @@ -0,0 +1,19 @@ +import { isNotEmpty } from 'rambda'; + +describe('R.isNotEmpty', () => { + it('happy', () => { + const readonlyArr: readonly number[] = []; + if (isNotEmpty(readonlyArr)) { + readonlyArr; // $ExpectType ReadonlyNonEmptyArray + } + const tuple: [number, string] = [1, '1']; + if (isNotEmpty(tuple)) { + tuple; // $ExpectType [number, string] + } + + const tuple2 = [1, 2, 3] as const; + if (isNotEmpty(tuple2)) { + tuple2; // $ExpectType readonly [1, 2, 3] + } + }); +}); diff --git a/source/isNotEmpty.js b/source/isNotEmpty.js new file mode 100644 index 00000000..4fab0dbc --- /dev/null +++ b/source/isNotEmpty.js @@ -0,0 +1,5 @@ +import { isEmpty } from './isEmpty.js'; + +export function isNotEmpty(input) { + return !isEmpty(input); +} diff --git a/source/isNotEmpty.spec.js b/source/isNotEmpty.spec.js new file mode 100644 index 00000000..df655a46 --- /dev/null +++ b/source/isNotEmpty.spec.js @@ -0,0 +1,16 @@ +import { isNotEmpty } from './isNotEmpty.js' + +test('happy', () => { + expect(isNotEmpty(undefined)).toBeTrue() + expect(isNotEmpty('')).toBeFalse() + expect(isNotEmpty(null)).toBeTrue() + expect(isNotEmpty(' ')).toBeTrue() + expect(isNotEmpty(new RegExp(''))).toBeTrue() + expect(isNotEmpty([])).toBeFalse() + expect(isNotEmpty([ [] ])).toBeTrue() + expect(isNotEmpty({})).toBeFalse() + expect(isNotEmpty({ x : 0 })).toBeTrue() + expect(isNotEmpty(0)).toBeTrue() + expect(isNotEmpty(NaN)).toBeTrue() + expect(isNotEmpty([ '' ])).toBeTrue() +}) diff --git a/source/lens-spec.ts b/source/lens-spec.ts index 271493c0..03a65035 100644 --- a/source/lens-spec.ts +++ b/source/lens-spec.ts @@ -1,4 +1,4 @@ -import {lens, assoc, lensProp, view, lensIndex, over, lensPath} from 'rambda' +import {lens, assoc, lensProp, view, lensIndex, over, lensPath} from 'ramda' interface Input { foo: string,