Fix To Base radix validation

This commit is contained in:
marko1olo 2026-06-06 03:58:14 +04:00
parent d735496641
commit dc08dc582d
3 changed files with 35 additions and 1 deletions

View File

@ -43,7 +43,7 @@ class ToBase extends Operation {
throw new OperationError("Error: Input must be a number"); throw new OperationError("Error: Input must be a number");
} }
const radix = args[0]; const radix = args[0];
if (radix < 2 || radix > 36) { if (!Number.isInteger(radix) || radix < 2 || radix > 36) {
throw new OperationError("Error: Radix argument must be between 2 and 36"); throw new OperationError("Error: Radix argument must be between 2 and 36");
} }
return input.toString(radix); return input.toString(radix);

View File

@ -21,6 +21,7 @@ import "./tests/AnalyseUUID.mjs";
import "./tests/AlternatingCaps.mjs"; import "./tests/AlternatingCaps.mjs";
import "./tests/AvroToJSON.mjs"; import "./tests/AvroToJSON.mjs";
import "./tests/BaconCipher.mjs"; import "./tests/BaconCipher.mjs";
import "./tests/Base.mjs";
import "./tests/Base32.mjs"; import "./tests/Base32.mjs";
import "./tests/Base45.mjs"; import "./tests/Base45.mjs";
import "./tests/Base58.mjs"; import "./tests/Base58.mjs";

View File

@ -0,0 +1,33 @@
/**
* Base conversion tests
*
* @author marko1olo
* @copyright Crown Copyright 2026
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
name: "To Base: base 2",
input: "63",
expectedOutput: "111111",
recipeConfig: [
{
"op": "To Base",
"args": [2]
}
]
},
{
name: "To Base: fractional radix",
input: "63",
expectedOutput: "Error: Radix argument must be between 2 and 36",
recipeConfig: [
{
"op": "To Base",
"args": [2.2]
}
]
},
]);