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`.
This commit is contained in:
parent
f10e2caf3d
commit
ab4a3e99a0
2 changed files with 5 additions and 2 deletions
|
|
@ -4,6 +4,7 @@ import type {
|
||||||
Class,
|
Class,
|
||||||
Falsy,
|
Falsy,
|
||||||
NodeStream,
|
NodeStream,
|
||||||
|
NonEmptyString,
|
||||||
ObservableLike,
|
ObservableLike,
|
||||||
Predicate,
|
Predicate,
|
||||||
Primitive,
|
Primitive,
|
||||||
|
|
@ -582,12 +583,12 @@ export function isNonEmptySet<T = unknown>(value: unknown): value is Set<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Use `not ''` when the `not` operator is available.
|
// 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;
|
return isString(value) && value.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Use `not ''` when the `not` operator is available.
|
// 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);
|
return isString(value) && !isEmptyStringOrWhitespace(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,3 +73,5 @@ export type NodeStream = {
|
||||||
} & NodeJS.EventEmitter;
|
} & NodeJS.EventEmitter;
|
||||||
|
|
||||||
export type Predicate = (value: unknown) => boolean;
|
export type Predicate = (value: unknown) => boolean;
|
||||||
|
|
||||||
|
export type NonEmptyString = string & {0: string};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue