toon-formatter 2.0.1 → 2.2.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/CHANGELOG.md +39 -1
- package/README.md +211 -0
- package/bin/toon-formatter.js +3 -0
- package/package.json +17 -7
- package/src/cli.js +271 -0
- 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
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSV <-> YAML Converter (for YamlConverter)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import yaml from 'js-yaml';
|
|
6
|
+
import { extractCsvFromString } from '../utils.js';
|
|
7
|
+
import { csvToJsonSync, jsonToCsvSync } from '../json_formatter/csv.js';
|
|
8
|
+
import { jsonToYamlSync, yamlToJsonSync } from '../json_formatter/yaml.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Convert CSV string to YAML string (Sync)
|
|
12
|
+
* Supports mixed text CSV.
|
|
13
|
+
* @param {string} csvString
|
|
14
|
+
* @returns {string} YAML string
|
|
15
|
+
*/
|
|
16
|
+
export function csvToYamlSync(csvString) {
|
|
17
|
+
if (!csvString || typeof csvString !== 'string') {
|
|
18
|
+
throw new Error('Input must be a non-empty string');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let convertedText = csvString;
|
|
22
|
+
let iterationCount = 0;
|
|
23
|
+
const maxIterations = 100;
|
|
24
|
+
let wasModified = false;
|
|
25
|
+
|
|
26
|
+
// Check pure CSV
|
|
27
|
+
const firstExtract = extractCsvFromString(csvString);
|
|
28
|
+
if (firstExtract === csvString.trim()) {
|
|
29
|
+
try {
|
|
30
|
+
const json = csvToJsonSync(csvString);
|
|
31
|
+
return yaml.dump(json);
|
|
32
|
+
} catch (e) {
|
|
33
|
+
return csvString;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Mixed Loop
|
|
38
|
+
while (iterationCount < maxIterations) {
|
|
39
|
+
const csvBlock = extractCsvFromString(convertedText);
|
|
40
|
+
if (!csvBlock) break;
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const jsonObject = csvToJsonSync(csvBlock);
|
|
44
|
+
const yamlOutput = yaml.dump(jsonObject).trim();
|
|
45
|
+
convertedText = convertedText.replace(csvBlock, yamlOutput);
|
|
46
|
+
wasModified = true;
|
|
47
|
+
iterationCount++;
|
|
48
|
+
} catch (e) {
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (wasModified) return convertedText;
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const json = csvToJsonSync(csvString);
|
|
57
|
+
return yaml.dump(json);
|
|
58
|
+
} catch (e) {
|
|
59
|
+
return csvString;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Convert CSV string to YAML string (Async)
|
|
65
|
+
* @param {string} csvString
|
|
66
|
+
* @returns {Promise<string>} YAML string
|
|
67
|
+
*/
|
|
68
|
+
export async function csvToYaml(csvString) {
|
|
69
|
+
return csvToYamlSync(csvString);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Convert YAML string to CSV string (Sync)
|
|
75
|
+
* Note: Does not support mixed text YAML extraction.
|
|
76
|
+
* @param {string} yamlString
|
|
77
|
+
* @returns {string} CSV string
|
|
78
|
+
*/
|
|
79
|
+
export function yamlToCsvSync(yamlString) {
|
|
80
|
+
if (!yamlString || typeof yamlString !== 'string') {
|
|
81
|
+
throw new Error('Input must be a non-empty string');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
// YAML -> JSON
|
|
86
|
+
const json = yamlToJsonSync(yamlString);
|
|
87
|
+
// JSON -> CSV
|
|
88
|
+
return jsonToCsvSync(json);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
throw new Error(`YAML to CSV conversion failed: ${e.message}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Convert YAML string to CSV string (Async)
|
|
96
|
+
* @param {string} yamlString
|
|
97
|
+
* @returns {Promise<string>} CSV string
|
|
98
|
+
*/
|
|
99
|
+
export async function yamlToCsv(yamlString) {
|
|
100
|
+
return yamlToCsvSync(yamlString);
|
|
101
|
+
}
|
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* YamlConverter Class
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { toonToYaml, toonToYamlSync, yamlToToon, yamlToToonSync } from '../yaml.js';
|
|
6
|
+
import { jsonToYaml, jsonToYamlSync, yamlToJson, yamlToJsonSync } from '../json_formatter/yaml.js';
|
|
7
|
+
import { xmlToYaml, xmlToYamlSync, yamlToXml, yamlToXmlSync } from './xml.js';
|
|
8
|
+
import { csvToYaml, csvToYamlSync, yamlToCsv, yamlToCsvSync } from './csv.js';
|
|
9
|
+
import { validateYamlString, validateYamlStringSync } from './validator.js';
|
|
10
|
+
import { extractJsonFromString } from '../utils.js';
|
|
11
|
+
|
|
12
|
+
export class YamlConverter {
|
|
13
|
+
/**
|
|
14
|
+
* Creates a YamlConverter instance
|
|
15
|
+
* @param {Object} [encryptor=null] - Optional Encryptor instance for encryption support
|
|
16
|
+
*/
|
|
17
|
+
constructor(encryptor = null) {
|
|
18
|
+
this.encryptor = encryptor;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// --- Helper Methods for Encryption ---
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Applies encryption logic based on conversion mode (synchronous)
|
|
25
|
+
* @private
|
|
26
|
+
* @param {Function} fn - The converter function to call
|
|
27
|
+
* @param {*} data - Data to convert
|
|
28
|
+
* @param {string} mode - Conversion mode: 'no_encryption', 'middleware', 'ingestion', 'export'
|
|
29
|
+
* @returns {*} Converted (and possibly encrypted) data
|
|
30
|
+
*/
|
|
31
|
+
_convertWithEncryption(fn, data, mode) {
|
|
32
|
+
if (!this.encryptor || mode === 'no_encryption') {
|
|
33
|
+
return fn(data);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
switch (mode) {
|
|
37
|
+
case 'middleware': // Decrypt -> Convert -> Encrypt
|
|
38
|
+
const decrypted = this.encryptor.decrypt(data);
|
|
39
|
+
const result = fn(decrypted);
|
|
40
|
+
return this.encryptor.encrypt(result);
|
|
41
|
+
case 'ingestion': // Decrypt -> Convert
|
|
42
|
+
const dec = this.encryptor.decrypt(data);
|
|
43
|
+
return fn(dec);
|
|
44
|
+
case 'export': // Convert -> Encrypt
|
|
45
|
+
const res = fn(data);
|
|
46
|
+
return this.encryptor.encrypt(res);
|
|
47
|
+
default:
|
|
48
|
+
return fn(data);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Applies encryption logic based on conversion mode (asynchronous)
|
|
54
|
+
* @private
|
|
55
|
+
* @param {Function} fn - The async converter function to call
|
|
56
|
+
* @param {*} data - Data to convert
|
|
57
|
+
* @param {string} mode - Conversion mode: 'no_encryption', 'middleware', 'ingestion', 'export'
|
|
58
|
+
* @returns {Promise<*>} Converted (and possibly encrypted) data
|
|
59
|
+
*/
|
|
60
|
+
async _convertWithEncryptionAsync(fn, data, mode) {
|
|
61
|
+
if (!this.encryptor || mode === 'no_encryption') {
|
|
62
|
+
return await fn(data);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
switch (mode) {
|
|
66
|
+
case 'middleware':
|
|
67
|
+
const decrypted = this.encryptor.decrypt(data);
|
|
68
|
+
const result = await fn(decrypted);
|
|
69
|
+
return this.encryptor.encrypt(result);
|
|
70
|
+
case 'ingestion':
|
|
71
|
+
const dec = this.encryptor.decrypt(data);
|
|
72
|
+
return await fn(dec);
|
|
73
|
+
case 'export':
|
|
74
|
+
const res = await fn(data);
|
|
75
|
+
return this.encryptor.encrypt(res);
|
|
76
|
+
default:
|
|
77
|
+
return await fn(data);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// --- TOON Conversions ---
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Convert TOON string to YAML (Sync)
|
|
85
|
+
* @param {string} toonString - TOON formatted string
|
|
86
|
+
* @param {Object} [options={}] - Conversion options
|
|
87
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
88
|
+
* @returns {string} YAML formatted string
|
|
89
|
+
*/
|
|
90
|
+
fromToon(toonString, options = {}) {
|
|
91
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
92
|
+
return this._convertWithEncryption(
|
|
93
|
+
(data) => toonToYamlSync(data),
|
|
94
|
+
toonString,
|
|
95
|
+
conversionMode
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Convert TOON string to YAML (Async)
|
|
101
|
+
* @param {string} toonString - TOON formatted string
|
|
102
|
+
* @param {Object} [options={}] - Conversion options
|
|
103
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
104
|
+
* @returns {Promise<string>} YAML formatted string
|
|
105
|
+
*/
|
|
106
|
+
async fromToonAsync(toonString, options = {}) {
|
|
107
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
108
|
+
return this._convertWithEncryptionAsync(
|
|
109
|
+
async (data) => toonToYaml(data),
|
|
110
|
+
toonString,
|
|
111
|
+
conversionMode
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Convert YAML to TOON string (Sync)
|
|
117
|
+
* @param {string} yamlString - YAML formatted string
|
|
118
|
+
* @param {Object} [options={}] - Conversion options
|
|
119
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
120
|
+
* @returns {string} TOON formatted string
|
|
121
|
+
*/
|
|
122
|
+
toToon(yamlString, options = {}) {
|
|
123
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
124
|
+
return this._convertWithEncryption(
|
|
125
|
+
(data) => yamlToToonSync(data),
|
|
126
|
+
yamlString,
|
|
127
|
+
conversionMode
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Convert YAML to TOON string (Async)
|
|
133
|
+
* @param {string} yamlString - YAML formatted string
|
|
134
|
+
* @param {Object} [options={}] - Conversion options
|
|
135
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
136
|
+
* @returns {Promise<string>} TOON formatted string
|
|
137
|
+
*/
|
|
138
|
+
async toToonAsync(yamlString, options = {}) {
|
|
139
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
140
|
+
return this._convertWithEncryptionAsync(
|
|
141
|
+
async (data) => yamlToToon(data),
|
|
142
|
+
yamlString,
|
|
143
|
+
conversionMode
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// --- JSON Conversions ---
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Convert JSON to YAML (Sync)
|
|
151
|
+
* @param {Object|string} jsonData - JSON data or string with embedded JSON
|
|
152
|
+
* @param {Object} [options={}] - Conversion options
|
|
153
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
154
|
+
* @returns {string} YAML formatted string
|
|
155
|
+
*/
|
|
156
|
+
fromJson(jsonData, options = {}) {
|
|
157
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
158
|
+
return this._convertWithEncryption(
|
|
159
|
+
(data) => jsonToYamlSync(data),
|
|
160
|
+
jsonData,
|
|
161
|
+
conversionMode
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Convert JSON to YAML (Async)
|
|
167
|
+
* @param {Object|string} jsonData - JSON data or string with embedded JSON
|
|
168
|
+
* @param {Object} [options={}] - Conversion options
|
|
169
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
170
|
+
* @returns {Promise<string>} YAML formatted string
|
|
171
|
+
*/
|
|
172
|
+
async fromJsonAsync(jsonData, options = {}) {
|
|
173
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
174
|
+
return this._convertWithEncryptionAsync(
|
|
175
|
+
async (data) => jsonToYaml(data),
|
|
176
|
+
jsonData,
|
|
177
|
+
conversionMode
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Convert YAML to JSON (Sync)
|
|
183
|
+
* @param {string} yamlString - YAML formatted string
|
|
184
|
+
* @param {Object} [options={}] - Conversion options
|
|
185
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
186
|
+
* @param {boolean} [options.returnJson=false] - If true, returns JSON string; if false, returns object
|
|
187
|
+
* @returns {Object|string} JSON result
|
|
188
|
+
*/
|
|
189
|
+
toJson(yamlString, options = {}) {
|
|
190
|
+
const { conversionMode = 'no_encryption', returnJson = false } = options;
|
|
191
|
+
return this._convertWithEncryption(
|
|
192
|
+
(data) => {
|
|
193
|
+
const res = yamlToJsonSync(data);
|
|
194
|
+
return returnJson ? JSON.stringify(res) : res;
|
|
195
|
+
},
|
|
196
|
+
yamlString,
|
|
197
|
+
conversionMode
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Convert YAML to JSON (Async)
|
|
203
|
+
* @param {string} yamlString - YAML formatted string
|
|
204
|
+
* @param {Object} [options={}] - Conversion options
|
|
205
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
206
|
+
* @param {boolean} [options.returnJson=false] - If true, returns JSON string; if false, returns object
|
|
207
|
+
* @returns {Promise<Object|string>} JSON result
|
|
208
|
+
*/
|
|
209
|
+
async toJsonAsync(yamlString, options = {}) {
|
|
210
|
+
const { conversionMode = 'no_encryption', returnJson = false } = options;
|
|
211
|
+
return this._convertWithEncryptionAsync(
|
|
212
|
+
async (data) => {
|
|
213
|
+
const res = await yamlToJson(data);
|
|
214
|
+
return returnJson ? JSON.stringify(res) : res;
|
|
215
|
+
},
|
|
216
|
+
yamlString,
|
|
217
|
+
conversionMode
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// --- XML Conversions ---
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Convert XML to YAML (Sync)
|
|
225
|
+
* @param {string} xmlString - XML formatted string (supports mixed text)
|
|
226
|
+
* @param {Object} [options={}] - Conversion options
|
|
227
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
228
|
+
* @returns {string} YAML formatted string
|
|
229
|
+
*/
|
|
230
|
+
fromXml(xmlString, options = {}) {
|
|
231
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
232
|
+
return this._convertWithEncryption(
|
|
233
|
+
(data) => xmlToYamlSync(data),
|
|
234
|
+
xmlString,
|
|
235
|
+
conversionMode
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Convert XML to YAML (Async)
|
|
241
|
+
* @param {string} xmlString - XML formatted string (supports mixed text)
|
|
242
|
+
* @param {Object} [options={}] - Conversion options
|
|
243
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
244
|
+
* @returns {Promise<string>} YAML formatted string
|
|
245
|
+
*/
|
|
246
|
+
async fromXmlAsync(xmlString, options = {}) {
|
|
247
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
248
|
+
return this._convertWithEncryptionAsync(
|
|
249
|
+
async (data) => xmlToYaml(data),
|
|
250
|
+
xmlString,
|
|
251
|
+
conversionMode
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Convert YAML to XML (Sync)
|
|
257
|
+
* @param {string} yamlString - YAML formatted string
|
|
258
|
+
* @param {Object} [options={}] - Conversion options
|
|
259
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
260
|
+
* @returns {string} XML formatted string
|
|
261
|
+
*/
|
|
262
|
+
toXml(yamlString, options = {}) {
|
|
263
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
264
|
+
return this._convertWithEncryption(
|
|
265
|
+
(data) => yamlToXmlSync(data),
|
|
266
|
+
yamlString,
|
|
267
|
+
conversionMode
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Convert YAML to XML (Async)
|
|
273
|
+
* @param {string} yamlString - YAML formatted string
|
|
274
|
+
* @param {Object} [options={}] - Conversion options
|
|
275
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
276
|
+
* @returns {Promise<string>} XML formatted string
|
|
277
|
+
*/
|
|
278
|
+
async toXmlAsync(yamlString, options = {}) {
|
|
279
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
280
|
+
return this._convertWithEncryptionAsync(
|
|
281
|
+
async (data) => yamlToXml(data),
|
|
282
|
+
yamlString,
|
|
283
|
+
conversionMode
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// --- CSV Conversions ---
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Convert CSV to YAML (Sync)
|
|
291
|
+
* @param {string} csvString - CSV formatted string (supports mixed text)
|
|
292
|
+
* @param {Object} [options={}] - Conversion options
|
|
293
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
294
|
+
* @returns {string} YAML formatted string
|
|
295
|
+
*/
|
|
296
|
+
fromCsv(csvString, options = {}) {
|
|
297
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
298
|
+
return this._convertWithEncryption(
|
|
299
|
+
(data) => csvToYamlSync(data),
|
|
300
|
+
csvString,
|
|
301
|
+
conversionMode
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Convert CSV to YAML (Async)
|
|
307
|
+
* @param {string} csvString - CSV formatted string (supports mixed text)
|
|
308
|
+
* @param {Object} [options={}] - Conversion options
|
|
309
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
310
|
+
* @returns {Promise<string>} YAML formatted string
|
|
311
|
+
*/
|
|
312
|
+
async fromCsvAsync(csvString, options = {}) {
|
|
313
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
314
|
+
return this._convertWithEncryptionAsync(
|
|
315
|
+
async (data) => csvToYaml(data),
|
|
316
|
+
csvString,
|
|
317
|
+
conversionMode
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Convert YAML to CSV (Sync)
|
|
323
|
+
* @param {string} yamlString - YAML formatted string
|
|
324
|
+
* @param {Object} [options={}] - Conversion options
|
|
325
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
326
|
+
* @returns {string} CSV formatted string
|
|
327
|
+
*/
|
|
328
|
+
toCsv(yamlString, options = {}) {
|
|
329
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
330
|
+
return this._convertWithEncryption(
|
|
331
|
+
(data) => yamlToCsvSync(data),
|
|
332
|
+
yamlString,
|
|
333
|
+
conversionMode
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Convert YAML to CSV (Async)
|
|
339
|
+
* @param {string} yamlString - YAML formatted string
|
|
340
|
+
* @param {Object} [options={}] - Conversion options
|
|
341
|
+
* @param {string} [options.conversionMode='no_encryption'] - Encryption mode
|
|
342
|
+
* @returns {Promise<string>} CSV formatted string
|
|
343
|
+
*/
|
|
344
|
+
async toCsvAsync(yamlString, options = {}) {
|
|
345
|
+
const { conversionMode = 'no_encryption' } = options;
|
|
346
|
+
return this._convertWithEncryptionAsync(
|
|
347
|
+
async (data) => yamlToCsv(data),
|
|
348
|
+
yamlString,
|
|
349
|
+
conversionMode
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// --- Validation ---
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Validate YAML string (Sync)
|
|
357
|
+
* @param {string} yamlString - YAML string to validate
|
|
358
|
+
* @returns {boolean} True if valid
|
|
359
|
+
*/
|
|
360
|
+
validate(yamlString) {
|
|
361
|
+
return validateYamlStringSync(yamlString);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Validate YAML string (Async)
|
|
366
|
+
* @param {string} yamlString - YAML string to validate
|
|
367
|
+
* @returns {Promise<boolean>} True if valid
|
|
368
|
+
*/
|
|
369
|
+
async validateAsync(yamlString) {
|
|
370
|
+
return validateYamlString(yamlString);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// ========================================
|
|
374
|
+
// Static Methods (Backward Compatibility)
|
|
375
|
+
// ========================================
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Convert TOON string to YAML (Sync)
|
|
379
|
+
* @param {string} toonString - TOON formatted string
|
|
380
|
+
* @returns {string} YAML formatted string
|
|
381
|
+
*/
|
|
382
|
+
static fromToon(toonString) {
|
|
383
|
+
return toonToYamlSync(toonString);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Convert TOON string to YAML (Async)
|
|
388
|
+
* @param {string} toonString - TOON formatted string
|
|
389
|
+
* @returns {Promise<string>} YAML formatted string
|
|
390
|
+
*/
|
|
391
|
+
static async fromToonAsync(toonString) {
|
|
392
|
+
return toonToYaml(toonString);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Convert YAML to TOON string (Sync)
|
|
397
|
+
* @param {string} yamlString - YAML formatted string
|
|
398
|
+
* @returns {string} TOON formatted string
|
|
399
|
+
*/
|
|
400
|
+
static toToon(yamlString) {
|
|
401
|
+
return yamlToToonSync(yamlString);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Convert YAML to TOON string (Async)
|
|
406
|
+
* @param {string} yamlString - YAML formatted string
|
|
407
|
+
* @returns {Promise<string>} TOON formatted string
|
|
408
|
+
*/
|
|
409
|
+
static async toToonAsync(yamlString) {
|
|
410
|
+
return yamlToToon(yamlString);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Convert JSON to YAML string (Sync)
|
|
415
|
+
* @param {Object|string} jsonData - JSON data or string with embedded JSON
|
|
416
|
+
* @returns {string} YAML formatted string
|
|
417
|
+
*/
|
|
418
|
+
static fromJson(jsonData) {
|
|
419
|
+
return jsonToYamlSync(jsonData);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Convert JSON to YAML string (Async)
|
|
424
|
+
* @param {Object|string} jsonData - JSON data or string with embedded JSON
|
|
425
|
+
* @returns {Promise<string>} YAML formatted string
|
|
426
|
+
*/
|
|
427
|
+
static async fromJsonAsync(jsonData) {
|
|
428
|
+
return jsonToYaml(jsonData);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Convert YAML to JSON (Sync)
|
|
433
|
+
* @param {string} yamlString - YAML formatted string
|
|
434
|
+
* @param {boolean} [returnJson=false] - If true, returns JSON string; if false, returns object
|
|
435
|
+
* @returns {Object|string} JSON result
|
|
436
|
+
*/
|
|
437
|
+
static toJson(yamlString, returnJson = false) {
|
|
438
|
+
const res = yamlToJsonSync(yamlString);
|
|
439
|
+
return returnJson ? JSON.stringify(res) : res;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Convert YAML to JSON (Async)
|
|
444
|
+
* @param {string} yamlString - YAML formatted string
|
|
445
|
+
* @param {boolean} [returnJson=false] - If true, returns JSON string; if false, returns object
|
|
446
|
+
* @returns {Promise<Object|string>} JSON result
|
|
447
|
+
*/
|
|
448
|
+
static async toJsonAsync(yamlString, returnJson = false) {
|
|
449
|
+
const res = await yamlToJson(yamlString);
|
|
450
|
+
return returnJson ? JSON.stringify(res) : res;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Convert XML to YAML string (Sync)
|
|
455
|
+
* @param {string} xmlString - XML formatted string
|
|
456
|
+
* @returns {string} YAML formatted string
|
|
457
|
+
*/
|
|
458
|
+
static fromXml(xmlString) {
|
|
459
|
+
return xmlToYamlSync(xmlString);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Convert XML to YAML string (Async)
|
|
464
|
+
* @param {string} xmlString - XML formatted string
|
|
465
|
+
* @returns {Promise<string>} YAML formatted string
|
|
466
|
+
*/
|
|
467
|
+
static async fromXmlAsync(xmlString) {
|
|
468
|
+
return xmlToYaml(xmlString);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Convert YAML to XML string (Sync)
|
|
473
|
+
* @param {string} yamlString - YAML formatted string
|
|
474
|
+
* @returns {string} XML formatted string
|
|
475
|
+
*/
|
|
476
|
+
static toXml(yamlString) {
|
|
477
|
+
return yamlToXmlSync(yamlString);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Convert YAML to XML string (Async)
|
|
482
|
+
* @param {string} yamlString - YAML formatted string
|
|
483
|
+
* @returns {Promise<string>} XML formatted string
|
|
484
|
+
*/
|
|
485
|
+
static async toXmlAsync(yamlString) {
|
|
486
|
+
return yamlToXml(yamlString);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Convert CSV to YAML string (Sync)
|
|
491
|
+
* @param {string} csvString - CSV formatted string
|
|
492
|
+
* @returns {string} YAML formatted string
|
|
493
|
+
*/
|
|
494
|
+
static fromCsv(csvString) {
|
|
495
|
+
return csvToYamlSync(csvString);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Convert CSV to YAML string (Async)
|
|
500
|
+
* @param {string} csvString - CSV formatted string
|
|
501
|
+
* @returns {Promise<string>} YAML formatted string
|
|
502
|
+
*/
|
|
503
|
+
static async fromCsvAsync(csvString) {
|
|
504
|
+
return csvToYaml(csvString);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Convert YAML to CSV string (Sync)
|
|
509
|
+
* @param {string} yamlString - YAML formatted string
|
|
510
|
+
* @returns {string} CSV formatted string
|
|
511
|
+
*/
|
|
512
|
+
static toCsv(yamlString) {
|
|
513
|
+
return yamlToCsvSync(yamlString);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Convert YAML to CSV string (Async)
|
|
518
|
+
* @param {string} yamlString - YAML formatted string
|
|
519
|
+
* @returns {Promise<string>} CSV formatted string
|
|
520
|
+
*/
|
|
521
|
+
static async toCsvAsync(yamlString) {
|
|
522
|
+
return yamlToCsv(yamlString);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Validate YAML string
|
|
527
|
+
* @param {string} yamlString - YAML string to validate
|
|
528
|
+
* @returns {boolean} True if valid
|
|
529
|
+
*/
|
|
530
|
+
static validate(yamlString) {
|
|
531
|
+
return validateYamlStringSync(yamlString);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Validate YAML string (Async)
|
|
536
|
+
* @param {string} yamlString - YAML string to validate
|
|
537
|
+
* @returns {Promise<boolean>} True if valid
|
|
538
|
+
*/
|
|
539
|
+
static async validateAsync(yamlString) {
|
|
540
|
+
return validateYamlString(yamlString);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* YAML Validator (for YamlConverter)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import yaml from 'js-yaml';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Validate YAML string (Sync)
|
|
9
|
+
* @param {string} yamlString
|
|
10
|
+
* @returns {boolean} True if valid, throws error if invalid
|
|
11
|
+
*/
|
|
12
|
+
export function validateYamlStringSync(yamlString) {
|
|
13
|
+
if (typeof yamlString !== 'string') {
|
|
14
|
+
throw new Error("Input must be a string.");
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
yaml.load(yamlString);
|
|
18
|
+
return true;
|
|
19
|
+
} catch (e) {
|
|
20
|
+
throw new Error(`Invalid YAML: ${e.message}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Validate YAML string (Async)
|
|
26
|
+
* @param {string} yamlString
|
|
27
|
+
* @returns {Promise<boolean>} True if valid
|
|
28
|
+
*/
|
|
29
|
+
export async function validateYamlString(yamlString) {
|
|
30
|
+
return validateYamlStringSync(yamlString);
|
|
31
|
+
}
|