Skip to content

Commit

Permalink
Added: trim sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqxx committed Feb 2, 2024
1 parent 745b18b commit a3af935
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/format/trim_sequence.test.ts
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'
)
}
})
}
)
27 changes: 27 additions & 0 deletions src/format/trim_sequence.ts
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);
}
36 changes: 36 additions & 0 deletions src/format/trim_sequence_end.test.ts
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'
)
}
})
}
)
28 changes: 28 additions & 0 deletions src/format/trim_sequence_end.ts
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;
}
36 changes: 36 additions & 0 deletions src/format/trim_sequence_start.test.ts
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'
)
}
})
}
)
28 changes: 28 additions & 0 deletions src/format/trim_sequence_start.ts
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;
}

0 comments on commit a3af935

Please sign in to comment.