toon-formatter 1.0.2 → 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/ENHANCEMENTS.md +124 -0
- package/README.md +398 -54
- package/package.json +1 -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/ENHANCEMENTS.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# TOON Converter Library - Enhancement Summary
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
Updated the `toon-converter-lib` package with improved validators, mixed text extraction capabilities, validation checks on reverse conversions, and loop-based conversion for embedded formats.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## ✨ New Features Added
|
|
9
|
+
|
|
10
|
+
### 1. **Enhanced Validator** (`src/validator.js`)
|
|
11
|
+
- **Empty Block Detection**: Validates that arrays with size > 0 have actual items
|
|
12
|
+
- **Tabular Array Support**: Properly handles tabular arrays with `{field1,field2}` syntax
|
|
13
|
+
- **List Item Context Checking**: Ensures `-` items only appear in array contexts
|
|
14
|
+
- **Better Error Messages**: Line numbers and specific error descriptions
|
|
15
|
+
|
|
16
|
+
**Example Error**:
|
|
17
|
+
```
|
|
18
|
+
L5: Array declared with size 3 but has no items (expected indented block).
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
### 2. **Format Extraction Functions** (`src/utils.js`)
|
|
24
|
+
Added utilities to extract specific formats from mixed text:
|
|
25
|
+
|
|
26
|
+
- `extractJsonFromString(text)` - Finds and extracts JSON objects/arrays
|
|
27
|
+
- `extractYamlFromString(text)` - Extracts YAML blocks
|
|
28
|
+
- `extractXmlFromString(text)` - Extracts XML elements with balance checking
|
|
29
|
+
- `extractCsvFromString(text)` - Identifies CSV data
|
|
30
|
+
|
|
31
|
+
**Use Case**: Allows conversion of mixed content (e.g., JSON embedded in markdown).
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
### 3. **Integrated Mixed Text Conversion**
|
|
36
|
+
The conversion functions now automatically detect and handle mixed text input using an extraction loop. This logic covers both embedded data and full data strings.
|
|
37
|
+
|
|
38
|
+
**Synchronous API (Main Logic)**:
|
|
39
|
+
- `jsonToToonSync(input)`
|
|
40
|
+
- `xmlToToonSync(input)`
|
|
41
|
+
- `csvToToonSync(input)`
|
|
42
|
+
- `yamlToToonSync(input)`
|
|
43
|
+
|
|
44
|
+
**Asynchronous API (Wrappers)**:
|
|
45
|
+
- `jsonToToon(input)`
|
|
46
|
+
- `xmlToToon(input)`
|
|
47
|
+
- `csvToToon(input)`
|
|
48
|
+
- `yamlToToon(input)`
|
|
49
|
+
|
|
50
|
+
**Behavior**:
|
|
51
|
+
1. Checks if input is a string.
|
|
52
|
+
2. Uses extraction logic (`extract*FromString`) in a loop to find and convert all data blocks.
|
|
53
|
+
3. If no blocks are found (and input is not valid data), it returns the original text (or throws if strict parsing fails inside the loop).
|
|
54
|
+
|
|
55
|
+
**Note**: `*TextToToon` functions have been removed in favor of this integrated approach.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
### 4. **Validation on Reverse Conversions**
|
|
62
|
+
All `toonTo*` functions now validate TOON input before conversion:
|
|
63
|
+
|
|
64
|
+
- `toonToJson()` - Validates before parsing
|
|
65
|
+
- `toonToYaml()` - Validates before conversion
|
|
66
|
+
- `toonToXml()` - Validates before conversion
|
|
67
|
+
- `toonToCsv()` - Validates before conversion
|
|
68
|
+
|
|
69
|
+
**Benefits**:
|
|
70
|
+
- Early error detection
|
|
71
|
+
- Clear error messages
|
|
72
|
+
- Prevents invalid conversions
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
### 6. **API Consistency (Sync & Async)**
|
|
77
|
+
All converters now support both Synchronous and Asynchronous operations for consistency.
|
|
78
|
+
|
|
79
|
+
| Format | Sync Method | Async Method |
|
|
80
|
+
|--------|-------------|--------------|
|
|
81
|
+
| **JSON** | `jsonToToon` | `jsonToToonAsync` |
|
|
82
|
+
| **YAML** | `yamlToToon` | `yamlToToonAsync` |
|
|
83
|
+
| **XML** | `xmlToToonSync`* | `xmlToToon` |
|
|
84
|
+
| **CSV** | `csvToToonSync` | `csvToToon` |
|
|
85
|
+
|
|
86
|
+
*\*Note: `xmlToToonSync` in Node.js requires a global `DOMParser` polyfill (e.g., via `jsdom`).*
|
|
87
|
+
|
|
88
|
+
### 7. **Unified Class API** (`ToonConverter`)
|
|
89
|
+
The `ToonConverter` class now exposes all variations:
|
|
90
|
+
|
|
91
|
+
```javascript
|
|
92
|
+
// Async
|
|
93
|
+
await ToonConverter.fromJsonAsync(data);
|
|
94
|
+
await ToonConverter.fromXmlAsync(xml);
|
|
95
|
+
await ToonConverter.fromCsvAsync(csv);
|
|
96
|
+
|
|
97
|
+
// Sync
|
|
98
|
+
ToonConverter.fromJson(data);
|
|
99
|
+
ToonConverter.fromXmlSync(xml);
|
|
100
|
+
ToonConverter.fromCsvSync(csv);
|
|
101
|
+
|
|
102
|
+
// Mixed Text
|
|
103
|
+
ToonConverter.fromJsonText(text);
|
|
104
|
+
await ToonConverter.fromXmlText(text);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## 🚀 Benefits
|
|
110
|
+
|
|
111
|
+
- **Robustness**: Validation prevents invalid conversions
|
|
112
|
+
- **Flexibility**: Handle mixed content seamlessly
|
|
113
|
+
- **Consistency**: Unified API for all formats
|
|
114
|
+
- **Developer Experience**: Clear errors, easy debugging
|
|
115
|
+
- **Reliability**: Comprehensive validation at every step
|
|
116
|
+
- **Reusability**: Extraction functions available for custom use
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
**Note**: History feature was intentionally NOT added to the package as requested.
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
Made with ❤️ for the TOON Formatter project
|