-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmod.ts
40 lines (32 loc) · 800 Bytes
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
export interface PalindromeOptions {
text: string;
caseSensitive?: boolean;
}
export interface PalindromeResult {
result: boolean;
reversed: string;
}
export function palindrome(options: PalindromeOptions | string): boolean;
export function palindrome(
options: PalindromeOptions | string,
verbose: boolean,
): PalindromeResult;
export function palindrome(
options: PalindromeOptions | string,
verbose = false,
) {
if (typeof options === "string") {
options = { text: options };
}
const { caseSensitive = true } = options;
let { text } = options;
if (!caseSensitive) {
text = text.toLowerCase();
}
const reversed = [...text].reverse().join("");
const result = text === reversed;
if (verbose) {
return { reversed, result };
}
return result;
}