Skip to content

Commit a3af935

Browse files
committed
Added: trim sequence
1 parent 745b18b commit a3af935

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed

src/format/trim_sequence.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { assertEquals } from "https://deno.land/std@0.203.0/assert/assert_equals.ts";
2+
import trimSequence from './trim_sequence.ts';
3+
4+
Deno.test(
5+
'Trims a sequence from a string.',
6+
async (test) => {
7+
await test.step({
8+
name: 'Empty input string',
9+
fn: () => {
10+
assertEquals(
11+
trimSequence('', '123'),
12+
''
13+
)
14+
}
15+
})
16+
await test.step({
17+
name: 'Empty sequence',
18+
fn: () => {
19+
assertEquals(
20+
trimSequence('321abc123', ''),
21+
'321abc123'
22+
)
23+
}
24+
})
25+
26+
await test.step({
27+
name: 'Normal input string',
28+
fn: () => {
29+
assertEquals(
30+
trimSequence('321abc123', '123'),
31+
'321abc'
32+
)
33+
}
34+
})
35+
36+
await test.step({
37+
name: 'Normal input string reversed',
38+
fn: () => {
39+
assertEquals(
40+
trimSequence('321abc123', '321', true),
41+
'abc'
42+
)
43+
}
44+
})
45+
}
46+
)

src/format/trim_sequence.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import reverseString from './reverse_string.ts';
2+
import trimSequenceEnd from "./trim_sequence_end.ts";
3+
import trimSequenceStart from "./trim_sequence_start.ts";
4+
5+
/**
6+
* This function searches for the `sequence` at the start and end of the `inputString` and trims it.
7+
*
8+
* @example
9+
* ```
10+
* console.log(trimSequence('321abc123', '123'))
11+
* // 321abc
12+
*
13+
* console.log(trimSequence('321abc123', '123', true))
14+
* // abc
15+
* ```
16+
* @param inputString The input string to trim the start and end
17+
* @param characters The characters to trim
18+
* @param [reverse=false] If the end sequence to search should be reversed
19+
* @returns The trimmed input string
20+
*/
21+
export default function trimSequence(
22+
inputString : string,
23+
sequence : string,
24+
reverse : boolean = false
25+
) : string {
26+
return trimSequenceStart(trimSequenceEnd(inputString, reverse ? reverseString(sequence) : sequence), sequence);
27+
}

src/format/trim_sequence_end.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { assertEquals } from "https://deno.land/std@0.203.0/assert/assert_equals.ts";
2+
import trimSequenceEnd from './trim_sequence_end.ts';
3+
4+
Deno.test(
5+
'Trims a sequence at the end of a string.',
6+
async (test) => {
7+
await test.step({
8+
name: 'Empty input string',
9+
fn: () => {
10+
assertEquals(
11+
trimSequenceEnd('', '123'),
12+
''
13+
)
14+
}
15+
})
16+
await test.step({
17+
name: 'Empty sequence',
18+
fn: () => {
19+
assertEquals(
20+
trimSequenceEnd('321abc123', ''),
21+
'321abc123'
22+
)
23+
}
24+
})
25+
26+
await test.step({
27+
name: 'Normal input string',
28+
fn: () => {
29+
assertEquals(
30+
trimSequenceEnd('321abc321', '321'),
31+
'321abc'
32+
)
33+
}
34+
})
35+
}
36+
)

src/format/trim_sequence_end.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* This function searches for the `sequence` at the end of the `inputString` and trims it.
3+
*
4+
* @example
5+
* ```
6+
* console.log(trimSequenceEnd('1abc123', 'c123'))
7+
* // 1ab
8+
* ```
9+
* @param inputString The input string to trim the end
10+
* @param sequence The sequence to trim
11+
* @returns The trimmed input string
12+
*/
13+
export default function trimSequenceEnd(
14+
inputString : string,
15+
sequence : string
16+
) : string {
17+
if (!sequence || !sequence) {
18+
return inputString;
19+
}
20+
21+
const sequenceLength = sequence.length;
22+
23+
if (inputString.endsWith(sequence)) {
24+
return inputString.slice(0, -sequenceLength);
25+
}
26+
27+
return inputString;
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { assertEquals } from "https://deno.land/std@0.203.0/assert/assert_equals.ts";
2+
import trimSequenceStart from './trim_sequence_start.ts';
3+
4+
Deno.test(
5+
'Trims a sequence at the start of a string.',
6+
async (test) => {
7+
await test.step({
8+
name: 'Empty input string',
9+
fn: () => {
10+
assertEquals(
11+
trimSequenceStart('', '123'),
12+
''
13+
)
14+
}
15+
})
16+
await test.step({
17+
name: 'Empty sequence',
18+
fn: () => {
19+
assertEquals(
20+
trimSequenceStart('321abc123', ''),
21+
'321abc123'
22+
)
23+
}
24+
})
25+
26+
await test.step({
27+
name: 'Normal input string',
28+
fn: () => {
29+
assertEquals(
30+
trimSequenceStart('321abc321', '321'),
31+
'abc321'
32+
)
33+
}
34+
})
35+
}
36+
)

src/format/trim_sequence_start.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* This function searches for the `sequence` at the start of the `inputString` and trims it.
3+
*
4+
* @example
5+
* ```
6+
* console.log(trimSequenceStart('1abc123', '1abc'))
7+
* // 123
8+
* ```
9+
* @param inputString The input string to trim the start
10+
* @param sequence The sequence to trim
11+
* @returns The trimmed input string
12+
*/
13+
export default function trimSequenceStart(
14+
inputString : string,
15+
sequence : string
16+
) : string {
17+
if (!sequence || !sequence) {
18+
return inputString;
19+
}
20+
21+
const sequenceLength = sequence.length;
22+
23+
if (inputString.startsWith(sequence)) {
24+
return inputString.slice(sequenceLength);
25+
}
26+
27+
return inputString;
28+
}

0 commit comments

Comments
 (0)