[refactor] Moved 'powerOfTwo' to 'isPowerOfTwo' TS file

This commit is contained in:
Steven Evans 2018-07-05 14:20:21 -04:00
parent 1a5208f78f
commit 015524f049
3 changed files with 15 additions and 9 deletions

@ -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 "";
}

@ -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,

@ -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;
}