caseparser 5.0.0 → 5.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 +184 -113
- package/dist/index.cjs +52 -227
- package/dist/index.d.cts +239 -36
- package/dist/index.d.ts +239 -36
- package/dist/index.js +52 -227
- package/package.json +11 -3
package/README.md
CHANGED
|
@@ -7,17 +7,28 @@
|
|
|
7
7
|
[](https://www.npmjs.com/package/caseparser)
|
|
8
8
|
[](./LICENSE)
|
|
9
9
|
|
|
10
|
-
Convert **
|
|
11
|
-
|
|
12
|
-
- Zero dependencies
|
|
13
|
-
- Written in TypeScript, published as ESM and CommonJS with type declarations
|
|
14
|
-
- Available on [npm](https://www.npmjs.com/package/caseparser) and [JSR](https://jsr.io/@nandomb/caseparser)
|
|
15
|
-
- Tree-shakeable
|
|
10
|
+
Convert **strings** and **object keys** to any case, **whatever case they are in now**, with the resulting keys inferred by TypeScript.
|
|
16
11
|
|
|
12
|
+
- **Any case in:** no need to know or normalize the input first. Keys in different cases can even be mixed in the same object
|
|
13
|
+
- **Deep:** nested objects and objects inside arrays are converted too
|
|
14
|
+
- **Typed:** your editor autocompletes the converted keys
|
|
15
|
+
- Zero dependencies, tree-shakeable, ESM and CommonJS, on [npm](https://www.npmjs.com/package/caseparser) and [JSR](https://jsr.io/@nandomb/caseparser)
|
|
17
16
|
|
|
18
17
|
###### Note:
|
|
19
18
|
> If you're looking for version 1.x.x, [click here](https://github.com/NandoMB/caseparser/tree/v1.x.x) to see the docs.
|
|
20
19
|
|
|
20
|
+
## Contents
|
|
21
|
+
|
|
22
|
+
- [Installation](#installation)
|
|
23
|
+
- [Quick start](#quick-start)
|
|
24
|
+
- [Cases](#cases)
|
|
25
|
+
- [Options](#options): [`allowSymbols`](#allowsymbols), [`transform`](#transform), [`separator`](#separator)
|
|
26
|
+
- [Type inference](#type-inference)
|
|
27
|
+
- [How keys are split into words](#how-keys-are-split-into-words)
|
|
28
|
+
- [Behavior and limitations](#behavior-and-limitations)
|
|
29
|
+
- [Compatibility](#compatibility)
|
|
30
|
+
- [Deprecated `<from>To<To>` functions](#deprecated-fromtoto-functions)
|
|
31
|
+
- [Security](#security)
|
|
21
32
|
|
|
22
33
|
## Installation
|
|
23
34
|
|
|
@@ -29,7 +40,7 @@ npm add caseparser # or: pnpm add caseparser / yarn add caseparser
|
|
|
29
40
|
deno add jsr:@nandomb/caseparser
|
|
30
41
|
```
|
|
31
42
|
```ts
|
|
32
|
-
import {
|
|
43
|
+
import { toSnake } from '@nandomb/caseparser';
|
|
33
44
|
```
|
|
34
45
|
###### Bun / Node.js from JSR
|
|
35
46
|
```sh
|
|
@@ -37,121 +48,167 @@ bunx jsr add @nandomb/caseparser
|
|
|
37
48
|
npx jsr add @nandomb/caseparser
|
|
38
49
|
```
|
|
39
50
|
|
|
40
|
-
##
|
|
51
|
+
## Quick start
|
|
41
52
|
|
|
42
|
-
|
|
43
|
-
| --- | --- |
|
|
44
|
-
| ESM (`import`) | Node.js 12.22+, Deno, Bun, bundlers |
|
|
45
|
-
| CommonJS (`require`) | Node.js 8+, Bun |
|
|
46
|
-
| TypeScript | 4.5+ for the `toX` functions, 4.1+ for the deprecated `<from>To<To>` functions (any `moduleResolution`: `node`, `node16`/`nodenext`, `bundler`) |
|
|
47
|
-
| Browsers | Any ES2015 browser (via bundler) |
|
|
48
|
-
| Edge | Cloudflare Workers |
|
|
53
|
+
The input doesn't need a specific or consistent shape. Every key below is in a different case, and one call converts all of them, deeply:
|
|
49
54
|
|
|
50
|
-
|
|
55
|
+
```ts
|
|
56
|
+
import { toCamel } from 'caseparser'; // ESM
|
|
57
|
+
// const { toCamel } = require('caseparser'); // CommonJS
|
|
58
|
+
|
|
59
|
+
const response = {
|
|
60
|
+
userId: 42, // camelCase
|
|
61
|
+
FirstName: 'Ada', // PascalCase
|
|
62
|
+
last_name: 'Lovelace', // snake_case
|
|
63
|
+
'birth-date': '1815-12-10', // kebab-case
|
|
64
|
+
ACCOUNT_STATUS: 'active', // UPPER_SNAKE_CASE
|
|
65
|
+
'Content-Type': 'application/json', // Train-Case
|
|
66
|
+
'Display Name': 'Countess', // Title Case
|
|
67
|
+
XMLHttpRequest: false, // acronym + word
|
|
68
|
+
$ref: '#/users/42', // symbol
|
|
69
|
+
'billing address': { // nested objects...
|
|
70
|
+
'Postal Code': '61105',
|
|
71
|
+
countryISO: 'US'
|
|
72
|
+
},
|
|
73
|
+
'recent.orders': [{ order_id: 1, TotalAmount: 99.9 }], // ...and objects inside arrays
|
|
74
|
+
tags: ['someTag', 'another_tag'] // values are never converted
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
toCamel(response);
|
|
78
|
+
// {
|
|
79
|
+
// userId: 42,
|
|
80
|
+
// firstName: 'Ada',
|
|
81
|
+
// lastName: 'Lovelace',
|
|
82
|
+
// birthDate: '1815-12-10',
|
|
83
|
+
// accountStatus: 'active',
|
|
84
|
+
// contentType: 'application/json',
|
|
85
|
+
// displayName: 'Countess',
|
|
86
|
+
// xmlHttpRequest: false,
|
|
87
|
+
// ref: '#/users/42',
|
|
88
|
+
// billingAddress: { postalCode: '61105', countryIso: 'US' },
|
|
89
|
+
// recentOrders: [{ orderId: 1, totalAmount: 99.9 }],
|
|
90
|
+
// tags: ['someTag', 'another_tag']
|
|
91
|
+
// }
|
|
92
|
+
```
|
|
51
93
|
|
|
52
|
-
|
|
94
|
+
Strings work the same way: `toSnake('helloWorld')`, `toSnake('Hello World')` and `toSnake('HELLO-WORLD')` all return `'hello_world'`.
|
|
53
95
|
|
|
54
|
-
|
|
96
|
+
The input is never mutated: a new object is returned. A typical use is converting API payloads on the way in and out:
|
|
55
97
|
|
|
56
98
|
```ts
|
|
57
|
-
import { toSnake } from 'caseparser';
|
|
58
|
-
// const { toSnake } = require('caseparser'); // CommonJS
|
|
99
|
+
import { toCamel, toSnake } from 'caseparser';
|
|
59
100
|
|
|
60
|
-
|
|
101
|
+
const res = await fetch('/api/users/1');
|
|
102
|
+
const user = toCamel(await res.json()); // { firstName, lastName, ... }
|
|
61
103
|
|
|
62
|
-
|
|
63
|
-
|
|
104
|
+
await fetch('/api/users/1', {
|
|
105
|
+
method: 'PUT',
|
|
106
|
+
body: JSON.stringify(toSnake(user)), // back to { first_name, ... }
|
|
107
|
+
});
|
|
64
108
|
```
|
|
65
109
|
|
|
66
|
-
|
|
110
|
+
## Cases
|
|
67
111
|
|
|
68
|
-
There is one function per target case, and
|
|
112
|
+
There is one function per target case, and each one accepts input in any case:
|
|
69
113
|
|
|
70
|
-
| Function |
|
|
71
|
-
| --- | --- | --- |
|
|
72
|
-
| `toCamel` | `helloWorld` | lowerCamelCase |
|
|
73
|
-
| `toPascal` | `HelloWorld` | UpperCamelCase |
|
|
74
|
-
| `toSnake` | `hello_world` | |
|
|
75
|
-
| `toKebab` | `hello-world` | dash-case, param-case |
|
|
76
|
-
| `toUpperSnake` | `HELLO_WORLD` | CONSTANT_CASE, SCREAMING_SNAKE_CASE |
|
|
77
|
-
| `toUpperKebab` | `HELLO-WORLD` | COBOL-CASE, SCREAMING-KEBAB-CASE |
|
|
78
|
-
| `toTrain` | `Hello-World` | Header-Case |
|
|
79
|
-
| `
|
|
80
|
-
| `
|
|
81
|
-
| `
|
|
82
|
-
| `
|
|
83
|
-
| `
|
|
84
|
-
| `
|
|
85
|
-
| `
|
|
86
|
-
| `
|
|
87
|
-
|
|
88
|
-
`toSpace`, `toPath` and `toDot` keep the original case of each word (`toPath('UserProfile')` → `'User/Profile'`); all the others lowercase the words first. To change the case of a whole string, use `toLowerCase()` or `toUpperCase()` on the result:
|
|
114
|
+
| Function | `'helloWorld'` → | `'LAST_login_AT'` → | Also known as |
|
|
115
|
+
| --- | --- | --- | --- |
|
|
116
|
+
| `toCamel` | `helloWorld` | `lastLoginAt` | lowerCamelCase |
|
|
117
|
+
| `toPascal` | `HelloWorld` | `LastLoginAt` | UpperCamelCase |
|
|
118
|
+
| `toSnake` | `hello_world` | `last_login_at` | |
|
|
119
|
+
| `toKebab` | `hello-world` | `last-login-at` | dash-case, param-case |
|
|
120
|
+
| `toUpperSnake` | `HELLO_WORLD` | `LAST_LOGIN_AT` | CONSTANT_CASE, SCREAMING_SNAKE_CASE |
|
|
121
|
+
| `toUpperKebab` | `HELLO-WORLD` | `LAST-LOGIN-AT` | COBOL-CASE, SCREAMING-KEBAB-CASE |
|
|
122
|
+
| `toTrain` | `Hello-World` | `Last-Login-At` | Header-Case |
|
|
123
|
+
| `toTitle` | `Hello World` | `Last Login At` | Capital Case |
|
|
124
|
+
| `toSentence` | `Hello world` | `Last login at` | |
|
|
125
|
+
| `toPascalSnake` | `Hello_World` | `Last_Login_At` | Ada_Case, Title_Snake_Case |
|
|
126
|
+
| `toLower` | `hello world` | `last login at` | no case |
|
|
127
|
+
| `toUpper` | `HELLO WORLD` | `LAST LOGIN AT` | |
|
|
128
|
+
| `toDot` | `hello.World` | `LAST.login.AT` | dot.notation |
|
|
129
|
+
| `toPath` | `hello/World` | `LAST/login/AT` | |
|
|
130
|
+
| `toSpace` | `hello World` | `LAST login AT` | |
|
|
89
131
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
toPath('helloWorld').toUpperCase(); // 'HELLO/WORLD'
|
|
94
|
-
```
|
|
132
|
+
`toDot`, `toPath` and `toSpace` only change the separator: they keep the original case of each word (upper, lower or mixed). To change it, see [`transform`](#transform).
|
|
133
|
+
|
|
134
|
+
## Options
|
|
95
135
|
|
|
96
|
-
|
|
136
|
+
Every function takes an optional second argument.
|
|
97
137
|
|
|
98
|
-
###
|
|
138
|
+
### `allowSymbols`
|
|
139
|
+
|
|
140
|
+
Symbols (ASCII punctuation such as `$`, `@`, `#` or `%`) are removed by default. `allowSymbols: true` keeps all of them, and an array keeps only the listed ones. It can be passed inside the options object, or directly as the second argument:
|
|
99
141
|
|
|
100
142
|
```ts
|
|
101
|
-
|
|
143
|
+
const data = { $ref: 1, '@type': 'User', 'discount%': 10 };
|
|
102
144
|
|
|
103
|
-
|
|
104
|
-
|
|
145
|
+
toCamel(data); // { ref: 1, type: 'User', discount: 10 }
|
|
146
|
+
toCamel(data, { allowSymbols: true }); // { $ref: 1, '@type': 'User', 'discount%': 10 }
|
|
147
|
+
toCamel(data, { allowSymbols: ['$'] }); // { $ref: 1, type: 'User', discount: 10 }
|
|
105
148
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
});
|
|
149
|
+
// shorthand
|
|
150
|
+
toCamel(data, true); // same as { allowSymbols: true }
|
|
151
|
+
toCamel(data, ['$']); // same as { allowSymbols: ['$'] }
|
|
110
152
|
```
|
|
111
153
|
|
|
112
|
-
|
|
154
|
+
A kept symbol sticks to the start of the next word: `toPascal('$id', true)` → `'$Id'`. See [How keys are split into words](#how-keys-are-split-into-words) for the details.
|
|
155
|
+
|
|
156
|
+
### `transform`
|
|
113
157
|
|
|
114
|
-
|
|
158
|
+
Only for `toDot` and `toPath`, which keep the original case of each word by default. `'lowercase'` or `'uppercase'` changes the case of the whole result, object keys included:
|
|
115
159
|
|
|
116
160
|
```ts
|
|
117
|
-
|
|
118
|
-
|
|
161
|
+
toDot('LAST_login_AT'); // 'LAST.login.AT'
|
|
162
|
+
toDot('LAST_login_AT', { transform: 'lowercase' }); // 'last.login.at'
|
|
163
|
+
toPath('LAST_login_AT', { transform: 'uppercase' }); // 'LAST/LOGIN/AT'
|
|
119
164
|
|
|
120
|
-
|
|
121
|
-
user.
|
|
122
|
-
```
|
|
165
|
+
toDot({ user_ID: 1, LAST_name: 'Doe' }, { transform: 'lowercase' });
|
|
166
|
+
// { 'user.id': 1, 'last.name': 'Doe' }
|
|
123
167
|
|
|
124
|
-
|
|
168
|
+
toDot('$UserId', { allowSymbols: true, transform: 'lowercase' }); // '$user.id'
|
|
169
|
+
```
|
|
125
170
|
|
|
126
|
-
|
|
171
|
+
`toSpace` has no `transform`: use `toLower` or `toUpper` instead. Don't chain functions for this (`toUpper(toPath(...))`): each one splits its input into words again, so `/` and `.` would be treated as separators or symbols.
|
|
127
172
|
|
|
128
|
-
|
|
129
|
-
import { toCamel, toSnake } from 'caseparser';
|
|
173
|
+
### `separator`
|
|
130
174
|
|
|
131
|
-
|
|
132
|
-
toSnake('Hello World'); // 'hello_world'
|
|
133
|
-
toSnake('HELLO-WORLD'); // 'hello_world'
|
|
175
|
+
Only for `toPath`. It joins the words with `'/'` by default, or with `'\\'`:
|
|
134
176
|
|
|
135
|
-
|
|
136
|
-
//
|
|
177
|
+
```ts
|
|
178
|
+
toPath('users/profilePicture'); // 'users/profile/Picture'
|
|
179
|
+
toPath('users/profilePicture', { separator: '\\' }); // 'users\\profile\\Picture'
|
|
180
|
+
toPath('users\\profilePicture', { separator: '/' }); // 'users/profile/Picture'
|
|
137
181
|
```
|
|
138
182
|
|
|
139
|
-
|
|
183
|
+
In `toPath`, `/` and `\` in the input also separate words, so paths can be converted from one separator to the other. In the other functions they are symbols.
|
|
140
184
|
|
|
141
|
-
|
|
185
|
+
## Type inference
|
|
142
186
|
|
|
143
|
-
|
|
187
|
+
The converted keys are inferred at the type level, so your editor autocompletes them and catches typos:
|
|
144
188
|
|
|
145
189
|
```ts
|
|
146
|
-
toCamel({
|
|
147
|
-
|
|
148
|
-
toCamel({ $ref: 1, '@type': 'user' }, ['$']); // { $ref: 1, type: 'user' }
|
|
190
|
+
const user = toCamel({ first_name: 'John', 'Home Addresses': [{ POSTAL_CODE: '61105' }] });
|
|
191
|
+
// ^? { firstName: string; homeAddresses: { postalCode: string }[] }
|
|
149
192
|
|
|
150
|
-
|
|
151
|
-
|
|
193
|
+
user.firstName; // ✅
|
|
194
|
+
user.first_name; // ❌ Property 'first_name' does not exist
|
|
152
195
|
```
|
|
153
196
|
|
|
154
|
-
|
|
197
|
+
The options are part of the inference: `toCamel({ $ref: 1 }, ['$'])` is typed `{ $ref: number }`, and `toDot({ UserId: 1 }, { transform: 'lowercase' })` is typed `{ 'user.id': number }`. When the options are only known at runtime (e.g. a `boolean` variable), the keys are typed as `string`.
|
|
198
|
+
|
|
199
|
+
## How keys are split into words
|
|
200
|
+
|
|
201
|
+
Every function first splits the input into words, then joins them in the target case. A new word starts:
|
|
202
|
+
|
|
203
|
+
- **at a separator:** `_`, `-`, `.` or a space, which are removed. Repeated or leading separators are ignored: `toSnake('__private_flag')` → `'private_flag'`
|
|
204
|
+
- **at an uppercase letter after a lowercase letter or a digit:** `helloWorld` → `hello`, `world`
|
|
205
|
+
- **at the last letter of an acronym:** `XMLHttpRequest` → `xml`, `http`, `request`, and `userID` → `user`, `id`
|
|
206
|
+
- **at a symbol**, whether it's kept or not
|
|
207
|
+
- **at `/` or `\`, only in `toPath`** (in the other functions they are symbols)
|
|
208
|
+
|
|
209
|
+
Digits stay attached to the previous word: `toSnake('html5Parser')` → `'html5_parser'`.
|
|
210
|
+
|
|
211
|
+
A kept symbol sticks to the start of the next word, or to the end of the previous one when no word follows:
|
|
155
212
|
|
|
156
213
|
```ts
|
|
157
214
|
toSnake('$$hello$World$$hi'); // 'hello_world_hi'
|
|
@@ -160,11 +217,51 @@ toSnake('user@name', true); // 'user_@name'
|
|
|
160
217
|
toSnake('total%_count', true); // 'total%_count'
|
|
161
218
|
```
|
|
162
219
|
|
|
163
|
-
The inferred types follow the same rules.
|
|
220
|
+
The inferred types follow the same rules.
|
|
221
|
+
|
|
222
|
+
## Behavior and limitations
|
|
223
|
+
|
|
224
|
+
- **Only keys are converted, never values.** In `{ userName: 'johnDoe' }`, `userName` becomes `user_name` but `'johnDoe'` is kept. Strings inside arrays are kept too.
|
|
225
|
+
- **Only plain objects are traversed.** `Date`, `Map`, `Set` and class instances are returned as they are (same reference), without converting their contents.
|
|
226
|
+
- **Acronyms are kept together, but not restored:** `toSnake('userID')` → `'user_id'`, and back `toCamel('user_id')` → `'userId'`.
|
|
227
|
+
- **Words are lowercased** before converting (except by `toDot`, `toPath` and `toSpace`), so `toCamel('X-API-Key')` → `'xApiKey'` and `toCamel('First Name')` → `'firstName'`.
|
|
228
|
+
- **Title Case capitalizes every word**, including short ones: `toTitle('termsOfUse')` → `'Terms Of Use'`.
|
|
229
|
+
- **Type inference has a key length limit.** TypeScript limits how deeply a type can recurse, and keys are converted character by character at the type level. Keys are inferred up to ~120 characters; longer keys fail to compile with `Type instantiation is excessively deep and possibly infinite`. The runtime conversion has no limit.
|
|
230
|
+
|
|
231
|
+
## Compatibility
|
|
232
|
+
|
|
233
|
+
| Environment | Supported |
|
|
234
|
+
| --- | --- |
|
|
235
|
+
| ESM (`import`) | Node.js 12.22+, Deno, Bun, bundlers |
|
|
236
|
+
| CommonJS (`require`) | Node.js 8+, Bun |
|
|
237
|
+
| TypeScript | 4.5+ for the `toX` functions, 4.1+ for the deprecated `<from>To<To>` functions (any `moduleResolution`: `node`, `node16`/`nodenext`, `bundler`) |
|
|
238
|
+
| Browsers | Any ES2015 browser (via bundler) |
|
|
239
|
+
| Edge | Cloudflare Workers |
|
|
240
|
+
|
|
241
|
+
On every change, CI runs the test suite on Node.js 22, 24 and 26, Bun and Deno, and in Chromium, Firefox and WebKit. It also checks the packed package with [publint](https://publint.dev) and [Are the Types Wrong?](https://arethetypeswrong.github.io), for ESM, CommonJS and every `moduleResolution`, and compiles the type declarations with TypeScript 4.1.
|
|
164
242
|
|
|
165
|
-
##
|
|
243
|
+
## Deprecated `<from>To<To>` functions
|
|
166
244
|
|
|
167
|
-
> **Deprecated:** the `<from>To<To>` functions
|
|
245
|
+
> **Deprecated:** the `<from>To<To>` functions (e.g. `snakeToCamel`) are deprecated in favor of the `toX` functions and will be removed in the next major version, which will also require TypeScript 4.7+. They keep working until then.
|
|
246
|
+
|
|
247
|
+
### Migrating to `toX`
|
|
248
|
+
|
|
249
|
+
Replace each `<from>To<To>` function with the `toX` function for its target case, whatever the source case: `camelToSnake`, `dashToSnake`, `titleToSnake`... all become `toSnake`. The dash cases were renamed: `<from>ToDash` becomes `toKebab`, and `<from>ToUpperDash` becomes `toUpperKebab`.
|
|
250
|
+
|
|
251
|
+
For well-formed keys (`firstName`, `first_name`) the result is the same. It differs when a key has consecutive uppercase letters or doesn't match the source case, and `toDot` keeps the original case of each word (pass `{ transform: 'lowercase' }` to get the old result):
|
|
252
|
+
|
|
253
|
+
| Call | `<from>To<To>` result | `toX` result |
|
|
254
|
+
| --- | --- | --- |
|
|
255
|
+
| `camelToSnake('userID')` / `toSnake('userID')` | `'user_i_d'` | `'user_id'` |
|
|
256
|
+
| `camelToSnake('XMLHttpRequest')` / `toSnake('XMLHttpRequest')` | `'_x_m_l_http_request'` | `'xml_http_request'` |
|
|
257
|
+
| `camelToSnake('HelloWorld')` / `toSnake('HelloWorld')` | `'_hello_world'` | `'hello_world'` |
|
|
258
|
+
| `snakeToCamel('user_ID')` / `toCamel('user_ID')` | `'userID'` | `'userId'` |
|
|
259
|
+
| `camelToDot('helloWorld')` / `toDot('helloWorld')` | `'hello.world'` | `'hello.World'` |
|
|
260
|
+
|
|
261
|
+
If your code reads keys like `user_i_d` produced by the old functions, update those reads when migrating. The inferred types follow the new results, so TypeScript points out every place to change.
|
|
262
|
+
|
|
263
|
+
<details>
|
|
264
|
+
<summary>All 90 deprecated functions</summary>
|
|
168
265
|
|
|
169
266
|
Every function is named `<from>To<To>`, e.g. `snakeToCamel`. The case names are:
|
|
170
267
|
|
|
@@ -181,8 +278,6 @@ Every function is named `<from>To<To>`, e.g. `snakeToCamel`. The case names are:
|
|
|
181
278
|
| `title` | `Hello World` |
|
|
182
279
|
| `sentence` | `Hello world` |
|
|
183
280
|
|
|
184
|
-
All 90 functions:
|
|
185
|
-
|
|
186
281
|
- **camelCase:** `camelToPascal`, `camelToSnake`, `camelToDash`, `camelToUpperSnake`, `camelToUpperDash`, `camelToTrain`, `camelToDot`, `camelToTitle`, `camelToSentence`
|
|
187
282
|
- **PascalCase:** `pascalToCamel`, `pascalToSnake`, `pascalToDash`, `pascalToUpperSnake`, `pascalToUpperDash`, `pascalToTrain`, `pascalToDot`, `pascalToTitle`, `pascalToSentence`
|
|
188
283
|
- **snake_case:** `snakeToCamel`, `snakeToPascal`, `snakeToDash`, `snakeToUpperSnake`, `snakeToUpperDash`, `snakeToTrain`, `snakeToDot`, `snakeToTitle`, `snakeToSentence`
|
|
@@ -194,31 +289,7 @@ All 90 functions:
|
|
|
194
289
|
- **Title Case:** `titleToCamel`, `titleToPascal`, `titleToSnake`, `titleToDash`, `titleToUpperSnake`, `titleToUpperDash`, `titleToTrain`, `titleToDot`, `titleToSentence`
|
|
195
290
|
- **Sentence case:** `sentenceToCamel`, `sentenceToPascal`, `sentenceToSnake`, `sentenceToDash`, `sentenceToUpperSnake`, `sentenceToUpperDash`, `sentenceToTrain`, `sentenceToDot`, `sentenceToTitle`
|
|
196
291
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
Replace each `<from>To<To>` function with the `toX` function for its target case, whatever the source case: `camelToSnake`, `dashToSnake`, `titleToSnake`... all become `toSnake`. The dash cases were renamed: `<from>ToDash` becomes `toKebab`, and `<from>ToUpperDash` becomes `toUpperKebab`.
|
|
200
|
-
|
|
201
|
-
For well-formed keys (`firstName`, `first_name`) the result is the same. It differs when a key has consecutive uppercase letters or doesn't match the source case, and `toDot` keeps the original case of each word:
|
|
202
|
-
|
|
203
|
-
| Call | `<from>To<To>` result | `toX` result |
|
|
204
|
-
| --- | --- | --- |
|
|
205
|
-
| `camelToSnake('userID')` / `toSnake('userID')` | `'user_i_d'` | `'user_id'` |
|
|
206
|
-
| `camelToSnake('XMLHttpRequest')` / `toSnake('XMLHttpRequest')` | `'_x_m_l_http_request'` | `'xml_http_request'` |
|
|
207
|
-
| `camelToSnake('HelloWorld')` / `toSnake('HelloWorld')` | `'_hello_world'` | `'hello_world'` |
|
|
208
|
-
| `snakeToCamel('user_ID')` / `toCamel('user_ID')` | `'userID'` | `'userId'` |
|
|
209
|
-
| `camelToDot('helloWorld')` / `toDot('helloWorld')` | `'hello.world'` | `'hello.World'` |
|
|
210
|
-
|
|
211
|
-
If your code reads keys like `user_i_d` produced by the old functions, update those reads when migrating. The inferred types follow the new results, so TypeScript points out every place to change.
|
|
212
|
-
|
|
213
|
-
## Behavior and limitations
|
|
214
|
-
|
|
215
|
-
- **Only keys are converted, never values.** In `{ userName: 'johnDoe' }`, `userName` becomes `user_name` but `'johnDoe'` is kept. Strings inside arrays are kept too.
|
|
216
|
-
- **Only plain objects are traversed.** `Date`, `Map`, `Set` and class instances are returned as they are (same reference), without converting their contents.
|
|
217
|
-
- **Acronyms are kept together, but not restored:** `toSnake('userID')` → `'user_id'`, and back `toCamel('user_id')` → `'userId'`.
|
|
218
|
-
- **Words are lowercased** before converting (except by `toSpace`, `toPath` and `toDot`), so `toCamel('X-API-Key')` → `'xApiKey'` and `toCamel('First Name')` → `'firstName'`.
|
|
219
|
-
- **Title Case capitalizes every word**, including short ones: `toTitle('termsOfUse')` → `'Terms Of Use'`.
|
|
220
|
-
- **Digits stay attached to the previous word:** `toSnake('html5Parser')` → `'html5_parser'`, `toSnake('user1Name')` → `'user1_name'`.
|
|
221
|
-
- **Type inference has a key length limit.** TypeScript limits how deeply a type can recurse, and keys are converted character by character at the type level. The `toX` functions infer keys up to ~120 characters; longer keys fail to compile with `Type instantiation is excessively deep and possibly infinite`. The runtime conversion has no limit.
|
|
292
|
+
</details>
|
|
222
293
|
|
|
223
294
|
## Security
|
|
224
295
|
|