inflekt 0.3.3 → 0.5.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/README.md +17 -10
- package/case.d.ts +22 -8
- package/case.js +39 -18
- package/esm/case.js +36 -17
- package/esm/index.js +1 -0
- package/esm/matching.js +88 -0
- package/esm/transform-keys.js +2 -2
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/matching.d.ts +61 -0
- package/matching.js +94 -0
- package/package.json +2 -2
- package/transform-keys.d.ts +2 -2
- package/transform-keys.js +2 -2
package/README.md
CHANGED
|
@@ -34,7 +34,9 @@ import {
|
|
|
34
34
|
distinctPluralize,
|
|
35
35
|
lcFirst,
|
|
36
36
|
ucFirst,
|
|
37
|
-
|
|
37
|
+
toCamelCase,
|
|
38
|
+
toPascalCase,
|
|
39
|
+
toScreamingSnake,
|
|
38
40
|
underscore,
|
|
39
41
|
toFieldName,
|
|
40
42
|
toQueryName,
|
|
@@ -58,9 +60,12 @@ pluralizeLast('UserProfile'); // 'UserProfiles'
|
|
|
58
60
|
lcFirst('UserProfile'); // 'userProfile'
|
|
59
61
|
ucFirst('userProfile'); // 'UserProfile'
|
|
60
62
|
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
63
|
+
// Case conversions (handles underscores, hyphens, and camelCase boundaries)
|
|
64
|
+
toCamelCase('user_profile'); // 'userProfile'
|
|
65
|
+
toCamelCase('user-profile'); // 'userProfile'
|
|
66
|
+
toPascalCase('user_profile'); // 'UserProfile'
|
|
67
|
+
toPascalCase('user-profile'); // 'UserProfile'
|
|
68
|
+
toScreamingSnake('userProfile'); // 'USER_PROFILE'
|
|
64
69
|
underscore('UserProfile'); // 'user_profile'
|
|
65
70
|
|
|
66
71
|
// GraphQL naming helpers
|
|
@@ -72,7 +77,7 @@ const apiResponse = {
|
|
|
72
77
|
user_name: 'John',
|
|
73
78
|
order_items: [{ item_id: 1, product_name: 'Widget' }]
|
|
74
79
|
};
|
|
75
|
-
inflektTree(apiResponse,
|
|
80
|
+
inflektTree(apiResponse, toCamelCase);
|
|
76
81
|
// Result: { userName: 'John', orderItems: [{ itemId: 1, productName: 'Widget' }] }
|
|
77
82
|
```
|
|
78
83
|
|
|
@@ -91,7 +96,9 @@ inflektTree(apiResponse, (key) => camelize(key, true));
|
|
|
91
96
|
|
|
92
97
|
- `lcFirst(str)` - Convert first character to lowercase (PascalCase to camelCase)
|
|
93
98
|
- `ucFirst(str)` - Convert first character to uppercase (camelCase to PascalCase)
|
|
94
|
-
- `
|
|
99
|
+
- `toCamelCase(str)` - Convert to camelCase (handles underscores, hyphens, and PascalCase input)
|
|
100
|
+
- `toPascalCase(str)` - Convert to PascalCase (handles underscores, hyphens, and camelCase input)
|
|
101
|
+
- `toScreamingSnake(str)` - Convert to SCREAMING_SNAKE_CASE
|
|
95
102
|
- `underscore(str)` - Convert PascalCase/camelCase to snake_case
|
|
96
103
|
- `fixCapitalisedPlural(str)` - Fix capitalized S after numbers (e.g., `Table1S` -> `Table1s`)
|
|
97
104
|
|
|
@@ -124,7 +131,7 @@ const apiResponse = {
|
|
|
124
131
|
]
|
|
125
132
|
};
|
|
126
133
|
|
|
127
|
-
const result = inflektTree(apiResponse,
|
|
134
|
+
const result = inflektTree(apiResponse, toCamelCase);
|
|
128
135
|
// Result:
|
|
129
136
|
// {
|
|
130
137
|
// userName: 'John',
|
|
@@ -156,18 +163,18 @@ const input = {
|
|
|
156
163
|
_metadata: { _internal: true }
|
|
157
164
|
};
|
|
158
165
|
|
|
159
|
-
const result = inflektTree(input,
|
|
166
|
+
const result = inflektTree(input, toCamelCase, {
|
|
160
167
|
skip: (key) => key.startsWith('_')
|
|
161
168
|
});
|
|
162
169
|
// Result: { userName: 'John', _private_field: 'secret', _metadata: { _internal: true } }
|
|
163
170
|
|
|
164
171
|
// Skip specific keys
|
|
165
|
-
const result2 = inflektTree(input,
|
|
172
|
+
const result2 = inflektTree(input, toCamelCase, {
|
|
166
173
|
skip: (key) => key === 'created_at' || key === 'updated_at'
|
|
167
174
|
});
|
|
168
175
|
|
|
169
176
|
// Skip based on path depth (only transform top 2 levels)
|
|
170
|
-
const result3 = inflektTree(deepObject,
|
|
177
|
+
const result3 = inflektTree(deepObject, toCamelCase, {
|
|
171
178
|
skip: (key, path) => path.length > 1
|
|
172
179
|
});
|
|
173
180
|
```
|
package/case.d.ts
CHANGED
|
@@ -17,17 +17,31 @@ export declare function ucFirst(str: string): string;
|
|
|
17
17
|
* @example "Table1S" -> "Table1s"
|
|
18
18
|
*/
|
|
19
19
|
export declare function fixCapitalisedPlural(str: string): string;
|
|
20
|
-
/**
|
|
21
|
-
* Convert snake_case to PascalCase (or camelCase if lowFirstLetter is true)
|
|
22
|
-
* @param str - The snake_case string to convert
|
|
23
|
-
* @param lowFirstLetter - If true, returns camelCase instead of PascalCase
|
|
24
|
-
* @example camelize('user_profile') -> 'UserProfile'
|
|
25
|
-
* @example camelize('user_profile', true) -> 'userProfile'
|
|
26
|
-
*/
|
|
27
|
-
export declare function camelize(str: string, lowFirstLetter?: boolean): string;
|
|
28
20
|
/**
|
|
29
21
|
* Convert PascalCase or camelCase to snake_case
|
|
30
22
|
* @example underscore('UserProfile') -> 'user_profile'
|
|
31
23
|
* @example underscore('userProfile') -> 'user_profile'
|
|
32
24
|
*/
|
|
33
25
|
export declare function underscore(str: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* Convert a hyphenated, underscored, or already-camelCased string to camelCase.
|
|
28
|
+
* Handles both `-` and `_` delimiters.
|
|
29
|
+
* @example toCamelCase('user-profile') -> 'userProfile'
|
|
30
|
+
* @example toCamelCase('user_profile') -> 'userProfile'
|
|
31
|
+
* @example toCamelCase('UserProfile') -> 'userProfile'
|
|
32
|
+
*/
|
|
33
|
+
export declare function toCamelCase(str: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Convert a hyphenated, underscored, or already-camelCased string to PascalCase.
|
|
36
|
+
* Handles both `-` and `_` delimiters.
|
|
37
|
+
* @example toPascalCase('user-profile') -> 'UserProfile'
|
|
38
|
+
* @example toPascalCase('user_profile') -> 'UserProfile'
|
|
39
|
+
* @example toPascalCase('userProfile') -> 'UserProfile'
|
|
40
|
+
*/
|
|
41
|
+
export declare function toPascalCase(str: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* Convert a camelCase or PascalCase string to SCREAMING_SNAKE_CASE.
|
|
44
|
+
* @example toScreamingSnake('userProfile') -> 'USER_PROFILE'
|
|
45
|
+
* @example toScreamingSnake('UserProfile') -> 'USER_PROFILE'
|
|
46
|
+
*/
|
|
47
|
+
export declare function toScreamingSnake(str: string): string;
|
package/case.js
CHANGED
|
@@ -6,8 +6,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.lcFirst = lcFirst;
|
|
7
7
|
exports.ucFirst = ucFirst;
|
|
8
8
|
exports.fixCapitalisedPlural = fixCapitalisedPlural;
|
|
9
|
-
exports.camelize = camelize;
|
|
10
9
|
exports.underscore = underscore;
|
|
10
|
+
exports.toCamelCase = toCamelCase;
|
|
11
|
+
exports.toPascalCase = toPascalCase;
|
|
12
|
+
exports.toScreamingSnake = toScreamingSnake;
|
|
11
13
|
/**
|
|
12
14
|
* Convert PascalCase to camelCase (lowercase first character)
|
|
13
15
|
* @example "UserProfile" -> "userProfile"
|
|
@@ -30,23 +32,6 @@ function ucFirst(str) {
|
|
|
30
32
|
function fixCapitalisedPlural(str) {
|
|
31
33
|
return str.replace(/[0-9]S(?=[A-Z]|$)/g, (match) => match.toLowerCase());
|
|
32
34
|
}
|
|
33
|
-
/**
|
|
34
|
-
* Convert snake_case to PascalCase (or camelCase if lowFirstLetter is true)
|
|
35
|
-
* @param str - The snake_case string to convert
|
|
36
|
-
* @param lowFirstLetter - If true, returns camelCase instead of PascalCase
|
|
37
|
-
* @example camelize('user_profile') -> 'UserProfile'
|
|
38
|
-
* @example camelize('user_profile', true) -> 'userProfile'
|
|
39
|
-
*/
|
|
40
|
-
function camelize(str, lowFirstLetter) {
|
|
41
|
-
const result = str
|
|
42
|
-
.split('_')
|
|
43
|
-
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
44
|
-
.join('');
|
|
45
|
-
if (lowFirstLetter) {
|
|
46
|
-
return lcFirst(result);
|
|
47
|
-
}
|
|
48
|
-
return result;
|
|
49
|
-
}
|
|
50
35
|
/**
|
|
51
36
|
* Convert PascalCase or camelCase to snake_case
|
|
52
37
|
* @example underscore('UserProfile') -> 'user_profile'
|
|
@@ -58,3 +43,39 @@ function underscore(str) {
|
|
|
58
43
|
.replace(/^_/, '')
|
|
59
44
|
.toLowerCase();
|
|
60
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Convert a hyphenated, underscored, or already-camelCased string to camelCase.
|
|
48
|
+
* Handles both `-` and `_` delimiters.
|
|
49
|
+
* @example toCamelCase('user-profile') -> 'userProfile'
|
|
50
|
+
* @example toCamelCase('user_profile') -> 'userProfile'
|
|
51
|
+
* @example toCamelCase('UserProfile') -> 'userProfile'
|
|
52
|
+
*/
|
|
53
|
+
function toCamelCase(str) {
|
|
54
|
+
return str
|
|
55
|
+
.replace(/[-_](.)/g, (_, char) => char.toUpperCase())
|
|
56
|
+
.replace(/^(.)/, (_, char) => char.toLowerCase());
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Convert a hyphenated, underscored, or already-camelCased string to PascalCase.
|
|
60
|
+
* Handles both `-` and `_` delimiters.
|
|
61
|
+
* @example toPascalCase('user-profile') -> 'UserProfile'
|
|
62
|
+
* @example toPascalCase('user_profile') -> 'UserProfile'
|
|
63
|
+
* @example toPascalCase('userProfile') -> 'UserProfile'
|
|
64
|
+
*/
|
|
65
|
+
function toPascalCase(str) {
|
|
66
|
+
return str
|
|
67
|
+
.replace(/[-_](.)/g, (_, char) => char.toUpperCase())
|
|
68
|
+
.replace(/^(.)/, (_, char) => char.toUpperCase());
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Convert a camelCase or PascalCase string to SCREAMING_SNAKE_CASE.
|
|
72
|
+
* @example toScreamingSnake('userProfile') -> 'USER_PROFILE'
|
|
73
|
+
* @example toScreamingSnake('UserProfile') -> 'USER_PROFILE'
|
|
74
|
+
*/
|
|
75
|
+
function toScreamingSnake(str) {
|
|
76
|
+
return str
|
|
77
|
+
.replace(/([A-Z])/g, '_$1')
|
|
78
|
+
.replace(/[-\s]/g, '_')
|
|
79
|
+
.toUpperCase()
|
|
80
|
+
.replace(/^_/, '');
|
|
81
|
+
}
|
package/esm/case.js
CHANGED
|
@@ -23,23 +23,6 @@ export function ucFirst(str) {
|
|
|
23
23
|
export function fixCapitalisedPlural(str) {
|
|
24
24
|
return str.replace(/[0-9]S(?=[A-Z]|$)/g, (match) => match.toLowerCase());
|
|
25
25
|
}
|
|
26
|
-
/**
|
|
27
|
-
* Convert snake_case to PascalCase (or camelCase if lowFirstLetter is true)
|
|
28
|
-
* @param str - The snake_case string to convert
|
|
29
|
-
* @param lowFirstLetter - If true, returns camelCase instead of PascalCase
|
|
30
|
-
* @example camelize('user_profile') -> 'UserProfile'
|
|
31
|
-
* @example camelize('user_profile', true) -> 'userProfile'
|
|
32
|
-
*/
|
|
33
|
-
export function camelize(str, lowFirstLetter) {
|
|
34
|
-
const result = str
|
|
35
|
-
.split('_')
|
|
36
|
-
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
37
|
-
.join('');
|
|
38
|
-
if (lowFirstLetter) {
|
|
39
|
-
return lcFirst(result);
|
|
40
|
-
}
|
|
41
|
-
return result;
|
|
42
|
-
}
|
|
43
26
|
/**
|
|
44
27
|
* Convert PascalCase or camelCase to snake_case
|
|
45
28
|
* @example underscore('UserProfile') -> 'user_profile'
|
|
@@ -51,3 +34,39 @@ export function underscore(str) {
|
|
|
51
34
|
.replace(/^_/, '')
|
|
52
35
|
.toLowerCase();
|
|
53
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Convert a hyphenated, underscored, or already-camelCased string to camelCase.
|
|
39
|
+
* Handles both `-` and `_` delimiters.
|
|
40
|
+
* @example toCamelCase('user-profile') -> 'userProfile'
|
|
41
|
+
* @example toCamelCase('user_profile') -> 'userProfile'
|
|
42
|
+
* @example toCamelCase('UserProfile') -> 'userProfile'
|
|
43
|
+
*/
|
|
44
|
+
export function toCamelCase(str) {
|
|
45
|
+
return str
|
|
46
|
+
.replace(/[-_](.)/g, (_, char) => char.toUpperCase())
|
|
47
|
+
.replace(/^(.)/, (_, char) => char.toLowerCase());
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Convert a hyphenated, underscored, or already-camelCased string to PascalCase.
|
|
51
|
+
* Handles both `-` and `_` delimiters.
|
|
52
|
+
* @example toPascalCase('user-profile') -> 'UserProfile'
|
|
53
|
+
* @example toPascalCase('user_profile') -> 'UserProfile'
|
|
54
|
+
* @example toPascalCase('userProfile') -> 'UserProfile'
|
|
55
|
+
*/
|
|
56
|
+
export function toPascalCase(str) {
|
|
57
|
+
return str
|
|
58
|
+
.replace(/[-_](.)/g, (_, char) => char.toUpperCase())
|
|
59
|
+
.replace(/^(.)/, (_, char) => char.toUpperCase());
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Convert a camelCase or PascalCase string to SCREAMING_SNAKE_CASE.
|
|
63
|
+
* @example toScreamingSnake('userProfile') -> 'USER_PROFILE'
|
|
64
|
+
* @example toScreamingSnake('UserProfile') -> 'USER_PROFILE'
|
|
65
|
+
*/
|
|
66
|
+
export function toScreamingSnake(str) {
|
|
67
|
+
return str
|
|
68
|
+
.replace(/([A-Z])/g, '_$1')
|
|
69
|
+
.replace(/[-\s]/g, '_')
|
|
70
|
+
.toUpperCase()
|
|
71
|
+
.replace(/^_/, '');
|
|
72
|
+
}
|
package/esm/index.js
CHANGED
package/esm/matching.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Name matching utilities for resolving PostGraphile v5 naming mismatches.
|
|
3
|
+
*
|
|
4
|
+
* PostGraphile v5 uses different inflection conventions in different contexts:
|
|
5
|
+
* - Table types are PascalCase (e.g., "Shipment", "DeliveryZone")
|
|
6
|
+
* - Relation codec names are raw snake_case or camelCase (e.g., "shipments", "deliveryZones")
|
|
7
|
+
*
|
|
8
|
+
* These helpers provide a single, shared way to normalize and compare names
|
|
9
|
+
* across those boundaries instead of duplicating fuzzy-match logic in every consumer.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Normalize a name for case-insensitive, delimiter-insensitive comparison.
|
|
13
|
+
* Strips underscores and lowercases.
|
|
14
|
+
*
|
|
15
|
+
* @example normalizeName("delivery_zone") // "deliveryzone"
|
|
16
|
+
* @example normalizeName("DeliveryZone") // "deliveryzone"
|
|
17
|
+
* @example normalizeName("deliveryZones") // "deliveryzones"
|
|
18
|
+
*/
|
|
19
|
+
export function normalizeName(name) {
|
|
20
|
+
return name.toLowerCase().replace(/_/g, '');
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Normalize a name to its singular base form for comparison.
|
|
24
|
+
* Strips underscores, lowercases, and removes a trailing 's' when present.
|
|
25
|
+
*
|
|
26
|
+
* @example normalizeNameSingular("shipments") // "shipment"
|
|
27
|
+
* @example normalizeNameSingular("DeliveryZone") // "deliveryzone"
|
|
28
|
+
* @example normalizeNameSingular("routes") // "route"
|
|
29
|
+
*/
|
|
30
|
+
export function normalizeNameSingular(name) {
|
|
31
|
+
const normalized = normalizeName(name);
|
|
32
|
+
return normalized.endsWith('s') ? normalized.slice(0, -1) : normalized;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Find a matching item by name using exact match first, then fuzzy
|
|
36
|
+
* case-insensitive / plural-insensitive fallback.
|
|
37
|
+
*
|
|
38
|
+
* This is the single shared implementation for resolving relation target names
|
|
39
|
+
* to table definitions, replacing ad-hoc fuzzy matching scattered across consumers.
|
|
40
|
+
*
|
|
41
|
+
* @param items - Array of items to search through
|
|
42
|
+
* @param targetName - The name to find (may be PascalCase, snake_case, plural, etc.)
|
|
43
|
+
* @param getName - Accessor to extract the comparable name from each item
|
|
44
|
+
* @returns The matched item, or undefined if no match found
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* // Find a table by its relation target name
|
|
48
|
+
* const table = fuzzyFindByName(allTables, "shipments", t => t.name);
|
|
49
|
+
* // Matches Table with name "Shipment"
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* // Works with snake_case codec names too
|
|
53
|
+
* const table = fuzzyFindByName(allTables, "delivery_zone", t => t.name);
|
|
54
|
+
* // Matches Table with name "DeliveryZone"
|
|
55
|
+
*/
|
|
56
|
+
export function fuzzyFindByName(items, targetName, getName) {
|
|
57
|
+
// 1. Exact match (fast path)
|
|
58
|
+
const exact = items.find((item) => getName(item) === targetName);
|
|
59
|
+
if (exact)
|
|
60
|
+
return exact;
|
|
61
|
+
// 2. Fuzzy match: case-insensitive, strip underscores, optional trailing 's'
|
|
62
|
+
const targetNormalized = normalizeName(targetName);
|
|
63
|
+
const targetBase = normalizeNameSingular(targetName);
|
|
64
|
+
return items.find((item) => {
|
|
65
|
+
const itemNormalized = normalizeName(getName(item));
|
|
66
|
+
return itemNormalized === targetNormalized || itemNormalized === targetBase;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Check whether two names refer to the same entity, ignoring case,
|
|
71
|
+
* underscores, and singular/plural differences.
|
|
72
|
+
*
|
|
73
|
+
* @example namesMatch("shipments", "Shipment") // true
|
|
74
|
+
* @example namesMatch("delivery_zone", "DeliveryZone") // true
|
|
75
|
+
* @example namesMatch("Route", "routes") // true
|
|
76
|
+
* @example namesMatch("User", "Post") // false
|
|
77
|
+
*/
|
|
78
|
+
export function namesMatch(a, b) {
|
|
79
|
+
if (a === b)
|
|
80
|
+
return true;
|
|
81
|
+
const aNorm = normalizeName(a);
|
|
82
|
+
const bNorm = normalizeName(b);
|
|
83
|
+
if (aNorm === bNorm)
|
|
84
|
+
return true;
|
|
85
|
+
const aBase = normalizeNameSingular(a);
|
|
86
|
+
const bBase = normalizeNameSingular(b);
|
|
87
|
+
return aBase === bBase;
|
|
88
|
+
}
|
package/esm/transform-keys.js
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* @example
|
|
15
15
|
* // Convert snake_case keys to camelCase
|
|
16
16
|
* const apiResponse = { user_name: 'John', order_items: [{ item_id: 1 }] };
|
|
17
|
-
* const result = inflektTree(apiResponse,
|
|
17
|
+
* const result = inflektTree(apiResponse, toCamelCase);
|
|
18
18
|
* // Result: { userName: 'John', orderItems: [{ itemId: 1 }] }
|
|
19
19
|
*
|
|
20
20
|
* @example
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
*
|
|
26
26
|
* @example
|
|
27
27
|
* // Skip keys starting with underscore
|
|
28
|
-
* inflektTree(obj,
|
|
28
|
+
* inflektTree(obj, toCamelCase, {
|
|
29
29
|
* skip: (key) => key.startsWith('_')
|
|
30
30
|
* });
|
|
31
31
|
*/
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -24,4 +24,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
24
24
|
__exportStar(require("./pluralize"), exports);
|
|
25
25
|
__exportStar(require("./case"), exports);
|
|
26
26
|
__exportStar(require("./naming"), exports);
|
|
27
|
+
__exportStar(require("./matching"), exports);
|
|
27
28
|
__exportStar(require("./transform-keys"), exports);
|
package/matching.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Name matching utilities for resolving PostGraphile v5 naming mismatches.
|
|
3
|
+
*
|
|
4
|
+
* PostGraphile v5 uses different inflection conventions in different contexts:
|
|
5
|
+
* - Table types are PascalCase (e.g., "Shipment", "DeliveryZone")
|
|
6
|
+
* - Relation codec names are raw snake_case or camelCase (e.g., "shipments", "deliveryZones")
|
|
7
|
+
*
|
|
8
|
+
* These helpers provide a single, shared way to normalize and compare names
|
|
9
|
+
* across those boundaries instead of duplicating fuzzy-match logic in every consumer.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Normalize a name for case-insensitive, delimiter-insensitive comparison.
|
|
13
|
+
* Strips underscores and lowercases.
|
|
14
|
+
*
|
|
15
|
+
* @example normalizeName("delivery_zone") // "deliveryzone"
|
|
16
|
+
* @example normalizeName("DeliveryZone") // "deliveryzone"
|
|
17
|
+
* @example normalizeName("deliveryZones") // "deliveryzones"
|
|
18
|
+
*/
|
|
19
|
+
export declare function normalizeName(name: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Normalize a name to its singular base form for comparison.
|
|
22
|
+
* Strips underscores, lowercases, and removes a trailing 's' when present.
|
|
23
|
+
*
|
|
24
|
+
* @example normalizeNameSingular("shipments") // "shipment"
|
|
25
|
+
* @example normalizeNameSingular("DeliveryZone") // "deliveryzone"
|
|
26
|
+
* @example normalizeNameSingular("routes") // "route"
|
|
27
|
+
*/
|
|
28
|
+
export declare function normalizeNameSingular(name: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* Find a matching item by name using exact match first, then fuzzy
|
|
31
|
+
* case-insensitive / plural-insensitive fallback.
|
|
32
|
+
*
|
|
33
|
+
* This is the single shared implementation for resolving relation target names
|
|
34
|
+
* to table definitions, replacing ad-hoc fuzzy matching scattered across consumers.
|
|
35
|
+
*
|
|
36
|
+
* @param items - Array of items to search through
|
|
37
|
+
* @param targetName - The name to find (may be PascalCase, snake_case, plural, etc.)
|
|
38
|
+
* @param getName - Accessor to extract the comparable name from each item
|
|
39
|
+
* @returns The matched item, or undefined if no match found
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* // Find a table by its relation target name
|
|
43
|
+
* const table = fuzzyFindByName(allTables, "shipments", t => t.name);
|
|
44
|
+
* // Matches Table with name "Shipment"
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* // Works with snake_case codec names too
|
|
48
|
+
* const table = fuzzyFindByName(allTables, "delivery_zone", t => t.name);
|
|
49
|
+
* // Matches Table with name "DeliveryZone"
|
|
50
|
+
*/
|
|
51
|
+
export declare function fuzzyFindByName<T>(items: T[], targetName: string, getName: (item: T) => string): T | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Check whether two names refer to the same entity, ignoring case,
|
|
54
|
+
* underscores, and singular/plural differences.
|
|
55
|
+
*
|
|
56
|
+
* @example namesMatch("shipments", "Shipment") // true
|
|
57
|
+
* @example namesMatch("delivery_zone", "DeliveryZone") // true
|
|
58
|
+
* @example namesMatch("Route", "routes") // true
|
|
59
|
+
* @example namesMatch("User", "Post") // false
|
|
60
|
+
*/
|
|
61
|
+
export declare function namesMatch(a: string, b: string): boolean;
|
package/matching.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Name matching utilities for resolving PostGraphile v5 naming mismatches.
|
|
4
|
+
*
|
|
5
|
+
* PostGraphile v5 uses different inflection conventions in different contexts:
|
|
6
|
+
* - Table types are PascalCase (e.g., "Shipment", "DeliveryZone")
|
|
7
|
+
* - Relation codec names are raw snake_case or camelCase (e.g., "shipments", "deliveryZones")
|
|
8
|
+
*
|
|
9
|
+
* These helpers provide a single, shared way to normalize and compare names
|
|
10
|
+
* across those boundaries instead of duplicating fuzzy-match logic in every consumer.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.normalizeName = normalizeName;
|
|
14
|
+
exports.normalizeNameSingular = normalizeNameSingular;
|
|
15
|
+
exports.fuzzyFindByName = fuzzyFindByName;
|
|
16
|
+
exports.namesMatch = namesMatch;
|
|
17
|
+
/**
|
|
18
|
+
* Normalize a name for case-insensitive, delimiter-insensitive comparison.
|
|
19
|
+
* Strips underscores and lowercases.
|
|
20
|
+
*
|
|
21
|
+
* @example normalizeName("delivery_zone") // "deliveryzone"
|
|
22
|
+
* @example normalizeName("DeliveryZone") // "deliveryzone"
|
|
23
|
+
* @example normalizeName("deliveryZones") // "deliveryzones"
|
|
24
|
+
*/
|
|
25
|
+
function normalizeName(name) {
|
|
26
|
+
return name.toLowerCase().replace(/_/g, '');
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Normalize a name to its singular base form for comparison.
|
|
30
|
+
* Strips underscores, lowercases, and removes a trailing 's' when present.
|
|
31
|
+
*
|
|
32
|
+
* @example normalizeNameSingular("shipments") // "shipment"
|
|
33
|
+
* @example normalizeNameSingular("DeliveryZone") // "deliveryzone"
|
|
34
|
+
* @example normalizeNameSingular("routes") // "route"
|
|
35
|
+
*/
|
|
36
|
+
function normalizeNameSingular(name) {
|
|
37
|
+
const normalized = normalizeName(name);
|
|
38
|
+
return normalized.endsWith('s') ? normalized.slice(0, -1) : normalized;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Find a matching item by name using exact match first, then fuzzy
|
|
42
|
+
* case-insensitive / plural-insensitive fallback.
|
|
43
|
+
*
|
|
44
|
+
* This is the single shared implementation for resolving relation target names
|
|
45
|
+
* to table definitions, replacing ad-hoc fuzzy matching scattered across consumers.
|
|
46
|
+
*
|
|
47
|
+
* @param items - Array of items to search through
|
|
48
|
+
* @param targetName - The name to find (may be PascalCase, snake_case, plural, etc.)
|
|
49
|
+
* @param getName - Accessor to extract the comparable name from each item
|
|
50
|
+
* @returns The matched item, or undefined if no match found
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* // Find a table by its relation target name
|
|
54
|
+
* const table = fuzzyFindByName(allTables, "shipments", t => t.name);
|
|
55
|
+
* // Matches Table with name "Shipment"
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* // Works with snake_case codec names too
|
|
59
|
+
* const table = fuzzyFindByName(allTables, "delivery_zone", t => t.name);
|
|
60
|
+
* // Matches Table with name "DeliveryZone"
|
|
61
|
+
*/
|
|
62
|
+
function fuzzyFindByName(items, targetName, getName) {
|
|
63
|
+
// 1. Exact match (fast path)
|
|
64
|
+
const exact = items.find((item) => getName(item) === targetName);
|
|
65
|
+
if (exact)
|
|
66
|
+
return exact;
|
|
67
|
+
// 2. Fuzzy match: case-insensitive, strip underscores, optional trailing 's'
|
|
68
|
+
const targetNormalized = normalizeName(targetName);
|
|
69
|
+
const targetBase = normalizeNameSingular(targetName);
|
|
70
|
+
return items.find((item) => {
|
|
71
|
+
const itemNormalized = normalizeName(getName(item));
|
|
72
|
+
return itemNormalized === targetNormalized || itemNormalized === targetBase;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Check whether two names refer to the same entity, ignoring case,
|
|
77
|
+
* underscores, and singular/plural differences.
|
|
78
|
+
*
|
|
79
|
+
* @example namesMatch("shipments", "Shipment") // true
|
|
80
|
+
* @example namesMatch("delivery_zone", "DeliveryZone") // true
|
|
81
|
+
* @example namesMatch("Route", "routes") // true
|
|
82
|
+
* @example namesMatch("User", "Post") // false
|
|
83
|
+
*/
|
|
84
|
+
function namesMatch(a, b) {
|
|
85
|
+
if (a === b)
|
|
86
|
+
return true;
|
|
87
|
+
const aNorm = normalizeName(a);
|
|
88
|
+
const bNorm = normalizeName(b);
|
|
89
|
+
if (aNorm === bNorm)
|
|
90
|
+
return true;
|
|
91
|
+
const aBase = normalizeNameSingular(a);
|
|
92
|
+
const bBase = normalizeNameSingular(b);
|
|
93
|
+
return aBase === bBase;
|
|
94
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inflekt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
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": "29ca836c31e0c462da8436db1970f215cecc53e2"
|
|
47
47
|
}
|
package/transform-keys.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export interface InflektTreeOptions {
|
|
|
24
24
|
* @example
|
|
25
25
|
* // Convert snake_case keys to camelCase
|
|
26
26
|
* const apiResponse = { user_name: 'John', order_items: [{ item_id: 1 }] };
|
|
27
|
-
* const result = inflektTree(apiResponse,
|
|
27
|
+
* const result = inflektTree(apiResponse, toCamelCase);
|
|
28
28
|
* // Result: { userName: 'John', orderItems: [{ itemId: 1 }] }
|
|
29
29
|
*
|
|
30
30
|
* @example
|
|
@@ -35,7 +35,7 @@ export interface InflektTreeOptions {
|
|
|
35
35
|
*
|
|
36
36
|
* @example
|
|
37
37
|
* // Skip keys starting with underscore
|
|
38
|
-
* inflektTree(obj,
|
|
38
|
+
* inflektTree(obj, toCamelCase, {
|
|
39
39
|
* skip: (key) => key.startsWith('_')
|
|
40
40
|
* });
|
|
41
41
|
*/
|
package/transform-keys.js
CHANGED
|
@@ -17,7 +17,7 @@ exports.inflektTree = inflektTree;
|
|
|
17
17
|
* @example
|
|
18
18
|
* // Convert snake_case keys to camelCase
|
|
19
19
|
* const apiResponse = { user_name: 'John', order_items: [{ item_id: 1 }] };
|
|
20
|
-
* const result = inflektTree(apiResponse,
|
|
20
|
+
* const result = inflektTree(apiResponse, toCamelCase);
|
|
21
21
|
* // Result: { userName: 'John', orderItems: [{ itemId: 1 }] }
|
|
22
22
|
*
|
|
23
23
|
* @example
|
|
@@ -28,7 +28,7 @@ exports.inflektTree = inflektTree;
|
|
|
28
28
|
*
|
|
29
29
|
* @example
|
|
30
30
|
* // Skip keys starting with underscore
|
|
31
|
-
* inflektTree(obj,
|
|
31
|
+
* inflektTree(obj, toCamelCase, {
|
|
32
32
|
* skip: (key) => key.startsWith('_')
|
|
33
33
|
* });
|
|
34
34
|
*/
|