-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+
trim
, trimStart
, and trimEnd
methods
- Loading branch information
Showing
9 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { trim } from "./trim"; | ||
|
||
describe("trim", () => { | ||
it("should trim single character from both sides of the string", async () => { | ||
must(trim("aaacataaa", "a")).equal("cat"); | ||
must(trim("abacteria", "a")).equal("bacteri"); | ||
must(trim("abc", "a")).equal("bc"); | ||
must(trim("abc", "c")).equal("ab"); | ||
must(trim("aajjja", "a")).equal("jjj"); | ||
}); | ||
|
||
it("should trim multiple characters as a whole", async () => { | ||
must(trim("abopopab", "ab")).equal("opop"); | ||
must(trim("atitleb", "ab")).equal("atitleb"); | ||
must(trim("abtitleab", "ab")).equal("title"); | ||
}); | ||
|
||
it("can trim into empty string", async () => { | ||
must(trim("abc", "abc")).equal(""); | ||
must(trim("a", "a")).equal(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { trimStart } from "./trimStart.js"; | ||
import { trimEnd } from "./trimEnd.js"; | ||
|
||
/** | ||
* Removes given characters from both sides of the string. | ||
* If you want to remove from one side of the string see {@link trimStart} and {@link trimEnd}. | ||
* | ||
* @param source - Source string. | ||
* @param characters - Characters to remove, taken as a whole. | ||
* | ||
* @example | ||
* trim("abcb", "ab"); // "cb" | ||
* trim("!aaa!", "!"); // "aaa" | ||
* trim("!aaa!!!", "!"); // "aaa" | ||
* trim("!aaa!!!", "!!!"); // "!aaa" | ||
*/ | ||
const trim = (source: string, characters: string) => { | ||
return trimStart(trimEnd(source, characters), characters); | ||
}; | ||
|
||
export { | ||
trim, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { trimEnd } from "./trimEnd"; | ||
|
||
describe("trimEnd", () => { | ||
it("should trim single character from the end of the string", async () => { | ||
must(trimEnd("abc?", "?")).equal("abc"); | ||
must(trimEnd("a? b?c???", "?")).equal("a? b?c"); | ||
}); | ||
|
||
it("should trim multiple characters as a whole", async () => { | ||
must(trimEnd("abc", "bc")).equal("a"); | ||
|
||
must(trimEnd("abcc", "bc")).equal("abcc"); | ||
must(trimEnd("abcb", "bc")).equal("abcb"); | ||
}); | ||
|
||
it("can trim into empty string", async () => { | ||
must(trimEnd("abc", "abc")).equal(""); | ||
must(trimEnd("a", "a")).equal(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Removes given characters from the end of the string. | ||
* See also: {@link trim} and {@link trimStart}. | ||
* | ||
* @param source - Source string. | ||
* @param characters - Characters to remove, taken as a whole. | ||
* | ||
* @example | ||
* trimEnd("abcxzyz", "yz"); // "abcxz" | ||
* trimEnd("!aaa!!", "!"); // "!aaa" | ||
*/ | ||
const trimEnd = (source: string, characters: string) => { | ||
let s = source; | ||
while (s.endsWith(characters)) { | ||
s = s.slice(0, -characters.length); | ||
} | ||
return s; | ||
}; | ||
|
||
export { | ||
trimEnd, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { trimStart } from "./trimStart"; | ||
|
||
describe("trimStart", () => { | ||
it("should trim single character from the start of the string", async () => { | ||
must(trimStart("?abc", "?")).equal("abc"); | ||
must(trimStart("aaabc", "a")).equal("bc"); | ||
}); | ||
|
||
it("should trim multiple characters as a whole", async () => { | ||
must(trimStart("abc", "ab")).equal("c"); | ||
|
||
must(trimStart("aabcc", "ab")).equal("aabcc"); | ||
must(trimStart("babcb", "ab")).equal("babcb"); | ||
}); | ||
|
||
it("should trim into empty string if needed", async () => { | ||
must(trimStart("abc", "abc")).equal(""); | ||
must(trimStart("aaa", "a")).equal(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Removes given characters from the start of the string. | ||
* See also: {@link trim} and {@link trimEnd}. | ||
* | ||
* @param source - Source string. | ||
* @param characters - Characters to remove, taken as a whole. | ||
* | ||
* @example | ||
* trimStart("abbcb", "ab"); // "bcb" | ||
* trimStart("!!aaa!", "!"); // "aaa!" | ||
*/ | ||
const trimStart = (source: string, characters: string) => { | ||
let s = source; | ||
while (s.startsWith(characters)) { | ||
s = s.slice(characters.length); | ||
} | ||
return s; | ||
}; | ||
|
||
export { | ||
trimStart, | ||
}; |