Skip to content

Commit 745b18b

Browse files
committed
Added: reverse string
1 parent aca7209 commit 745b18b

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/format/reverse_string.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { assertEquals } from 'https://deno.land/std@0.203.0/assert/assert_equals.ts';
2+
import reverseString from './reverse_string.ts';
3+
4+
Deno.test(
5+
'Reverse a string.',
6+
async (test) => {
7+
await test.step({
8+
name: 'Empty string',
9+
fn: () => {
10+
assertEquals(
11+
reverseString(''),
12+
'',
13+
);
14+
},
15+
});
16+
17+
await test.step({
18+
name: 'Normal string',
19+
fn: () => {
20+
assertEquals(
21+
reverseString('abc'),
22+
'cba',
23+
);
24+
},
25+
});
26+
},
27+
);

src/format/reverse_string.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* This function reverses a string.
3+
*
4+
* @param inputString The string to reverse
5+
* @returns The reversed string
6+
*/
7+
export default function reverseString(inputString : string) : string {
8+
if (!inputString || inputString.length === 1) {
9+
return inputString;
10+
}
11+
return inputString.split('').reverse().join('');
12+
}

0 commit comments

Comments
 (0)