inflekt 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Constructive <developers@constructive.io>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,166 @@
1
+ # inflekt
2
+
3
+ <p align="center">
4
+ <img src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" height="250">
5
+ <br />
6
+ <strong>smart pluralization and inflection for the modern web</strong>
7
+ <br />
8
+ <br />
9
+ Inflection utilities for pluralization and singularization with PostGraphile-compatible Latin suffix handling
10
+ <br />
11
+ <br />
12
+ <a href="https://github.com/constructive-io/dev-utils/actions/workflows/ci.yml">
13
+ <img height="20" src="https://github.com/constructive-io/dev-utils/actions/workflows/ci.yml/badge.svg" />
14
+ </a>
15
+ <a href="https://github.com/constructive-io/dev-utils/blob/main/LICENSE">
16
+ <img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/>
17
+ </a>
18
+ </p>
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ npm install inflekt
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ ```typescript
29
+ import {
30
+ singularize,
31
+ pluralize,
32
+ singularizeLast,
33
+ pluralizeLast,
34
+ distinctPluralize,
35
+ lcFirst,
36
+ ucFirst,
37
+ camelize,
38
+ underscore,
39
+ toPascalCase,
40
+ toCamelCase,
41
+ toSnakeCase,
42
+ toKebabCase,
43
+ toConstantCase,
44
+ toFieldName,
45
+ toQueryName,
46
+ } from 'inflekt';
47
+
48
+ // Basic singularization/pluralization
49
+ singularize('Users'); // 'User'
50
+ pluralize('User'); // 'Users'
51
+
52
+ // Latin suffix handling (PostGraphile-compatible)
53
+ singularize('Schemata'); // 'Schema' (not 'Schematum')
54
+ singularize('Criteria'); // 'Criterion'
55
+ singularize('Media'); // 'Medium'
56
+
57
+ // Compound word handling (only transforms last word)
58
+ singularizeLast('UserProfiles'); // 'UserProfile'
59
+ pluralizeLast('UserProfile'); // 'UserProfiles'
60
+
61
+ // First character case transformations
62
+ lcFirst('UserProfile'); // 'userProfile'
63
+ ucFirst('userProfile'); // 'UserProfile'
64
+
65
+ // Snake case / camel case conversions
66
+ camelize('user_profile'); // 'UserProfile'
67
+ camelize('user_profile', true); // 'userProfile'
68
+ underscore('UserProfile'); // 'user_profile'
69
+
70
+ // Full case transformations (re-exported from komoji)
71
+ toPascalCase('hello-world'); // 'HelloWorld'
72
+ toCamelCase('hello-world'); // 'helloWorld'
73
+ toSnakeCase('helloWorld'); // 'hello_world'
74
+ toKebabCase('HelloWorld'); // 'hello-world'
75
+ toConstantCase('helloWorld'); // 'HELLO_WORLD'
76
+
77
+ // GraphQL naming helpers
78
+ toFieldName('Users'); // 'user'
79
+ toQueryName('User'); // 'users'
80
+ ```
81
+
82
+ ## API
83
+
84
+ ### Pluralization
85
+
86
+ - `singularize(word)` - Convert a word to singular form with Latin suffix handling
87
+ - `pluralize(word)` - Convert a word to plural form
88
+ - `singularizeLast(str)` - Singularize only the last word in a compound name
89
+ - `pluralizeLast(str)` - Pluralize only the last word in a compound name
90
+ - `distinctPluralize(str)` - Create a distinct plural form (handles cases where singular === plural)
91
+ - `distinctPluralizeLast(str)` - Distinctly pluralize only the last word
92
+
93
+ ### Case Transformations
94
+
95
+ - `lcFirst(str)` - Convert first character to lowercase (PascalCase to camelCase)
96
+ - `ucFirst(str)` - Convert first character to uppercase (camelCase to PascalCase)
97
+ - `camelize(str, lowFirstLetter?)` - Convert snake_case to PascalCase (or camelCase if lowFirstLetter is true)
98
+ - `underscore(str)` - Convert PascalCase/camelCase to snake_case
99
+ - `fixCapitalisedPlural(str)` - Fix capitalized S after numbers (e.g., `Table1S` -> `Table1s`)
100
+ - `toPascalCase(str)` - Convert to PascalCase (re-exported from komoji)
101
+ - `toCamelCase(str)` - Convert to camelCase (re-exported from komoji)
102
+ - `toSnakeCase(str)` - Convert to snake_case (re-exported from komoji)
103
+ - `toKebabCase(str)` - Convert to kebab-case (re-exported from komoji)
104
+ - `toConstantCase(str)` - Convert to CONSTANT_CASE (re-exported from komoji)
105
+
106
+ ### Naming Helpers
107
+
108
+ - `toFieldName(pluralTypeName)` - Convert plural PascalCase to singular camelCase field name
109
+ - `toQueryName(singularTypeName)` - Convert singular PascalCase to plural camelCase query name
110
+
111
+ ## Latin Suffix Overrides
112
+
113
+ This library handles Latin plural suffixes differently than the standard `inflection` package to match PostGraphile's behavior:
114
+
115
+ | Plural | Singular |
116
+ |--------|----------|
117
+ | schemata | schema |
118
+ | criteria | criterion |
119
+ | phenomena | phenomenon |
120
+ | media | medium |
121
+ | memoranda | memorandum |
122
+ | strata | stratum |
123
+ | curricula | curriculum |
124
+ | data | datum |
125
+
126
+ ## License
127
+
128
+ MIT
129
+
130
+ ---
131
+
132
+ ## Development
133
+
134
+ ### Setup
135
+
136
+ 1. Clone the repository:
137
+
138
+ ```bash
139
+ git clone https://github.com/constructive-io/dev-utils.git
140
+ ```
141
+
142
+ 2. Install dependencies:
143
+
144
+ ```bash
145
+ cd dev-utils
146
+ pnpm install
147
+ pnpm build
148
+ ```
149
+
150
+ 3. Test the package of interest:
151
+
152
+ ```bash
153
+ cd packages/<packagename>
154
+ pnpm test:watch
155
+ ```
156
+
157
+ ## Credits
158
+
159
+ Built for developers, with developers.
160
+ 👉 https://launchql.com | https://hyperweb.io
161
+
162
+ ## Disclaimer
163
+
164
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
165
+
166
+ No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
package/case.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Case transformation utilities
3
+ */
4
+ export { toPascalCase, toCamelCase, toSnakeCase, toKebabCase, toConstantCase, } from 'komoji';
5
+ /**
6
+ * Convert PascalCase to camelCase (lowercase first character)
7
+ * @example "UserProfile" -> "userProfile"
8
+ */
9
+ export declare function lcFirst(str: string): string;
10
+ /**
11
+ * Convert camelCase to PascalCase (uppercase first character)
12
+ * @example "userProfile" -> "UserProfile"
13
+ */
14
+ export declare function ucFirst(str: string): string;
15
+ /**
16
+ * Fix capitalized plurals that end with a number followed by 'S'
17
+ * This solves the issue with `blah-table1s` becoming `blahTable1S`
18
+ * @example "Table1S" -> "Table1s"
19
+ */
20
+ export declare function fixCapitalisedPlural(str: string): string;
21
+ /**
22
+ * Convert snake_case to PascalCase (or camelCase if lowFirstLetter is true)
23
+ * @param str - The snake_case string to convert
24
+ * @param lowFirstLetter - If true, returns camelCase instead of PascalCase
25
+ * @example camelize('user_profile') -> 'UserProfile'
26
+ * @example camelize('user_profile', true) -> 'userProfile'
27
+ */
28
+ export declare function camelize(str: string, lowFirstLetter?: boolean): string;
29
+ /**
30
+ * Convert PascalCase or camelCase to snake_case
31
+ * @example underscore('UserProfile') -> 'user_profile'
32
+ * @example underscore('userProfile') -> 'user_profile'
33
+ */
34
+ export declare function underscore(str: string): string;
package/case.js ADDED
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toConstantCase = exports.toKebabCase = exports.toSnakeCase = exports.toCamelCase = exports.toPascalCase = void 0;
4
+ exports.lcFirst = lcFirst;
5
+ exports.ucFirst = ucFirst;
6
+ exports.fixCapitalisedPlural = fixCapitalisedPlural;
7
+ exports.camelize = camelize;
8
+ exports.underscore = underscore;
9
+ /**
10
+ * Case transformation utilities
11
+ */
12
+ var komoji_1 = require("komoji");
13
+ Object.defineProperty(exports, "toPascalCase", { enumerable: true, get: function () { return komoji_1.toPascalCase; } });
14
+ Object.defineProperty(exports, "toCamelCase", { enumerable: true, get: function () { return komoji_1.toCamelCase; } });
15
+ Object.defineProperty(exports, "toSnakeCase", { enumerable: true, get: function () { return komoji_1.toSnakeCase; } });
16
+ Object.defineProperty(exports, "toKebabCase", { enumerable: true, get: function () { return komoji_1.toKebabCase; } });
17
+ Object.defineProperty(exports, "toConstantCase", { enumerable: true, get: function () { return komoji_1.toConstantCase; } });
18
+ /**
19
+ * Convert PascalCase to camelCase (lowercase first character)
20
+ * @example "UserProfile" -> "userProfile"
21
+ */
22
+ function lcFirst(str) {
23
+ return str.charAt(0).toLowerCase() + str.slice(1);
24
+ }
25
+ /**
26
+ * Convert camelCase to PascalCase (uppercase first character)
27
+ * @example "userProfile" -> "UserProfile"
28
+ */
29
+ function ucFirst(str) {
30
+ return str.charAt(0).toUpperCase() + str.slice(1);
31
+ }
32
+ /**
33
+ * Fix capitalized plurals that end with a number followed by 'S'
34
+ * This solves the issue with `blah-table1s` becoming `blahTable1S`
35
+ * @example "Table1S" -> "Table1s"
36
+ */
37
+ function fixCapitalisedPlural(str) {
38
+ return str.replace(/[0-9]S(?=[A-Z]|$)/g, (match) => match.toLowerCase());
39
+ }
40
+ /**
41
+ * Convert snake_case to PascalCase (or camelCase if lowFirstLetter is true)
42
+ * @param str - The snake_case string to convert
43
+ * @param lowFirstLetter - If true, returns camelCase instead of PascalCase
44
+ * @example camelize('user_profile') -> 'UserProfile'
45
+ * @example camelize('user_profile', true) -> 'userProfile'
46
+ */
47
+ function camelize(str, lowFirstLetter) {
48
+ const result = str
49
+ .split('_')
50
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
51
+ .join('');
52
+ if (lowFirstLetter) {
53
+ return lcFirst(result);
54
+ }
55
+ return result;
56
+ }
57
+ /**
58
+ * Convert PascalCase or camelCase to snake_case
59
+ * @example underscore('UserProfile') -> 'user_profile'
60
+ * @example underscore('userProfile') -> 'user_profile'
61
+ */
62
+ function underscore(str) {
63
+ return str
64
+ .replace(/([A-Z])/g, '_$1')
65
+ .replace(/^_/, '')
66
+ .toLowerCase();
67
+ }
package/esm/case.js ADDED
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Case transformation utilities
3
+ */
4
+ export { toPascalCase, toCamelCase, toSnakeCase, toKebabCase, toConstantCase, } from 'komoji';
5
+ /**
6
+ * Convert PascalCase to camelCase (lowercase first character)
7
+ * @example "UserProfile" -> "userProfile"
8
+ */
9
+ export function lcFirst(str) {
10
+ return str.charAt(0).toLowerCase() + str.slice(1);
11
+ }
12
+ /**
13
+ * Convert camelCase to PascalCase (uppercase first character)
14
+ * @example "userProfile" -> "UserProfile"
15
+ */
16
+ export function ucFirst(str) {
17
+ return str.charAt(0).toUpperCase() + str.slice(1);
18
+ }
19
+ /**
20
+ * Fix capitalized plurals that end with a number followed by 'S'
21
+ * This solves the issue with `blah-table1s` becoming `blahTable1S`
22
+ * @example "Table1S" -> "Table1s"
23
+ */
24
+ export function fixCapitalisedPlural(str) {
25
+ return str.replace(/[0-9]S(?=[A-Z]|$)/g, (match) => match.toLowerCase());
26
+ }
27
+ /**
28
+ * Convert snake_case to PascalCase (or camelCase if lowFirstLetter is true)
29
+ * @param str - The snake_case string to convert
30
+ * @param lowFirstLetter - If true, returns camelCase instead of PascalCase
31
+ * @example camelize('user_profile') -> 'UserProfile'
32
+ * @example camelize('user_profile', true) -> 'userProfile'
33
+ */
34
+ export function camelize(str, lowFirstLetter) {
35
+ const result = str
36
+ .split('_')
37
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
38
+ .join('');
39
+ if (lowFirstLetter) {
40
+ return lcFirst(result);
41
+ }
42
+ return result;
43
+ }
44
+ /**
45
+ * Convert PascalCase or camelCase to snake_case
46
+ * @example underscore('UserProfile') -> 'user_profile'
47
+ * @example underscore('userProfile') -> 'user_profile'
48
+ */
49
+ export function underscore(str) {
50
+ return str
51
+ .replace(/([A-Z])/g, '_$1')
52
+ .replace(/^_/, '')
53
+ .toLowerCase();
54
+ }
package/esm/index.js ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Inflection utilities for pluralization and singularization
3
+ *
4
+ * This library provides consistent inflection behavior for PostGraphile and GraphQL codegen.
5
+ * It uses the 'inflection' package with custom overrides for Latin plural suffixes
6
+ * that PostGraphile handles differently than standard English pluralization.
7
+ */
8
+ export * from './pluralize';
9
+ export * from './case';
10
+ export * from './naming';
package/esm/naming.js ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Naming utilities for GraphQL field and query name generation
3
+ */
4
+ import { lcFirst } from './case';
5
+ import { pluralize, singularize } from './pluralize';
6
+ /**
7
+ * Convert a plural PascalCase type name to singular camelCase field name
8
+ * @example "Users" -> "user", "OrderItems" -> "orderItem"
9
+ */
10
+ export function toFieldName(pluralTypeName) {
11
+ return lcFirst(singularize(pluralTypeName));
12
+ }
13
+ /**
14
+ * Convert a singular PascalCase type name to plural camelCase query name
15
+ * @example "User" -> "users", "OrderItem" -> "orderItems"
16
+ */
17
+ export function toQueryName(singularTypeName) {
18
+ return lcFirst(pluralize(singularTypeName));
19
+ }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Pluralization utilities with PostGraphile-compatible Latin suffix handling
3
+ *
4
+ * Uses the 'inflection' package with custom overrides for Latin plural suffixes
5
+ * that PostGraphile handles differently than standard English pluralization.
6
+ */
7
+ import * as inflection from 'inflection';
8
+ /**
9
+ * Latin plural suffixes that inflection handles differently than PostGraphile.
10
+ *
11
+ * The inflection library correctly singularizes Latin words (schemata -> schematum),
12
+ * but PostGraphile uses English-style naming (schemata -> schema).
13
+ *
14
+ * Format: [pluralSuffix, singularSuffix]
15
+ */
16
+ const LATIN_SUFFIX_OVERRIDES = [
17
+ ['schemata', 'schema'],
18
+ ['criteria', 'criterion'],
19
+ ['phenomena', 'phenomenon'],
20
+ ['media', 'medium'],
21
+ ['memoranda', 'memorandum'],
22
+ ['strata', 'stratum'],
23
+ ['curricula', 'curriculum'],
24
+ ['data', 'datum'],
25
+ ];
26
+ /**
27
+ * Convert a word to its singular form with PostGraphile-compatible Latin handling
28
+ * @example "Users" -> "User", "People" -> "Person", "Schemata" -> "Schema", "ApiSchemata" -> "ApiSchema"
29
+ */
30
+ export function singularize(word) {
31
+ const lowerWord = word.toLowerCase();
32
+ for (const [pluralSuffix, singularSuffix] of LATIN_SUFFIX_OVERRIDES) {
33
+ if (lowerWord.endsWith(pluralSuffix)) {
34
+ const suffixStart = word.length - pluralSuffix.length;
35
+ const prefix = word.slice(0, suffixStart);
36
+ const originalSuffix = word.slice(suffixStart);
37
+ const isAllCaps = originalSuffix === originalSuffix.toUpperCase();
38
+ const isUpperSuffix = originalSuffix[0] === originalSuffix[0].toUpperCase();
39
+ let newSuffix;
40
+ if (isAllCaps) {
41
+ newSuffix = singularSuffix.toUpperCase();
42
+ }
43
+ else if (isUpperSuffix) {
44
+ newSuffix = singularSuffix.charAt(0).toUpperCase() + singularSuffix.slice(1);
45
+ }
46
+ else {
47
+ newSuffix = singularSuffix;
48
+ }
49
+ return prefix + newSuffix;
50
+ }
51
+ }
52
+ return inflection.singularize(word);
53
+ }
54
+ /**
55
+ * Convert a word to its plural form
56
+ * @example "User" -> "Users", "Person" -> "People"
57
+ */
58
+ export function pluralize(word) {
59
+ return inflection.pluralize(word);
60
+ }
61
+ /**
62
+ * Pluralize/singularize only the final segment of a compound name.
63
+ * This is important for names like "user_profiles" where only "profiles" should be pluralized.
64
+ *
65
+ * @param fn - The singularize or pluralize function to apply
66
+ * @param str - The string to transform
67
+ * @returns The transformed string with only the final segment changed
68
+ */
69
+ function changeLastWord(fn, str) {
70
+ const matches = str.match(/([A-Z]|_[a-z0-9])[a-z0-9]*_*$/);
71
+ const index = matches ? (matches.index ?? 0) + matches[1].length - 1 : 0;
72
+ const suffixMatches = str.match(/_*$/);
73
+ const suffixIndex = suffixMatches && suffixMatches.index !== undefined
74
+ ? suffixMatches.index
75
+ : str.length;
76
+ const prefix = str.slice(0, index);
77
+ const word = str.slice(index, suffixIndex);
78
+ const suffix = str.slice(suffixIndex);
79
+ return `${prefix}${fn(word)}${suffix}`;
80
+ }
81
+ /**
82
+ * Singularize only the final segment of a compound name
83
+ * @example "user_profiles" -> "user_profile", "UserProfiles" -> "UserProfile"
84
+ */
85
+ export function singularizeLast(str) {
86
+ return changeLastWord(singularize, str);
87
+ }
88
+ /**
89
+ * Pluralize only the final segment of a compound name
90
+ * @example "user_profile" -> "user_profiles", "UserProfile" -> "UserProfiles"
91
+ */
92
+ export function pluralizeLast(str) {
93
+ return changeLastWord(pluralize, str);
94
+ }
95
+ /**
96
+ * Create a distinct plural form, handling cases where singular === plural
97
+ * @example "sheep" -> "sheeps" (forced distinct), "user" -> "users"
98
+ */
99
+ export function distinctPluralize(str) {
100
+ const singular = singularize(str);
101
+ const plural = pluralize(singular);
102
+ if (singular !== plural) {
103
+ return plural;
104
+ }
105
+ if (plural.endsWith('ch') ||
106
+ plural.endsWith('s') ||
107
+ plural.endsWith('sh') ||
108
+ plural.endsWith('x') ||
109
+ plural.endsWith('z')) {
110
+ return `${plural}es`;
111
+ }
112
+ else if (plural.endsWith('y')) {
113
+ return `${plural.slice(0, -1)}ies`;
114
+ }
115
+ else {
116
+ return `${plural}s`;
117
+ }
118
+ }
119
+ /**
120
+ * Create a distinct plural form for the last word in a compound name
121
+ * @example "user_sheep" -> "user_sheeps", "UserSheep" -> "UserSheeps"
122
+ */
123
+ export function distinctPluralizeLast(str) {
124
+ return changeLastWord(distinctPluralize, str);
125
+ }
package/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Inflection utilities for pluralization and singularization
3
+ *
4
+ * This library provides consistent inflection behavior for PostGraphile and GraphQL codegen.
5
+ * It uses the 'inflection' package with custom overrides for Latin plural suffixes
6
+ * that PostGraphile handles differently than standard English pluralization.
7
+ */
8
+ export * from './pluralize';
9
+ export * from './case';
10
+ export * from './naming';
package/index.js ADDED
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /**
18
+ * Inflection utilities for pluralization and singularization
19
+ *
20
+ * This library provides consistent inflection behavior for PostGraphile and GraphQL codegen.
21
+ * It uses the 'inflection' package with custom overrides for Latin plural suffixes
22
+ * that PostGraphile handles differently than standard English pluralization.
23
+ */
24
+ __exportStar(require("./pluralize"), exports);
25
+ __exportStar(require("./case"), exports);
26
+ __exportStar(require("./naming"), exports);
package/naming.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Convert a plural PascalCase type name to singular camelCase field name
3
+ * @example "Users" -> "user", "OrderItems" -> "orderItem"
4
+ */
5
+ export declare function toFieldName(pluralTypeName: string): string;
6
+ /**
7
+ * Convert a singular PascalCase type name to plural camelCase query name
8
+ * @example "User" -> "users", "OrderItem" -> "orderItems"
9
+ */
10
+ export declare function toQueryName(singularTypeName: string): string;
package/naming.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toFieldName = toFieldName;
4
+ exports.toQueryName = toQueryName;
5
+ /**
6
+ * Naming utilities for GraphQL field and query name generation
7
+ */
8
+ const case_1 = require("./case");
9
+ const pluralize_1 = require("./pluralize");
10
+ /**
11
+ * Convert a plural PascalCase type name to singular camelCase field name
12
+ * @example "Users" -> "user", "OrderItems" -> "orderItem"
13
+ */
14
+ function toFieldName(pluralTypeName) {
15
+ return (0, case_1.lcFirst)((0, pluralize_1.singularize)(pluralTypeName));
16
+ }
17
+ /**
18
+ * Convert a singular PascalCase type name to plural camelCase query name
19
+ * @example "User" -> "users", "OrderItem" -> "orderItems"
20
+ */
21
+ function toQueryName(singularTypeName) {
22
+ return (0, case_1.lcFirst)((0, pluralize_1.pluralize)(singularTypeName));
23
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "inflekt",
3
+ "version": "0.1.0",
4
+ "description": "Inflection utilities for pluralization and singularization with PostGraphile-compatible Latin suffix handling",
5
+ "author": "Constructive <developers@constructive.io>",
6
+ "homepage": "https://github.com/constructive-io/dev-utils",
7
+ "license": "MIT",
8
+ "main": "index.js",
9
+ "module": "esm/index.js",
10
+ "types": "index.d.ts",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "scripts": {
16
+ "copy": "makage assets",
17
+ "clean": "makage clean",
18
+ "prepublishOnly": "npm run build",
19
+ "build": "makage build",
20
+ "lint": "eslint . --fix",
21
+ "test": "jest",
22
+ "test:watch": "jest --watch"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/constructive-io/dev-utils"
27
+ },
28
+ "keywords": [
29
+ "inflekt",
30
+ "inflection",
31
+ "pluralize",
32
+ "singularize",
33
+ "postgraphile",
34
+ "graphql",
35
+ "constructive"
36
+ ],
37
+ "bugs": {
38
+ "url": "https://github.com/constructive-io/dev-utils/issues"
39
+ },
40
+ "devDependencies": {
41
+ "makage": "0.1.8"
42
+ },
43
+ "dependencies": {
44
+ "inflection": "^3.0.0",
45
+ "komoji": "^0.7.14"
46
+ },
47
+ "gitHead": "40094d76873a8c84ca007a175b06db5393094f61"
48
+ }
package/pluralize.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Convert a word to its singular form with PostGraphile-compatible Latin handling
3
+ * @example "Users" -> "User", "People" -> "Person", "Schemata" -> "Schema", "ApiSchemata" -> "ApiSchema"
4
+ */
5
+ export declare function singularize(word: string): string;
6
+ /**
7
+ * Convert a word to its plural form
8
+ * @example "User" -> "Users", "Person" -> "People"
9
+ */
10
+ export declare function pluralize(word: string): string;
11
+ /**
12
+ * Singularize only the final segment of a compound name
13
+ * @example "user_profiles" -> "user_profile", "UserProfiles" -> "UserProfile"
14
+ */
15
+ export declare function singularizeLast(str: string): string;
16
+ /**
17
+ * Pluralize only the final segment of a compound name
18
+ * @example "user_profile" -> "user_profiles", "UserProfile" -> "UserProfiles"
19
+ */
20
+ export declare function pluralizeLast(str: string): string;
21
+ /**
22
+ * Create a distinct plural form, handling cases where singular === plural
23
+ * @example "sheep" -> "sheeps" (forced distinct), "user" -> "users"
24
+ */
25
+ export declare function distinctPluralize(str: string): string;
26
+ /**
27
+ * Create a distinct plural form for the last word in a compound name
28
+ * @example "user_sheep" -> "user_sheeps", "UserSheep" -> "UserSheeps"
29
+ */
30
+ export declare function distinctPluralizeLast(str: string): string;
package/pluralize.js ADDED
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.singularize = singularize;
37
+ exports.pluralize = pluralize;
38
+ exports.singularizeLast = singularizeLast;
39
+ exports.pluralizeLast = pluralizeLast;
40
+ exports.distinctPluralize = distinctPluralize;
41
+ exports.distinctPluralizeLast = distinctPluralizeLast;
42
+ /**
43
+ * Pluralization utilities with PostGraphile-compatible Latin suffix handling
44
+ *
45
+ * Uses the 'inflection' package with custom overrides for Latin plural suffixes
46
+ * that PostGraphile handles differently than standard English pluralization.
47
+ */
48
+ const inflection = __importStar(require("inflection"));
49
+ /**
50
+ * Latin plural suffixes that inflection handles differently than PostGraphile.
51
+ *
52
+ * The inflection library correctly singularizes Latin words (schemata -> schematum),
53
+ * but PostGraphile uses English-style naming (schemata -> schema).
54
+ *
55
+ * Format: [pluralSuffix, singularSuffix]
56
+ */
57
+ const LATIN_SUFFIX_OVERRIDES = [
58
+ ['schemata', 'schema'],
59
+ ['criteria', 'criterion'],
60
+ ['phenomena', 'phenomenon'],
61
+ ['media', 'medium'],
62
+ ['memoranda', 'memorandum'],
63
+ ['strata', 'stratum'],
64
+ ['curricula', 'curriculum'],
65
+ ['data', 'datum'],
66
+ ];
67
+ /**
68
+ * Convert a word to its singular form with PostGraphile-compatible Latin handling
69
+ * @example "Users" -> "User", "People" -> "Person", "Schemata" -> "Schema", "ApiSchemata" -> "ApiSchema"
70
+ */
71
+ function singularize(word) {
72
+ const lowerWord = word.toLowerCase();
73
+ for (const [pluralSuffix, singularSuffix] of LATIN_SUFFIX_OVERRIDES) {
74
+ if (lowerWord.endsWith(pluralSuffix)) {
75
+ const suffixStart = word.length - pluralSuffix.length;
76
+ const prefix = word.slice(0, suffixStart);
77
+ const originalSuffix = word.slice(suffixStart);
78
+ const isAllCaps = originalSuffix === originalSuffix.toUpperCase();
79
+ const isUpperSuffix = originalSuffix[0] === originalSuffix[0].toUpperCase();
80
+ let newSuffix;
81
+ if (isAllCaps) {
82
+ newSuffix = singularSuffix.toUpperCase();
83
+ }
84
+ else if (isUpperSuffix) {
85
+ newSuffix = singularSuffix.charAt(0).toUpperCase() + singularSuffix.slice(1);
86
+ }
87
+ else {
88
+ newSuffix = singularSuffix;
89
+ }
90
+ return prefix + newSuffix;
91
+ }
92
+ }
93
+ return inflection.singularize(word);
94
+ }
95
+ /**
96
+ * Convert a word to its plural form
97
+ * @example "User" -> "Users", "Person" -> "People"
98
+ */
99
+ function pluralize(word) {
100
+ return inflection.pluralize(word);
101
+ }
102
+ /**
103
+ * Pluralize/singularize only the final segment of a compound name.
104
+ * This is important for names like "user_profiles" where only "profiles" should be pluralized.
105
+ *
106
+ * @param fn - The singularize or pluralize function to apply
107
+ * @param str - The string to transform
108
+ * @returns The transformed string with only the final segment changed
109
+ */
110
+ function changeLastWord(fn, str) {
111
+ const matches = str.match(/([A-Z]|_[a-z0-9])[a-z0-9]*_*$/);
112
+ const index = matches ? (matches.index ?? 0) + matches[1].length - 1 : 0;
113
+ const suffixMatches = str.match(/_*$/);
114
+ const suffixIndex = suffixMatches && suffixMatches.index !== undefined
115
+ ? suffixMatches.index
116
+ : str.length;
117
+ const prefix = str.slice(0, index);
118
+ const word = str.slice(index, suffixIndex);
119
+ const suffix = str.slice(suffixIndex);
120
+ return `${prefix}${fn(word)}${suffix}`;
121
+ }
122
+ /**
123
+ * Singularize only the final segment of a compound name
124
+ * @example "user_profiles" -> "user_profile", "UserProfiles" -> "UserProfile"
125
+ */
126
+ function singularizeLast(str) {
127
+ return changeLastWord(singularize, str);
128
+ }
129
+ /**
130
+ * Pluralize only the final segment of a compound name
131
+ * @example "user_profile" -> "user_profiles", "UserProfile" -> "UserProfiles"
132
+ */
133
+ function pluralizeLast(str) {
134
+ return changeLastWord(pluralize, str);
135
+ }
136
+ /**
137
+ * Create a distinct plural form, handling cases where singular === plural
138
+ * @example "sheep" -> "sheeps" (forced distinct), "user" -> "users"
139
+ */
140
+ function distinctPluralize(str) {
141
+ const singular = singularize(str);
142
+ const plural = pluralize(singular);
143
+ if (singular !== plural) {
144
+ return plural;
145
+ }
146
+ if (plural.endsWith('ch') ||
147
+ plural.endsWith('s') ||
148
+ plural.endsWith('sh') ||
149
+ plural.endsWith('x') ||
150
+ plural.endsWith('z')) {
151
+ return `${plural}es`;
152
+ }
153
+ else if (plural.endsWith('y')) {
154
+ return `${plural.slice(0, -1)}ies`;
155
+ }
156
+ else {
157
+ return `${plural}s`;
158
+ }
159
+ }
160
+ /**
161
+ * Create a distinct plural form for the last word in a compound name
162
+ * @example "user_sheep" -> "user_sheeps", "UserSheep" -> "UserSheeps"
163
+ */
164
+ function distinctPluralizeLast(str) {
165
+ return changeLastWord(distinctPluralize, str);
166
+ }