File tree Expand file tree Collapse file tree 2 files changed +98
-0
lines changed Expand file tree Collapse file tree 2 files changed +98
-0
lines changed Original file line number Diff line number Diff line change 1+ import { assertEquals } from "https://deno.land/std@0.203.0/assert/assert_equals.ts" ;
2+ import trimCharactersStart from "./trim_characters_start.ts" ;
3+
4+ Deno . test (
5+ 'Trim characters at the start of a string.' ,
6+ async ( test ) => {
7+ await test . step ( {
8+ name : 'Empty input string, empty characters' ,
9+ fn : ( ) => {
10+ assertEquals (
11+ trimCharactersStart ( '' , '' ) ,
12+ ''
13+ )
14+ }
15+ } )
16+
17+ await test . step ( {
18+ name : 'Empty characters' ,
19+ fn : ( ) => {
20+ assertEquals (
21+ trimCharactersStart ( 'abc123' , '' ) ,
22+ 'abc123'
23+ )
24+ }
25+ } )
26+
27+ await test . step ( {
28+ name : 'Empty input string' ,
29+ fn : ( ) => {
30+ assertEquals (
31+ trimCharactersStart ( '' , 'abc' ) ,
32+ ''
33+ )
34+ }
35+ } )
36+
37+ await test . step ( {
38+ name : 'Fake Escaped characters in input string' ,
39+ fn : ( ) => {
40+ assertEquals (
41+ trimCharactersStart ( '\aabc123\a' , '\a' ) ,
42+ 'bc123\a'
43+ )
44+ }
45+ } )
46+
47+ await test . step ( {
48+ name : 'Real Escaped characters in input string' ,
49+ fn : ( ) => {
50+ assertEquals (
51+ trimCharactersStart ( '\ttabc123' , '\t' ) ,
52+ 'tabc123'
53+ )
54+ }
55+ } )
56+
57+ await test . step ( {
58+ name : 'Skip characters in input string' ,
59+ fn : ( ) => {
60+ assertEquals (
61+ trimCharactersStart ( 'abc123' , '1' ) ,
62+ 'abc123'
63+ )
64+ }
65+ } )
66+
67+ await test . step ( {
68+ name : 'Normal trim' ,
69+ fn : ( ) => {
70+ assertEquals (
71+ trimCharactersStart ( 'abc123' , 'cab23' ) ,
72+ '123'
73+ )
74+ }
75+ } )
76+ }
77+ )
Original file line number Diff line number Diff line change 1+ /**
2+ * This function is similar to the standard `String.prototype.trimStart()` function. With this function, several
3+ * characters can be specified as a string which are then trimmed at the start of the `inputString`.
4+ *
5+ * @param inputString The input string to trim the start
6+ * @param characters The characters to trim
7+ * @returns The trimmed input string
8+ */
9+ export default function trimCharactersStart (
10+ inputString : string ,
11+ characters : string
12+ ) : string {
13+ const charactersSet = new Set ( characters . split ( '' ) ) ;
14+ let startIndex = 0 ;
15+
16+ while ( startIndex < inputString . length && charactersSet . has ( inputString [ startIndex ] ) ) {
17+ startIndex ++ ;
18+ }
19+
20+ return inputString . substring ( startIndex ) ;
21+ }
You can’t perform that action at this time.
0 commit comments