deep-case-crafter 0.0.4 → 1.0.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/dist/transform/transform.d.ts +9 -0
- package/package.json +24 -1
- package/readme.md +119 -39
|
@@ -6,6 +6,15 @@ declare function transform<T, From extends StringCase, To extends StringCase>(da
|
|
|
6
6
|
sourceCase: From;
|
|
7
7
|
targetCase: To;
|
|
8
8
|
}): TransformRecursive<T, From, To>;
|
|
9
|
+
/**
|
|
10
|
+
* Transform data to a specified output type.
|
|
11
|
+
*
|
|
12
|
+
* Note: When using this overload, type safety is not enforced between the input, options, and output type.
|
|
13
|
+
* The 'sourceCase' option is intentionally omitted, as it is not used for type inference here.
|
|
14
|
+
*/
|
|
15
|
+
declare function transform<T, K>(data: T, options?: Omit<TransformOptions, 'sourceCase'> & {
|
|
16
|
+
depth?: number;
|
|
17
|
+
}): K;
|
|
9
18
|
declare function transform<T>(data: T, options?: TransformOptions & {
|
|
10
19
|
depth?: number;
|
|
11
20
|
}): unknown;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deep-case-crafter",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Transforms deeply nested object, array, Map, and Set keys between common case formats while preserving TypeScript type safety",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -23,6 +23,29 @@
|
|
|
23
23
|
"author": "gyrgy",
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"private": false,
|
|
26
|
+
"keywords": [
|
|
27
|
+
"camelcase",
|
|
28
|
+
"pascalcase",
|
|
29
|
+
"snakecase",
|
|
30
|
+
"kebabcase",
|
|
31
|
+
"case-conversion",
|
|
32
|
+
"deep-conversion",
|
|
33
|
+
"object-transform",
|
|
34
|
+
"key-transform",
|
|
35
|
+
"typescript",
|
|
36
|
+
"typescript-utils",
|
|
37
|
+
"deep-map",
|
|
38
|
+
"map-keys",
|
|
39
|
+
"case-utils",
|
|
40
|
+
"case-changer",
|
|
41
|
+
"json-utils",
|
|
42
|
+
"case-format",
|
|
43
|
+
"deep-case",
|
|
44
|
+
"convert-case",
|
|
45
|
+
"string-utils",
|
|
46
|
+
"key-mapper",
|
|
47
|
+
"key-converter"
|
|
48
|
+
],
|
|
26
49
|
"scripts": {
|
|
27
50
|
"build": "rollup -c",
|
|
28
51
|
"prepare": "husky",
|
package/readme.md
CHANGED
|
@@ -1,30 +1,67 @@
|
|
|
1
1
|
# DeepCaseCrafter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
🚀 **DeepCaseCrafter** is a TypeScript utility that **transforms deeply nested object keys** into different case formats while maintaining **type safety**. It supports **objects, arrays, Maps, and Sets** and allows **custom recursion depth**.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/deep-case-crafter)
|
|
6
|
+
[](https://github.com/your-repo/deep-case-crafter/blob/main/LICENSE)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
4
8
|
|
|
5
9
|
## Features
|
|
6
10
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
- ✅
|
|
10
|
-
- 🔍
|
|
11
|
-
- ⚡
|
|
12
|
-
-
|
|
13
|
-
- 📦 Lightweight bundle size (~27.33 KB).
|
|
11
|
+
- 🔄 **Automatically converts keys** to `camelCase`, `PascalCase`, `snake_case`, and `kebab-case`.
|
|
12
|
+
- 🌍 **Works with deeply nested structures**, including objects, arrays, Maps, and Sets.
|
|
13
|
+
- ✅ **Maintains TypeScript type inference** for better auto-completion.
|
|
14
|
+
- 🔍 **Preserves special keys** (`__`, `--`, leading numbers, special characters).
|
|
15
|
+
- ⚡ **Performance-optimized** with minimal overhead.
|
|
16
|
+
- 🎛 **Configurable recursion depth** (default: `3`).
|
|
14
17
|
|
|
15
|
-
## Installation
|
|
18
|
+
## 📦 Installation
|
|
16
19
|
|
|
17
20
|
```bash
|
|
18
21
|
npm install deep-case-crafter
|
|
19
22
|
```
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 🚀 Quick Start
|
|
22
27
|
|
|
23
|
-
###
|
|
28
|
+
### 1️⃣ **Basic Usage – Just Call `transform`**
|
|
24
29
|
|
|
25
30
|
```typescript
|
|
26
31
|
import transform from 'deep-case-crafter';
|
|
27
32
|
|
|
33
|
+
const input = { user_id: 1, first_name: 'John' };
|
|
34
|
+
const result = transform(input);
|
|
35
|
+
|
|
36
|
+
console.log(result);
|
|
37
|
+
// Output: { userId: 1, firstName: 'John' }
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
_(Automatically converts from `snake_case` to `camelCase`)_
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
### 2️⃣ **Specify the `targetCase`**
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
const input = { user_id: 1, first_name: 'John' };
|
|
48
|
+
|
|
49
|
+
const pascalCaseResult = transform(input, { targetCase: 'pascal' });
|
|
50
|
+
console.log(pascalCaseResult);
|
|
51
|
+
// Output: { UserId: 1, FirstName: 'John' }
|
|
52
|
+
|
|
53
|
+
const kebabCaseResult = transform(input, { targetCase: 'kebab' });
|
|
54
|
+
console.log(kebabCaseResult);
|
|
55
|
+
// Output: { "user-id": 1, "first-name": "John" }
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
### 3️⃣ **Explicitly Define `sourceCase` for TypeScript Benefits**
|
|
61
|
+
|
|
62
|
+
_TypeScript automatically infers key changes when `sourceCase` is set._
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
28
65
|
const input = { user_id: 1, first_name: 'John' };
|
|
29
66
|
const result = transform(input, { targetCase: 'camel', sourceCase: 'snake' });
|
|
30
67
|
|
|
@@ -32,7 +69,9 @@ console.log(result);
|
|
|
32
69
|
// Output: { userId: 1, firstName: 'John' }
|
|
33
70
|
```
|
|
34
71
|
|
|
35
|
-
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
### 4️⃣ **Transform Deeply Nested Structures**
|
|
36
75
|
|
|
37
76
|
```typescript
|
|
38
77
|
const nestedInput = {
|
|
@@ -40,8 +79,12 @@ const nestedInput = {
|
|
|
40
79
|
posts: [{ post_id: 1, post_title: 'Hello' }],
|
|
41
80
|
};
|
|
42
81
|
|
|
43
|
-
const transformed = transform(nestedInput, {
|
|
82
|
+
const transformed = transform(nestedInput, {
|
|
83
|
+
targetCase: 'camel',
|
|
84
|
+
sourceCase: 'snake',
|
|
85
|
+
});
|
|
44
86
|
|
|
87
|
+
console.log(transformed);
|
|
45
88
|
// Output:
|
|
46
89
|
// {
|
|
47
90
|
// userInfo: { firstName: 'John', lastName: 'Doe' },
|
|
@@ -49,7 +92,9 @@ const transformed = transform(nestedInput, { targetCase: 'camel' });
|
|
|
49
92
|
// }
|
|
50
93
|
```
|
|
51
94
|
|
|
52
|
-
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
### 5️⃣ **Transforming a `Map`**
|
|
53
98
|
|
|
54
99
|
```typescript
|
|
55
100
|
const mapInput = new Map([
|
|
@@ -66,7 +111,9 @@ console.log(result.get('userId')); // 1
|
|
|
66
111
|
console.log(result.get('firstName')); // 'John'
|
|
67
112
|
```
|
|
68
113
|
|
|
69
|
-
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
### 6️⃣ **Transforming a `Set`**
|
|
70
117
|
|
|
71
118
|
```typescript
|
|
72
119
|
const setInput = new Set(['user_id', 'first_name']);
|
|
@@ -78,44 +125,77 @@ const result = transform(setInput, {
|
|
|
78
125
|
console.log(result); // Set { 'userId', 'firstName' }
|
|
79
126
|
```
|
|
80
127
|
|
|
81
|
-
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## 📖 API
|
|
82
131
|
|
|
83
132
|
### `transform(data, options)`
|
|
84
133
|
|
|
85
134
|
- **data**: The data structure to transform (`object`, `array`, `Map`, or `Set`).
|
|
86
135
|
- **options**:
|
|
87
|
-
- `targetCase` (optional): `'camel' | 'snake' | 'pascal' | 'kebab'`
|
|
88
|
-
- `sourceCase` (optional):
|
|
89
|
-
- `depth` (optional):
|
|
136
|
+
- `targetCase` (optional): `'camel' | 'snake' | 'pascal' | 'kebab'` (default: `'camel'`)
|
|
137
|
+
- `sourceCase` (optional): **Required for TypeScript inference** (`'snake' | 'camel' | 'pascal' | 'kebab'`).
|
|
138
|
+
- `depth` (optional): **Recursion depth** (default: `3`, use `Infinity` for full recursion).
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## 🧑💻 Advanced: Explicit Output Type Overload
|
|
143
|
+
|
|
144
|
+
If you want to specify the output type explicitly (for example, when you know the exact shape you want), you can use the following overload:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
const result = transform<Input, Output>(obj, { targetCase: 'camel' });
|
|
148
|
+
const typedResult: Output = result; // TypeScript will enforce Output type here
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**Note:**
|
|
152
|
+
- When using this overload, you cannot pass `sourceCase` in the options. If you do, TypeScript will show an error:
|
|
153
|
+
> Object literal may only specify known properties, and 'sourceCase' does not exist in type 'Omit<TransformOptions, "sourceCase"> & { depth?: number }'.
|
|
154
|
+
- This overload is useful when you want to take full control of the output type, but type safety between input, options, and output is not enforced by the library.
|
|
155
|
+
- If you want to use `sourceCase` and benefit from type inference, use the standard overload:
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
const result = transform(obj, { targetCase: 'camel', sourceCase: 'snake' }); // Output type is inferred
|
|
159
|
+
```
|
|
90
160
|
|
|
91
|
-
|
|
161
|
+
---
|
|
92
162
|
|
|
93
|
-
|
|
163
|
+
## 🚨 Key Preservation Rules
|
|
94
164
|
|
|
95
|
-
-
|
|
96
|
-
-
|
|
97
|
-
- Numbers at the end are
|
|
165
|
+
- **Keys that start with special characters or numbers** are **not transformed**.
|
|
166
|
+
- **Keys with `__` or `--` remain unchanged**.
|
|
167
|
+
- **Numbers at the end of keys** are preserved.
|
|
98
168
|
|
|
99
|
-
|
|
169
|
+
---
|
|
100
170
|
|
|
101
|
-
|
|
102
|
-
- Specifying `sourceCase` enhances TypeScript type inference but does not influence runtime detection.
|
|
171
|
+
## ⚡ Performance & Benchmarks
|
|
103
172
|
|
|
104
|
-
|
|
173
|
+
DeepCaseCrafter is optimized for speed and efficiency:
|
|
105
174
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
Fastest is Transform Deep Object (Depth 5, Width 10)
|
|
175
|
+
```plaintext
|
|
176
|
+
Transform Deep Object (Depth 5, Width 10)
|
|
177
|
+
x 361,538 ops/sec ±0.12% (91 runs sampled)
|
|
178
|
+
```
|
|
111
179
|
|
|
112
|
-
|
|
180
|
+
---
|
|
113
181
|
|
|
114
|
-
|
|
115
|
-
2. Create a new branch.
|
|
116
|
-
3. Commit your changes.
|
|
117
|
-
4. Submit a pull request.
|
|
182
|
+
## 💡 Contributing
|
|
118
183
|
|
|
119
|
-
|
|
184
|
+
1. **Fork** the repository.
|
|
185
|
+
2. **Create a new branch** (`feature/my-feature`).
|
|
186
|
+
3. **Commit your changes** (`git commit -m "Add new feature"`).
|
|
187
|
+
4. **Submit a pull request** 🚀.
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## 📜 License
|
|
120
192
|
|
|
121
193
|
MIT
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## 🔗 Links
|
|
198
|
+
|
|
199
|
+
- [NPM Package](https://www.npmjs.com/package/deep-case-crafter)
|
|
200
|
+
- [GitHub Repository](https://github.com/your-repo/deep-case-crafter)
|
|
201
|
+
- [TypeScript Docs](https://www.typescriptlang.org/)
|