inflekt 0.3.0 → 0.3.2

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 CHANGED
@@ -38,6 +38,7 @@ import {
38
38
  underscore,
39
39
  toFieldName,
40
40
  toQueryName,
41
+ inflektTree,
41
42
  } from 'inflekt';
42
43
 
43
44
  // Basic singularization/pluralization
@@ -65,6 +66,14 @@ underscore('UserProfile'); // 'user_profile'
65
66
  // GraphQL naming helpers
66
67
  toFieldName('Users'); // 'user'
67
68
  toQueryName('User'); // 'users'
69
+
70
+ // Deep object key transformation
71
+ const apiResponse = {
72
+ user_name: 'John',
73
+ order_items: [{ item_id: 1, product_name: 'Widget' }]
74
+ };
75
+ inflektTree(apiResponse, (key) => camelize(key, true));
76
+ // Result: { userName: 'John', orderItems: [{ itemId: 1, productName: 'Widget' }] }
68
77
  ```
69
78
 
70
79
  ## API
@@ -91,6 +100,86 @@ toQueryName('User'); // 'users'
91
100
  - `toFieldName(pluralTypeName)` - Convert plural PascalCase to singular camelCase field name
92
101
  - `toQueryName(singularTypeName)` - Convert singular PascalCase to plural camelCase query name
93
102
 
103
+ ### Deep Object Transformation
104
+
105
+ - `inflektTree(obj, transformer, options?)` - Recursively transform all property names in an object tree
106
+
107
+ ## Deep Object Key Transformation
108
+
109
+ The `inflektTree` function recursively transforms all property names in an object tree using any transformer function. This is useful for converting API responses between naming conventions.
110
+
111
+ ### Basic Usage
112
+
113
+ ```typescript
114
+ // Convert snake_case API response to camelCase for frontend
115
+ const apiResponse = {
116
+ user_name: 'John',
117
+ user_profile: {
118
+ profile_image: 'https://example.com/avatar.jpg',
119
+ account_status: 'active'
120
+ },
121
+ order_items: [
122
+ { item_id: 1, item_name: 'Product A' },
123
+ { item_id: 2, item_name: 'Product B' }
124
+ ]
125
+ };
126
+
127
+ const result = inflektTree(apiResponse, (key) => camelize(key, true));
128
+ // Result:
129
+ // {
130
+ // userName: 'John',
131
+ // userProfile: {
132
+ // profileImage: 'https://example.com/avatar.jpg',
133
+ // accountStatus: 'active'
134
+ // },
135
+ // orderItems: [
136
+ // { itemId: 1, itemName: 'Product A' },
137
+ // { itemId: 2, itemName: 'Product B' }
138
+ // ]
139
+ // }
140
+
141
+ // Convert camelCase frontend data to snake_case for API
142
+ const frontendData = { userName: 'John', orderItems: [{ itemId: 1 }] };
143
+ const payload = inflektTree(frontendData, underscore);
144
+ // Result: { user_name: 'John', order_items: [{ item_id: 1 }] }
145
+ ```
146
+
147
+ ### Skipping Keys
148
+
149
+ Use the `skip` option to preserve certain keys:
150
+
151
+ ```typescript
152
+ // Skip keys starting with underscore
153
+ const input = {
154
+ user_name: 'John',
155
+ _private_field: 'secret',
156
+ _metadata: { _internal: true }
157
+ };
158
+
159
+ const result = inflektTree(input, (key) => camelize(key, true), {
160
+ skip: (key) => key.startsWith('_')
161
+ });
162
+ // Result: { userName: 'John', _private_field: 'secret', _metadata: { _internal: true } }
163
+
164
+ // Skip specific keys
165
+ const result2 = inflektTree(input, (key) => camelize(key, true), {
166
+ skip: (key) => key === 'created_at' || key === 'updated_at'
167
+ });
168
+
169
+ // Skip based on path depth (only transform top 2 levels)
170
+ const result3 = inflektTree(deepObject, (key) => camelize(key, true), {
171
+ skip: (key, path) => path.length > 1
172
+ });
173
+ ```
174
+
175
+ ### Features
176
+
177
+ - Handles nested objects and arrays of any depth
178
+ - Preserves `Date` objects (clones them)
179
+ - Preserves `null` and `undefined` values
180
+ - Returns primitives unchanged
181
+ - Works with any transformer function
182
+
94
183
  ## Latin Suffix Overrides
95
184
 
96
185
  This library handles Latin plural suffixes differently than the standard `inflection` package to match PostGraphile's behavior:
@@ -139,8 +228,7 @@ pnpm test:watch
139
228
 
140
229
  ## Credits
141
230
 
142
- Built for developers, with developers.
143
- 👉 https://launchql.com | https://hyperweb.io
231
+ **🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
144
232
 
145
233
  ## Disclaimer
146
234
 
package/esm/index.js CHANGED
@@ -8,3 +8,4 @@
8
8
  export * from './pluralize';
9
9
  export * from './case';
10
10
  export * from './naming';
11
+ export * from './transform-keys';
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Deep object key transformation utilities
3
+ *
4
+ * Transform all property names (keys) in an object tree using pluggable inflekt transformers.
5
+ */
6
+ /**
7
+ * Recursively traverse an object and transform all property names using the provided transformer.
8
+ *
9
+ * @param obj - The object to transform
10
+ * @param transformer - Function that transforms a key string
11
+ * @param options - Optional configuration
12
+ * @returns A new object with transformed keys
13
+ *
14
+ * @example
15
+ * // Convert snake_case keys to camelCase
16
+ * const apiResponse = { user_name: 'John', order_items: [{ item_id: 1 }] };
17
+ * const result = inflektTree(apiResponse, (key) => camelize(key, true));
18
+ * // Result: { userName: 'John', orderItems: [{ itemId: 1 }] }
19
+ *
20
+ * @example
21
+ * // Convert camelCase keys to snake_case
22
+ * const frontendObj = { userName: 'John', orderItems: [{ itemId: 1 }] };
23
+ * const result = inflektTree(frontendObj, underscore);
24
+ * // Result: { user_name: 'John', order_items: [{ item_id: 1 }] }
25
+ *
26
+ * @example
27
+ * // Skip keys starting with underscore
28
+ * inflektTree(obj, (key) => camelize(key, true), {
29
+ * skip: (key) => key.startsWith('_')
30
+ * });
31
+ */
32
+ export function inflektTree(obj, transformer, options) {
33
+ return transformKeys(obj, transformer, options, []);
34
+ }
35
+ function transformKeys(obj, transformer, options, path) {
36
+ // Handle primitives (null, undefined, non-objects)
37
+ if (obj == null || typeof obj !== 'object') {
38
+ return obj;
39
+ }
40
+ // Handle Date - clone and return
41
+ if (obj instanceof Date) {
42
+ return new Date(obj.getTime());
43
+ }
44
+ // Handle Array - recursively transform each element
45
+ if (Array.isArray(obj)) {
46
+ return obj.map((item, index) => transformKeys(item, transformer, options, path));
47
+ }
48
+ // Handle Object - create new object with transformed keys
49
+ const result = {};
50
+ for (const key of Object.keys(obj)) {
51
+ const shouldSkip = options?.skip?.(key, path) ?? false;
52
+ const newKey = shouldSkip ? key : transformer(key);
53
+ const newPath = [...path, key];
54
+ result[newKey] = transformKeys(obj[key], transformer, options, newPath);
55
+ }
56
+ return result;
57
+ }
package/index.d.ts CHANGED
@@ -8,3 +8,4 @@
8
8
  export * from './pluralize';
9
9
  export * from './case';
10
10
  export * from './naming';
11
+ export * from './transform-keys';
package/index.js CHANGED
@@ -24,3 +24,4 @@ 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("./transform-keys"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inflekt",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
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": "c6f85c28e227126d86839ba1628748ba7d857da9"
46
+ "gitHead": "1d2795da97e2217a7df24b2942a0b4e5879ac16f"
47
47
  }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Deep object key transformation utilities
3
+ *
4
+ * Transform all property names (keys) in an object tree using pluggable inflekt transformers.
5
+ */
6
+ export type KeyTransformer = (key: string) => string;
7
+ export interface InflektTreeOptions {
8
+ /**
9
+ * Optional function to skip transformation for specific keys
10
+ * @param key - The current key being processed
11
+ * @param path - Array of keys representing the path to the current key
12
+ * @returns true to skip transformation, false to transform
13
+ */
14
+ skip?: (key: string, path: string[]) => boolean;
15
+ }
16
+ /**
17
+ * Recursively traverse an object and transform all property names using the provided transformer.
18
+ *
19
+ * @param obj - The object to transform
20
+ * @param transformer - Function that transforms a key string
21
+ * @param options - Optional configuration
22
+ * @returns A new object with transformed keys
23
+ *
24
+ * @example
25
+ * // Convert snake_case keys to camelCase
26
+ * const apiResponse = { user_name: 'John', order_items: [{ item_id: 1 }] };
27
+ * const result = inflektTree(apiResponse, (key) => camelize(key, true));
28
+ * // Result: { userName: 'John', orderItems: [{ itemId: 1 }] }
29
+ *
30
+ * @example
31
+ * // Convert camelCase keys to snake_case
32
+ * const frontendObj = { userName: 'John', orderItems: [{ itemId: 1 }] };
33
+ * const result = inflektTree(frontendObj, underscore);
34
+ * // Result: { user_name: 'John', order_items: [{ item_id: 1 }] }
35
+ *
36
+ * @example
37
+ * // Skip keys starting with underscore
38
+ * inflektTree(obj, (key) => camelize(key, true), {
39
+ * skip: (key) => key.startsWith('_')
40
+ * });
41
+ */
42
+ export declare function inflektTree(obj: any, transformer: KeyTransformer, options?: InflektTreeOptions): any;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ /**
3
+ * Deep object key transformation utilities
4
+ *
5
+ * Transform all property names (keys) in an object tree using pluggable inflekt transformers.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.inflektTree = inflektTree;
9
+ /**
10
+ * Recursively traverse an object and transform all property names using the provided transformer.
11
+ *
12
+ * @param obj - The object to transform
13
+ * @param transformer - Function that transforms a key string
14
+ * @param options - Optional configuration
15
+ * @returns A new object with transformed keys
16
+ *
17
+ * @example
18
+ * // Convert snake_case keys to camelCase
19
+ * const apiResponse = { user_name: 'John', order_items: [{ item_id: 1 }] };
20
+ * const result = inflektTree(apiResponse, (key) => camelize(key, true));
21
+ * // Result: { userName: 'John', orderItems: [{ itemId: 1 }] }
22
+ *
23
+ * @example
24
+ * // Convert camelCase keys to snake_case
25
+ * const frontendObj = { userName: 'John', orderItems: [{ itemId: 1 }] };
26
+ * const result = inflektTree(frontendObj, underscore);
27
+ * // Result: { user_name: 'John', order_items: [{ item_id: 1 }] }
28
+ *
29
+ * @example
30
+ * // Skip keys starting with underscore
31
+ * inflektTree(obj, (key) => camelize(key, true), {
32
+ * skip: (key) => key.startsWith('_')
33
+ * });
34
+ */
35
+ function inflektTree(obj, transformer, options) {
36
+ return transformKeys(obj, transformer, options, []);
37
+ }
38
+ function transformKeys(obj, transformer, options, path) {
39
+ // Handle primitives (null, undefined, non-objects)
40
+ if (obj == null || typeof obj !== 'object') {
41
+ return obj;
42
+ }
43
+ // Handle Date - clone and return
44
+ if (obj instanceof Date) {
45
+ return new Date(obj.getTime());
46
+ }
47
+ // Handle Array - recursively transform each element
48
+ if (Array.isArray(obj)) {
49
+ return obj.map((item, index) => transformKeys(item, transformer, options, path));
50
+ }
51
+ // Handle Object - create new object with transformed keys
52
+ const result = {};
53
+ for (const key of Object.keys(obj)) {
54
+ const shouldSkip = options?.skip?.(key, path) ?? false;
55
+ const newKey = shouldSkip ? key : transformer(key);
56
+ const newPath = [...path, key];
57
+ result[newKey] = transformKeys(obj[key], transformer, options, newPath);
58
+ }
59
+ return result;
60
+ }