Skip to content

Commit f4824a2

Browse files
committed
fix: add Latin plural overrides for PostGraphile compatibility
- Add LATIN_PLURAL_OVERRIDES to pluralize schema -> schemata, criterion -> criteria, etc. - This maintains backward compatibility with PostGraphile's Latin-style pluralization - Fixes breaking change where GraphQL field names changed from schemata to schemas
1 parent 50809e5 commit f4824a2

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

packages/inflection/src/pluralize.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ const LATIN_SUFFIX_OVERRIDES: Array<[string, string]> = [
2525
['data', 'datum'],
2626
];
2727

28+
/**
29+
* Latin plural overrides for pluralization.
30+
*
31+
* PostGraphile uses Latin-style plurals for certain words (schema -> schemata).
32+
* The inflection library uses English-style plurals (schema -> schemas).
33+
*
34+
* Format: [singularSuffix, pluralSuffix]
35+
*/
36+
const LATIN_PLURAL_OVERRIDES: Array<[string, string]> = [
37+
['schema', 'schemata'],
38+
['criterion', 'criteria'],
39+
['phenomenon', 'phenomena'],
40+
['medium', 'media'],
41+
['memorandum', 'memoranda'],
42+
['stratum', 'strata'],
43+
['curriculum', 'curricula'],
44+
['datum', 'data'],
45+
];
46+
2847
/**
2948
* Convert a word to its singular form with PostGraphile-compatible Latin handling
3049
* @example "Users" -> "User", "People" -> "Person", "Schemata" -> "Schema", "ApiSchemata" -> "ApiSchema"
@@ -59,10 +78,35 @@ export function singularize(word: string): string {
5978
}
6079

6180
/**
62-
* Convert a word to its plural form
63-
* @example "User" -> "Users", "Person" -> "People"
81+
* Convert a word to its plural form with PostGraphile-compatible Latin handling
82+
* @example "User" -> "Users", "Person" -> "People", "Schema" -> "Schemata", "ApiSchema" -> "ApiSchemata"
6483
*/
6584
export function pluralize(word: string): string {
85+
const lowerWord = word.toLowerCase();
86+
87+
for (const [singularSuffix, pluralSuffix] of LATIN_PLURAL_OVERRIDES) {
88+
if (lowerWord.endsWith(singularSuffix)) {
89+
const suffixStart = word.length - singularSuffix.length;
90+
const prefix = word.slice(0, suffixStart);
91+
const originalSuffix = word.slice(suffixStart);
92+
93+
const isAllCaps = originalSuffix === originalSuffix.toUpperCase();
94+
const isUpperSuffix =
95+
originalSuffix[0] === originalSuffix[0].toUpperCase();
96+
97+
let newSuffix: string;
98+
if (isAllCaps) {
99+
newSuffix = pluralSuffix.toUpperCase();
100+
} else if (isUpperSuffix) {
101+
newSuffix = pluralSuffix.charAt(0).toUpperCase() + pluralSuffix.slice(1);
102+
} else {
103+
newSuffix = pluralSuffix;
104+
}
105+
106+
return prefix + newSuffix;
107+
}
108+
}
109+
66110
return inflection.pluralize(word);
67111
}
68112

0 commit comments

Comments
 (0)