Skip to content

Commit 6065426

Browse files
committed
docs
1 parent 82f8fac commit 6065426

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

packages/inflekt/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
underscore,
3939
toFieldName,
4040
toQueryName,
41+
inflektTree,
4142
} from 'inflekt';
4243

4344
// Basic singularization/pluralization
@@ -65,6 +66,14 @@ underscore('UserProfile'); // 'user_profile'
6566
// GraphQL naming helpers
6667
toFieldName('Users'); // 'user'
6768
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' }] }
6877
```
6978

7079
## API
@@ -91,6 +100,86 @@ toQueryName('User'); // 'users'
91100
- `toFieldName(pluralTypeName)` - Convert plural PascalCase to singular camelCase field name
92101
- `toQueryName(singularTypeName)` - Convert singular PascalCase to plural camelCase query name
93102

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+
94183
## Latin Suffix Overrides
95184

96185
This library handles Latin plural suffixes differently than the standard `inflection` package to match PostGraphile's behavior:

0 commit comments

Comments
 (0)