Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
"types": "./dist/storage.d.ts",
"import": "./dist/storage.mjs",
"default": "./dist/storage.js"
},
"./trim": {
"types": "./dist/trim.d.ts",
"import": "./dist/trim.mjs",
"default": "./dist/trim.js"
}
},
"keywords": [],
Expand Down
1 change: 1 addition & 0 deletions src/entrypoints/recommended.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/// <reference path="map-has.d.ts" />
/// <reference path="array-index-of.d.ts" />
/// <reference path="promise-catch.d.ts" />
/// <reference path="trim.d.ts" />
17 changes: 17 additions & 0 deletions src/entrypoints/trim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type Whitespace = " " | "\n" | "\t";

type TrimLeft<S extends string> = S extends `${Whitespace}${infer Rest}`
? TrimLeft<Rest>
: S;

type TrimRight<S extends string> = S extends `${infer Rest}${Whitespace}`
? TrimRight<Rest>
: S;

interface String {
trim<S extends string>(this: S): TrimLeft<TrimRight<S>>;
trimRight<S extends string>(this: S): TrimRight<S>;
trimStart<S extends string>(this: S): TrimLeft<S>;
trimLeft<S extends string>(this: S): TrimLeft<S>;
trimEnd<S extends string>(this: S): TrimRight<S>;
}
70 changes: 70 additions & 0 deletions src/tests/trim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { doNotExecute, Equal, Expect } from "./utils";

doNotExecute(() => {
const str = " hello " as const;

// trim tests
const trimmed = str.trim();
type trimTest = [Expect<Equal<typeof trimmed, "hello">>];

// trimStart/trimLeft tests
const trimmedStart = str.trimStart();
type trimStartTest = Expect<Equal<typeof trimmedStart, "hello ">>;

const trimmedLeft = str.trimLeft();
type trimLeftTest = Expect<Equal<typeof trimmedLeft, "hello ">>;

// trimEnd/trimRight tests
const trimmedEnd = str.trimEnd();
type trimEndTest = Expect<Equal<typeof trimmedEnd, " hello">>;

const trimmedRight = str.trimRight();
type trimRightTest = Expect<Equal<typeof trimmedRight, " hello">>;
});

doNotExecute(() => {
const str = "\thello\n" as const;

const trimmed = str.trim();
type trimTest = Expect<Equal<typeof trimmed, "hello">>;

const trimmedStart = str.trimStart();
type trimStartTest = Expect<Equal<typeof trimmedStart, "hello\n">>;

const trimmedEnd = str.trimEnd();
type trimEndTest = Expect<Equal<typeof trimmedEnd, "\thello">>;
});

doNotExecute(() => {
// Test with string literals that contain different whitespace combinations
const str = " \t\nhello world\t \n" as const;

const trimmed = str.trim();
type trimTest = Expect<Equal<typeof trimmed, "hello world">>;
});

doNotExecute(() => {
// Test with empty string and whitespace-only string
const emptyStr = "" as const;
const whitespaceStr = " \t\n" as const;

const trimmedEmpty = emptyStr.trim();
type trimEmptyTest = Expect<Equal<typeof trimmedEmpty, "">>;

const trimmedWhitespace = whitespaceStr.trim();
type trimWhitespaceTest = Expect<Equal<typeof trimmedWhitespace, "">>;
});

doNotExecute(() => {
// Test with string type (not literals)
const str: string = " hello ";

const trimmed = str.trim();
type trimTest = Expect<Equal<typeof trimmed, string>>;

const trimmedStart = str.trimStart();
type trimStartTest = Expect<Equal<typeof trimmedStart, string>>;

const trimmedEnd = str.trimEnd();
type trimEndTest = Expect<Equal<typeof trimmedEnd, string>>;
});