Skip to content

Commit eec5777

Browse files
authored
Merge pull request #31 from benjGam/develop
Release 2.5.0
2 parents 459f785 + 7c6d1b7 commit eec5777

File tree

8 files changed

+127
-3
lines changed

8 files changed

+127
-3
lines changed

.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
node_modules/
22
package-lock.json
33
src/
4+
.github/
5+
coverage/
6+
test/
47
tsconfig.json

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
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+
115
# Version 2.4.0
216

317
- `Added`:

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "string-utils-ts",
3-
"version": "2.4.0",
3+
"version": "2.5.0",
44
"description": "Provide some useful functions for strings",
55
"main": "./lib",
66
"scripts": {

src/main.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class StringUtils {
8989
* str: 'Test'
9090
* index: '2'
9191
* toReplaceWith: 'hello'
92-
* returns: 'Tehellost'
92+
* returns: 'Tehellot'
9393
*/
9494
public static replaceAt(
9595
str: string,
@@ -162,4 +162,38 @@ export class StringUtils {
162162

163163
return revelantSubSequences;
164164
}
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+
}
165199
}

src/word/utils.ts

+24
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,28 @@ export default class StringUtilsWord {
227227
.map((subSequence) => this.formatWord(subSequence))
228228
.join(' ');
229229
}
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+
}
230254
}

test/main.spec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,38 @@ runner.runBasicTests(
7171
[' '.split(' '), false],
7272
]),
7373
);
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+
);

test/test.utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class JestRunner {
1717
this.checkInvokation(fn);
1818

1919
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}''`, () => {
2121
(inputPropertiesToTestName && output
2222
? expect(
2323
this._classToInvoke[fn.name](

test/words.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,18 @@ describe('Normalization of stuffs', () => {
143143
['s', ''],
144144
]),
145145
);
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+
);
146160
});

0 commit comments

Comments
 (0)