From ab4a3e99a04564143f1c68d0da56fe7183c181d1 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Thu, 26 Oct 2023 14:56:07 +0200 Subject: [PATCH] feat: ensure there's at least 1 char in non-empty string This is useful e.g. when trying to leverage a non-empty string for accessing its first character. We can say there's always at least 1 char in non-empty string so this allows us to do `s[0]` with ts option `noUncheckedIndexedAccess: true`. --- source/index.ts | 5 +++-- source/types.ts | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/source/index.ts b/source/index.ts index 848af80..2ee08d3 100644 --- a/source/index.ts +++ b/source/index.ts @@ -4,6 +4,7 @@ import type { Class, Falsy, NodeStream, + NonEmptyString, ObservableLike, Predicate, Primitive, @@ -582,12 +583,12 @@ export function isNonEmptySet(value: unknown): value is Set { } // TODO: Use `not ''` when the `not` operator is available. -export function isNonEmptyString(value: unknown): value is string { +export function isNonEmptyString(value: unknown): value is NonEmptyString { return isString(value) && value.length > 0; } // TODO: Use `not ''` when the `not` operator is available. -export function isNonEmptyStringAndNotWhitespace(value: unknown): value is string { +export function isNonEmptyStringAndNotWhitespace(value: unknown): value is NonEmptyString { return isString(value) && !isEmptyStringOrWhitespace(value); } diff --git a/source/types.ts b/source/types.ts index 621c771..e77d73a 100644 --- a/source/types.ts +++ b/source/types.ts @@ -73,3 +73,5 @@ export type NodeStream = { } & NodeJS.EventEmitter; export type Predicate = (value: unknown) => boolean; + +export type NonEmptyString = string & {0: string};