inflekt 0.3.3 → 0.5.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/case.d.ts CHANGED
@@ -31,3 +31,24 @@ export declare function camelize(str: string, lowFirstLetter?: boolean): string;
31
31
  * @example underscore('userProfile') -> 'user_profile'
32
32
  */
33
33
  export declare function underscore(str: string): string;
34
+ /**
35
+ * Convert a hyphenated or underscored string to camelCase.
36
+ * Unlike `camelize`, this also handles hyphens and preserves
37
+ * camelCase boundaries that are already present.
38
+ * @example toCamelCase('user-profile') -> 'userProfile'
39
+ * @example toCamelCase('user_profile') -> 'userProfile'
40
+ */
41
+ export declare function toCamelCase(str: string): string;
42
+ /**
43
+ * Convert a hyphenated or underscored string to PascalCase.
44
+ * Unlike `camelize`, this also handles hyphens.
45
+ * @example toPascalCase('user-profile') -> 'UserProfile'
46
+ * @example toPascalCase('user_profile') -> 'UserProfile'
47
+ */
48
+ export declare function toPascalCase(str: string): string;
49
+ /**
50
+ * Convert a camelCase or PascalCase string to SCREAMING_SNAKE_CASE.
51
+ * @example toScreamingSnake('userProfile') -> 'USER_PROFILE'
52
+ * @example toScreamingSnake('UserProfile') -> 'USER_PROFILE'
53
+ */
54
+ export declare function toScreamingSnake(str: string): string;
package/case.js CHANGED
@@ -8,6 +8,9 @@ exports.ucFirst = ucFirst;
8
8
  exports.fixCapitalisedPlural = fixCapitalisedPlural;
9
9
  exports.camelize = camelize;
10
10
  exports.underscore = underscore;
11
+ exports.toCamelCase = toCamelCase;
12
+ exports.toPascalCase = toPascalCase;
13
+ exports.toScreamingSnake = toScreamingSnake;
11
14
  /**
12
15
  * Convert PascalCase to camelCase (lowercase first character)
13
16
  * @example "UserProfile" -> "userProfile"
@@ -58,3 +61,38 @@ function underscore(str) {
58
61
  .replace(/^_/, '')
59
62
  .toLowerCase();
60
63
  }
64
+ /**
65
+ * Convert a hyphenated or underscored string to camelCase.
66
+ * Unlike `camelize`, this also handles hyphens and preserves
67
+ * camelCase boundaries that are already present.
68
+ * @example toCamelCase('user-profile') -> 'userProfile'
69
+ * @example toCamelCase('user_profile') -> 'userProfile'
70
+ */
71
+ function toCamelCase(str) {
72
+ return str
73
+ .replace(/[-_](.)/g, (_, char) => char.toUpperCase())
74
+ .replace(/^(.)/, (_, char) => char.toLowerCase());
75
+ }
76
+ /**
77
+ * Convert a hyphenated or underscored string to PascalCase.
78
+ * Unlike `camelize`, this also handles hyphens.
79
+ * @example toPascalCase('user-profile') -> 'UserProfile'
80
+ * @example toPascalCase('user_profile') -> 'UserProfile'
81
+ */
82
+ function toPascalCase(str) {
83
+ return str
84
+ .replace(/[-_](.)/g, (_, char) => char.toUpperCase())
85
+ .replace(/^(.)/, (_, char) => char.toUpperCase());
86
+ }
87
+ /**
88
+ * Convert a camelCase or PascalCase string to SCREAMING_SNAKE_CASE.
89
+ * @example toScreamingSnake('userProfile') -> 'USER_PROFILE'
90
+ * @example toScreamingSnake('UserProfile') -> 'USER_PROFILE'
91
+ */
92
+ function toScreamingSnake(str) {
93
+ return str
94
+ .replace(/([A-Z])/g, '_$1')
95
+ .replace(/[-\s]/g, '_')
96
+ .toUpperCase()
97
+ .replace(/^_/, '');
98
+ }
package/esm/case.js CHANGED
@@ -51,3 +51,38 @@ export function underscore(str) {
51
51
  .replace(/^_/, '')
52
52
  .toLowerCase();
53
53
  }
54
+ /**
55
+ * Convert a hyphenated or underscored string to camelCase.
56
+ * Unlike `camelize`, this also handles hyphens and preserves
57
+ * camelCase boundaries that are already present.
58
+ * @example toCamelCase('user-profile') -> 'userProfile'
59
+ * @example toCamelCase('user_profile') -> 'userProfile'
60
+ */
61
+ export function toCamelCase(str) {
62
+ return str
63
+ .replace(/[-_](.)/g, (_, char) => char.toUpperCase())
64
+ .replace(/^(.)/, (_, char) => char.toLowerCase());
65
+ }
66
+ /**
67
+ * Convert a hyphenated or underscored string to PascalCase.
68
+ * Unlike `camelize`, this also handles hyphens.
69
+ * @example toPascalCase('user-profile') -> 'UserProfile'
70
+ * @example toPascalCase('user_profile') -> 'UserProfile'
71
+ */
72
+ export function toPascalCase(str) {
73
+ return str
74
+ .replace(/[-_](.)/g, (_, char) => char.toUpperCase())
75
+ .replace(/^(.)/, (_, char) => char.toUpperCase());
76
+ }
77
+ /**
78
+ * Convert a camelCase or PascalCase string to SCREAMING_SNAKE_CASE.
79
+ * @example toScreamingSnake('userProfile') -> 'USER_PROFILE'
80
+ * @example toScreamingSnake('UserProfile') -> 'USER_PROFILE'
81
+ */
82
+ export function toScreamingSnake(str) {
83
+ return str
84
+ .replace(/([A-Z])/g, '_$1')
85
+ .replace(/[-\s]/g, '_')
86
+ .toUpperCase()
87
+ .replace(/^_/, '');
88
+ }
package/esm/index.js CHANGED
@@ -8,4 +8,5 @@
8
8
  export * from './pluralize';
9
9
  export * from './case';
10
10
  export * from './naming';
11
+ export * from './matching';
11
12
  export * from './transform-keys';
@@ -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/index.d.ts CHANGED
@@ -8,4 +8,5 @@
8
8
  export * from './pluralize';
9
9
  export * from './case';
10
10
  export * from './naming';
11
+ export * from './matching';
11
12
  export * from './transform-keys';
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.3",
3
+ "version": "0.5.0",
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": "5ae067a647da5dbfb48cd9796a8a3391b6c658ba"
46
+ "gitHead": "d45378ff6379b3e8db0a38dfe17258f623859c6b"
47
47
  }