File tree 8 files changed +127
-3
lines changed
8 files changed +127
-3
lines changed Original file line number Diff line number Diff line change 1
1
node_modules /
2
2
package-lock.json
3
3
src /
4
+ .github /
5
+ coverage /
6
+ test /
4
7
tsconfig.json
Original file line number Diff line number Diff line change
1
+ # Version 2.5.0 [ ON-GOING]
2
+
3
+ - ` Added ` :
4
+ - ` getFirstCharIndex(string: string): number ` method has been implemented.
5
+ - ` normalizeSentence(sentence: string): string ` method has been implemented, this method is used to upper the first word char & lower the rest of them & append a dot at the end of it.
6
+ - ` getFirstCharIndex(string: string) ` method has been implemented.
7
+ - ` getFirstEndingPunctuationIndex(string: string): number ` method has been implemented.
8
+ - ` getIndexOfUnfullyDeterminated(string: string, toSearch: string | string[]): number ` method has been implemented to get the first index of a given set of (only one) char in a given string.
9
+
10
+ # Version 2.4.1
11
+
12
+ - ` Patch ` :
13
+ - Remove useless files from package (When ` npm publish ` is performed)
14
+
1
15
# Version 2.4.0
2
16
3
17
- ` Added ` :
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " string-utils-ts" ,
3
- "version" : " 2.4 .0" ,
3
+ "version" : " 2.5 .0" ,
4
4
"description" : " Provide some useful functions for strings" ,
5
5
"main" : " ./lib" ,
6
6
"scripts" : {
Original file line number Diff line number Diff line change @@ -89,7 +89,7 @@ export class StringUtils {
89
89
* str: 'Test'
90
90
* index: '2'
91
91
* toReplaceWith: 'hello'
92
- * returns: 'Tehellost '
92
+ * returns: 'Tehellot '
93
93
*/
94
94
public static replaceAt (
95
95
str : string ,
@@ -162,4 +162,38 @@ export class StringUtils {
162
162
163
163
return revelantSubSequences ;
164
164
}
165
+
166
+ /**
167
+ * Returns the index of the first char
168
+ *
169
+ *
170
+ * @param string - The string to search in
171
+ */
172
+ public static getFirstCharIndex ( string : string ) {
173
+ return this . getFirstIndexOfUnfullyDeterminated (
174
+ string . toLowerCase ( ) ,
175
+ 'abcdefghijklmnopqrstuvwxyz' ,
176
+ ) ;
177
+ }
178
+
179
+ /**
180
+ * Returns the index of the ending punctuation char index
181
+ *
182
+ * @param string - The string to search in
183
+ *
184
+ */
185
+ public static getFirstEndingPunctuationIndex ( string : string ) {
186
+ return this . getFirstIndexOfUnfullyDeterminated ( string , '?!;.' ) ;
187
+ }
188
+
189
+ /**
190
+ * Returns first index of a given set of char in a given string
191
+ *
192
+ */
193
+ public static getFirstIndexOfUnfullyDeterminated (
194
+ string : string ,
195
+ toSearch : string | string [ ] ,
196
+ ) : number {
197
+ return string . split ( '' ) . findIndex ( ( char ) => toSearch . includes ( char ) ) ;
198
+ }
165
199
}
Original file line number Diff line number Diff line change @@ -227,4 +227,28 @@ export default class StringUtilsWord {
227
227
. map ( ( subSequence ) => this . formatWord ( subSequence ) )
228
228
. join ( ' ' ) ;
229
229
}
230
+
231
+ /**
232
+ *
233
+ * Returns a dot at end of sentence & first word char uppered.
234
+ *
235
+ * @param sentence - A sentence in a string
236
+ *
237
+ * @example
238
+ * sentence: ' this is a sentence'
239
+ * returns: 'This is a sentence.'
240
+ */
241
+ public static normalizeSentence ( sentence : string ) : string {
242
+ const firstCharIndex = StringUtils . getFirstCharIndex ( sentence ) ;
243
+ if ( firstCharIndex == - 1 )
244
+ // There's no char in given string
245
+ return sentence ;
246
+ sentence = this . normalizeSpacesBetweenWords (
247
+ sentence . substring ( firstCharIndex , sentence . length ) ,
248
+ ) ;
249
+ const arrayOfSentence = sentence . split ( '' ) ;
250
+ sentence = sentence . toLowerCase ( ) ;
251
+ arrayOfSentence [ 0 ] = arrayOfSentence [ 0 ] . toUpperCase ( ) ;
252
+ return `${ arrayOfSentence . join ( '' ) } ${ StringUtils . getFirstEndingPunctuationIndex ( sentence ) == sentence . length - 1 ? '' : '.' } ` ;
253
+ }
230
254
}
Original file line number Diff line number Diff line change @@ -71,3 +71,38 @@ runner.runBasicTests(
71
71
[ ' ' . split ( ' ' ) , false ] ,
72
72
] ) ,
73
73
) ;
74
+
75
+ runner . runBasicTests (
76
+ StringUtils . getFirstCharIndex ,
77
+ new Map < string , number > ( [
78
+ [ ' this is a sentence' , 3 ] ,
79
+ [ 'this is a sentence' , 0 ] ,
80
+ [ '!!this' , 2 ] ,
81
+ [ '!!!' , - 1 ] ,
82
+ ] ) ,
83
+ ) ;
84
+
85
+ runner . runBasicTests (
86
+ StringUtils . getFirstEndingPunctuationIndex ,
87
+ new Map < string , number > ( [
88
+ [ '!' , 0 ] ,
89
+ [ 'This' , - 1 ] ,
90
+ [ 'This ?' , 5 ] ,
91
+ [ 'This !' , 5 ] ,
92
+ [ 'This.' , 4 ] ,
93
+ [ '!This' , 0 ] ,
94
+ [ 'This;' , 4 ] ,
95
+ [ '' , - 1 ] ,
96
+ ] ) ,
97
+ ) ;
98
+
99
+ runner . runBasicTests (
100
+ StringUtils . getFirstIndexOfUnfullyDeterminated ,
101
+ new Map < any , number > ( [
102
+ [ ( ) => [ 'test' , 'te' ] , 0 ] ,
103
+ [ ( ) => [ '' , '' ] , - 1 ] ,
104
+ [ ( ) => [ 'hello' , 'o' ] , 4 ] ,
105
+ [ ( ) => [ 'hey ' , ' ' ] , 3 ] ,
106
+ [ ( ) => [ 'bouh' , 'hello' ] , 1 ] ,
107
+ ] ) ,
108
+ ) ;
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ export default class JestRunner {
17
17
this . checkInvokation ( fn ) ;
18
18
19
19
for ( const [ input , output ] of expectedReturns . entries ( ) ) {
20
- test ( `[${ fn . name } ] Should return '${ output } for '${ input } ''` , ( ) => {
20
+ test ( `[${ fn . name } ] Should return '${ output } ' for '${ input } ''` , ( ) => {
21
21
( inputPropertiesToTestName && output
22
22
? expect (
23
23
this . _classToInvoke [ fn . name ] (
Original file line number Diff line number Diff line change @@ -143,4 +143,18 @@ describe('Normalization of stuffs', () => {
143
143
[ 's' , '' ] ,
144
144
] ) ,
145
145
) ;
146
+
147
+ runner . runBasicTests (
148
+ StringUtilsWord . normalizeSentence ,
149
+ new Map < string , string > ( [
150
+ [ ' this is a sentence' , 'This is a sentence.' ] ,
151
+ [ ' ' , ' ' ] ,
152
+ [ ' !!!!!! hello you' , 'Hello you.' ] ,
153
+ [ ' a !!!! story' , 'A !!!! story.' ] ,
154
+ [ ' This is a story.' , 'This is a story.' ] ,
155
+ [ 'This is a story !' , 'This is a story !' ] ,
156
+ [ 'Is this a story ?' , 'Is this a story ?' ] ,
157
+ [ 'Is this a story ? No' , 'Is this a story ? No.' ] ,
158
+ ] ) ,
159
+ ) ;
146
160
} ) ;
You can’t perform that action at this time.
0 commit comments