caseparser 4.0.0 → 4.1.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/README.md +70 -142
- package/dist/index.cjs +395 -1
- package/dist/index.d.cts +300 -8
- package/dist/index.d.ts +300 -8
- package/dist/index.js +370 -2
- package/package.json +15 -7
package/README.md
CHANGED
|
@@ -21,17 +21,8 @@ Convert **Strings** and **JSON (Object Keys)** from a **case type** to another o
|
|
|
21
21
|
|
|
22
22
|
## Installation
|
|
23
23
|
|
|
24
|
-
###### Yarn
|
|
25
24
|
```sh
|
|
26
|
-
yarn add caseparser
|
|
27
|
-
```
|
|
28
|
-
###### NPM
|
|
29
|
-
```sh
|
|
30
|
-
npm add caseparser
|
|
31
|
-
```
|
|
32
|
-
###### PNPM
|
|
33
|
-
```sh
|
|
34
|
-
pnpm add caseparser
|
|
25
|
+
npm add caseparser # or: pnpm add caseparser / yarn add caseparser
|
|
35
26
|
```
|
|
36
27
|
###### Deno (JSR)
|
|
37
28
|
```sh
|
|
@@ -51,131 +42,88 @@ npx jsr add @nandomb/caseparser
|
|
|
51
42
|
| Environment | Supported |
|
|
52
43
|
| --- | --- |
|
|
53
44
|
| ESM (`import`) | Node.js 12.22+, Deno, Bun, bundlers |
|
|
54
|
-
| CommonJS (`require`) | Node.js 8
|
|
45
|
+
| CommonJS (`require`) | Node.js 8+, Bun |
|
|
55
46
|
| TypeScript | 4.1+ (any `moduleResolution`: `node`, `node16`/`nodenext`, `bundler`) |
|
|
56
47
|
| Browsers | Any ES2015 browser (via bundler) |
|
|
48
|
+
| Edge | Cloudflare Workers |
|
|
57
49
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
```
|
|
50
|
+
Every change is tested in CI on Node.js 22, 24 and 26, Bun, Deno, Cloudflare Workers, and in Chromium, Firefox and WebKit.
|
|
51
|
+
|
|
52
|
+
Ready-to-run projects for each environment (Node.js ESM/CommonJS, TypeScript, TypeScript 4.1, Bun, Deno, the browser and Cloudflare Workers) are in [examples/](./examples).
|
|
62
53
|
|
|
63
54
|
## How to use
|
|
64
55
|
|
|
65
|
-
###### Example 1
|
|
66
|
-
Passing **String** as parameter:
|
|
67
56
|
```ts
|
|
68
|
-
import { camelToSnake } from 'caseparser';
|
|
57
|
+
import { camelToSnake } from 'caseparser'; // ESM
|
|
58
|
+
// const { camelToSnake } = require('caseparser'); // CommonJS
|
|
59
|
+
|
|
60
|
+
camelToSnake('helloWorld'); // 'hello_world'
|
|
69
61
|
|
|
70
|
-
camelToSnake('
|
|
62
|
+
camelToSnake({ firstName: 'John', addresses: [{ postalCode: '61105' }] });
|
|
63
|
+
// { first_name: 'John', addresses: [{ postal_code: '61105' }] }
|
|
71
64
|
```
|
|
72
|
-
|
|
65
|
+
|
|
66
|
+
Objects are converted deeply, including objects inside arrays. The input is never mutated: a new object is returned.
|
|
67
|
+
|
|
68
|
+
### Typical use: API responses
|
|
69
|
+
|
|
73
70
|
```ts
|
|
74
|
-
'
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const data = [
|
|
84
|
-
{
|
|
85
|
-
id: 1,
|
|
86
|
-
firstName: 'John',
|
|
87
|
-
lastName: 'Doe',
|
|
88
|
-
email: 'john.doe@example.com',
|
|
89
|
-
addresses: [
|
|
90
|
-
{
|
|
91
|
-
country: 'United States',
|
|
92
|
-
state: 'Illinois',
|
|
93
|
-
city: 'Rockford',
|
|
94
|
-
postalCode: '61105',
|
|
95
|
-
street: {
|
|
96
|
-
streetName: '41 Forest Run Circle',
|
|
97
|
-
streetNumber: '539'
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
]
|
|
101
|
-
}
|
|
102
|
-
];
|
|
103
|
-
const result = camelToSnake(data);
|
|
71
|
+
import { snakeToCamel, camelToSnake } from 'caseparser';
|
|
72
|
+
|
|
73
|
+
const res = await fetch('/api/users/1');
|
|
74
|
+
const user = snakeToCamel(await res.json()); // { firstName, lastName, ... }
|
|
75
|
+
|
|
76
|
+
await fetch('/api/users/1', {
|
|
77
|
+
method: 'PUT',
|
|
78
|
+
body: JSON.stringify(camelToSnake(user)), // back to { first_name, ... }
|
|
79
|
+
});
|
|
104
80
|
```
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
"state": "Illinois",
|
|
117
|
-
"city": "Rockford",
|
|
118
|
-
"postal_code": "61105",
|
|
119
|
-
"street": {
|
|
120
|
-
"street_name": "41 Forest Run Circle",
|
|
121
|
-
"street_number": "539"
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
]
|
|
125
|
-
}
|
|
126
|
-
]
|
|
81
|
+
|
|
82
|
+
### Type inference
|
|
83
|
+
|
|
84
|
+
The resulting keys are inferred at the type level, so your editor autocompletes the converted names:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const user = snakeToCamel({ first_name: 'John', addresses: [{ postal_code: '61105' }] });
|
|
88
|
+
// ^? { firstName: string; addresses: { postalCode: string }[] }
|
|
89
|
+
|
|
90
|
+
user.firstName; // ✅
|
|
91
|
+
user.first_name; // ❌ Property 'first_name' does not exist
|
|
127
92
|
```
|
|
128
93
|
|
|
129
94
|
## Conversion Types
|
|
130
95
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
dashToCamel
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
```
|
|
163
|
-
###### upperSnakeCase to ...
|
|
164
|
-
```js
|
|
165
|
-
upperSnakeToCamel(data);
|
|
166
|
-
upperSnakeToDash(data);
|
|
167
|
-
upperSnakeToPascal(data);
|
|
168
|
-
upperSnakeToSnake(data);
|
|
169
|
-
upperSnakeToUpperDash(data);
|
|
170
|
-
```
|
|
171
|
-
###### upperDashCase to ...
|
|
172
|
-
```js
|
|
173
|
-
upperDashToCamel(data);
|
|
174
|
-
upperDashToDash(data);
|
|
175
|
-
upperDashToPascal(data);
|
|
176
|
-
upperDashToSnake(data);
|
|
177
|
-
upperDashToUpperSnake(data);
|
|
178
|
-
```
|
|
96
|
+
Every function is named `<from>To<To>`, e.g. `snakeToCamel`. The case names are:
|
|
97
|
+
|
|
98
|
+
| Name | Example |
|
|
99
|
+
| --- | --- |
|
|
100
|
+
| `camel` | `helloWorld` |
|
|
101
|
+
| `pascal` | `HelloWorld` |
|
|
102
|
+
| `snake` | `hello_world` |
|
|
103
|
+
| `dash` | `hello-world` |
|
|
104
|
+
| `upperSnake` | `HELLO_WORLD` |
|
|
105
|
+
| `upperDash` | `HELLO-WORLD` |
|
|
106
|
+
| `train` | `Hello-World` |
|
|
107
|
+
| `dot` | `hello.world` |
|
|
108
|
+
|
|
109
|
+
All 56 functions:
|
|
110
|
+
|
|
111
|
+
- **camelCase:** `camelToPascal`, `camelToSnake`, `camelToDash`, `camelToUpperSnake`, `camelToUpperDash`, `camelToTrain`, `camelToDot`
|
|
112
|
+
- **PascalCase:** `pascalToCamel`, `pascalToSnake`, `pascalToDash`, `pascalToUpperSnake`, `pascalToUpperDash`, `pascalToTrain`, `pascalToDot`
|
|
113
|
+
- **snake_case:** `snakeToCamel`, `snakeToPascal`, `snakeToDash`, `snakeToUpperSnake`, `snakeToUpperDash`, `snakeToTrain`, `snakeToDot`
|
|
114
|
+
- **dash-case:** `dashToCamel`, `dashToPascal`, `dashToSnake`, `dashToUpperSnake`, `dashToUpperDash`, `dashToTrain`, `dashToDot`
|
|
115
|
+
- **UPPER_SNAKE_CASE:** `upperSnakeToCamel`, `upperSnakeToPascal`, `upperSnakeToSnake`, `upperSnakeToDash`, `upperSnakeToUpperDash`, `upperSnakeToTrain`, `upperSnakeToDot`
|
|
116
|
+
- **UPPER-DASH-CASE:** `upperDashToCamel`, `upperDashToPascal`, `upperDashToSnake`, `upperDashToDash`, `upperDashToUpperSnake`, `upperDashToTrain`, `upperDashToDot`
|
|
117
|
+
- **Train-Case:** `trainToCamel`, `trainToPascal`, `trainToSnake`, `trainToDash`, `trainToUpperSnake`, `trainToUpperDash`, `trainToDot`
|
|
118
|
+
- **dot.case:** `dotToCamel`, `dotToPascal`, `dotToSnake`, `dotToDash`, `dotToUpperSnake`, `dotToUpperDash`, `dotToTrain`
|
|
119
|
+
|
|
120
|
+
## Behavior and limitations
|
|
121
|
+
|
|
122
|
+
- **Only keys are converted, never values.** In `{ userName: 'johnDoe' }`, `userName` becomes `user_name` but `'johnDoe'` is kept. Strings inside arrays are kept too.
|
|
123
|
+
- **Only plain objects are traversed.** `Date`, `Map`, `Set` and class instances are returned as they are (same reference), without converting their contents.
|
|
124
|
+
- **Acronyms are split letter by letter**, because every uppercase letter starts a new word: `camelToSnake('userID')` → `'user_i_d'`. Prefer `userId` style keys.
|
|
125
|
+
- **Train-Case words are lowercased** before converting, so `trainToCamel('X-API-Key')` → `'xApiKey'`.
|
|
126
|
+
- **Numbers are not word boundaries:** `camelToSnake('html5Parser')` → `'html5_parser'`, `snakeToCamel('user_1_name')` → `'user1Name'`.
|
|
179
127
|
|
|
180
128
|
## Security
|
|
181
129
|
|
|
@@ -186,25 +134,5 @@ Releases are built and published from GitHub Actions without long-lived tokens (
|
|
|
186
134
|
Found a vulnerability? Please report it privately, see [SECURITY.md](./SECURITY.md).
|
|
187
135
|
|
|
188
136
|
## License
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
Copyright (c) 2017 Fernando Machado Bernardino
|
|
192
|
-
[NandoMB](https://github.com/NandoMB). https://github.com/NandoMB/caseparser
|
|
193
|
-
|
|
194
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
195
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
196
|
-
in the Software without restriction, including without limitation the rights
|
|
197
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
198
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
199
|
-
furnished to do so, subject to the following conditions:
|
|
200
|
-
|
|
201
|
-
The above copyright notice and this permission notice shall be included in all
|
|
202
|
-
copies or substantial portions of the Software.
|
|
203
|
-
|
|
204
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
205
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
206
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
207
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
208
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
209
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
210
|
-
SOFTWARE.
|
|
137
|
+
|
|
138
|
+
[MIT](./LICENSE) © 2017 Fernando Machado Bernardino
|