1
1
const stringClassExtensions = {
2
2
3
3
/**
4
- * Returns true if the string contains vowels.
5
- * @returns {Boolean }
4
+ * hasVowels
5
+ * Returns true if the string contains vowels or false if it does not
6
+ * @returns {Boolean } true or false
6
7
*/
7
8
hasVowels ( ) {
8
9
const vowelRegex = / [ a e i o u ] / i;
9
10
return vowelRegex . test ( this ) ;
10
11
} ,
11
12
12
13
/**
13
- * Returns the String in question but with all characters in upper cases as applicable.
14
- * @returns {String }
14
+ * toUpper
15
+ * Returns the String in question but with
16
+ * all characters in upper cases as applicable
17
+ * @returns {String } string with all characters in upper case
15
18
*/
16
19
toUpper ( ) {
17
20
return this . replace ( / [ a - z ] / g, match => String
18
21
. fromCharCode ( match . charCodeAt ( 0 ) - 32 ) ) ;
19
22
} ,
20
23
21
24
/**
22
- * Returns the String in question but with all characters in their lower cases as applicable.
23
- * @returns {String }
25
+ * toLower
26
+ * Returns the string in question but with all
27
+ * characters in their lower cases as applicable
28
+ * @returns {String } string with all characters to lower case
24
29
*/
25
30
toLower ( ) {
26
31
return this . replace ( / [ A - Z ] / g, match => String
27
32
. fromCharCode ( match . charCodeAt ( 0 ) + 32 ) ) ;
28
33
} ,
29
34
30
35
/**
31
- * Returns the String in question but changes the First Character to an Upper case.
32
- * @returns {String }
36
+ * ucFirst
37
+ * Returns the string in question but changes the first character
38
+ * to an Upper case.
39
+ * @returns {String } string with capitalized first letter
33
40
*/
34
41
ucFirst ( ) {
35
42
return this . replace ( this [ 0 ] , this [ 0 ] . toUpper ( ) ) ;
36
43
} ,
37
44
38
45
/**
46
+ * isQuestion
39
47
* Returns true if the string is a question (ending with a question mark)
40
- * @returns {Boolean }
48
+ * @returns {Boolean } true or false
41
49
*/
42
50
isQuestion ( ) {
43
51
const regexType = / \? $ / g;
44
52
return regexType . test ( this ) ;
45
53
} ,
46
54
47
55
/**
48
- * Returns a list of the words in the string, as an Array.
49
- * @returns {Array }
56
+ * words
57
+ * Returns a list of the words in the string, as an Array
58
+ * @returns {Array } array of words in the string
50
59
*/
51
60
words ( ) {
52
61
return this . replace ( / [ ^ a - z 0 - 9 A - Z \s ] / g, '' ) . split ( / \s + / ) ;
53
62
} ,
54
63
55
64
/**
65
+ * wordCount
56
66
* Returns the number of words in the string
57
- * @returns {Number }
67
+ * @returns {Number } number of words in the string
58
68
*/
59
69
wordCount ( ) {
60
70
return this . words ( ) . length ;
61
71
} ,
62
72
63
73
/**
74
+ * toCurrency
64
75
* Returns a currency representation of the String
65
- * @returns {String }
76
+ * @returns {String } string of numbers
66
77
*/
67
78
toCurrency ( ) {
79
+ if ( ! Number ( this ) ) {
80
+ return 'This is not a Number' ;
81
+ }
68
82
let [ number , decimal ] = this . split ( / \. / g) ;
69
83
if ( decimal === undefined ) {
70
84
decimal = '00' ;
@@ -76,7 +90,9 @@ const stringClassExtensions = {
76
90
} ,
77
91
78
92
/**
93
+ * fromCurrency
79
94
* Returns a number representation of the Currency String
95
+ * @returns {String } string of numbers
80
96
*/
81
97
fromCurrency ( ) {
82
98
let [ number , decimal ] = this . split ( / \. / g) ;
@@ -90,26 +106,29 @@ const stringClassExtensions = {
90
106
} ,
91
107
92
108
/**
109
+ * inverseCase
93
110
* Returns each letter in the string as an inverse of its current case
94
- * @returns {String }
111
+ * @returns {String } inverted form of the string
95
112
*/
96
113
inverseCase ( ) {
97
114
return this . replace ( / \w / g, character => ( / [ a - z ] /
98
115
. test ( character ) ? character . toUpper ( ) : character . toLower ( ) ) ) ;
99
116
} ,
100
117
101
118
/**
119
+ * alternatingCase
102
120
* Returns the letters in alternating cases. It must start with a lower case
103
- * @returns {String }
121
+ * @returns {String } string in alternating cases starting with lower case
104
122
*/
105
123
alternatingCase ( ) {
106
124
return this . replace ( / \w / g, ( match , count ) =>
107
125
( count % 2 === 0 ? match . toLower ( ) : match . toUpper ( ) ) ) ;
108
126
} ,
109
127
110
128
/**
129
+ * getMiddle
111
130
* Returns the character(s) in the middle of the string
112
- * @returns {String }
131
+ * @returns {String } character(s) in the middle of the string
113
132
*/
114
133
getMiddle ( ) {
115
134
const middlePosition = this . length / 2 ;
@@ -118,26 +137,33 @@ const stringClassExtensions = {
118
137
} ,
119
138
120
139
/**
121
- * Returns true if a string contains double characters(including whitespace character)
122
- * @return {Boolean }
140
+ * doubleCheck
141
+ * Returns true if a string contains double characters
142
+ * (including whitespace character)
143
+ * @return {Boolean } true or false
123
144
*/
124
145
doubleCheck ( ) {
125
146
return / ( .) \1{ 1 } / . test ( this ) ;
126
147
} ,
127
148
128
149
/**
150
+ * isDigit
129
151
* Test if string is single digit
130
- * @return {Boolean }
152
+ * @return {Boolean } true or false
131
153
*/
132
154
isDigit ( ) {
133
155
return / ^ \d { 1 } $ / g. test ( this ) ;
134
156
} ,
135
157
136
158
/**
159
+ * numberWords
137
160
* Returns the numbers in words e.g 325 should return three two five.
138
- * @returns {String }
161
+ * @returns {String } string form of numbers
139
162
*/
140
163
numberWords ( ) {
164
+ if ( ! Number ( this ) ) {
165
+ return 'This is not a Number' ;
166
+ }
141
167
const digitWordsMap = {
142
168
0 : 'zero' ,
143
169
1 : 'one' ,
0 commit comments