diff --git a/CHANGELOG.md b/CHANGELOG.md index bde6c03..ef07a89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,15 @@ +# Version 2.4.0 + +- `Added`: + - `WordEnding` class has been implemented to replace `IWordEnding` interface. + - `getPluralOf(str: string): string`, `getSingularOf(str: string): string` methods has been implemented to get respectively `plural` & `singular` form of a given one. +- `Removed` [BREAKING CHANGES]: + - `IWordEnding` interface has been removed. + # Version 2.3.1 - `Fixes`: - - Export for cases component are now availables. + - Exports for cases component are now availables. # Version 2.3.0 @@ -49,7 +57,7 @@ - Added: - Unit testing is now part of this project. - TSDoc has been adopted and is now part of this project. - - Code quality components is now part of this project (ESLint & Prettier) + - Code quality components are now part of this project (ESLint & Prettier) - Performance: - Code has been reworked to improve performance (**A performance measurer should be implemented in next realases**) @@ -75,4 +83,4 @@ - `isPlural();` - [FIX] Bring a fix to case where provided words ended with `ss`, there's no confusion, method now manage those cases. - `isSingular();`, `pluralize();`, `singularize();` - moved from `StringUtils` to `StringUtilsWord` class. - `formatWord();` - has been reworked and now use `StringUtils.replaceAt();` method to optimize process. - - `normalizeSpacesBetweenWords();` - to be be rework in next release. + - `normalizeSpacesBetweenWords();` - to be rework in next release. diff --git a/package.json b/package.json index 774d33d..68593fc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "string-utils-ts", - "version": "2.3.1", + "version": "2.4.0", "description": "Provide some useful functions for strings", "main": "./lib", "scripts": { diff --git a/src/case/camel-case.ts b/src/case/camel-case.ts index 7120aaf..a84c329 100644 --- a/src/case/camel-case.ts +++ b/src/case/camel-case.ts @@ -1,6 +1,6 @@ import Case from './Case'; import StringUtilsCase from './utils'; -import StringUtilsWord from '../word'; +import StringUtilsWord from '../word/utils'; import { StringUtils } from '../main'; export default class CamelCase extends Case { diff --git a/src/case/pascal-case.ts b/src/case/pascal-case.ts index 505b830..dd241ac 100644 --- a/src/case/pascal-case.ts +++ b/src/case/pascal-case.ts @@ -1,5 +1,5 @@ import Case from './Case'; -import StringUtilsWord from '../word'; +import StringUtilsWord from '../word/utils'; import { StringUtils } from '../main'; export default class PascalCase extends Case { diff --git a/src/case/snake-case.ts b/src/case/snake-case.ts index e4b5985..7b0711a 100644 --- a/src/case/snake-case.ts +++ b/src/case/snake-case.ts @@ -1,6 +1,6 @@ import Case from './Case'; import StringUtilsCase from './utils'; -import StringUtilsWord from '../word'; +import StringUtilsWord from '../word/utils'; export default class SnakeCase extends Case { protected _matcher = /(\w+)_(\w+)/; diff --git a/src/index.ts b/src/index.ts index 884bd41..b3ecf58 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export * from './main'; -export * from './word'; +export * from './word/'; +export * from './case'; diff --git a/src/main.ts b/src/main.ts index a82ea74..06a5cd0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import StringUtilsWord from './word'; +import StringUtilsWord from './word/utils'; export class StringUtils { /** diff --git a/src/word/index.ts b/src/word/index.ts new file mode 100644 index 0000000..9968618 --- /dev/null +++ b/src/word/index.ts @@ -0,0 +1,2 @@ +export * from './word-ending'; +export * from './utils'; diff --git a/src/word.ts b/src/word/utils.ts similarity index 83% rename from src/word.ts rename to src/word/utils.ts index a3629c7..1d4a533 100644 --- a/src/word.ts +++ b/src/word/utils.ts @@ -1,42 +1,15 @@ -import { StringUtils } from './main'; - -/** - * This interface provide a structure to register word endings forms - * - * @interface IWordEnding - * @field {string} pluralForm is used to store a plural form of ending - * @field {string} singularForm is used to store the singular form of the plural form ending - * - * @example - * pluralForm: 'ies' - * singularForm: 'y' - */ -export interface IWordEnding { - pluralForm: string; - singularForm: string; -} +import { StringUtils } from '../main'; +import { WordEnding } from './word-ending'; /** * This object is used to list plural and singular forms * of words. */ -const wordEndings: IWordEnding[] = [ - { - pluralForm: 'sses', - singularForm: 'ss', - }, - { - pluralForm: 'ies', - singularForm: 'y', - }, - { - pluralForm: 'es', - singularForm: 'e', - }, - { - pluralForm: 's', - singularForm: '', - }, +const wordEndings: WordEnding[] = [ + new WordEnding('sses', 'ss'), + new WordEnding('ies', 'y'), + new WordEnding('es', 'e'), + new WordEnding('s', ''), ]; /** @@ -79,13 +52,47 @@ export default class StringUtilsWord { * If you just want to do some word operation, prefer * @method getWordEnding */ - public static getCorrespondingEnding(word: string): IWordEnding { + public static getCorrespondingEnding(word: string): WordEnding { return wordEndings.find( (ending) => word.endsWith(ending.pluralForm) || word.endsWith(ending.singularForm), ); } + /** + * Returns the plural form of a singular one. + * + * @param {string} str - Should be a singular form + * + * @example + * str: 'y' + * returns: 'ies' + * + * @example + * str: 'ss' + * returns: 'sses' + */ + public static getPluralOf(str: string): string { + return this.getCorrespondingEnding(str).pluralForm; + } + + /** + * Returns the singular form of a plural one. + * + * @param {string} str - Should be a plural form + * + * @example + * str: 'ies' + * returns: 'y' + * + * @example + * str: 'sses' + * returns: 'ss' + */ + public static getSingularOf(str: string): string { + return this.getCorrespondingEnding(str).singularForm; + } + /** * Check the ending form of a word and return a boolean * diff --git a/src/word/word-ending.ts b/src/word/word-ending.ts new file mode 100644 index 0000000..2af5e1f --- /dev/null +++ b/src/word/word-ending.ts @@ -0,0 +1,23 @@ +export class WordEnding { + private _pluralForm: string; + private _singularForm: string; + + constructor(pluralForm: string, singularForm: string) { + this._pluralForm = pluralForm; + this._singularForm = singularForm; + } + + /** + * Returns the plural of stored singular form + */ + public get pluralForm(): string { + return this._pluralForm; + } + + /** + * Returns the singular of stored plural form + */ + public get singularForm(): string { + return this._singularForm; + } +} diff --git a/test/words.spec.ts b/test/words.spec.ts index 2f1fbaa..e5b63e7 100644 --- a/test/words.spec.ts +++ b/test/words.spec.ts @@ -1,4 +1,5 @@ -import StringUtilsWord, { IWordEnding } from '../src/word'; +import StringUtilsWord from '../src/word/utils'; +import { WordEnding } from '../src/word/word-ending'; import JestRunner from './test.utils'; const runner = new JestRunner(StringUtilsWord); @@ -20,63 +21,15 @@ describe('Get word ending', () => { runner.runBasicTests( StringUtilsWord.getCorrespondingEnding, - new Map([ - [ - 'Passes', - { - pluralForm: 'sses', - singularForm: 'ss', - }, - ], - [ - 'Pass', - { - pluralForm: 'sses', - singularForm: 'ss', - }, - ], - [ - 'Categories', - { - pluralForm: 'ies', - singularForm: 'y', - }, - ], - [ - 'Category', - { - pluralForm: 'ies', - singularForm: 'y', - }, - ], - [ - 'Bees', - { - pluralForm: 'es', - singularForm: 'e', - }, - ], - [ - 'Bee', - { - pluralForm: 'es', - singularForm: 'e', - }, - ], - [ - 'Cars', - { - pluralForm: 's', - singularForm: '', - }, - ], - [ - 'Car', - { - pluralForm: 's', - singularForm: '', - }, - ], + new Map([ + ['Passes', new WordEnding('sses', 'ss')], + ['Pass', new WordEnding('sses', 'ss')], + ['Categories', new WordEnding('ies', 'y')], + ['Category', new WordEnding('ies', 'y')], + ['Bees', new WordEnding('es', 'e')], + ['Bee', new WordEnding('es', 'e')], + ['Cars', new WordEnding('s', '')], + ['Car', new WordEnding('s', '')], ]), ); }); @@ -170,4 +123,24 @@ describe('Normalization of stuffs', () => { [['This ', 'is ', 'my ', 'test'], 'This Is My Test'], ]), ); + + runner.runBasicTests( + StringUtilsWord.getPluralOf, + new Map([ + ['y', 'ies'], + ['ss', 'sses'], + ['e', 'es'], + ['', 's'], + ]), + ); + + runner.runBasicTests( + StringUtilsWord.getSingularOf, + new Map([ + ['ies', 'y'], + ['sses', 'ss'], + ['es', 'e'], + ['s', ''], + ]), + ); });