Skip to content

Commit

Permalink
fix(Trim): preserve whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell committed Mar 20, 2023
1 parent 92ee3f4 commit 38ac6b7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
31 changes: 4 additions & 27 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
// Source: https://github.com/sindresorhus/type-fest/blob/main/source/internal.d.ts#L49
type Whitespace =
| '\u{9}' // '\t'
type Newline =
| '\u{A}' // '\n'
| '\u{B}' // '\v'
| '\u{C}' // '\f'
| '\u{D}' // '\r'
| '\u{20}' // ' '
| '\u{85}'
| '\u{A0}'
| '\u{1680}'
| '\u{2000}'
| '\u{2001}'
| '\u{2002}'
| '\u{2003}'
| '\u{2004}'
| '\u{2005}'
| '\u{2006}'
| '\u{2007}'
| '\u{2008}'
| '\u{2009}'
| '\u{200A}'
| '\u{2028}'
| '\u{2029}'
| '\u{202F}'
| '\u{205F}'
| '\u{3000}'
| '\u{FEFF}';
;

// Source: https://github.com/sindresorhus/type-fest/blob/main/source/trim.d.ts
type TrimStart<S extends string> = S extends `${Whitespace}${infer R}` ? TrimStart<R> : S;
type TrimStart<S extends string> = S extends `${Newline}${infer R}` ? TrimStart<R> : S;

type TrimEnd<S extends string> = S extends `${infer R}${Whitespace}` ? TrimEnd<R> : S;
type TrimEnd<S extends string> = S extends `${infer R}${Newline}` ? TrimEnd<R> : S;

export type Trim<S extends string> = TrimStart<TrimEnd<S>>;

Expand Down
2 changes: 2 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ expectType<'🦄'>(trimNewlines('\n🦄\r\n'));
expectType<'🦄\n'>(trimNewlinesStart('\n\n🦄\n'));
expectType<'\n🦄'>(trimNewlinesEnd('\n🦄\n\n'));

expectType<' 🦄\n '>(trimNewlines('\n 🦄\n \n'));

declare const _string: string;

expectType<string>(trimNewlines(_string));
Expand Down

0 comments on commit 38ac6b7

Please sign in to comment.