Skip to content

Add yaml beautify #2056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
"jq-web": "^0.5.1",
"jquery": "3.7.1",
"js-sha3": "^0.9.3",
"js-yaml": "^4.1.0",
"jsesc": "^3.0.2",
"json5": "^2.2.3",
"jsonata": "^2.0.3",
Expand Down
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@
"SQL Beautify",
"SQL Minify",
"CSS Beautify",
"YAML Beautify",
"CSS Minify",
"XPath expression",
"JPath expression",
Expand Down
51 changes: 51 additions & 0 deletions src/core/operations/YAMLBeautify.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @author MrMadFox [[email protected]]
* @copyright Crown Copyright 2025
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import YAML from "js-yaml";
import OperationError from "../errors/OperationError.mjs";

/**
* YAMLBeautify operation
*/
class YAMLBeautify extends Operation {

/**
* YAMLBeautify constructor
*/
constructor() {
super();

this.name = "YAML Beautify";
this.module = "Code";
this.description = "Indents and prettifies YAML code.";
this.infoURL = "https://yaml.org/";
this.inputType = "string";
this.outputType = "string";
this.args = [];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
try {
return YAML.dump(YAML.load(input), {
styles: {
"!!null": "empty"
}
});
} catch (err) {
if (err instanceof YAML.YAMLException) {
throw new OperationError(err.message);
}
}
}
}

export default YAMLBeautify;
45 changes: 45 additions & 0 deletions tests/operations/tests/YAMLBeautify.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* YamlBeautifier tests.
*
* @author MrMadFox [[email protected]]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/

import TestRegister from "../../lib/TestRegister.mjs";

TestRegister.addTests(
[{
name: "YAML Beautify: basic YAML",
input: "key1: value1\nkey2: value2",
expectedOutput: "key1: value1\nkey2: value2\n",
recipeConfig: [{
"op": "YAML Beautify",
"args": []
}]
}, {
name: "YAML Beautify: nested YAML",
input: "key1:\n subkey1: value1\n subkey2: value2\nkey2: value3",
expectedOutput: "key1:\n subkey1: value1\n subkey2: value2\nkey2: value3\n",
recipeConfig: [{
"op": "YAML Beautify",
"args": []
}]
}, {
name: "YAML Beautify: empty YAML",
input: "",
expectedOutput: "",
recipeConfig: [{
"op": "YAML Beautify",
"args": []
}]
}, {
name: "YAML Beautify: malformed YAML",
input: "key1: value1\nkey2: value2\nkey3",
expectedOutput: "key1: value",
recipeConfig: [{
"op": "YAML Beautify",
"args": []
}]
}
]);
Loading