[refactor] Moved 'addOffset' to its own TS file

This commit is contained in:
Steven Evans 2018-07-05 13:52:37 -04:00
parent 875f7b4438
commit d20516b03c
5 changed files with 29 additions and 17 deletions

@ -11,13 +11,14 @@ import {KEY} from "./Terminal";
import {createProgressBarText} from "../utils/helpers/createProgressBarText";
import {dialogBoxCreate} from "../utils/DialogBox";
import {addOffset, clearObject,
import {clearObject,
removeChildrenFromElement,
appendLineBreaks,
removeElement} from "../utils/HelperFunctions";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import numeral from "numeral/min/numeral.min";
import {addOffset} from "../utils/helpers/addOffset";
import {createElement} from "../utils/uiHelpers/createElement";
import {createPopup} from "../utils/uiHelpers/createPopup";
import {exceptionAlert} from "../utils/helpers/exceptionAlert";

@ -3,9 +3,8 @@ import {Engine} from "./engine";
import {displayFactionContent} from "./Faction";
import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox";
import {addOffset,
clearEventListenersEl,
clearEventListeners} from "../utils/HelperFunctions";
import {clearEventListeners} from "../utils/HelperFunctions";
import {addOffset} from "../utils/helpers/addOffset";
import {formatNumber} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt";
import {isString} from "../utils/helpers/isString";

@ -35,8 +35,9 @@ import {TextFile, getTextFile} from "./TextFile";
import {containsAllStrings, longestCommonStart,
formatNumber} from "../utils/StringHelperFunctions";
import {addOffset} from "../utils/helpers/addOffset";
import {isString} from "../utils/helpers/isString";
import {addOffset, printArray} from "../utils/HelperFunctions";
import {printArray} from "../utils/HelperFunctions";
import {logBoxCreate} from "../utils/LogBox";
import {yesNoBoxCreate,
yesNoBoxGetYesButton,

@ -19,17 +19,6 @@ function clearObject(obj) {
}
}
//Adds a random offset to a number within a certain percentage
//e.g. addOffset(100, 5) will return anything from 95 to 105.
//The percentage argument must be between 0 and 100;
function addOffset(n, percentage) {
if (percentage < 0 || percentage > 100) {return n;}
var offset = n * (percentage / 100);
return n + ((Math.random() * (2 * offset)) - offset);
}
//Given an element by its Id(usually an 'a' element), removes all event listeners
//from that element by cloning and replacing. Then returns the new cloned element
function clearEventListeners(elemId) {
@ -133,7 +122,6 @@ function powerOfTwo(n) {
export {sizeOfObject,
clearObject,
addOffset,
clearEventListeners,
compareArrays,
printArray,

@ -0,0 +1,23 @@
/**
* Adds a random offset to a number within a certain percentage
* @example
* // Returns between 95-105
* addOffset(100, 5);
* @example
* // Returns between 63-77
* addOffSet(70, 10);
* @param midpoint The number to be the midpoint of the offset range
* @param percentage The percentage (in a range of 0-100) to offset
*/
export function addOffset(midpoint: number, percentage: number) {
const maxPercent: number = 100;
if (percentage < 0 || percentage > maxPercent) {
return midpoint;
}
const offset: number = midpoint * (percentage / maxPercent);
// Double the range to account for both sides of the midpoint.
// tslint:disable-next-line:no-magic-numbers
return midpoint + ((Math.random() * (offset * 2)) - offset);
}