Skip to content

Commit

Permalink
feat: ensure there's at least 1 char in non-empty string
Browse files Browse the repository at this point in the history
This is useful e.g. when trying to leverage a non empty string for accessing it's 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`.
  • Loading branch information
simPod committed Oct 26, 2023
1 parent f10e2ca commit 13cc067
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 3 additions & 2 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
TypedArray,
WeakRef,
} from './types.js';
import {NonEmptyString} from "./types.js";

Check failure on line 13 in source/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

All imports in the declaration are only used as types. Use `import type`.

Check failure on line 13 in source/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Strings must use singlequote.

Check failure on line 13 in source/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

All imports in the declaration are only used as types. Use `import type`.

Check failure on line 13 in source/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Strings must use singlequote.

Check failure on line 13 in source/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

All imports in the declaration are only used as types. Use `import type`.

Check failure on line 13 in source/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

Strings must use singlequote.

const typedArrayTypeNames = [
'Int8Array',
Expand Down Expand Up @@ -582,12 +583,12 @@ export function isNonEmptySet<T = unknown>(value: unknown): value is Set<T> {
}

// 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);
}

Expand Down
2 changes: 2 additions & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ export type NodeStream = {
} & NodeJS.EventEmitter;

export type Predicate = (value: unknown) => boolean;

export type NonEmptyString = string & {0: string};

0 comments on commit 13cc067

Please sign in to comment.