File tree 6 files changed +60
-3
lines changed
6 files changed +60
-3
lines changed Original file line number Diff line number Diff line change @@ -26,13 +26,16 @@ For the majority of the remaining Str documentation
26
26
[ contains] ( #method-contains )
27
27
[ ucFirst] ( #method-ucFirst )
28
28
[ lower] ( #method-lower )
29
+ [ palindrome] ( #method-palindrome )
29
30
30
31
#### <a name =" method-contains " ></a > ` contains() `
31
32
32
33
#### <a name =" method-ucFirst " ></a > ` ucFirst() `
33
34
34
35
#### <a name =" method-lower " ></a > ` lower() `
35
36
37
+ #### <a name =" method-palindrome " ></a > ` palindrome() `
38
+
36
39
## <a name =" arr " ></a > Array - Arr
37
40
38
41
### <a name =" aam " ></a > Available methods
Original file line number Diff line number Diff line change
1
+ export * from './str.exception' ;
Original file line number Diff line number Diff line change
1
+ export class StrException extends Error {
2
+ name : 'StrException' ;
3
+
4
+ constructor ( message : string ) {
5
+ super ( message ) ;
6
+ }
7
+ }
Original file line number Diff line number Diff line change @@ -2,4 +2,5 @@ export interface IString {
2
2
ucFirst ( value : string ) : string ;
3
3
contains ( value : string , needles : string | string [ ] ) : boolean ;
4
4
lower ( value : string ) : string ;
5
+ palindrome ( value : string ) : boolean ;
5
6
}
Original file line number Diff line number Diff line change 1
1
import { Str } from './string' ;
2
+ import { StrException } from '../exceptions' ;
2
3
3
4
describe ( 'Str' , ( ) => {
4
5
let str = new Str ( ) ;
@@ -60,4 +61,33 @@ describe('Str', () => {
60
61
expect ( str . lower ( 'Hello World!' ) ) . toEqual ( 'hello world!' ) ;
61
62
} ) ;
62
63
} ) ;
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
+ } ) ;
63
93
} ) ;
Original file line number Diff line number Diff line change 1
1
import { IString } from './string.interface' ;
2
+ import { StrException } from '../exceptions' ;
2
3
3
4
export class Str implements IString {
4
5
/**
@@ -13,8 +14,8 @@ export class Str implements IString {
13
14
14
15
/**
15
16
* Determine if a given string contains a given substring.
16
- * @param value
17
- * @param needles
17
+ * @param { string } value
18
+ * @param { string } needles
18
19
* @return boolean
19
20
*/
20
21
contains ( value : string , needles : string | string [ ] ) : boolean {
@@ -23,9 +24,23 @@ export class Str implements IString {
23
24
24
25
/**
25
26
* Convert the given string to lower-case.
26
- * @param value
27
+ * @param {string } value
28
+ * @return string
27
29
*/
28
30
lower ( value : string ) : string {
29
31
return value . toLowerCase ( ) ;
30
32
}
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
+ }
31
46
}
You can’t perform that action at this time.
0 commit comments