inflekt 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/pluralize.js +43 -6
- package/package.json +2 -2
- package/pluralize.js +43 -6
package/esm/pluralize.js
CHANGED
|
@@ -23,17 +23,49 @@ const LATIN_SUFFIX_OVERRIDES = [
|
|
|
23
23
|
['curricula', 'curriculum'],
|
|
24
24
|
['data', 'datum'],
|
|
25
25
|
];
|
|
26
|
+
const TRAILING_TRIPLE_S_REGEX = /[sS]{3,}$/;
|
|
27
|
+
const TRAILING_TRIPLE_S_BEFORE_ES_REGEX = /[sS]{3,}(?=e[sS]$)/;
|
|
28
|
+
function normalizeTrailingSRun(suffix) {
|
|
29
|
+
return suffix === suffix.toUpperCase() ? 'SS' : 'ss';
|
|
30
|
+
}
|
|
31
|
+
function normalizeTripleSBeforeEs(word) {
|
|
32
|
+
return word.replace(TRAILING_TRIPLE_S_BEFORE_ES_REGEX, normalizeTrailingSRun);
|
|
33
|
+
}
|
|
34
|
+
function normalizeTrailingTripleS(word) {
|
|
35
|
+
const match = word.match(TRAILING_TRIPLE_S_REGEX);
|
|
36
|
+
if (!match) {
|
|
37
|
+
return word;
|
|
38
|
+
}
|
|
39
|
+
const suffix = match[0];
|
|
40
|
+
const prefix = word.slice(0, -suffix.length);
|
|
41
|
+
const normalizedSuffix = normalizeTrailingSRun(suffix);
|
|
42
|
+
return `${prefix}${normalizedSuffix}`;
|
|
43
|
+
}
|
|
44
|
+
function normalizeMalformedDoubleS(word) {
|
|
45
|
+
return normalizeTrailingTripleS(normalizeTripleSBeforeEs(word));
|
|
46
|
+
}
|
|
47
|
+
function enforceDoubleSPlural(singularWord, pluralWord) {
|
|
48
|
+
if (!singularWord.toLowerCase().endsWith('ss')) {
|
|
49
|
+
return pluralWord;
|
|
50
|
+
}
|
|
51
|
+
// Defensive normalization for malformed outputs like "hazardClasss".
|
|
52
|
+
if (pluralWord === `${singularWord}s`) {
|
|
53
|
+
return `${singularWord}es`;
|
|
54
|
+
}
|
|
55
|
+
return pluralWord;
|
|
56
|
+
}
|
|
26
57
|
/**
|
|
27
58
|
* Convert a word to its singular form with PostGraphile-compatible Latin handling
|
|
28
59
|
* @example "Users" -> "User", "People" -> "Person", "Schemata" -> "Schema", "ApiSchemata" -> "ApiSchema"
|
|
29
60
|
*/
|
|
30
61
|
export function singularize(word) {
|
|
31
|
-
const
|
|
62
|
+
const normalizedWord = normalizeMalformedDoubleS(word);
|
|
63
|
+
const lowerWord = normalizedWord.toLowerCase();
|
|
32
64
|
for (const [pluralSuffix, singularSuffix] of LATIN_SUFFIX_OVERRIDES) {
|
|
33
65
|
if (lowerWord.endsWith(pluralSuffix)) {
|
|
34
|
-
const suffixStart =
|
|
35
|
-
const prefix =
|
|
36
|
-
const originalSuffix =
|
|
66
|
+
const suffixStart = normalizedWord.length - pluralSuffix.length;
|
|
67
|
+
const prefix = normalizedWord.slice(0, suffixStart);
|
|
68
|
+
const originalSuffix = normalizedWord.slice(suffixStart);
|
|
37
69
|
const isAllCaps = originalSuffix === originalSuffix.toUpperCase();
|
|
38
70
|
const isUpperSuffix = originalSuffix[0] === originalSuffix[0].toUpperCase();
|
|
39
71
|
let newSuffix;
|
|
@@ -49,14 +81,19 @@ export function singularize(word) {
|
|
|
49
81
|
return prefix + newSuffix;
|
|
50
82
|
}
|
|
51
83
|
}
|
|
52
|
-
return inflection.singularize(
|
|
84
|
+
return normalizeMalformedDoubleS(inflection.singularize(normalizedWord));
|
|
85
|
+
}
|
|
86
|
+
function pluralizeCanonical(word) {
|
|
87
|
+
const normalizedWord = normalizeMalformedDoubleS(word);
|
|
88
|
+
const pluralWord = normalizeMalformedDoubleS(inflection.pluralize(normalizedWord));
|
|
89
|
+
return enforceDoubleSPlural(singularize(normalizedWord), pluralWord);
|
|
53
90
|
}
|
|
54
91
|
/**
|
|
55
92
|
* Convert a word to its plural form
|
|
56
93
|
* @example "User" -> "Users", "Person" -> "People"
|
|
57
94
|
*/
|
|
58
95
|
export function pluralize(word) {
|
|
59
|
-
return
|
|
96
|
+
return pluralizeCanonical(word);
|
|
60
97
|
}
|
|
61
98
|
/**
|
|
62
99
|
* Pluralize/singularize only the final segment of a compound name.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inflekt",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Inflection utilities for pluralization and singularization with PostGraphile-compatible Latin suffix handling",
|
|
5
5
|
"author": "Constructive <developers@constructive.io>",
|
|
6
6
|
"homepage": "https://github.com/constructive-io/dev-utils",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"inflection": "^3.0.0"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "5ae067a647da5dbfb48cd9796a8a3391b6c658ba"
|
|
47
47
|
}
|
package/pluralize.js
CHANGED
|
@@ -64,17 +64,49 @@ const LATIN_SUFFIX_OVERRIDES = [
|
|
|
64
64
|
['curricula', 'curriculum'],
|
|
65
65
|
['data', 'datum'],
|
|
66
66
|
];
|
|
67
|
+
const TRAILING_TRIPLE_S_REGEX = /[sS]{3,}$/;
|
|
68
|
+
const TRAILING_TRIPLE_S_BEFORE_ES_REGEX = /[sS]{3,}(?=e[sS]$)/;
|
|
69
|
+
function normalizeTrailingSRun(suffix) {
|
|
70
|
+
return suffix === suffix.toUpperCase() ? 'SS' : 'ss';
|
|
71
|
+
}
|
|
72
|
+
function normalizeTripleSBeforeEs(word) {
|
|
73
|
+
return word.replace(TRAILING_TRIPLE_S_BEFORE_ES_REGEX, normalizeTrailingSRun);
|
|
74
|
+
}
|
|
75
|
+
function normalizeTrailingTripleS(word) {
|
|
76
|
+
const match = word.match(TRAILING_TRIPLE_S_REGEX);
|
|
77
|
+
if (!match) {
|
|
78
|
+
return word;
|
|
79
|
+
}
|
|
80
|
+
const suffix = match[0];
|
|
81
|
+
const prefix = word.slice(0, -suffix.length);
|
|
82
|
+
const normalizedSuffix = normalizeTrailingSRun(suffix);
|
|
83
|
+
return `${prefix}${normalizedSuffix}`;
|
|
84
|
+
}
|
|
85
|
+
function normalizeMalformedDoubleS(word) {
|
|
86
|
+
return normalizeTrailingTripleS(normalizeTripleSBeforeEs(word));
|
|
87
|
+
}
|
|
88
|
+
function enforceDoubleSPlural(singularWord, pluralWord) {
|
|
89
|
+
if (!singularWord.toLowerCase().endsWith('ss')) {
|
|
90
|
+
return pluralWord;
|
|
91
|
+
}
|
|
92
|
+
// Defensive normalization for malformed outputs like "hazardClasss".
|
|
93
|
+
if (pluralWord === `${singularWord}s`) {
|
|
94
|
+
return `${singularWord}es`;
|
|
95
|
+
}
|
|
96
|
+
return pluralWord;
|
|
97
|
+
}
|
|
67
98
|
/**
|
|
68
99
|
* Convert a word to its singular form with PostGraphile-compatible Latin handling
|
|
69
100
|
* @example "Users" -> "User", "People" -> "Person", "Schemata" -> "Schema", "ApiSchemata" -> "ApiSchema"
|
|
70
101
|
*/
|
|
71
102
|
function singularize(word) {
|
|
72
|
-
const
|
|
103
|
+
const normalizedWord = normalizeMalformedDoubleS(word);
|
|
104
|
+
const lowerWord = normalizedWord.toLowerCase();
|
|
73
105
|
for (const [pluralSuffix, singularSuffix] of LATIN_SUFFIX_OVERRIDES) {
|
|
74
106
|
if (lowerWord.endsWith(pluralSuffix)) {
|
|
75
|
-
const suffixStart =
|
|
76
|
-
const prefix =
|
|
77
|
-
const originalSuffix =
|
|
107
|
+
const suffixStart = normalizedWord.length - pluralSuffix.length;
|
|
108
|
+
const prefix = normalizedWord.slice(0, suffixStart);
|
|
109
|
+
const originalSuffix = normalizedWord.slice(suffixStart);
|
|
78
110
|
const isAllCaps = originalSuffix === originalSuffix.toUpperCase();
|
|
79
111
|
const isUpperSuffix = originalSuffix[0] === originalSuffix[0].toUpperCase();
|
|
80
112
|
let newSuffix;
|
|
@@ -90,14 +122,19 @@ function singularize(word) {
|
|
|
90
122
|
return prefix + newSuffix;
|
|
91
123
|
}
|
|
92
124
|
}
|
|
93
|
-
return inflection.singularize(
|
|
125
|
+
return normalizeMalformedDoubleS(inflection.singularize(normalizedWord));
|
|
126
|
+
}
|
|
127
|
+
function pluralizeCanonical(word) {
|
|
128
|
+
const normalizedWord = normalizeMalformedDoubleS(word);
|
|
129
|
+
const pluralWord = normalizeMalformedDoubleS(inflection.pluralize(normalizedWord));
|
|
130
|
+
return enforceDoubleSPlural(singularize(normalizedWord), pluralWord);
|
|
94
131
|
}
|
|
95
132
|
/**
|
|
96
133
|
* Convert a word to its plural form
|
|
97
134
|
* @example "User" -> "Users", "Person" -> "People"
|
|
98
135
|
*/
|
|
99
136
|
function pluralize(word) {
|
|
100
|
-
return
|
|
137
|
+
return pluralizeCanonical(word);
|
|
101
138
|
}
|
|
102
139
|
/**
|
|
103
140
|
* Pluralize/singularize only the final segment of a compound name.
|