generated from TypeScriptPlayground/Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
201 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts"; | ||
import trimSequence from './trim_sequence.ts'; | ||
|
||
Deno.test( | ||
'Trims a sequence from a string.', | ||
async (test) => { | ||
await test.step({ | ||
name: 'Empty input string', | ||
fn: () => { | ||
assertEquals( | ||
trimSequence('', '123'), | ||
'' | ||
) | ||
} | ||
}) | ||
await test.step({ | ||
name: 'Empty sequence', | ||
fn: () => { | ||
assertEquals( | ||
trimSequence('321abc123', ''), | ||
'321abc123' | ||
) | ||
} | ||
}) | ||
|
||
await test.step({ | ||
name: 'Normal input string', | ||
fn: () => { | ||
assertEquals( | ||
trimSequence('321abc123', '123'), | ||
'321abc' | ||
) | ||
} | ||
}) | ||
|
||
await test.step({ | ||
name: 'Normal input string reversed', | ||
fn: () => { | ||
assertEquals( | ||
trimSequence('321abc123', '321', true), | ||
'abc' | ||
) | ||
} | ||
}) | ||
} | ||
) |
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,27 @@ | ||
import reverseString from './reverse_string.ts'; | ||
import trimSequenceEnd from "./trim_sequence_end.ts"; | ||
import trimSequenceStart from "./trim_sequence_start.ts"; | ||
|
||
/** | ||
* This function searches for the `sequence` at the start and end of the `inputString` and trims it. | ||
* | ||
* @example | ||
* ``` | ||
* console.log(trimSequence('321abc123', '123')) | ||
* // 321abc | ||
* | ||
* console.log(trimSequence('321abc123', '123', true)) | ||
* // abc | ||
* ``` | ||
* @param inputString The input string to trim the start and end | ||
* @param characters The characters to trim | ||
* @param [reverse=false] If the end sequence to search should be reversed | ||
* @returns The trimmed input string | ||
*/ | ||
export default function trimSequence( | ||
inputString : string, | ||
sequence : string, | ||
reverse : boolean = false | ||
) : string { | ||
return trimSequenceStart(trimSequenceEnd(inputString, reverse ? reverseString(sequence) : sequence), sequence); | ||
} |
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,36 @@ | ||
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts"; | ||
import trimSequenceEnd from './trim_sequence_end.ts'; | ||
|
||
Deno.test( | ||
'Trims a sequence at the end of a string.', | ||
async (test) => { | ||
await test.step({ | ||
name: 'Empty input string', | ||
fn: () => { | ||
assertEquals( | ||
trimSequenceEnd('', '123'), | ||
'' | ||
) | ||
} | ||
}) | ||
await test.step({ | ||
name: 'Empty sequence', | ||
fn: () => { | ||
assertEquals( | ||
trimSequenceEnd('321abc123', ''), | ||
'321abc123' | ||
) | ||
} | ||
}) | ||
|
||
await test.step({ | ||
name: 'Normal input string', | ||
fn: () => { | ||
assertEquals( | ||
trimSequenceEnd('321abc321', '321'), | ||
'321abc' | ||
) | ||
} | ||
}) | ||
} | ||
) |
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,28 @@ | ||
/** | ||
* This function searches for the `sequence` at the end of the `inputString` and trims it. | ||
* | ||
* @example | ||
* ``` | ||
* console.log(trimSequenceEnd('1abc123', 'c123')) | ||
* // 1ab | ||
* ``` | ||
* @param inputString The input string to trim the end | ||
* @param sequence The sequence to trim | ||
* @returns The trimmed input string | ||
*/ | ||
export default function trimSequenceEnd( | ||
inputString : string, | ||
sequence : string | ||
) : string { | ||
if (!sequence || !sequence) { | ||
return inputString; | ||
} | ||
|
||
const sequenceLength = sequence.length; | ||
|
||
if (inputString.endsWith(sequence)) { | ||
return inputString.slice(0, -sequenceLength); | ||
} | ||
|
||
return inputString; | ||
} |
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,36 @@ | ||
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts"; | ||
import trimSequenceStart from './trim_sequence_start.ts'; | ||
|
||
Deno.test( | ||
'Trims a sequence at the start of a string.', | ||
async (test) => { | ||
await test.step({ | ||
name: 'Empty input string', | ||
fn: () => { | ||
assertEquals( | ||
trimSequenceStart('', '123'), | ||
'' | ||
) | ||
} | ||
}) | ||
await test.step({ | ||
name: 'Empty sequence', | ||
fn: () => { | ||
assertEquals( | ||
trimSequenceStart('321abc123', ''), | ||
'321abc123' | ||
) | ||
} | ||
}) | ||
|
||
await test.step({ | ||
name: 'Normal input string', | ||
fn: () => { | ||
assertEquals( | ||
trimSequenceStart('321abc321', '321'), | ||
'abc321' | ||
) | ||
} | ||
}) | ||
} | ||
) |
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,28 @@ | ||
/** | ||
* This function searches for the `sequence` at the start of the `inputString` and trims it. | ||
* | ||
* @example | ||
* ``` | ||
* console.log(trimSequenceStart('1abc123', '1abc')) | ||
* // 123 | ||
* ``` | ||
* @param inputString The input string to trim the start | ||
* @param sequence The sequence to trim | ||
* @returns The trimmed input string | ||
*/ | ||
export default function trimSequenceStart( | ||
inputString : string, | ||
sequence : string | ||
) : string { | ||
if (!sequence || !sequence) { | ||
return inputString; | ||
} | ||
|
||
const sequenceLength = sequence.length; | ||
|
||
if (inputString.startsWith(sequence)) { | ||
return inputString.slice(sequenceLength); | ||
} | ||
|
||
return inputString; | ||
} |