caseparser 3.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 CHANGED
@@ -1,163 +1,138 @@
1
- # [CaseParser 2](https://github.com/NandoMB/caseparser)
1
+ # [CaseParser](https://github.com/NandoMB/caseparser)
2
+
3
+ [![npm version](https://img.shields.io/npm/v/caseparser.svg)](https://www.npmjs.com/package/caseparser)
4
+ [![JSR](https://jsr.io/badges/@nandomb/caseparser)](https://jsr.io/@nandomb/caseparser)
5
+ [![JSR Score](https://jsr.io/badges/@nandomb/caseparser/score)](https://jsr.io/@nandomb/caseparser)
6
+ [![CI](https://github.com/NandoMB/caseparser/actions/workflows/main.yml/badge.svg)](https://github.com/NandoMB/caseparser/actions/workflows/main.yml)
7
+ [![npm downloads](https://img.shields.io/npm/dm/caseparser.svg)](https://www.npmjs.com/package/caseparser)
8
+ [![license](https://img.shields.io/npm/l/caseparser.svg)](./LICENSE)
9
+
2
10
  Convert **Strings** and **JSON (Object Keys)** from a **case type** to another one with **type inference** based on parameter's type.
3
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
16
+
4
17
 
5
18
  ###### Note:
6
- > This is the **version 2.x.x** that supports **Typescript**. If you're looking for version 1.x.x, [click here](https://github.com/NandoMB/caseparser/tree/v1.x.x) to see the docs.
19
+ > If you're looking for version 1.x.x, [click here](https://github.com/NandoMB/caseparser/tree/v1.x.x) to see the docs.
7
20
 
8
21
 
9
22
  ## Installation
10
23
 
11
- ###### Yarn
12
24
  ```sh
13
- yarn add caseparser
25
+ npm add caseparser # or: pnpm add caseparser / yarn add caseparser
14
26
  ```
15
- ###### NPM
27
+ ###### Deno (JSR)
16
28
  ```sh
17
- npm add caseparser
29
+ deno add jsr:@nandomb/caseparser
30
+ ```
31
+ ```ts
32
+ import { camelToSnake } from '@nandomb/caseparser';
18
33
  ```
19
- ###### PNPM
34
+ ###### Bun / Node.js from JSR
20
35
  ```sh
21
- pnpm add caseparser
36
+ bunx jsr add @nandomb/caseparser
37
+ npx jsr add @nandomb/caseparser
22
38
  ```
23
39
 
40
+ ## Compatibility
41
+
42
+ | Environment | Supported |
43
+ | --- | --- |
44
+ | ESM (`import`) | Node.js 12.22+, Deno, Bun, bundlers |
45
+ | CommonJS (`require`) | Node.js 8+, Bun |
46
+ | TypeScript | 4.1+ (any `moduleResolution`: `node`, `node16`/`nodenext`, `bundler`) |
47
+ | Browsers | Any ES2015 browser (via bundler) |
48
+ | Edge | Cloudflare Workers |
49
+
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).
53
+
24
54
  ## How to use
25
55
 
26
- ###### Example 1
27
- Passing **String** as parameter:
28
56
  ```ts
29
- import { camelToSnake } from 'caseparser';
57
+ import { camelToSnake } from 'caseparser'; // ESM
58
+ // const { camelToSnake } = require('caseparser'); // CommonJS
59
+
60
+ camelToSnake('helloWorld'); // 'hello_world'
30
61
 
31
- camelToSnake('loremIpsumIsSimplyDummyTextOfThePrintingAndTypesettingIndustry');
62
+ camelToSnake({ firstName: 'John', addresses: [{ postalCode: '61105' }] });
63
+ // { first_name: 'John', addresses: [{ postal_code: '61105' }] }
32
64
  ```
33
- Will result:
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
+
34
70
  ```ts
35
- 'lorem_ipsum_is_simply_dummy_text_of_the_printing_and_typesetting_industry'
36
- ```
37
- <br/>
38
-
39
- ###### Example 2
40
- Passing **JSON** as parameter:
41
- ```js
42
- import { camelToSnake } from 'caseparser';
43
-
44
- const data = [
45
- {
46
- id: 1,
47
- firstName: 'John',
48
- lastName: 'Doe',
49
- email: 'john.doe@example.com',
50
- addresses: [
51
- {
52
- country: 'United States',
53
- state: 'Illinois',
54
- city: 'Rockford',
55
- postalCode: '61105',
56
- street: {
57
- streetName: '41 Forest Run Circle',
58
- streetNumber: '539'
59
- }
60
- }
61
- ]
62
- }
63
- ];
64
- 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
+ });
65
80
  ```
66
- Will Result:
67
- ```json
68
- [
69
- {
70
- "id": 1,
71
- "first_name": "John",
72
- "last_name": "Doe",
73
- "email": "john.doe@example.com",
74
- "addresses": [
75
- {
76
- "country": "United States",
77
- "state": "Illinois",
78
- "city": "Rockford",
79
- "postal_code": "61105",
80
- "street": {
81
- "street_name": "41 Forest Run Circle",
82
- "street_number": "539"
83
- }
84
- }
85
- ]
86
- }
87
- ]
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
88
92
  ```
89
93
 
90
94
  ## Conversion Types
91
95
 
92
- ###### camelCase to ...
93
- ```js
94
- camelToDash(data);
95
- camelToPascal(data);
96
- camelToSnake(data);
97
- camelToUpperDash(data);
98
- camelToUpperSnake(data);
99
- ```
100
- ###### snakeCase to ...
101
- ```js
102
- snakeToCamel(data);
103
- snakeToDash(data);
104
- snakeToPascal(data);
105
- snakeToUpperDash(data);
106
- snakeToUpperSnake(data);
107
- ```
108
- ###### dashCase to ...
109
- ```js
110
- dashToCamel(data);
111
- dashToPascal(data);
112
- dashToSnake(data);
113
- dashToUpperDash(data);
114
- dashToUpperSnake(data);
115
- ```
116
- ###### pascalCase to ...
117
- ```js
118
- pascalToCamel(data);
119
- pascalToDash(data);
120
- pascalToSnake(data);
121
- pascalToUpperDash(data);
122
- pascalToUpperSnake(data);
123
- ```
124
- ###### upperSnakeCase to ...
125
- ```js
126
- upperSnakeToCamel(data);
127
- upperSnakeToDash(data);
128
- upperSnakeToPascal(data);
129
- upperSnakeToSnake(data);
130
- upperSnakeToUpperDash(data);
131
- ```
132
- ###### upperDashCase to ...
133
- ```js
134
- upperDashToCamel(data);
135
- upperDashToDash(data);
136
- upperDashToPascal(data);
137
- upperDashToSnake(data);
138
- upperDashToUpperSnake(data);
139
- ```
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'`.
127
+
128
+ ## Security
129
+
130
+ caseparser is safe to use with untrusted input (e.g. request bodies or `JSON.parse` output): keys such as `__proto__` are copied as regular keys and never change an object's prototype, and only the object's own properties are converted.
131
+
132
+ Releases are built and published from GitHub Actions without long-lived tokens (OIDC), with [npm provenance](https://docs.npmjs.com/generating-provenance-statements), so every published version can be traced back to the exact commit and workflow that built it. On npm, new versions are [staged](https://docs.npmjs.com/staged-publishing/) and only go live after a maintainer approves them with 2FA.
133
+
134
+ Found a vulnerability? Please report it privately, see [SECURITY.md](./SECURITY.md).
140
135
 
141
136
  ## License
142
- The MIT License (MIT)
143
-
144
- Copyright (c) 2017 Fernando Machado Bernardino
145
- [NandoMB](https://github.com/NandoMB). https://github.com/NandoMB/caseparser
146
-
147
- Permission is hereby granted, free of charge, to any person obtaining a copy
148
- of this software and associated documentation files (the "Software"), to deal
149
- in the Software without restriction, including without limitation the rights
150
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
151
- copies of the Software, and to permit persons to whom the Software is
152
- furnished to do so, subject to the following conditions:
153
-
154
- The above copyright notice and this permission notice shall be included in all
155
- copies or substantial portions of the Software.
156
-
157
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
159
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
161
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
162
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
163
- SOFTWARE.
137
+
138
+ [MIT](./LICENSE) © 2017 Fernando Machado Bernardino