Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 16x 16x 16x 16x 16x 2x 2x 2x 2x 2x 2x 2x 28275x 28275x 28275x 5087x 13193x 5087x 5087x 28275x 28275x 5088x 15068x 5088x 5088x 18100x 2x 2x 2x 2x | /**
* This module contains the function jsonToYaml that converts a JSON file to a YAML file.
*
* @module yamlConverter
*/
'use strict';
import { jsonParse } from './index.js';
import { writeFileSync } from 'fs';
/**
* Converts a JSON file to a YAML file.
* @param {string} jsonFile JSON file to convert.
* @param {string} yamlFile YAML file to output.
* @returns {void}
* @throws {Error} If the JSON file could not be converted to YAML.
*/
function jsonToYaml(jsonFile, yamlFile) {
const JSON_AST = jsonParse(jsonFile, true);
const YAML = astToYamlString(JSON_AST);
writeFileSync(yamlFile, YAML);
}
/**
* Converts a JSON AST to a YAML string.
* @param {Object} ast AST to convert.
* @param {number} indent Current indentation level.
* @returns {string} The YAML string.
*/
function astToYamlString(ast, indent = 0) {
let yamlString = '';
if (ast.type === 'object') {
yamlString += ast.properties.map((pair) => {
return ' '.repeat(indent) + pair.key + ': ' + astToYamlString(pair.value, indent + 2);
}).join('\n');
return yamlString.length === 0 ? '{}' : indent === 0 ? yamlString : '\n' + yamlString;
}
if (ast.type === 'array') {
yamlString += ast.elements.map((element) => {
return ' '.repeat(indent) + '- ' + astToYamlString(element, indent + 2);
}).join('\n');
return yamlString.length === 0 ? '[]' : indent === 0 ? yamlString : '\n' + yamlString;
}
return String(ast.value);
}
export { jsonToYaml };
|