From 706423462d78b5d43ff6de8a08c294db91d95674 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Fri, 31 Mar 2017 16:00:23 +0000 Subject: [PATCH] 'From Base' operation now supports numbers with fractional parts. --- src/core/operations/Base.js | 14 +++++++++++++- src/node/index.js | 8 +++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/core/operations/Base.js b/src/core/operations/Base.js index 39e0b7412f..34e3b524eb 100755 --- a/src/core/operations/Base.js +++ b/src/core/operations/Base.js @@ -46,7 +46,19 @@ const Base = { if (radix < 2 || radix > 36) { throw "Error: Radix argument must be between 2 and 36"; } - return parseInt(input.replace(/\s/g, ""), radix); + + var number = input.replace(/\s/g, "").split("."), + result = parseInt(number[0], radix) || 0; + + if (number.length === 1) return result; + + // Fractional part + for (var i = 0; i < number[1].length; i++) { + var digit = parseInt(number[1][i], radix); + result += digit / Math.pow(radix, i+1); + } + + return result; }, }; diff --git a/src/node/index.js b/src/node/index.js index 06db248c58..16ec4b3dd7 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -13,7 +13,13 @@ const CyberChef = module.exports = { bake: function(input, recipeConfig) { this.chef = new Chef(); - return this.chef.bake(input, recipeConfig, {}, 0, false); + return this.chef.bake( + input, + recipeConfig, + {}, + 0, + false + ); } };