|
1 | | -import { StringUtils } from './main'; |
2 | | - |
3 | | -/** |
4 | | - * This interface provide a structure to register word endings forms |
5 | | - * |
6 | | - * @interface IWordEnding |
7 | | - * @field {string} pluralForm is used to store a plural form of ending |
8 | | - * @field {string} singularForm is used to store the singular form of the plural form ending |
9 | | - * |
10 | | - * @example |
11 | | - * pluralForm: 'ies' |
12 | | - * singularForm: 'y' |
13 | | - */ |
14 | | -export interface IWordEnding { |
15 | | - pluralForm: string; |
16 | | - singularForm: string; |
17 | | -} |
| 1 | +import { StringUtils } from '../main'; |
| 2 | +import { WordEnding } from './word-ending'; |
18 | 3 |
|
19 | 4 | /** |
20 | 5 | * This object is used to list plural and singular forms |
21 | 6 | * of words. |
22 | 7 | */ |
23 | | -const wordEndings: IWordEnding[] = [ |
24 | | - { |
25 | | - pluralForm: 'sses', |
26 | | - singularForm: 'ss', |
27 | | - }, |
28 | | - { |
29 | | - pluralForm: 'ies', |
30 | | - singularForm: 'y', |
31 | | - }, |
32 | | - { |
33 | | - pluralForm: 'es', |
34 | | - singularForm: 'e', |
35 | | - }, |
36 | | - { |
37 | | - pluralForm: 's', |
38 | | - singularForm: '', |
39 | | - }, |
| 8 | +const wordEndings: WordEnding[] = [ |
| 9 | + new WordEnding('sses', 'ss'), |
| 10 | + new WordEnding('ies', 'y'), |
| 11 | + new WordEnding('es', 'e'), |
| 12 | + new WordEnding('s', ''), |
40 | 13 | ]; |
41 | 14 |
|
42 | 15 | /** |
@@ -79,13 +52,47 @@ export default class StringUtilsWord { |
79 | 52 | * If you just want to do some word operation, prefer |
80 | 53 | * @method getWordEnding |
81 | 54 | */ |
82 | | - public static getCorrespondingEnding(word: string): IWordEnding { |
| 55 | + public static getCorrespondingEnding(word: string): WordEnding { |
83 | 56 | return wordEndings.find( |
84 | 57 | (ending) => |
85 | 58 | word.endsWith(ending.pluralForm) || word.endsWith(ending.singularForm), |
86 | 59 | ); |
87 | 60 | } |
88 | 61 |
|
| 62 | + /** |
| 63 | + * Returns the plural form of a singular one. |
| 64 | + * |
| 65 | + * @param {string} str - Should be a singular form |
| 66 | + * |
| 67 | + * @example |
| 68 | + * str: 'y' |
| 69 | + * returns: 'ies' |
| 70 | + * |
| 71 | + * @example |
| 72 | + * str: 'ss' |
| 73 | + * returns: 'sses' |
| 74 | + */ |
| 75 | + public static getPluralOf(str: string): string { |
| 76 | + return this.getCorrespondingEnding(str).pluralForm; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the singular form of a plural one. |
| 81 | + * |
| 82 | + * @param {string} str - Should be a plural form |
| 83 | + * |
| 84 | + * @example |
| 85 | + * str: 'ies' |
| 86 | + * returns: 'y' |
| 87 | + * |
| 88 | + * @example |
| 89 | + * str: 'sses' |
| 90 | + * returns: 'ss' |
| 91 | + */ |
| 92 | + public static getSingularOf(str: string): string { |
| 93 | + return this.getCorrespondingEnding(str).singularForm; |
| 94 | + } |
| 95 | + |
89 | 96 | /** |
90 | 97 | * Check the ending form of a word and return a boolean |
91 | 98 | * |
|
0 commit comments