From 015524f049e020eaf6bc9897c6d2e84d184fd461 Mon Sep 17 00:00:00 2001 From: Steven Evans Date: Thu, 5 Jul 2018 14:20:21 -0400 Subject: [PATCH] [refactor] Moved 'powerOfTwo' to 'isPowerOfTwo' TS file --- src/NetscriptFunctions.js | 4 ++-- utils/HelperFunctions.js | 7 ------- utils/helpers/isPowerOfTwo.ts | 13 +++++++++++++ 3 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 utils/helpers/isPowerOfTwo.ts diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index f5f8df91d..af4786e67 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -48,7 +48,7 @@ import {NetscriptPort} from "./NetscriptPort"; import Decimal from "decimal.js"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {powerOfTwo} from "../utils/HelperFunctions"; +import {isPowerOfTwo} from "../utils/helpers/isPowerOfTwo"; import {arrayToString} from "../utils/helpers/arrayToString"; import {createRandomIp} from "../utils/IPAddress"; import {formatNumber, isHTML} from "../utils/StringHelperFunctions"; @@ -1548,7 +1548,7 @@ function NetscriptFunctions(workerScript) { } ram = Math.round(ram); - if (isNaN(ram) || !powerOfTwo(ram)) { + if (isNaN(ram) || !isPowerOfTwo(ram)) { workerScript.scriptRef.log("ERROR: purchaseServer() failed due to invalid ram argument. Must be numeric and a power of 2"); return ""; } diff --git a/utils/HelperFunctions.js b/utils/HelperFunctions.js index bb27dc046..d4a7a75af 100644 --- a/utils/HelperFunctions.js +++ b/utils/HelperFunctions.js @@ -110,17 +110,10 @@ function compareArrays(a1, a2) { return true; } -//Returns bool indicating whether or not its a power of 2 -function powerOfTwo(n) { - if (isNaN(n)) {return false;} - return n && (n & (n-1)) === 0; -} - export {sizeOfObject, clearObject, clearEventListeners, compareArrays, - powerOfTwo, clearEventListenersEl, removeElement, createAccordionElement, diff --git a/utils/helpers/isPowerOfTwo.ts b/utils/helpers/isPowerOfTwo.ts new file mode 100644 index 000000000..ea7924954 --- /dev/null +++ b/utils/helpers/isPowerOfTwo.ts @@ -0,0 +1,13 @@ +/** + * Determines if the number is a power of 2 + * @param n The number to check. + */ +export function isPowerOfTwo(n: number) { + if (isNaN(n)) { + return false; + } + + // Disabiling the bitwise rule because it's honestly the most effecient way to check for this. + // tslint:disable-next-line:no-bitwise + return n && (n & (n - 1)) === 0; +}