inflekt 0.8.0 → 0.8.1
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/exceptions.js +375 -34
- package/esm/pluralize.js +52 -39
- package/esm/rules.js +136 -3
- package/exceptions.d.ts +19 -0
- package/exceptions.js +376 -35
- package/package.json +4 -2
- package/pluralize.d.ts +2 -2
- package/pluralize.js +50 -70
- package/rules.d.ts +26 -2
- package/rules.js +138 -3
package/esm/pluralize.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Inflection with PostGraphile-compatible Latin suffix handling.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* Both directions are the same shape: an exception table proven against a
|
|
5
|
+
* system dictionary (exceptions.ts) in front of the suffix rules (rules.ts),
|
|
6
|
+
* so dictionary words are right by construction and coined identifiers fall
|
|
7
|
+
* through to the rules. Both are idempotent — `singularize` of a singular and
|
|
8
|
+
* `pluralize` of a plural return the word unchanged.
|
|
8
9
|
*/
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
11
|
-
import { enforceDoubleSPlural, normalizeMalformedDoubleS, pluralizeFStem, restoreWordCase, singularizeByRules, } from './rules';
|
|
10
|
+
import { PLURAL_EXCEPTIONS, SINGULAR_EXCEPTIONS } from './exceptions';
|
|
11
|
+
import { isRulesPlural, normalizeMalformedDoubleS, pluralizeByRules, restoreWordCase, singularizeByRules, } from './rules';
|
|
12
12
|
const LAST_SEGMENT_REGEX = /([A-Z]|_[a-z0-9])[a-z0-9]*_*$/;
|
|
13
13
|
/**
|
|
14
14
|
* Locate the final word segment of a compound name ("user_cookies" -> "cookies",
|
|
@@ -25,12 +25,29 @@ function splitLastSegment(str) {
|
|
|
25
25
|
}
|
|
26
26
|
return { prefix: str.slice(0, index), word: str.slice(index) };
|
|
27
27
|
}
|
|
28
|
-
function
|
|
28
|
+
function lookup(table, word) {
|
|
29
29
|
const key = word.toLowerCase();
|
|
30
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
30
|
+
if (!Object.prototype.hasOwnProperty.call(table, key)) {
|
|
31
31
|
return null;
|
|
32
32
|
}
|
|
33
|
-
return restoreWordCase(word,
|
|
33
|
+
return restoreWordCase(word, table[key]);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Apply an exception table to a word, or to the final segment of a compound
|
|
37
|
+
* name ("user_cookies" -> "cookies") so compounds inflect like the word they
|
|
38
|
+
* end in.
|
|
39
|
+
*/
|
|
40
|
+
function applyTable(table, word) {
|
|
41
|
+
const whole = lookup(table, word);
|
|
42
|
+
if (whole) {
|
|
43
|
+
return whole;
|
|
44
|
+
}
|
|
45
|
+
const segment = splitLastSegment(word);
|
|
46
|
+
if (!segment) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const segmentException = lookup(table, segment.word);
|
|
50
|
+
return segmentException ? segment.prefix + segmentException : null;
|
|
34
51
|
}
|
|
35
52
|
/**
|
|
36
53
|
* Convert a word to its singular form with PostGraphile-compatible Latin handling
|
|
@@ -38,44 +55,40 @@ function lookupException(word) {
|
|
|
38
55
|
*/
|
|
39
56
|
export function singularize(word) {
|
|
40
57
|
const normalizedWord = normalizeMalformedDoubleS(word);
|
|
41
|
-
const exception =
|
|
58
|
+
const exception = applyTable(SINGULAR_EXCEPTIONS, normalizedWord);
|
|
42
59
|
if (exception) {
|
|
43
60
|
return exception;
|
|
44
61
|
}
|
|
45
|
-
const segment = splitLastSegment(normalizedWord);
|
|
46
|
-
if (segment) {
|
|
47
|
-
const segmentException = lookupException(segment.word);
|
|
48
|
-
if (segmentException) {
|
|
49
|
-
return segment.prefix + segmentException;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
62
|
return singularizeByRules(normalizedWord);
|
|
53
63
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const pluralWord = normalizeMalformedDoubleS(inflection.pluralize(normalizedWord));
|
|
66
|
-
// -is nouns the inflection library leaves untouched (iris, chassis) rather
|
|
67
|
-
// than treating as Greek/Latin (analysis -> analyses).
|
|
68
|
-
if (pluralWord === normalizedWord && /is$/i.test(normalizedWord)) {
|
|
69
|
-
return `${normalizedWord}es`;
|
|
64
|
+
/**
|
|
65
|
+
* Whether `word` is already a plural, and so must be returned unchanged.
|
|
66
|
+
*
|
|
67
|
+
* SINGULAR_EXCEPTIONS answers this for the dictionary (its keys are plurals,
|
|
68
|
+
* except the self-mapped singulars it carries to protect them from the rules),
|
|
69
|
+
* and the rules answer it for everything else, coined identifiers included.
|
|
70
|
+
*/
|
|
71
|
+
function isPlural(word) {
|
|
72
|
+
const singular = applyTable(SINGULAR_EXCEPTIONS, word);
|
|
73
|
+
if (singular) {
|
|
74
|
+
return singular !== word;
|
|
70
75
|
}
|
|
71
|
-
return
|
|
76
|
+
return isRulesPlural(word);
|
|
72
77
|
}
|
|
73
78
|
/**
|
|
74
|
-
* Convert a word to its plural form
|
|
75
|
-
* @example "User" -> "Users", "Person" -> "People"
|
|
79
|
+
* Convert a word to its plural form. Already-plural input is returned as-is.
|
|
80
|
+
* @example "User" -> "Users", "Person" -> "People", "Users" -> "Users"
|
|
76
81
|
*/
|
|
77
82
|
export function pluralize(word) {
|
|
78
|
-
|
|
83
|
+
const normalizedWord = normalizeMalformedDoubleS(word);
|
|
84
|
+
if (isPlural(normalizedWord)) {
|
|
85
|
+
return normalizedWord;
|
|
86
|
+
}
|
|
87
|
+
const exception = applyTable(PLURAL_EXCEPTIONS, normalizedWord);
|
|
88
|
+
if (exception) {
|
|
89
|
+
return exception;
|
|
90
|
+
}
|
|
91
|
+
return pluralizeByRules(normalizedWord);
|
|
79
92
|
}
|
|
80
93
|
/**
|
|
81
94
|
* Pluralize/singularize only the final segment of a compound name.
|
package/esm/rules.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Rule-based
|
|
2
|
+
* Rule-based inflection: the suffix rules alone, with no exception tables.
|
|
3
3
|
*
|
|
4
4
|
* Kept separate from pluralize.ts so that scripts/generate-exceptions.ts can
|
|
5
|
-
* diff the rules against a dictionary without the exception
|
|
6
|
-
* very defects
|
|
5
|
+
* diff the rules against a dictionary without the exception tables masking the
|
|
6
|
+
* very defects they are meant to record. Not part of the public API.
|
|
7
7
|
*/
|
|
8
8
|
import * as inflection from 'inflection';
|
|
9
9
|
/**
|
|
@@ -16,6 +16,8 @@ import * as inflection from 'inflection';
|
|
|
16
16
|
*/
|
|
17
17
|
const LATIN_SUFFIX_OVERRIDES = [
|
|
18
18
|
['schemata', 'schema'],
|
|
19
|
+
['corpora', 'corpus'],
|
|
20
|
+
['genera', 'genus'],
|
|
19
21
|
['criteria', 'criterion'],
|
|
20
22
|
['phenomena', 'phenomenon'],
|
|
21
23
|
['media', 'medium'],
|
|
@@ -24,6 +26,34 @@ const LATIN_SUFFIX_OVERRIDES = [
|
|
|
24
26
|
['curricula', 'curriculum'],
|
|
25
27
|
['data', 'datum'],
|
|
26
28
|
];
|
|
29
|
+
/**
|
|
30
|
+
* The `-a` plurals of LATIN_SUFFIX_OVERRIDES, as a set: these are the only
|
|
31
|
+
* words the rules may read as an existing plural rather than pluralizing them.
|
|
32
|
+
*
|
|
33
|
+
* Everything else ending in "a" is treated as a singular that takes "-as"
|
|
34
|
+
* (deltas, betas, quotas, schemas, replicas). The inflection library assumes
|
|
35
|
+
* the opposite — that "-a" is Latin — and so leaves every one of them
|
|
36
|
+
* unchanged, which for an identifier means the plural silently equals the
|
|
37
|
+
* singular.
|
|
38
|
+
*/
|
|
39
|
+
const LATIN_A_PLURALS = LATIN_SUFFIX_OVERRIDES.map(([plural]) => plural);
|
|
40
|
+
/** Whether `lowerWord` is (or ends in) one of the Latin `-a` plurals. */
|
|
41
|
+
function isLatinAPlural(lowerWord) {
|
|
42
|
+
return LATIN_A_PLURALS.some((plural) => lowerWord.endsWith(plural));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The Latin plurals to *produce*, the inverse of LATIN_SUFFIX_OVERRIDES minus
|
|
46
|
+
* "schema" (whose plural is "schemas" here, per PostGraphile). Matched as a
|
|
47
|
+
* suffix so compounds follow: metadatum -> metadata.
|
|
48
|
+
*
|
|
49
|
+
* Without this, pluralize and singularize disagree: "criteria" singularizes to
|
|
50
|
+
* "criterion", but "criterion" would pluralize to "criterions".
|
|
51
|
+
*
|
|
52
|
+
* Format: [singularSuffix, pluralSuffix]
|
|
53
|
+
*/
|
|
54
|
+
const LATIN_PLURAL_SUFFIXES = LATIN_SUFFIX_OVERRIDES
|
|
55
|
+
.filter(([plural]) => plural !== 'schemata')
|
|
56
|
+
.map(([plural, singular]) => [singular, plural]);
|
|
27
57
|
/**
|
|
28
58
|
* Compound words ending in "base" that the inflection library incorrectly
|
|
29
59
|
* singularizes via its (b)a branch in the ses$ rule (e.g. codebases -> codebasis).
|
|
@@ -85,6 +115,14 @@ export const F_STEM_PLURALS = [
|
|
|
85
115
|
* which is what coined identifiers want: apis -> api, cpus -> cpu, uris -> uri.
|
|
86
116
|
*/
|
|
87
117
|
const ADJECTIVE_OUS_REGEX = /ous$/i;
|
|
118
|
+
/**
|
|
119
|
+
* Words ending in "a" are singular (delta, schema, replica, quota, aorta): the
|
|
120
|
+
* only English plurals spelled that way are the Latin ones in
|
|
121
|
+
* LATIN_SUFFIX_OVERRIDES. The inflection library reads the suffix as Latin
|
|
122
|
+
* regardless and rewrites it to "-um", coining "deltum" and "schemum", which
|
|
123
|
+
* then reads back as a plural and leaves pluralize with nothing to do.
|
|
124
|
+
*/
|
|
125
|
+
const A_SINGULAR_REGEX = /a$/i;
|
|
88
126
|
/**
|
|
89
127
|
* The only -ice plurals. The inflection library's rule is written `([m|l])ice`,
|
|
90
128
|
* whose character class also matches "|", and it fires on any word ending in
|
|
@@ -188,12 +226,107 @@ function applyGenericSingularRules(word) {
|
|
|
188
226
|
* Singularize using suffix rules only — the fallback for words absent from the
|
|
189
227
|
* exception table (including coined identifiers that no dictionary knows).
|
|
190
228
|
*/
|
|
229
|
+
const US_REGEX = /us$/i;
|
|
230
|
+
const A_REGEX = /a$/i;
|
|
231
|
+
const UM_REGEX = /um$/i;
|
|
232
|
+
const F_OR_FE_REGEX = /(?:f|fe)$/i;
|
|
233
|
+
/**
|
|
234
|
+
* Nouns ending in "is" that are not the Greek/Latin `-sis`/`-xis` family: their
|
|
235
|
+
* plural adds "es" (iris -> irises, chassis -> chassises). Only the Greek ones
|
|
236
|
+
* rewrite the "is" itself (analysis -> analyses, axis -> axes), which is the
|
|
237
|
+
* rule the inflection library applies to all of them, coining "chasses".
|
|
238
|
+
*/
|
|
239
|
+
const NON_GREEK_IS_REGEX = /(?<![sx])is$/i;
|
|
240
|
+
/**
|
|
241
|
+
* Pluralize using suffix rules only, with no exception table — the fallback for
|
|
242
|
+
* words the dictionary does not know, which is every coined identifier.
|
|
243
|
+
*
|
|
244
|
+
* The inflection library's Latin instincts are wrong for identifiers in three
|
|
245
|
+
* shapes, all of which it leaves *unchanged* (so the "plural" equals the
|
|
246
|
+
* singular, and a schema ends up with `radius` and `radius` fields):
|
|
247
|
+
*
|
|
248
|
+
* radius -> radiuses (not radius)
|
|
249
|
+
* delta -> deltas (not delta)
|
|
250
|
+
* forum -> forums (not fora)
|
|
251
|
+
*
|
|
252
|
+
* The genuine Latin plurals are a closed set of dictionary words, carried by
|
|
253
|
+
* LATIN_SUFFIX_OVERRIDES and by the generated PLURAL_EXCEPTIONS table.
|
|
254
|
+
*
|
|
255
|
+
* Words the inflection library holds uncountable (sheep, series, aircraft) are
|
|
256
|
+
* left alone here: "no plural" is a dictionary fact like any other, and the
|
|
257
|
+
* ones it gets wrong (bias, atlas, aid) are corrected by PLURAL_EXCEPTIONS.
|
|
258
|
+
*/
|
|
259
|
+
export function pluralizeByRules(word) {
|
|
260
|
+
const normalizedWord = normalizeMalformedDoubleS(word);
|
|
261
|
+
// Adjectives have no plural, so leave them be rather than coining
|
|
262
|
+
// "anxiouses"; see ADJECTIVE_OUS_REGEX.
|
|
263
|
+
if (ADJECTIVE_OUS_REGEX.test(normalizedWord)) {
|
|
264
|
+
return normalizedWord;
|
|
265
|
+
}
|
|
266
|
+
// -f/-fe words: only the f-stem nouns take -ves; the rest take a plain "s".
|
|
267
|
+
const fStem = pluralizeFStem(normalizedWord);
|
|
268
|
+
if (fStem) {
|
|
269
|
+
return fStem;
|
|
270
|
+
}
|
|
271
|
+
if (F_OR_FE_REGEX.test(normalizedWord)) {
|
|
272
|
+
return matchWordCase(normalizedWord, `${normalizedWord}s`);
|
|
273
|
+
}
|
|
274
|
+
const lowerWord = normalizedWord.toLowerCase();
|
|
275
|
+
for (const [singularSuffix, pluralSuffix] of LATIN_PLURAL_SUFFIXES) {
|
|
276
|
+
if (!lowerWord.endsWith(singularSuffix)) {
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
const stem = normalizedWord.slice(0, -singularSuffix.length);
|
|
280
|
+
const matched = normalizedWord.slice(-singularSuffix.length);
|
|
281
|
+
return stem + matchCase(matched, pluralSuffix);
|
|
282
|
+
}
|
|
283
|
+
if (NON_GREEK_IS_REGEX.test(normalizedWord)) {
|
|
284
|
+
return matchWordCase(normalizedWord, `${normalizedWord}es`);
|
|
285
|
+
}
|
|
286
|
+
const pluralWord = normalizeMalformedDoubleS(inflection.pluralize(normalizedWord));
|
|
287
|
+
if (pluralWord === normalizedWord) {
|
|
288
|
+
// Latin-looking shapes it declines to pluralize, and the uncountable list
|
|
289
|
+
// it applies to countable nouns (bias, atlas, aid, air).
|
|
290
|
+
if (US_REGEX.test(normalizedWord)) {
|
|
291
|
+
return matchWordCase(normalizedWord, `${normalizedWord}es`);
|
|
292
|
+
}
|
|
293
|
+
if (UM_REGEX.test(normalizedWord)) {
|
|
294
|
+
return matchWordCase(normalizedWord, `${normalizedWord}s`);
|
|
295
|
+
}
|
|
296
|
+
if (A_REGEX.test(normalizedWord) && !isLatinAPlural(lowerWord)) {
|
|
297
|
+
return matchWordCase(normalizedWord, `${normalizedWord}s`);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return matchWordCase(normalizedWord, enforceDoubleSPlural(singularizeByRules(normalizedWord), pluralWord));
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Keep an all-caps word all-caps: the suffix rules and the inflection library
|
|
304
|
+
* both append a lowercase "s", turning API_KEY into API_KEYs.
|
|
305
|
+
*/
|
|
306
|
+
function matchWordCase(word, plural) {
|
|
307
|
+
const isAllCaps = word === word.toUpperCase() && /[A-Z]/.test(word);
|
|
308
|
+
return isAllCaps ? plural.toUpperCase() : plural;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Whether the rules already read `word` as a plural: it singularizes to
|
|
312
|
+
* something else, and that singular pluralizes back to exactly `word`.
|
|
313
|
+
*
|
|
314
|
+
* This is what keeps pluralize idempotent — `pluralize('apis')` must be `apis`,
|
|
315
|
+
* not `apises` — without a table of every plural in the language.
|
|
316
|
+
*/
|
|
317
|
+
export function isRulesPlural(word) {
|
|
318
|
+
const singular = singularizeByRules(word);
|
|
319
|
+
return singular !== word && pluralizeByRules(singular) === word;
|
|
320
|
+
}
|
|
191
321
|
export function singularizeByRules(word) {
|
|
192
322
|
const normalizedWord = normalizeMalformedDoubleS(word);
|
|
193
323
|
const lowerWord = normalizedWord.toLowerCase();
|
|
194
324
|
if (ADJECTIVE_OUS_REGEX.test(normalizedWord)) {
|
|
195
325
|
return normalizedWord;
|
|
196
326
|
}
|
|
327
|
+
if (A_SINGULAR_REGEX.test(normalizedWord) && !isLatinAPlural(lowerWord)) {
|
|
328
|
+
return normalizedWord;
|
|
329
|
+
}
|
|
197
330
|
if (ICE_REGEX.test(normalizedWord)) {
|
|
198
331
|
const mouse = Object.prototype.hasOwnProperty.call(MOUSE_PLURALS, lowerWord)
|
|
199
332
|
? MOUSE_PLURALS[lowerWord]
|
package/exceptions.d.ts
CHANGED
|
@@ -16,3 +16,22 @@
|
|
|
16
16
|
* pluralization returns the key (see scripts/generate-exceptions.ts).
|
|
17
17
|
*/
|
|
18
18
|
export declare const SINGULAR_EXCEPTIONS: Record<string, string>;
|
|
19
|
+
/**
|
|
20
|
+
* Singular -> plural pairs that no generic suffix rule gets right.
|
|
21
|
+
*
|
|
22
|
+
* Pluralization is not rule-decidable either: `-o` takes "es" for "potatoes"
|
|
23
|
+
* but only "s" for "photos"; `-us` is "-uses" for "buses" but "-i" for "fungi";
|
|
24
|
+
* `-um` is "-ums" for "forums" but "-a" for "data". The inflection library also
|
|
25
|
+
* holds many countable nouns uncountable (bias, atlas, aid), which for an
|
|
26
|
+
* identifier means the plural silently equals the singular — those are
|
|
27
|
+
* corrected here.
|
|
28
|
+
*
|
|
29
|
+
* Entries whose key is itself a plural (`biases: 'biases'`) exist so that
|
|
30
|
+
* pluralize is idempotent for the plurals the rules cannot re-derive.
|
|
31
|
+
*
|
|
32
|
+
* GENERATED — do not hand-edit. Regenerate with:
|
|
33
|
+
*
|
|
34
|
+
* sudo apt-get install -y wamerican
|
|
35
|
+
* npx ts-node scripts/generate-exceptions.ts
|
|
36
|
+
*/
|
|
37
|
+
export declare const PLURAL_EXCEPTIONS: Record<string, string>;
|