Skip to content

Commit 459f785

Browse files
authored
Merge pull request #30 from benjGam/develop
Release 2.4.0
2 parents e91a23a + 9551653 commit 459f785

11 files changed

+116
-102
lines changed

CHANGELOG.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
# Version 2.4.0
2+
3+
- `Added`:
4+
- `WordEnding` class has been implemented to replace `IWordEnding` interface.
5+
- `getPluralOf(str: string): string`, `getSingularOf(str: string): string` methods has been implemented to get respectively `plural` & `singular` form of a given one.
6+
- `Removed` [BREAKING CHANGES]:
7+
- `IWordEnding` interface has been removed.
8+
19
# Version 2.3.1
210

311
- `Fixes`:
4-
- Export for cases component are now availables.
12+
- Exports for cases component are now availables.
513

614
# Version 2.3.0
715

@@ -49,7 +57,7 @@
4957
- Added:
5058
- Unit testing is now part of this project.
5159
- TSDoc has been adopted and is now part of this project.
52-
- Code quality components is now part of this project (ESLint & Prettier)
60+
- Code quality components are now part of this project (ESLint & Prettier)
5361
- Performance:
5462
- Code has been reworked to improve performance (**A performance measurer should be implemented in next realases**)
5563

@@ -75,4 +83,4 @@
7583
- `isPlural();` - [FIX] Bring a fix to case where provided words ended with `ss`, there's no confusion, method now manage those cases.
7684
- `isSingular();`, `pluralize();`, `singularize();` - moved from `StringUtils` to `StringUtilsWord` class.
7785
- `formatWord();` - has been reworked and now use `StringUtils.replaceAt();` method to optimize process.
78-
- `normalizeSpacesBetweenWords();` - to be be rework in next release.
86+
- `normalizeSpacesBetweenWords();` - to be rework in next release.

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.3.1",
3+
"version": "2.4.0",
44
"description": "Provide some useful functions for strings",
55
"main": "./lib",
66
"scripts": {

src/case/camel-case.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Case from './Case';
22
import StringUtilsCase from './utils';
3-
import StringUtilsWord from '../word';
3+
import StringUtilsWord from '../word/utils';
44
import { StringUtils } from '../main';
55

66
export default class CamelCase extends Case {

src/case/pascal-case.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Case from './Case';
2-
import StringUtilsWord from '../word';
2+
import StringUtilsWord from '../word/utils';
33
import { StringUtils } from '../main';
44

55
export default class PascalCase extends Case {

src/case/snake-case.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Case from './Case';
22
import StringUtilsCase from './utils';
3-
import StringUtilsWord from '../word';
3+
import StringUtilsWord from '../word/utils';
44

55
export default class SnakeCase extends Case {
66
protected _matcher = /(\w+)_(\w+)/;

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './main';
2-
export * from './word';
2+
export * from './word/';
3+
export * from './case';

src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import StringUtilsWord from './word';
1+
import StringUtilsWord from './word/utils';
22

33
export class StringUtils {
44
/**

src/word/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './word-ending';
2+
export * from './utils';

src/word.ts renamed to src/word/utils.ts

+42-35
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
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';
183

194
/**
205
* This object is used to list plural and singular forms
216
* of words.
227
*/
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', ''),
4013
];
4114

4215
/**
@@ -79,13 +52,47 @@ export default class StringUtilsWord {
7952
* If you just want to do some word operation, prefer
8053
* @method getWordEnding
8154
*/
82-
public static getCorrespondingEnding(word: string): IWordEnding {
55+
public static getCorrespondingEnding(word: string): WordEnding {
8356
return wordEndings.find(
8457
(ending) =>
8558
word.endsWith(ending.pluralForm) || word.endsWith(ending.singularForm),
8659
);
8760
}
8861

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+
8996
/**
9097
* Check the ending form of a word and return a boolean
9198
*

src/word/word-ending.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export class WordEnding {
2+
private _pluralForm: string;
3+
private _singularForm: string;
4+
5+
constructor(pluralForm: string, singularForm: string) {
6+
this._pluralForm = pluralForm;
7+
this._singularForm = singularForm;
8+
}
9+
10+
/**
11+
* Returns the plural of stored singular form
12+
*/
13+
public get pluralForm(): string {
14+
return this._pluralForm;
15+
}
16+
17+
/**
18+
* Returns the singular of stored plural form
19+
*/
20+
public get singularForm(): string {
21+
return this._singularForm;
22+
}
23+
}

test/words.spec.ts

+31-58
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import StringUtilsWord, { IWordEnding } from '../src/word';
1+
import StringUtilsWord from '../src/word/utils';
2+
import { WordEnding } from '../src/word/word-ending';
23
import JestRunner from './test.utils';
34

45
const runner = new JestRunner(StringUtilsWord);
@@ -20,63 +21,15 @@ describe('Get word ending', () => {
2021

2122
runner.runBasicTests(
2223
StringUtilsWord.getCorrespondingEnding,
23-
new Map<string, IWordEnding>([
24-
[
25-
'Passes',
26-
{
27-
pluralForm: 'sses',
28-
singularForm: 'ss',
29-
},
30-
],
31-
[
32-
'Pass',
33-
{
34-
pluralForm: 'sses',
35-
singularForm: 'ss',
36-
},
37-
],
38-
[
39-
'Categories',
40-
{
41-
pluralForm: 'ies',
42-
singularForm: 'y',
43-
},
44-
],
45-
[
46-
'Category',
47-
{
48-
pluralForm: 'ies',
49-
singularForm: 'y',
50-
},
51-
],
52-
[
53-
'Bees',
54-
{
55-
pluralForm: 'es',
56-
singularForm: 'e',
57-
},
58-
],
59-
[
60-
'Bee',
61-
{
62-
pluralForm: 'es',
63-
singularForm: 'e',
64-
},
65-
],
66-
[
67-
'Cars',
68-
{
69-
pluralForm: 's',
70-
singularForm: '',
71-
},
72-
],
73-
[
74-
'Car',
75-
{
76-
pluralForm: 's',
77-
singularForm: '',
78-
},
79-
],
24+
new Map<string, WordEnding>([
25+
['Passes', new WordEnding('sses', 'ss')],
26+
['Pass', new WordEnding('sses', 'ss')],
27+
['Categories', new WordEnding('ies', 'y')],
28+
['Category', new WordEnding('ies', 'y')],
29+
['Bees', new WordEnding('es', 'e')],
30+
['Bee', new WordEnding('es', 'e')],
31+
['Cars', new WordEnding('s', '')],
32+
['Car', new WordEnding('s', '')],
8033
]),
8134
);
8235
});
@@ -170,4 +123,24 @@ describe('Normalization of stuffs', () => {
170123
[['This ', 'is ', 'my ', 'test'], 'This Is My Test'],
171124
]),
172125
);
126+
127+
runner.runBasicTests(
128+
StringUtilsWord.getPluralOf,
129+
new Map<string, string>([
130+
['y', 'ies'],
131+
['ss', 'sses'],
132+
['e', 'es'],
133+
['', 's'],
134+
]),
135+
);
136+
137+
runner.runBasicTests(
138+
StringUtilsWord.getSingularOf,
139+
new Map<string, string>([
140+
['ies', 'y'],
141+
['sses', 'ss'],
142+
['es', 'e'],
143+
['s', ''],
144+
]),
145+
);
173146
});

0 commit comments

Comments
 (0)