toon-formatter 2.0.0 → 2.1.1
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/CHANGELOG.md +39 -1
- package/README.md +152 -1
- package/package.json +14 -7
- package/src/csv.js +19 -50
- package/src/csv_formatter/index.js +496 -0
- package/src/csv_formatter/validator.js +36 -0
- package/src/index.js +9 -1
- package/src/json.js +117 -67
- package/src/json_formatter/csv.js +145 -0
- package/src/json_formatter/index.js +525 -0
- package/src/json_formatter/validator.js +29 -0
- package/src/json_formatter/xml.js +206 -0
- package/src/json_formatter/yaml.js +81 -0
- package/src/utils.js +262 -64
- package/src/xml.js +24 -79
- package/src/xml_formatter/csv.js +122 -0
- package/src/xml_formatter/index.js +488 -0
- package/src/xml_formatter/validator.js +53 -0
- package/src/yaml_formatter/csv.js +101 -0
- package/src/yaml_formatter/index.js +542 -0
- package/src/yaml_formatter/validator.js +31 -0
- package/src/yaml_formatter/xml.js +116 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,29 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [2.1.1] - 2025-12-18
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- **🔄 Unified Format Converters**
|
|
12
|
+
- New classes: `JsonConverter`, `YamlConverter`, `XmlConverter`, and `CsvConverter`
|
|
13
|
+
- Direct translation between formats (e.g., XML ↔ JSON, CSV ↔ YAML)
|
|
14
|
+
- Full encryption middleware support for all unified converters
|
|
15
|
+
- Static and Instance API parity across all converters
|
|
16
|
+
- **📝 Enhanced Mixed-Text Support**
|
|
17
|
+
- Standardized mixed-text extraction for JSON, XML, and CSV
|
|
18
|
+
- Preserves surrounding text while converting embedded data blocks
|
|
19
|
+
- **📦 Improved Packaging**
|
|
20
|
+
- Specific exports for all converters in `package.json`
|
|
21
|
+
- Fixed Node.js XML support by moving `xmldom` to production dependencies
|
|
22
|
+
- **📚 Documentation Hub**
|
|
23
|
+
- Comprehensive README updates with Unified Converter examples
|
|
24
|
+
- New `toon-formatter.json` documentation for the web platform
|
|
25
|
+
|
|
26
|
+
### Changed
|
|
27
|
+
- Standardized `returnJson` default to `false` (returns objects/arrays) for consistency
|
|
28
|
+
- Refined internal conversion logic to better handle edge cases in mixed-text scenarios
|
|
29
|
+
- Improved package keywords and description for better SEO
|
|
30
|
+
|
|
8
31
|
## [2.0.0] - 2025-12-08
|
|
9
32
|
|
|
10
33
|
### Added
|
|
@@ -65,6 +88,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
65
88
|
|
|
66
89
|
## Migration Guide
|
|
67
90
|
|
|
91
|
+
### From v2.0 to v2.1
|
|
92
|
+
|
|
93
|
+
**To use Unified Converters:**
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
import { JsonConverter, XmlConverter } from 'toon-formatter';
|
|
97
|
+
|
|
98
|
+
// Direct format-to-format
|
|
99
|
+
const xml = JsonConverter.toXml({ name: 'Alice' });
|
|
100
|
+
|
|
101
|
+
// With encryption
|
|
102
|
+
const converter = new JsonConverter(encryptor);
|
|
103
|
+
const encryptedXml = converter.toXml({ name: 'Alice' }, { conversionMode: 'export' });
|
|
104
|
+
```
|
|
105
|
+
|
|
68
106
|
### From v1.x to v2.0
|
|
69
107
|
|
|
70
108
|
**No changes required!** Version 2.0 is fully backward compatible.
|
|
@@ -73,7 +111,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
73
111
|
|
|
74
112
|
```javascript
|
|
75
113
|
// Old code (still works)
|
|
76
|
-
import
|
|
114
|
+
import ToonConverter from 'toon-formatter';
|
|
77
115
|
const toon = ToonConverter.fromJson(data);
|
|
78
116
|
|
|
79
117
|
// New code (with encryption)
|
package/README.md
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
# 🚀 TOON Converter
|
|
2
2
|
|
|
3
|
-
A lightweight
|
|
3
|
+
A lightweight library to convert between **TOON** (Token-Oriented Object Notation) and popular data formats (JSON, YAML, XML, CSV).
|
|
4
4
|
|
|
5
5
|
**Reduce your LLM token costs by up to 40%** using the TOON format!
|
|
6
6
|
|
|
7
|
+
- **Documentation**: https://toonformatter.net/docs.html?package=toon-formatter
|
|
8
|
+
- **Source Code**: https://github.com/ankitpal181/toon-formatter-lib
|
|
9
|
+
- **Bug Reports**: https://github.com/ankitpal181/toon-formatter-lib/issues
|
|
10
|
+
- **POC Tool**: https://toonformatter.net/
|
|
11
|
+
|
|
7
12
|
\* *Only external dependencies: `js-yaml` and `papaparse` for YAML and CSV parsing*
|
|
8
13
|
|
|
9
14
|
---
|
|
@@ -1081,6 +1086,152 @@ ToonConverter.toJsonAsync(toonString, returnJson?)
|
|
|
1081
1086
|
|
|
1082
1087
|
---
|
|
1083
1088
|
|
|
1089
|
+
### Unified Format Converters (v2.0.0+)
|
|
1090
|
+
|
|
1091
|
+
The library now includes specialized, unified converters for each major format. These are perfect when you need to convert between non-TOON formats (like XML to JSON or CSV to YAML) while still having access to TOON and encryption features.
|
|
1092
|
+
|
|
1093
|
+
#### Available Unified Converters:
|
|
1094
|
+
- `JsonConverter`: Specialized in JSON input/output
|
|
1095
|
+
- `YamlConverter`: Specialized in YAML input/output
|
|
1096
|
+
- `XmlConverter`: Specialized in XML input/output
|
|
1097
|
+
- `CsvConverter`: Specialized in CSV input/output
|
|
1098
|
+
|
|
1099
|
+
#### Example: Cross-Format Conversion
|
|
1100
|
+
```javascript
|
|
1101
|
+
import { XmlConverter, YamlConverter } from 'toon-formatter';
|
|
1102
|
+
|
|
1103
|
+
// Convert XML directly to YAML
|
|
1104
|
+
const xmlData = '<user><name>Alice</name></user>';
|
|
1105
|
+
const yamlData = XmlConverter.toYaml(xmlData);
|
|
1106
|
+
|
|
1107
|
+
// Convert YAML directly to CSV
|
|
1108
|
+
const csvData = YamlConverter.toCsv("name: Alice\nrole: admin");
|
|
1109
|
+
```
|
|
1110
|
+
|
|
1111
|
+
---
|
|
1112
|
+
|
|
1113
|
+
### JsonConverter Class
|
|
1114
|
+
|
|
1115
|
+
Specialized for JSON-centric workflows. It can convert JSON to any format and any format back to JSON.
|
|
1116
|
+
|
|
1117
|
+
#### Instance Methods (with Encryption)
|
|
1118
|
+
```javascript
|
|
1119
|
+
import { JsonConverter, Encryptor } from 'toon-formatter';
|
|
1120
|
+
|
|
1121
|
+
const converter = new JsonConverter(new Encryptor(key, 'aes-256-gcm'));
|
|
1122
|
+
|
|
1123
|
+
// JSON -> TOON (Encrypted)
|
|
1124
|
+
const encryptedToon = converter.toToon(data, { conversionMode: 'export' });
|
|
1125
|
+
|
|
1126
|
+
// XML -> JSON (Decrypted)
|
|
1127
|
+
const jsonData = converter.fromXml(encryptedXml, { conversionMode: 'ingestion' });
|
|
1128
|
+
```
|
|
1129
|
+
|
|
1130
|
+
#### Static Methods (No Encryption)
|
|
1131
|
+
```javascript
|
|
1132
|
+
// Convert TOON to JSON object
|
|
1133
|
+
const obj = JsonConverter.fromToon(toonString);
|
|
1134
|
+
|
|
1135
|
+
// Convert TOON to JSON string
|
|
1136
|
+
const json = JsonConverter.fromToon(toonString, { returnJson: true });
|
|
1137
|
+
|
|
1138
|
+
// Convert JSON to XML
|
|
1139
|
+
const xml = JsonConverter.toXml({ name: "Alice" });
|
|
1140
|
+
```
|
|
1141
|
+
|
|
1142
|
+
**Available Methods:**
|
|
1143
|
+
- `fromToon(toonString, options?)` / `fromToonAsync(toonString, options?)`
|
|
1144
|
+
- `toToon(data, options?)` / `toToonAsync(data, options?)`
|
|
1145
|
+
- `fromYaml(yamlString, options?)` / `fromYamlAsync(yamlString, options?)`
|
|
1146
|
+
- `toYaml(data, options?)` / `toYamlAsync(data, options?)`
|
|
1147
|
+
- `fromXml(xmlString, options?)` / `fromXmlAsync(xmlString, options?)`
|
|
1148
|
+
- `toXml(data, options?)` / `toXmlAsync(data, options?)`
|
|
1149
|
+
- `fromCsv(csvString, options?)` / `fromCsvAsync(csvString, options?)`
|
|
1150
|
+
- `toCsv(data, options?)` / `toCsvAsync(data, options?)`
|
|
1151
|
+
|
|
1152
|
+
---
|
|
1153
|
+
|
|
1154
|
+
### YamlConverter Class
|
|
1155
|
+
|
|
1156
|
+
Specialized for YAML workflows.
|
|
1157
|
+
|
|
1158
|
+
#### Usage Example
|
|
1159
|
+
```javascript
|
|
1160
|
+
import { YamlConverter } from 'toon-formatter';
|
|
1161
|
+
|
|
1162
|
+
// YAML to TOON
|
|
1163
|
+
const toon = YamlConverter.fromToon(testToon);
|
|
1164
|
+
|
|
1165
|
+
// YAML to JSON
|
|
1166
|
+
const json = YamlConverter.toJson(yamlString, { returnJson: true });
|
|
1167
|
+
```
|
|
1168
|
+
|
|
1169
|
+
**Available Methods:**
|
|
1170
|
+
- `fromToon(toonString, options?)` / `fromToonAsync(toonString, options?)`
|
|
1171
|
+
- `toToon(yamlString, options?)` / `toToonAsync(yamlString, options?)`
|
|
1172
|
+
- `fromJson(jsonData, options?)` / `fromJsonAsync(jsonData, options?)`
|
|
1173
|
+
- `toJson(yamlString, options?)` / `toJsonAsync(yamlString, options?)`
|
|
1174
|
+
- `fromXml(xmlString, options?)` / `fromXmlAsync(xmlString, options?)`
|
|
1175
|
+
- `toXml(yamlString, options?)` / `toXmlAsync(yamlString, options?)`
|
|
1176
|
+
- `fromCsv(csvString, options?)` / `fromCsvAsync(csvString, options?)`
|
|
1177
|
+
- `toCsv(yamlString, options?)` / `toCsvAsync(yamlString, options?)`
|
|
1178
|
+
|
|
1179
|
+
---
|
|
1180
|
+
|
|
1181
|
+
### XmlConverter Class
|
|
1182
|
+
|
|
1183
|
+
Specialized for XML workflows. Supports mixed-text extraction automatically.
|
|
1184
|
+
|
|
1185
|
+
#### Usage Example
|
|
1186
|
+
```javascript
|
|
1187
|
+
import { XmlConverter } from 'toon-formatter';
|
|
1188
|
+
|
|
1189
|
+
// XML to TOON
|
|
1190
|
+
const toon = XmlConverter.fromToon(xmlString);
|
|
1191
|
+
|
|
1192
|
+
// JSON to XML
|
|
1193
|
+
const xml = XmlConverter.fromJson(jsonData);
|
|
1194
|
+
```
|
|
1195
|
+
|
|
1196
|
+
**Available Methods:**
|
|
1197
|
+
- `fromToon(toonString, options?)` / `fromToonAsync(toonString, options?)`
|
|
1198
|
+
- `toToon(xmlString, options?)` / `toToonAsync(xmlString, options?)`
|
|
1199
|
+
- `fromJson(jsonData, options?)` / `fromJsonAsync(jsonData, options?)`
|
|
1200
|
+
- `toJson(xmlString, options?)` / `toJsonAsync(xmlString, options?)`
|
|
1201
|
+
- `fromYaml(yamlString, options?)` / `fromYamlAsync(yamlString, options?)`
|
|
1202
|
+
- `toYaml(xmlString, options?)` / `toYamlAsync(xmlString, options?)`
|
|
1203
|
+
- `fromCsv(csvString, options?)` / `fromCsvAsync(csvString, options?)`
|
|
1204
|
+
- `toCsv(xmlString, options?)` / `toCsvAsync(xmlString, options?)`
|
|
1205
|
+
|
|
1206
|
+
---
|
|
1207
|
+
|
|
1208
|
+
### CsvConverter Class
|
|
1209
|
+
|
|
1210
|
+
Specialized for CSV workflows.
|
|
1211
|
+
|
|
1212
|
+
#### Usage Example
|
|
1213
|
+
```javascript
|
|
1214
|
+
import { CsvConverter } from 'toon-formatter';
|
|
1215
|
+
|
|
1216
|
+
// CSV to TOON
|
|
1217
|
+
const toon = CsvConverter.fromToon(csvString);
|
|
1218
|
+
|
|
1219
|
+
// JSON to CSV
|
|
1220
|
+
const csv = CsvConverter.fromJson(jsonData);
|
|
1221
|
+
```
|
|
1222
|
+
|
|
1223
|
+
**Available Methods:**
|
|
1224
|
+
- `fromToon(toonString, options?)` / `fromToonAsync(toonString, options?)`
|
|
1225
|
+
- `toToon(csvString, options?)` / `toToonAsync(csvString, options?)`
|
|
1226
|
+
- `fromJson(jsonData, options?)` / `fromJsonAsync(jsonData, options?)`
|
|
1227
|
+
- `toJson(csvString, options?)` / `toJsonAsync(csvString, options?)`
|
|
1228
|
+
- `fromYaml(yamlString, options?)` / `fromYamlAsync(yamlString, options?)`
|
|
1229
|
+
- `toYaml(csvString, options?)` / `toYamlAsync(csvString, options?)`
|
|
1230
|
+
- `fromXml(xmlString, options?)` / `fromXmlAsync(xmlString, options?)`
|
|
1231
|
+
- `toXml(csvString, options?)` / `toXmlAsync(csvString, options?)`
|
|
1232
|
+
|
|
1233
|
+
---
|
|
1234
|
+
|
|
1084
1235
|
## 🎨 TOON Format Guide
|
|
1085
1236
|
|
|
1086
1237
|
### Primitives
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "toon-formatter",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"funding": {
|
|
5
5
|
"type": "github",
|
|
6
6
|
"url": "https://github.com/sponsors/ankitpal181"
|
|
7
7
|
},
|
|
8
|
-
"description": "A
|
|
8
|
+
"description": "A powerful library to convert between TOON, JSON, YAML, XML, and CSV with end-to-end encryption. Includes Unified Converters for direct format-to-format translation and LLM token optimization.",
|
|
9
9
|
"main": "src/index.js",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"scripts": {
|
|
@@ -30,7 +30,11 @@
|
|
|
30
30
|
"security",
|
|
31
31
|
"crypto",
|
|
32
32
|
"secure-data",
|
|
33
|
-
"end-to-end-encryption"
|
|
33
|
+
"end-to-end-encryption",
|
|
34
|
+
"unified-converter",
|
|
35
|
+
"json-to-xml",
|
|
36
|
+
"csv-to-yaml",
|
|
37
|
+
"xml-to-json"
|
|
34
38
|
],
|
|
35
39
|
"author": "Ankit Pal",
|
|
36
40
|
"license": "MIT",
|
|
@@ -47,11 +51,10 @@
|
|
|
47
51
|
},
|
|
48
52
|
"dependencies": {
|
|
49
53
|
"js-yaml": "^4.1.0",
|
|
50
|
-
"papaparse": "^5.4.1"
|
|
51
|
-
},
|
|
52
|
-
"devDependencies": {
|
|
54
|
+
"papaparse": "^5.4.1",
|
|
53
55
|
"xmldom": "^0.6.0"
|
|
54
56
|
},
|
|
57
|
+
"devDependencies": {},
|
|
55
58
|
"exports": {
|
|
56
59
|
".": "./src/index.js",
|
|
57
60
|
"./json": "./src/json.js",
|
|
@@ -60,6 +63,10 @@
|
|
|
60
63
|
"./csv": "./src/csv.js",
|
|
61
64
|
"./validator": "./src/validator.js",
|
|
62
65
|
"./utils": "./src/utils.js",
|
|
63
|
-
"./encryptor": "./src/encryptor.js"
|
|
66
|
+
"./encryptor": "./src/encryptor.js",
|
|
67
|
+
"./json-converter": "./src/json_formatter/index.js",
|
|
68
|
+
"./yaml-converter": "./src/yaml_formatter/index.js",
|
|
69
|
+
"./xml-converter": "./src/xml_formatter/index.js",
|
|
70
|
+
"./csv-converter": "./src/csv_formatter/index.js"
|
|
64
71
|
}
|
|
65
72
|
}
|
package/src/csv.js
CHANGED
|
@@ -4,38 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import Papa from 'papaparse';
|
|
6
6
|
import { jsonToToonSync, toonToJsonSync } from './json.js';
|
|
7
|
-
import { extractCsvFromString } from './utils.js';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Internal core function to convert pure CSV string to TOON (Async)
|
|
11
|
-
* @param {string} csvString
|
|
12
|
-
* @returns {Promise<string>}
|
|
13
|
-
*/
|
|
14
|
-
function parseCsvToToon(csvString) {
|
|
15
|
-
return new Promise((resolve, reject) => {
|
|
16
|
-
Papa.parse(csvString, {
|
|
17
|
-
header: true,
|
|
18
|
-
dynamicTyping: true,
|
|
19
|
-
complete: function (results) {
|
|
20
|
-
try {
|
|
21
|
-
const jsonObject = results.data;
|
|
22
|
-
|
|
23
|
-
if (typeof jsonObject !== "object" || jsonObject === null) {
|
|
24
|
-
throw new Error("CSV parsing failed — cannot convert.");
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const toonString = jsonToToonSync(jsonObject);
|
|
28
|
-
resolve(toonString);
|
|
29
|
-
} catch (error) {
|
|
30
|
-
reject(error);
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
error: function (error) {
|
|
34
|
-
reject(new Error(`CSV parsing error: ${error.message}`));
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
}
|
|
7
|
+
import { extractCsvFromString, flattenObject } from './utils.js';
|
|
39
8
|
|
|
40
9
|
/**
|
|
41
10
|
* Internal core function to convert pure CSV string to TOON (Sync)
|
|
@@ -46,6 +15,7 @@ function parseCsvToToonSync(csvString) {
|
|
|
46
15
|
const results = Papa.parse(csvString, {
|
|
47
16
|
header: true,
|
|
48
17
|
dynamicTyping: true,
|
|
18
|
+
skipEmptyLines: true,
|
|
49
19
|
});
|
|
50
20
|
|
|
51
21
|
if (results.errors && results.errors.length > 0) {
|
|
@@ -62,20 +32,9 @@ function parseCsvToToonSync(csvString) {
|
|
|
62
32
|
}
|
|
63
33
|
|
|
64
34
|
/**
|
|
65
|
-
* Converts CSV (or mixed text with CSV) to TOON format (
|
|
66
|
-
* @param {string} csvString - CSV formatted string or mixed text
|
|
67
|
-
* @returns {Promise<string>} TOON formatted string
|
|
68
|
-
* @throws {Error} If CSV is invalid
|
|
69
|
-
*/
|
|
70
|
-
export async function csvToToon(csvString) {
|
|
71
|
-
return csvToToonSync(csvString);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Converts CSV (or mixed text with CSV) to TOON format (synchronous version)
|
|
35
|
+
* Converts CSV (or mixed text with CSV) to TOON format (Sync)
|
|
76
36
|
* @param {string} csvString - CSV formatted string or mixed text
|
|
77
37
|
* @returns {string} TOON formatted string
|
|
78
|
-
* @throws {Error} If CSV is invalid
|
|
79
38
|
*/
|
|
80
39
|
export function csvToToonSync(csvString) {
|
|
81
40
|
if (!csvString || typeof csvString !== 'string') {
|
|
@@ -104,10 +63,18 @@ export function csvToToonSync(csvString) {
|
|
|
104
63
|
}
|
|
105
64
|
|
|
106
65
|
/**
|
|
107
|
-
* Converts
|
|
66
|
+
* Converts CSV (or mixed text with CSV) to TOON format (Async)
|
|
67
|
+
* @param {string} csvString - CSV formatted string or mixed text
|
|
68
|
+
* @returns {Promise<string>} TOON formatted string
|
|
69
|
+
*/
|
|
70
|
+
export async function csvToToon(csvString) {
|
|
71
|
+
return csvToToonSync(csvString);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Converts TOON to CSV format (Sync)
|
|
108
76
|
* @param {string} toonString - TOON formatted string
|
|
109
77
|
* @returns {string} CSV formatted string
|
|
110
|
-
* @throws {Error} If TOON is invalid
|
|
111
78
|
*/
|
|
112
79
|
export function toonToCsvSync(toonString) {
|
|
113
80
|
if (!toonString || typeof toonString !== 'string') {
|
|
@@ -116,12 +83,14 @@ export function toonToCsvSync(toonString) {
|
|
|
116
83
|
|
|
117
84
|
const jsonObject = toonToJsonSync(toonString);
|
|
118
85
|
|
|
119
|
-
|
|
86
|
+
// Flatten the object for CSV
|
|
87
|
+
const dataToUnparse = Array.isArray(jsonObject)
|
|
88
|
+
? jsonObject.map(row => flattenObject(row))
|
|
89
|
+
: [flattenObject(jsonObject)];
|
|
90
|
+
|
|
91
|
+
return Papa.unparse(dataToUnparse, {
|
|
120
92
|
header: true,
|
|
121
|
-
dynamicTyping: true,
|
|
122
93
|
});
|
|
123
|
-
|
|
124
|
-
return csvString;
|
|
125
94
|
}
|
|
126
95
|
|
|
127
96
|
/**
|