From 98c008a0f1fb7708282c60b1735786297919ba19 Mon Sep 17 00:00:00 2001 From: sai praneeth Date: Thu, 29 May 2025 07:45:04 +0000 Subject: [PATCH 1/2] Add yaml beautify --- package-lock.json | 12 +++---- package.json | 1 + src/core/config/Categories.json | 1 + src/core/operations/YAMLBeautify.mjs | 51 ++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 src/core/operations/YAMLBeautify.mjs diff --git a/package-lock.json b/package-lock.json index b374df4b..11b41b50 100644 --- a/package-lock.json +++ b/package-lock.json @@ -54,6 +54,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", @@ -101,6 +102,7 @@ "vkbeautify": "^0.99.3", "xpath": "0.0.34", "xregexp": "^5.1.1", + "yaml": "^2.8.0", "zlibjs": "^0.3.1" }, "devDependencies": { @@ -12366,7 +12368,6 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", - "dev": true, "license": "MIT", "dependencies": { "argparse": "^2.0.1" @@ -19245,16 +19246,15 @@ "license": "ISC" }, "node_modules/yaml": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.7.0.tgz", - "integrity": "sha512-+hSoy/QHluxmC9kCIJyL/uyFmLmc+e5CFR5Wa+bpIhIj85LVb9ZH2nVnqrHoSvKogwODv0ClqZkmiSSaIH5LTA==", - "dev": true, + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.8.0.tgz", + "integrity": "sha512-4lLa/EcQCB0cJkyts+FpIRx5G/llPxfP6VQU5KByHEhLxY3IJCH0f0Hy1MHI8sClTvsIb8qwRJ6R/ZdlDJ/leQ==", "license": "ISC", "bin": { "yaml": "bin.mjs" }, "engines": { - "node": ">= 14" + "node": ">= 14.6" } }, "node_modules/yargs": { diff --git a/package.json b/package.json index 9191ab6f..5b991cae 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 434c8bb6..cd66f56a 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -470,6 +470,7 @@ "SQL Beautify", "SQL Minify", "CSS Beautify", + "YAML Beautify", "CSS Minify", "XPath expression", "JPath expression", diff --git a/src/core/operations/YAMLBeautify.mjs b/src/core/operations/YAMLBeautify.mjs new file mode 100644 index 00000000..45714d84 --- /dev/null +++ b/src/core/operations/YAMLBeautify.mjs @@ -0,0 +1,51 @@ +/** + * @author c.saipraneeth888@gmail.com [c.saipraneeth888@gmail.com] + * @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; From a2e9bd86d2f0f52e76404ca51e8cc666712f433b Mon Sep 17 00:00:00 2001 From: sai praneeth Date: Sat, 7 Jun 2025 15:09:28 +0000 Subject: [PATCH 2/2] added tests --- package-lock.json | 2 +- src/core/operations/YAMLBeautify.mjs | 2 +- tests/operations/tests/YAMLBeautify.mjs | 45 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 tests/operations/tests/YAMLBeautify.mjs diff --git a/package-lock.json b/package-lock.json index 11b41b50..93e7a62c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -102,7 +102,6 @@ "vkbeautify": "^0.99.3", "xpath": "0.0.34", "xregexp": "^5.1.1", - "yaml": "^2.8.0", "zlibjs": "^0.3.1" }, "devDependencies": { @@ -19249,6 +19248,7 @@ "version": "2.8.0", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.8.0.tgz", "integrity": "sha512-4lLa/EcQCB0cJkyts+FpIRx5G/llPxfP6VQU5KByHEhLxY3IJCH0f0Hy1MHI8sClTvsIb8qwRJ6R/ZdlDJ/leQ==", + "dev": true, "license": "ISC", "bin": { "yaml": "bin.mjs" diff --git a/src/core/operations/YAMLBeautify.mjs b/src/core/operations/YAMLBeautify.mjs index 45714d84..a0ad6098 100644 --- a/src/core/operations/YAMLBeautify.mjs +++ b/src/core/operations/YAMLBeautify.mjs @@ -1,5 +1,5 @@ /** - * @author c.saipraneeth888@gmail.com [c.saipraneeth888@gmail.com] + * @author MrMadFox [c.saipraneeth888@gmail.com] * @copyright Crown Copyright 2025 * @license Apache-2.0 */ diff --git a/tests/operations/tests/YAMLBeautify.mjs b/tests/operations/tests/YAMLBeautify.mjs new file mode 100644 index 00000000..9101bbc7 --- /dev/null +++ b/tests/operations/tests/YAMLBeautify.mjs @@ -0,0 +1,45 @@ +/** + * YamlBeautifier tests. + * + * @author MrMadFox [c.saipraneeth888@gmail.com] + * @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": [] + }] + } + ]);