type-fest
Version:
A collection of essential TypeScript types
36 lines (28 loc) • 898 B
TypeScript
import type {WhitespaceCharacter} from './whitespace-character.d.ts';
import type {UnwrapBrandedPrimitive} from './unwrap-branded-primitive.d.ts';
/**
Remove spaces from the left side.
*/
// Inspect one character instead of matching every whitespace alternative at each step.
type TrimLeft<Value extends string> = Value extends `${infer First}${infer Rest}`
? First extends WhitespaceCharacter
? TrimLeft<Rest>
: Value
: Value;
/**
Remove spaces from the right side.
*/
type TrimRight<V extends string> = V extends `${infer R}${WhitespaceCharacter}` ? TrimRight<R> : V;
/**
Remove leading and trailing spaces from a string.
@example
```
import type {Trim} from 'type-fest';
type Example = Trim<' foo '>;
//=> 'foo'
```
@category String
@category Template literal
*/
export type Trim<V extends string> = TrimLeft<TrimRight<V extends object ? UnwrapBrandedPrimitive<V> : V>>;
export {};