Skip to content

Commit d9a96bd

Browse files
authored
feat(): add new method palindrome (#16)
1 parent 192e487 commit d9a96bd

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ For the majority of the remaining Str documentation
2626
[contains](#method-contains)
2727
[ucFirst](#method-ucFirst)
2828
[lower](#method-lower)
29+
[palindrome](#method-palindrome)
2930

3031
#### <a name="method-contains"></a> `contains()`
3132

3233
#### <a name="method-ucFirst"></a> `ucFirst()`
3334

3435
#### <a name="method-lower"></a> `lower()`
3536

37+
#### <a name="method-palindrome"></a> `palindrome()`
38+
3639
## <a name="arr"></a> Array - Arr
3740

3841
### <a name="aam"></a> Available methods

lib/exceptions/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './str.exception';

lib/exceptions/str.exception.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class StrException extends Error {
2+
name: 'StrException';
3+
4+
constructor(message: string) {
5+
super(message);
6+
}
7+
}

lib/str/string.interface.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export interface IString {
22
ucFirst(value: string): string;
33
contains(value: string, needles: string | string[]): boolean;
44
lower(value: string): string;
5+
palindrome(value: string): boolean;
56
}

lib/str/string.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Str } from './string';
2+
import { StrException } from '../exceptions';
23

34
describe('Str', () => {
45
let str = new Str();
@@ -60,4 +61,33 @@ describe('Str', () => {
6061
expect(str.lower('Hello World!')).toEqual('hello world!');
6162
});
6263
});
64+
65+
describe('Property palindrome', () => {
66+
it('should be have property palindrome', () => {
67+
expect(str).toHaveProperty('palindrome');
68+
});
69+
70+
it('string is palindrome', () => {
71+
expect(str.palindrome('kayak')).toEqual(true);
72+
expect(str.palindrome('Kayak')).toEqual(true);
73+
expect(str.palindrome('KaYak')).toEqual(true);
74+
expect(str.palindrome('kayaK')).toEqual(true);
75+
expect(str.palindrome('KayaK')).toEqual(true);
76+
expect(str.palindrome('KaYaK')).toEqual(true);
77+
});
78+
79+
it('string is not palindrome', () => {
80+
expect(str.palindrome('Hello')).toEqual(false);
81+
});
82+
83+
it('palindrome method return exception if value is more 1 word', () => {
84+
expect(() => {
85+
str.palindrome('Hello world');
86+
}).toThrow('please define a single word');
87+
88+
expect(() => {
89+
str.palindrome('Hello world');
90+
}).toThrowError(StrException);
91+
});
92+
});
6393
});

lib/str/string.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { IString } from './string.interface';
2+
import { StrException } from '../exceptions';
23

34
export class Str implements IString {
45
/**
@@ -13,8 +14,8 @@ export class Str implements IString {
1314

1415
/**
1516
* Determine if a given string contains a given substring.
16-
* @param value
17-
* @param needles
17+
* @param {string} value
18+
* @param {string} needles
1819
* @return boolean
1920
*/
2021
contains(value: string, needles: string | string[]): boolean {
@@ -23,9 +24,23 @@ export class Str implements IString {
2324

2425
/**
2526
* Convert the given string to lower-case.
26-
* @param value
27+
* @param {string} value
28+
* @return string
2729
*/
2830
lower(value: string): string {
2931
return value.toLowerCase();
3032
}
33+
34+
/**
35+
* Check string is palindrome.
36+
* @param {string} value
37+
* @return boolean
38+
*/
39+
palindrome(value: string): boolean {
40+
if (value.match(/[\W_]/g)) {
41+
throw new StrException(`please define a single word`);
42+
}
43+
44+
return value.toLowerCase() === value.toLowerCase().split('').reverse().join('');
45+
}
3146
}

0 commit comments

Comments
 (0)