diff --git a/.npmignore b/.npmignore index c7ceb10..60b47a5 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,7 @@ node_modules/ package-lock.json src/ +.github/ +coverage/ +test/ tsconfig.json \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index ef07a89..9deefb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,17 @@ +# Version 2.5.0 [ON-GOING] + +- `Added`: + - `getFirstCharIndex(string: string): number` method has been implemented. + - `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. + - `getFirstCharIndex(string: string)` method has been implemented. + - `getFirstEndingPunctuationIndex(string: string): number` method has been implemented. + - `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. + +# Version 2.4.1 + +- `Patch`: + - Remove useless files from package (When `npm publish` is performed) + # Version 2.4.0 - `Added`: diff --git a/package.json b/package.json index 68593fc..b44becc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "string-utils-ts", - "version": "2.4.0", + "version": "2.5.0", "description": "Provide some useful functions for strings", "main": "./lib", "scripts": { diff --git a/src/main.ts b/src/main.ts index 06a5cd0..51f9ff1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -89,7 +89,7 @@ export class StringUtils { * str: 'Test' * index: '2' * toReplaceWith: 'hello' - * returns: 'Tehellost' + * returns: 'Tehellot' */ public static replaceAt( str: string, @@ -162,4 +162,38 @@ export class StringUtils { return revelantSubSequences; } + + /** + * Returns the index of the first char + * + * + * @param string - The string to search in + */ + public static getFirstCharIndex(string: string) { + return this.getFirstIndexOfUnfullyDeterminated( + string.toLowerCase(), + 'abcdefghijklmnopqrstuvwxyz', + ); + } + + /** + * Returns the index of the ending punctuation char index + * + * @param string - The string to search in + * + */ + public static getFirstEndingPunctuationIndex(string: string) { + return this.getFirstIndexOfUnfullyDeterminated(string, '?!;.'); + } + + /** + * Returns first index of a given set of char in a given string + * + */ + public static getFirstIndexOfUnfullyDeterminated( + string: string, + toSearch: string | string[], + ): number { + return string.split('').findIndex((char) => toSearch.includes(char)); + } } diff --git a/src/word/utils.ts b/src/word/utils.ts index 1d4a533..f54dffd 100644 --- a/src/word/utils.ts +++ b/src/word/utils.ts @@ -227,4 +227,28 @@ export default class StringUtilsWord { .map((subSequence) => this.formatWord(subSequence)) .join(' '); } + + /** + * + * Returns a dot at end of sentence & first word char uppered. + * + * @param sentence - A sentence in a string + * + * @example + * sentence: ' this is a sentence' + * returns: 'This is a sentence.' + */ + public static normalizeSentence(sentence: string): string { + const firstCharIndex = StringUtils.getFirstCharIndex(sentence); + if (firstCharIndex == -1) + // There's no char in given string + return sentence; + sentence = this.normalizeSpacesBetweenWords( + sentence.substring(firstCharIndex, sentence.length), + ); + const arrayOfSentence = sentence.split(''); + sentence = sentence.toLowerCase(); + arrayOfSentence[0] = arrayOfSentence[0].toUpperCase(); + return `${arrayOfSentence.join('')}${StringUtils.getFirstEndingPunctuationIndex(sentence) == sentence.length - 1 ? '' : '.'}`; + } } diff --git a/test/main.spec.ts b/test/main.spec.ts index 5570b47..99b356b 100644 --- a/test/main.spec.ts +++ b/test/main.spec.ts @@ -71,3 +71,38 @@ runner.runBasicTests( [' '.split(' '), false], ]), ); + +runner.runBasicTests( + StringUtils.getFirstCharIndex, + new Map([ + [' this is a sentence', 3], + ['this is a sentence', 0], + ['!!this', 2], + ['!!!', -1], + ]), +); + +runner.runBasicTests( + StringUtils.getFirstEndingPunctuationIndex, + new Map([ + ['!', 0], + ['This', -1], + ['This ?', 5], + ['This !', 5], + ['This.', 4], + ['!This', 0], + ['This;', 4], + ['', -1], + ]), +); + +runner.runBasicTests( + StringUtils.getFirstIndexOfUnfullyDeterminated, + new Map([ + [() => ['test', 'te'], 0], + [() => ['', ''], -1], + [() => ['hello', 'o'], 4], + [() => ['hey ', ' '], 3], + [() => ['bouh', 'hello'], 1], + ]), +); diff --git a/test/test.utils.ts b/test/test.utils.ts index 21935cb..9416cd1 100644 --- a/test/test.utils.ts +++ b/test/test.utils.ts @@ -17,7 +17,7 @@ export default class JestRunner { this.checkInvokation(fn); for (const [input, output] of expectedReturns.entries()) { - test(`[${fn.name}] Should return '${output} for '${input}''`, () => { + test(`[${fn.name}] Should return '${output}' for '${input}''`, () => { (inputPropertiesToTestName && output ? expect( this._classToInvoke[fn.name]( diff --git a/test/words.spec.ts b/test/words.spec.ts index e5b63e7..898c356 100644 --- a/test/words.spec.ts +++ b/test/words.spec.ts @@ -143,4 +143,18 @@ describe('Normalization of stuffs', () => { ['s', ''], ]), ); + + runner.runBasicTests( + StringUtilsWord.normalizeSentence, + new Map([ + [' this is a sentence', 'This is a sentence.'], + [' ', ' '], + [' !!!!!! hello you', 'Hello you.'], + [' a !!!! story', 'A !!!! story.'], + [' This is a story.', 'This is a story.'], + ['This is a story !', 'This is a story !'], + ['Is this a story ?', 'Is this a story ?'], + ['Is this a story ? No', 'Is this a story ? No.'], + ]), + ); });