toon-formatter 1.0.1 → 1.0.3
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/.github/FUNDING.yml +15 -0
- package/ENHANCEMENTS.md +124 -0
- package/README.md +398 -54
- package/package.json +5 -1
- package/src/csv.js +66 -24
- package/src/index.js +103 -22
- package/src/json.js +65 -25
- package/src/utils.js +155 -0
- package/src/validator.js +73 -11
- package/src/xml.js +86 -34
- package/src/yaml.js +27 -8
- package/test/basic.test.js +17 -17
- package/test/converters.test.js +9 -9
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ A lightweight, zero-dependency* library to convert between **TOON** (Token-Orien
|
|
|
11
11
|
## 📦 Installation
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
|
-
npm install toon-
|
|
14
|
+
npm install toon-formatter
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
---
|
|
@@ -52,14 +52,14 @@ users[3]{id,name,active}:
|
|
|
52
52
|
|
|
53
53
|
## 🚀 Quick Start
|
|
54
54
|
|
|
55
|
-
### Basic Usage
|
|
55
|
+
### Basic Usage (Synchronous)
|
|
56
56
|
|
|
57
57
|
```javascript
|
|
58
|
-
import {
|
|
58
|
+
import { jsonToToonSync, toonToJsonSync } from 'toon-formatter';
|
|
59
59
|
|
|
60
60
|
// JSON to TOON
|
|
61
61
|
const jsonData = { name: "Alice", age: 30, active: true };
|
|
62
|
-
const toonString =
|
|
62
|
+
const toonString = jsonToToonSync(jsonData);
|
|
63
63
|
console.log(toonString);
|
|
64
64
|
// Output:
|
|
65
65
|
// name: "Alice"
|
|
@@ -68,48 +68,239 @@ console.log(toonString);
|
|
|
68
68
|
|
|
69
69
|
// TOON to JSON
|
|
70
70
|
const toonInput = `name: "Alice"\nage: 30\nactive: true`;
|
|
71
|
-
const jsonOutput =
|
|
71
|
+
const jsonOutput = toonToJsonSync(toonInput);
|
|
72
72
|
console.log(jsonOutput);
|
|
73
73
|
// Output: { name: "Alice", age: 30, active: true }
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
+
### Basic Usage (Asynchronous)
|
|
77
|
+
|
|
78
|
+
```javascript
|
|
79
|
+
import { jsonToToon, toonToJson } from 'toon-formatter';
|
|
80
|
+
|
|
81
|
+
// JSON to TOON (async)
|
|
82
|
+
const jsonData = { name: "Alice", age: 30, active: true };
|
|
83
|
+
const toonString = await jsonToToon(jsonData);
|
|
84
|
+
console.log(toonString);
|
|
85
|
+
|
|
86
|
+
// TOON to JSON (async)
|
|
87
|
+
const toonInput = `name: "Alice"\nage: 30\nactive: true`;
|
|
88
|
+
const jsonOutput = await toonToJson(toonInput);
|
|
89
|
+
console.log(jsonOutput);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 🎯 Mixed Text Support (Embedded Data)
|
|
93
|
+
|
|
94
|
+
**JSON, XML, and CSV to TOON conversions support both full data strings AND mixed text with embedded data!**
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
import { jsonToToonSync, xmlToToon, csvToToonSync } from 'toon-formatter';
|
|
98
|
+
|
|
99
|
+
// Example 1: Extract and convert JSON from mixed text
|
|
100
|
+
const mixedText = `
|
|
101
|
+
Here's some user data:
|
|
102
|
+
{"name": "Alice", "age": 30, "role": "Engineer"}
|
|
103
|
+
|
|
104
|
+
And here's another object:
|
|
105
|
+
{"name": "Bob", "age": 25, "role": "Designer"}
|
|
106
|
+
`;
|
|
107
|
+
|
|
108
|
+
const result = jsonToToonSync(mixedText);
|
|
109
|
+
console.log(result);
|
|
110
|
+
// Output:
|
|
111
|
+
// Here's some user data:
|
|
112
|
+
// name: "Alice"
|
|
113
|
+
// age: 30
|
|
114
|
+
// role: "Engineer"
|
|
115
|
+
//
|
|
116
|
+
// And here's another object:
|
|
117
|
+
// name: "Bob"
|
|
118
|
+
// age: 25
|
|
119
|
+
// role: "Designer"
|
|
120
|
+
|
|
121
|
+
// Example 2: Extract and convert XML from mixed text
|
|
122
|
+
const xmlMixedText = `
|
|
123
|
+
The user profile is:
|
|
124
|
+
<user><name>Alice</name><age>30</age></user>
|
|
125
|
+
`;
|
|
126
|
+
|
|
127
|
+
const xmlResult = await xmlToToon(xmlMixedText);
|
|
128
|
+
console.log(xmlResult);
|
|
129
|
+
// Output:
|
|
130
|
+
// The user profile is:
|
|
131
|
+
// user:
|
|
132
|
+
// name: "Alice"
|
|
133
|
+
// age: "30"
|
|
134
|
+
|
|
135
|
+
// Example 3: Extract and convert CSV from mixed text
|
|
136
|
+
const csvMixedText = `
|
|
137
|
+
Employee data:
|
|
138
|
+
name,role,salary
|
|
139
|
+
Alice,Engineer,100000
|
|
140
|
+
Bob,Designer,95000
|
|
141
|
+
`;
|
|
142
|
+
|
|
143
|
+
const csvResult = csvToToonSync(csvMixedText);
|
|
144
|
+
// Converts the CSV table to TOON format while preserving surrounding text
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
**Important Notes:**
|
|
148
|
+
- ✅ **Supports mixed text**: `jsonToToon`, `xmlToToon`, `csvToToon`, `yamlToToon`
|
|
149
|
+
- ❌ **Pure data only**: `toonToJson`, `toonToXml`, `toonToCsv`, `toonToYaml`
|
|
150
|
+
|
|
76
151
|
### Using the ToonConverter Class
|
|
77
152
|
|
|
78
153
|
```javascript
|
|
79
|
-
import ToonConverter from 'toon-
|
|
154
|
+
import ToonConverter from 'toon-formatter';
|
|
80
155
|
|
|
81
|
-
//
|
|
156
|
+
// Synchronous conversions (default methods)
|
|
82
157
|
const toonFromJson = ToonConverter.fromJson({ key: "value" });
|
|
83
158
|
const toonFromYaml = ToonConverter.fromYaml("key: value");
|
|
84
159
|
const toonFromXml = ToonConverter.fromXml("<root><key>value</key></root>");
|
|
85
|
-
const toonFromCsv =
|
|
160
|
+
const toonFromCsv = ToonConverter.fromCsv("name,age\nAlice,30");
|
|
161
|
+
|
|
162
|
+
// Asynchronous conversions (methods with 'Async' suffix)
|
|
163
|
+
const toonFromJsonAsync = await ToonConverter.fromJsonAsync({ key: "value" });
|
|
164
|
+
const toonFromYamlAsync = await ToonConverter.fromYamlAsync("key: value");
|
|
165
|
+
const toonFromXmlAsync = await ToonConverter.fromXmlAsync("<root><key>value</key></root>");
|
|
166
|
+
const toonFromCsvAsync = await ToonConverter.fromCsvAsync("name,age\nAlice,30");
|
|
86
167
|
|
|
87
|
-
// Convert to various formats
|
|
168
|
+
// Convert to various formats (synchronous by default)
|
|
88
169
|
const jsonData = ToonConverter.toJson(toonString);
|
|
89
170
|
const yamlData = ToonConverter.toYaml(toonString);
|
|
90
171
|
const xmlData = ToonConverter.toXml(toonString);
|
|
91
172
|
const csvData = ToonConverter.toCsv(toonString);
|
|
92
173
|
|
|
93
|
-
//
|
|
174
|
+
// Asynchronous versions (methods with 'Async' suffix)
|
|
175
|
+
const jsonDataAsync = await ToonConverter.toJsonAsync(toonString);
|
|
176
|
+
const yamlDataAsync = await ToonConverter.toYamlAsync(toonString);
|
|
177
|
+
const xmlDataAsync = await ToonConverter.toXmlAsync(toonString);
|
|
178
|
+
const csvDataAsync = await ToonConverter.toCsvAsync(toonString);
|
|
179
|
+
|
|
180
|
+
// Validate TOON (synchronous by default)
|
|
94
181
|
const result = ToonConverter.validate(toonString);
|
|
95
182
|
if (result.isValid) {
|
|
96
183
|
console.log("Valid TOON!");
|
|
97
184
|
} else {
|
|
98
185
|
console.error("Invalid TOON:", result.error);
|
|
99
186
|
}
|
|
187
|
+
|
|
188
|
+
// Validate TOON (asynchronous)
|
|
189
|
+
const resultAsync = await ToonConverter.validateAsync(toonString);
|
|
100
190
|
```
|
|
101
191
|
|
|
102
192
|
---
|
|
103
193
|
|
|
104
|
-
##
|
|
194
|
+
## 🔄 Sync vs Async API
|
|
195
|
+
|
|
196
|
+
All conversion functions are available in both **synchronous** and **asynchronous** versions:
|
|
197
|
+
|
|
198
|
+
### Direct Function Imports
|
|
199
|
+
|
|
200
|
+
**Synchronous Functions (Suffix: `Sync`)**
|
|
201
|
+
- `jsonToToonSync()`, `toonToJsonSync()`
|
|
202
|
+
- `yamlToToonSync()`, `toonToYamlSync()`
|
|
203
|
+
- `xmlToToonSync()`, `toonToXmlSync()`
|
|
204
|
+
- `csvToToonSync()`, `toonToCsvSync()`
|
|
205
|
+
- `validateToonStringSync()`
|
|
206
|
+
|
|
207
|
+
**Use when:** You need immediate results and are working in a synchronous context.
|
|
208
|
+
|
|
209
|
+
**Asynchronous Functions (No suffix)**
|
|
210
|
+
- `jsonToToon()`, `toonToJson()`
|
|
211
|
+
- `yamlToToon()`, `toonToYaml()`
|
|
212
|
+
- `xmlToToon()`, `toonToXml()`
|
|
213
|
+
- `csvToToon()`, `toonToCsv()`
|
|
214
|
+
- `validateToonString()`
|
|
215
|
+
|
|
216
|
+
**Use when:** You're in an async context or want to maintain consistency with async/await patterns.
|
|
217
|
+
|
|
218
|
+
### ToonConverter Class Methods
|
|
219
|
+
|
|
220
|
+
**Synchronous Methods (No suffix - default)**
|
|
221
|
+
- `ToonConverter.fromJson()`, `ToonConverter.toJson()`
|
|
222
|
+
- `ToonConverter.fromYaml()`, `ToonConverter.toYaml()`
|
|
223
|
+
- `ToonConverter.fromXml()`, `ToonConverter.toXml()`
|
|
224
|
+
- `ToonConverter.fromCsv()`, `ToonConverter.toCsv()`
|
|
225
|
+
- `ToonConverter.validate()`
|
|
226
|
+
|
|
227
|
+
**Asynchronous Methods (Suffix: `Async`)**
|
|
228
|
+
- `ToonConverter.fromJsonAsync()`, `ToonConverter.toJsonAsync()`
|
|
229
|
+
- `ToonConverter.fromYamlAsync()`, `ToonConverter.toYamlAsync()`
|
|
230
|
+
- `ToonConverter.fromXmlAsync()`, `ToonConverter.toXmlAsync()`
|
|
231
|
+
- `ToonConverter.fromCsvAsync()`, `ToonConverter.toCsvAsync()`
|
|
232
|
+
- `ToonConverter.validateAsync()`
|
|
233
|
+
|
|
234
|
+
**Note:**
|
|
235
|
+
- For **direct imports**, sync functions have `Sync` suffix, async functions have no suffix
|
|
236
|
+
- For **ToonConverter class**, sync methods have no suffix (default), async methods have `Async` suffix
|
|
237
|
+
- For XML conversions in Node.js, the async version automatically loads the `xmldom` package if needed
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
## 🎯 Mixed Text Support
|
|
242
|
+
|
|
243
|
+
### What is Mixed Text?
|
|
244
|
+
|
|
245
|
+
Mixed text support allows you to convert data that's embedded within regular text, not just pure data strings. This is incredibly useful for processing documentation, API responses, or any content that contains data snippets.
|
|
246
|
+
|
|
247
|
+
### Supported Conversions
|
|
248
|
+
|
|
249
|
+
| Conversion | Full Data | Mixed Text | Notes |
|
|
250
|
+
|------------|-----------|------------|-------|
|
|
251
|
+
| `jsonToToon()` | ✅ | ✅ | Extracts all JSON objects/arrays |
|
|
252
|
+
| `xmlToToon()` | ✅ | ✅ | Extracts all XML elements |
|
|
253
|
+
| `csvToToon()` | ✅ | ✅ | Extracts CSV tables |
|
|
254
|
+
| `yamlToToon()` | ✅ | ✅ | Extracts YAML blocks |
|
|
255
|
+
| `toonToJson()` | ✅ | ❌ | Pure TOON only |
|
|
256
|
+
| `toonToXml()` | ✅ | ❌ | Pure TOON only |
|
|
257
|
+
| `toonToCsv()` | ✅ | ❌ | Pure TOON only |
|
|
258
|
+
| `toonToYaml()` | ✅ | ❌ | Pure TOON only |
|
|
259
|
+
|
|
260
|
+
### Example
|
|
261
|
+
|
|
262
|
+
```javascript
|
|
263
|
+
import { jsonToToonSync } from 'toon-formatter';
|
|
264
|
+
|
|
265
|
+
const documentation = `
|
|
266
|
+
# API Documentation
|
|
267
|
+
|
|
268
|
+
## User Endpoint
|
|
269
|
+
Returns: {"id": 1, "name": "Alice", "role": "admin"}
|
|
270
|
+
|
|
271
|
+
## Product Endpoint
|
|
272
|
+
Returns: {"id": 101, "title": "Widget", "price": 29.99}
|
|
273
|
+
`;
|
|
274
|
+
|
|
275
|
+
const converted = jsonToToonSync(documentation);
|
|
276
|
+
console.log(converted);
|
|
277
|
+
// Output:
|
|
278
|
+
// # API Documentation
|
|
279
|
+
//
|
|
280
|
+
// ## User Endpoint
|
|
281
|
+
// Returns: id: 1
|
|
282
|
+
// name: "Alice"
|
|
283
|
+
// role: "admin"
|
|
284
|
+
//
|
|
285
|
+
// ## Product Endpoint
|
|
286
|
+
// Returns: id: 101
|
|
287
|
+
// title: "Widget"
|
|
288
|
+
// price: 29.99
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
## �📚 API Reference
|
|
105
294
|
|
|
106
295
|
### JSON Converters
|
|
107
296
|
|
|
108
|
-
#### `
|
|
109
|
-
Converts JSON data to TOON format.
|
|
297
|
+
#### `jsonToToonSync(data, key?, depth?)`
|
|
298
|
+
Converts JSON data to TOON format (synchronous).
|
|
299
|
+
|
|
300
|
+
**Supports:** ✅ Full JSON data, ✅ Mixed text with embedded JSON
|
|
110
301
|
|
|
111
302
|
**Parameters:**
|
|
112
|
-
- `data` (any): JSON data to convert
|
|
303
|
+
- `data` (any): JSON data to convert, or string containing JSON
|
|
113
304
|
- `key` (string, optional): Key name for root object
|
|
114
305
|
- `depth` (number, optional): Initial indentation depth
|
|
115
306
|
|
|
@@ -117,46 +308,85 @@ Converts JSON data to TOON format.
|
|
|
117
308
|
|
|
118
309
|
**Example:**
|
|
119
310
|
```javascript
|
|
120
|
-
import {
|
|
311
|
+
import { jsonToToonSync } from 'toon-formatter';
|
|
121
312
|
|
|
313
|
+
// Full JSON data
|
|
122
314
|
const data = {
|
|
123
315
|
users: [
|
|
124
316
|
{ id: 1, name: "Alice" },
|
|
125
317
|
{ id: 2, name: "Bob" }
|
|
126
318
|
]
|
|
127
319
|
};
|
|
320
|
+
const toon = jsonToToonSync(data);
|
|
128
321
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
//
|
|
133
|
-
// 2,"Bob"
|
|
322
|
+
// Mixed text with embedded JSON
|
|
323
|
+
const mixedText = 'User: {"name": "Alice", "age": 30}';
|
|
324
|
+
const result = jsonToToonSync(mixedText);
|
|
325
|
+
// Output: User: name: "Alice"\nage: 30
|
|
134
326
|
```
|
|
135
327
|
|
|
136
|
-
#### `
|
|
137
|
-
Converts
|
|
328
|
+
#### `jsonToToon(data)`
|
|
329
|
+
Converts JSON data to TOON format (asynchronous).
|
|
330
|
+
|
|
331
|
+
**Supports:** ✅ Full JSON data, ✅ Mixed text with embedded JSON
|
|
332
|
+
|
|
333
|
+
**Parameters:**
|
|
334
|
+
- `data` (any): JSON data to convert, or string containing JSON
|
|
335
|
+
|
|
336
|
+
**Returns:** `Promise<string>` - TOON formatted string
|
|
337
|
+
|
|
338
|
+
#### `toonToJsonSync(toonString)`
|
|
339
|
+
Converts TOON string to JSON (synchronous).
|
|
340
|
+
|
|
341
|
+
**Supports:** ❌ Pure TOON data only (no mixed text)
|
|
138
342
|
|
|
139
343
|
**Parameters:**
|
|
140
344
|
- `toonString` (string): TOON formatted string
|
|
141
345
|
|
|
142
346
|
**Returns:** `any` - Parsed JSON data
|
|
143
347
|
|
|
348
|
+
#### `toonToJson(toonString)`
|
|
349
|
+
Converts TOON string to JSON (asynchronous).
|
|
350
|
+
|
|
351
|
+
**Supports:** ❌ Pure TOON data only (no mixed text)
|
|
352
|
+
|
|
353
|
+
**Parameters:**
|
|
354
|
+
- `toonString` (string): TOON formatted string
|
|
355
|
+
|
|
356
|
+
**Returns:** `Promise<any>` - Parsed JSON data
|
|
357
|
+
|
|
144
358
|
---
|
|
145
359
|
|
|
146
360
|
### YAML Converters
|
|
147
361
|
|
|
148
|
-
#### `
|
|
149
|
-
Converts YAML to TOON format.
|
|
362
|
+
#### `yamlToToonSync(yamlString)`
|
|
363
|
+
Converts YAML to TOON format (synchronous).
|
|
364
|
+
|
|
365
|
+
**Supports:** ✅ Full YAML data, ✅ Mixed text with embedded YAML
|
|
150
366
|
|
|
151
367
|
**Parameters:**
|
|
152
|
-
- `yamlString` (string): YAML formatted string
|
|
368
|
+
- `yamlString` (string): YAML formatted string or mixed text
|
|
153
369
|
|
|
154
370
|
**Returns:** `string` - TOON formatted string
|
|
155
371
|
|
|
156
372
|
**Throws:** `Error` if YAML is invalid
|
|
157
373
|
|
|
158
|
-
#### `
|
|
159
|
-
Converts
|
|
374
|
+
#### `yamlToToon(yamlString)`
|
|
375
|
+
Converts YAML to TOON format (asynchronous).
|
|
376
|
+
|
|
377
|
+
**Supports:** ✅ Full YAML data, ✅ Mixed text with embedded YAML
|
|
378
|
+
|
|
379
|
+
**Parameters:**
|
|
380
|
+
- `yamlString` (string): YAML formatted string or mixed text
|
|
381
|
+
|
|
382
|
+
**Returns:** `Promise<string>` - TOON formatted string
|
|
383
|
+
|
|
384
|
+
**Throws:** `Error` if YAML is invalid
|
|
385
|
+
|
|
386
|
+
#### `toonToYamlSync(toonString)`
|
|
387
|
+
Converts TOON to YAML format (synchronous).
|
|
388
|
+
|
|
389
|
+
**Supports:** ❌ Pure TOON data only (no mixed text)
|
|
160
390
|
|
|
161
391
|
**Parameters:**
|
|
162
392
|
- `toonString` (string): TOON formatted string
|
|
@@ -165,15 +395,29 @@ Converts TOON to YAML format.
|
|
|
165
395
|
|
|
166
396
|
**Throws:** `Error` if TOON is invalid
|
|
167
397
|
|
|
398
|
+
#### `toonToYaml(toonString)`
|
|
399
|
+
Converts TOON to YAML format (asynchronous).
|
|
400
|
+
|
|
401
|
+
**Supports:** ❌ Pure TOON data only (no mixed text)
|
|
402
|
+
|
|
403
|
+
**Parameters:**
|
|
404
|
+
- `toonString` (string): TOON formatted string
|
|
405
|
+
|
|
406
|
+
**Returns:** `Promise<string>` - YAML formatted string
|
|
407
|
+
|
|
408
|
+
**Throws:** `Error` if TOON is invalid
|
|
409
|
+
|
|
168
410
|
---
|
|
169
411
|
|
|
170
412
|
### XML Converters
|
|
171
413
|
|
|
172
|
-
#### `
|
|
173
|
-
Converts XML to TOON format.
|
|
414
|
+
#### `xmlToToonSync(xmlString)`
|
|
415
|
+
Converts XML to TOON format (synchronous).
|
|
416
|
+
|
|
417
|
+
**Supports:** ✅ Full XML data, ✅ Mixed text with embedded XML
|
|
174
418
|
|
|
175
419
|
**Parameters:**
|
|
176
|
-
- `xmlString` (string): XML formatted string
|
|
420
|
+
- `xmlString` (string): XML formatted string or mixed text
|
|
177
421
|
|
|
178
422
|
**Returns:** `string` - TOON formatted string
|
|
179
423
|
|
|
@@ -181,8 +425,24 @@ Converts XML to TOON format.
|
|
|
181
425
|
|
|
182
426
|
**Note:** Requires `DOMParser` (browser) or `xmldom` package (Node.js)
|
|
183
427
|
|
|
184
|
-
#### `
|
|
185
|
-
Converts
|
|
428
|
+
#### `xmlToToon(xmlString)`
|
|
429
|
+
Converts XML to TOON format (asynchronous).
|
|
430
|
+
|
|
431
|
+
**Supports:** ✅ Full XML data, ✅ Mixed text with embedded XML
|
|
432
|
+
|
|
433
|
+
**Parameters:**
|
|
434
|
+
- `xmlString` (string): XML formatted string or mixed text
|
|
435
|
+
|
|
436
|
+
**Returns:** `Promise<string>` - TOON formatted string
|
|
437
|
+
|
|
438
|
+
**Throws:** `Error` if XML is invalid
|
|
439
|
+
|
|
440
|
+
**Note:** Automatically loads `xmldom` in Node.js environments
|
|
441
|
+
|
|
442
|
+
#### `toonToXmlSync(toonString)`
|
|
443
|
+
Converts TOON to XML format (synchronous).
|
|
444
|
+
|
|
445
|
+
**Supports:** ❌ Pure TOON data only (no mixed text)
|
|
186
446
|
|
|
187
447
|
**Parameters:**
|
|
188
448
|
- `toonString` (string): TOON formatted string
|
|
@@ -191,37 +451,67 @@ Converts TOON to XML format.
|
|
|
191
451
|
|
|
192
452
|
**Throws:** `Error` if TOON is invalid
|
|
193
453
|
|
|
454
|
+
#### `toonToXml(toonString)`
|
|
455
|
+
Converts TOON to XML format (asynchronous).
|
|
456
|
+
|
|
457
|
+
**Supports:** ❌ Pure TOON data only (no mixed text)
|
|
458
|
+
|
|
459
|
+
**Parameters:**
|
|
460
|
+
- `toonString` (string): TOON formatted string
|
|
461
|
+
|
|
462
|
+
**Returns:** `Promise<string>` - XML formatted string
|
|
463
|
+
|
|
464
|
+
**Throws:** `Error` if TOON is invalid
|
|
465
|
+
|
|
194
466
|
---
|
|
195
467
|
|
|
196
468
|
### CSV Converters
|
|
197
469
|
|
|
470
|
+
#### `csvToToonSync(csvString)`
|
|
471
|
+
Converts CSV to TOON format (synchronous).
|
|
472
|
+
|
|
473
|
+
**Supports:** ✅ Full CSV data, ✅ Mixed text with embedded CSV
|
|
474
|
+
|
|
475
|
+
**Parameters:**
|
|
476
|
+
- `csvString` (string): CSV formatted string or mixed text
|
|
477
|
+
|
|
478
|
+
**Returns:** `string` - TOON formatted string
|
|
479
|
+
|
|
480
|
+
**Throws:** `Error` if CSV is invalid
|
|
481
|
+
|
|
198
482
|
#### `csvToToon(csvString)`
|
|
199
|
-
Converts CSV to TOON format (
|
|
483
|
+
Converts CSV to TOON format (asynchronous).
|
|
484
|
+
|
|
485
|
+
**Supports:** ✅ Full CSV data, ✅ Mixed text with embedded CSV
|
|
200
486
|
|
|
201
487
|
**Parameters:**
|
|
202
|
-
- `csvString` (string): CSV formatted string
|
|
488
|
+
- `csvString` (string): CSV formatted string or mixed text
|
|
203
489
|
|
|
204
490
|
**Returns:** `Promise<string>` - TOON formatted string
|
|
205
491
|
|
|
206
492
|
**Throws:** `Error` if CSV is invalid
|
|
207
493
|
|
|
208
|
-
#### `
|
|
209
|
-
Converts
|
|
494
|
+
#### `toonToCsvSync(toonString)`
|
|
495
|
+
Converts TOON to CSV format (synchronous).
|
|
496
|
+
|
|
497
|
+
**Supports:** ❌ Pure TOON data only (no mixed text)
|
|
210
498
|
|
|
211
499
|
**Parameters:**
|
|
212
|
-
- `
|
|
500
|
+
- `toonString` (string): TOON formatted string
|
|
213
501
|
|
|
214
|
-
**Returns:** `string` -
|
|
502
|
+
**Returns:** `string` - CSV formatted string
|
|
215
503
|
|
|
216
|
-
**Throws:** `Error` if
|
|
504
|
+
**Throws:** `Error` if TOON is invalid
|
|
217
505
|
|
|
218
506
|
#### `toonToCsv(toonString)`
|
|
219
|
-
Converts TOON to CSV format.
|
|
507
|
+
Converts TOON to CSV format (asynchronous).
|
|
508
|
+
|
|
509
|
+
**Supports:** ❌ Pure TOON data only (no mixed text)
|
|
220
510
|
|
|
221
511
|
**Parameters:**
|
|
222
512
|
- `toonString` (string): TOON formatted string
|
|
223
513
|
|
|
224
|
-
**Returns:** `string
|
|
514
|
+
**Returns:** `Promise<string>` - CSV formatted string
|
|
225
515
|
|
|
226
516
|
**Throws:** `Error` if TOON is invalid
|
|
227
517
|
|
|
@@ -229,8 +519,8 @@ Converts TOON to CSV format.
|
|
|
229
519
|
|
|
230
520
|
### Validator
|
|
231
521
|
|
|
232
|
-
#### `
|
|
233
|
-
Validates a TOON string for syntax and structural correctness.
|
|
522
|
+
#### `validateToonStringSync(toonString)`
|
|
523
|
+
Validates a TOON string for syntax and structural correctness (synchronous).
|
|
234
524
|
|
|
235
525
|
**Parameters:**
|
|
236
526
|
- `toonString` (string): TOON string to validate
|
|
@@ -239,9 +529,9 @@ Validates a TOON string for syntax and structural correctness.
|
|
|
239
529
|
|
|
240
530
|
**Example:**
|
|
241
531
|
```javascript
|
|
242
|
-
import {
|
|
532
|
+
import { validateToonStringSync } from 'toon-formatter';
|
|
243
533
|
|
|
244
|
-
const result =
|
|
534
|
+
const result = validateToonStringSync(`
|
|
245
535
|
users[2]{id,name}:
|
|
246
536
|
1,"Alice"
|
|
247
537
|
2,"Bob"
|
|
@@ -254,6 +544,14 @@ if (result.isValid) {
|
|
|
254
544
|
}
|
|
255
545
|
```
|
|
256
546
|
|
|
547
|
+
#### `validateToonString(toonString)`
|
|
548
|
+
Validates a TOON string for syntax and structural correctness (asynchronous).
|
|
549
|
+
|
|
550
|
+
**Parameters:**
|
|
551
|
+
- `toonString` (string): TOON string to validate
|
|
552
|
+
|
|
553
|
+
**Returns:** `Promise<{isValid: boolean, error: string|null}>`
|
|
554
|
+
|
|
257
555
|
---
|
|
258
556
|
|
|
259
557
|
## 🎨 TOON Format Guide
|
|
@@ -311,16 +609,16 @@ company:
|
|
|
311
609
|
|
|
312
610
|
## 💡 Use Cases
|
|
313
611
|
|
|
314
|
-
### 1. LLM API Optimization
|
|
612
|
+
### 1. LLM API Optimization (Synchronous)
|
|
315
613
|
```javascript
|
|
316
|
-
import {
|
|
614
|
+
import { jsonToToonSync } from 'toon-formatter';
|
|
317
615
|
|
|
318
616
|
// Before: Sending JSON to LLM
|
|
319
617
|
const jsonPrompt = JSON.stringify(largeDataset);
|
|
320
618
|
// 1000+ tokens
|
|
321
619
|
|
|
322
620
|
// After: Sending TOON to LLM
|
|
323
|
-
const toonPrompt =
|
|
621
|
+
const toonPrompt = jsonToToonSync(largeDataset);
|
|
324
622
|
// 600 tokens (40% reduction!)
|
|
325
623
|
|
|
326
624
|
const response = await openai.chat.completions.create({
|
|
@@ -328,9 +626,24 @@ const response = await openai.chat.completions.create({
|
|
|
328
626
|
});
|
|
329
627
|
```
|
|
330
628
|
|
|
331
|
-
### 2.
|
|
629
|
+
### 2. LLM API Optimization (Asynchronous)
|
|
630
|
+
```javascript
|
|
631
|
+
import { jsonToToon, toonToJson } from 'toon-formatter';
|
|
632
|
+
|
|
633
|
+
// Convert to TOON before sending
|
|
634
|
+
const toonPrompt = await jsonToToon(largeDataset);
|
|
635
|
+
|
|
636
|
+
const response = await openai.chat.completions.create({
|
|
637
|
+
messages: [{ role: "user", content: toonPrompt }]
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
// Parse TOON response back to JSON
|
|
641
|
+
const result = await toonToJson(response.choices[0].message.content);
|
|
642
|
+
```
|
|
643
|
+
|
|
644
|
+
### 3. Data Pipeline Integration
|
|
332
645
|
```javascript
|
|
333
|
-
import { csvToToonSync,
|
|
646
|
+
import { csvToToonSync, toonToJsonSync } from 'toon-formatter';
|
|
334
647
|
|
|
335
648
|
// Read CSV, convert to TOON, process, convert back
|
|
336
649
|
const csvData = fs.readFileSync('data.csv', 'utf-8');
|
|
@@ -340,18 +653,49 @@ const toonData = csvToToonSync(csvData);
|
|
|
340
653
|
const processedToon = await processWithLLM(toonData);
|
|
341
654
|
|
|
342
655
|
// Convert back to JSON for your app
|
|
343
|
-
const jsonResult =
|
|
656
|
+
const jsonResult = toonToJsonSync(processedToon);
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
### 4. Mixed Text Processing
|
|
660
|
+
```javascript
|
|
661
|
+
import { jsonToToonSync, xmlToToon } from 'toon-formatter';
|
|
662
|
+
|
|
663
|
+
// Extract and convert JSON from API documentation
|
|
664
|
+
const apiDocs = `
|
|
665
|
+
The user endpoint returns:
|
|
666
|
+
{"id": 123, "name": "Alice", "email": "alice@example.com"}
|
|
667
|
+
|
|
668
|
+
The product endpoint returns:
|
|
669
|
+
{"id": 456, "title": "Widget", "price": 29.99}
|
|
670
|
+
`;
|
|
671
|
+
|
|
672
|
+
const convertedDocs = jsonToToonSync(apiDocs);
|
|
673
|
+
// Both JSON objects are converted to TOON while preserving the text
|
|
674
|
+
|
|
675
|
+
// Extract and convert XML from mixed content
|
|
676
|
+
const xmlContent = `
|
|
677
|
+
Server response:
|
|
678
|
+
<response><status>success</status><data>processed</data></response>
|
|
679
|
+
`;
|
|
680
|
+
|
|
681
|
+
const result = await xmlToToon(xmlContent);
|
|
682
|
+
// XML is converted to TOON format
|
|
344
683
|
```
|
|
345
684
|
|
|
346
|
-
###
|
|
685
|
+
### 5. Configuration Files
|
|
347
686
|
```javascript
|
|
348
|
-
import {
|
|
687
|
+
import { yamlToToonSync, toonToYamlSync } from 'toon-formatter';
|
|
349
688
|
|
|
350
689
|
// Convert YAML config to TOON for LLM analysis
|
|
351
690
|
const yamlConfig = fs.readFileSync('config.yaml', 'utf-8');
|
|
352
|
-
const toonConfig =
|
|
691
|
+
const toonConfig = yamlToToonSync(yamlConfig);
|
|
353
692
|
|
|
354
693
|
// LLM can analyze and suggest improvements...
|
|
694
|
+
const improvedToon = await analyzeWithLLM(toonConfig);
|
|
695
|
+
|
|
696
|
+
// Convert back to YAML
|
|
697
|
+
const improvedYaml = toonToYamlSync(improvedToon);
|
|
698
|
+
fs.writeFileSync('config.yaml', improvedYaml);
|
|
355
699
|
```
|
|
356
700
|
|
|
357
701
|
---
|
package/package.json
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "toon-formatter",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"funding": {
|
|
5
|
+
"type": "github",
|
|
6
|
+
"url": "https://github.com/sponsors/ankitpal181"
|
|
7
|
+
},
|
|
4
8
|
"description": "A lightweight library to convert between TOON (Token-Oriented Object Notation) and popular data formats (JSON, YAML, XML, CSV). Reduce LLM token costs by up to 40%.",
|
|
5
9
|
"main": "src/index.js",
|
|
6
10
|
"type": "module",
|