|
| 1 | +import Utils from "../Utils.js"; |
| 2 | + |
| 3 | + |
| 4 | +/** |
| 5 | + * Math operations on numbers. |
| 6 | + * |
| 7 | + * @author bwhitn [[email protected]] |
| 8 | + * @copyright Crown Copyright 2016 |
| 9 | + * @license Apache-2.0 |
| 10 | + * |
| 11 | + * @namespace |
| 12 | + */ |
| 13 | +const Arithmetic = { |
| 14 | + |
| 15 | + /** |
| 16 | + * @constant |
| 17 | + * @default |
| 18 | + */ |
| 19 | + DELIM_OPTIONS: ["Line feed", "Space", "Comma", "Semi-colon", "Colon", "CRLF"], |
| 20 | + |
| 21 | + |
| 22 | + /** |
| 23 | + * Splits a string based on a delimiter and calculates the sum of numbers. |
| 24 | + * |
| 25 | + * @param {string} input |
| 26 | + * @param {Object[]} args |
| 27 | + * @returns {number} |
| 28 | + */ |
| 29 | + runSum: function(input, args) { |
| 30 | + const val = Arithmetic._sum(Arithmetic._createNumArray(input, args[0])); |
| 31 | + return typeof(val) === "number" ? val : NaN; |
| 32 | + }, |
| 33 | + |
| 34 | + |
| 35 | + /** |
| 36 | + * Splits a string based on a delimiter and subtracts all the numbers. |
| 37 | + * |
| 38 | + * @param {string} input |
| 39 | + * @param {Object[]} args |
| 40 | + * @returns {number} |
| 41 | + */ |
| 42 | + runSub: function(input, args) { |
| 43 | + let val = Arithmetic._sub(Arithmetic._createNumArray(input, args[0])); |
| 44 | + return typeof(val) === "number" ? val : NaN; |
| 45 | + }, |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * Splits a string based on a delimiter and multiplies the numbers. |
| 50 | + * |
| 51 | + * @param {string} input |
| 52 | + * @param {Object[]} args |
| 53 | + * @returns {number} |
| 54 | + */ |
| 55 | + runMulti: function(input, args) { |
| 56 | + let val = Arithmetic._multi(Arithmetic._createNumArray(input, args[0])); |
| 57 | + return typeof(val) === "number" ? val : NaN; |
| 58 | + }, |
| 59 | + |
| 60 | + |
| 61 | + /** |
| 62 | + * Splits a string based on a delimiter and divides the numbers. |
| 63 | + * |
| 64 | + * @param {string} input |
| 65 | + * @param {Object[]} args |
| 66 | + * @returns {number} |
| 67 | + */ |
| 68 | + runDiv: function(input, args) { |
| 69 | + let val = Arithmetic._div(Arithmetic._createNumArray(input, args[0])); |
| 70 | + return typeof(val) === "number" ? val : NaN; |
| 71 | + }, |
| 72 | + |
| 73 | + |
| 74 | + /** |
| 75 | + * Splits a string based on a delimiter and computes the mean (average). |
| 76 | + * |
| 77 | + * @param {string} input |
| 78 | + * @param {Object[]} args |
| 79 | + * @returns {number} |
| 80 | + */ |
| 81 | + runMean: function(input, args) { |
| 82 | + let val = Arithmetic._mean(Arithmetic._createNumArray(input, args[0])); |
| 83 | + return typeof(val) === "number" ? val : NaN; |
| 84 | + }, |
| 85 | + |
| 86 | + |
| 87 | + /** |
| 88 | + * Splits a string based on a delimiter and finds the median. |
| 89 | + * |
| 90 | + * @param {string} input |
| 91 | + * @param {Object[]} args |
| 92 | + * @returns {number} |
| 93 | + */ |
| 94 | + runMedian: function(input, args) { |
| 95 | + let val = Arithmetic._median(Arithmetic._createNumArray(input, args[0])); |
| 96 | + return typeof(val) === "number" ? val : NaN; |
| 97 | + }, |
| 98 | + |
| 99 | + |
| 100 | + /** |
| 101 | + * splits a string based on a delimiter and computes the standard deviation. |
| 102 | + * |
| 103 | + * @param {string} input |
| 104 | + * @param {Object[]} args |
| 105 | + * @returns {number} |
| 106 | + */ |
| 107 | + runStdDev: function(input, args) { |
| 108 | + let val = Arithmetic._stdDev(Arithmetic._createNumArray(input, args[0])); |
| 109 | + return typeof(val) === "number" ? val : NaN; |
| 110 | + }, |
| 111 | + |
| 112 | + |
| 113 | + /** |
| 114 | + * Converts a string array to a number array. |
| 115 | + * |
| 116 | + * @private |
| 117 | + * @param {string[]} input |
| 118 | + * @param {string} delim |
| 119 | + * @returns {number[]} |
| 120 | + */ |
| 121 | + _createNumArray: function(input, delim) { |
| 122 | + delim = Utils.charRep[delim || "Space"]; |
| 123 | + let splitNumbers = input.split(delim), |
| 124 | + numbers = [], |
| 125 | + num; |
| 126 | + |
| 127 | + for (let i = 0; i < splitNumbers.length; i++) { |
| 128 | + if (splitNumbers[i].indexOf(".") >= 0) { |
| 129 | + num = parseFloat(splitNumbers[i].trim()); |
| 130 | + } else { |
| 131 | + num = parseInt(splitNumbers[i].trim(), 0); |
| 132 | + } |
| 133 | + if (!isNaN(num)) { |
| 134 | + numbers.push(num); |
| 135 | + } |
| 136 | + } |
| 137 | + return numbers; |
| 138 | + }, |
| 139 | + |
| 140 | + |
| 141 | + /** |
| 142 | + * Adds an array of numbers and returns the value. |
| 143 | + * |
| 144 | + * @private |
| 145 | + * @param {number[]} data |
| 146 | + * @returns {number} |
| 147 | + */ |
| 148 | + _sum: function(data) { |
| 149 | + if (data.length > 0) { |
| 150 | + return data.reduce((acc, curr) => acc + curr); |
| 151 | + } |
| 152 | + }, |
| 153 | + |
| 154 | + |
| 155 | + /** |
| 156 | + * Subtracts an array of numbers and returns the value. |
| 157 | + * |
| 158 | + * @private |
| 159 | + * @param {number[]} data |
| 160 | + * @returns {number} |
| 161 | + */ |
| 162 | + _sub: function(data) { |
| 163 | + if (data.length > 0) { |
| 164 | + return data.reduce((acc, curr) => acc - curr); |
| 165 | + } |
| 166 | + }, |
| 167 | + |
| 168 | + |
| 169 | + /** |
| 170 | + * Multiplies an array of numbers and returns the value. |
| 171 | + * |
| 172 | + * @private |
| 173 | + * @param {number[]} data |
| 174 | + * @returns {number} |
| 175 | + */ |
| 176 | + _multi: function(data) { |
| 177 | + if (data.length > 0) { |
| 178 | + return data.reduce((acc, curr) => acc * curr); |
| 179 | + } |
| 180 | + }, |
| 181 | + |
| 182 | + |
| 183 | + /** |
| 184 | + * Divides an array of numbers and returns the value. |
| 185 | + * |
| 186 | + * @private |
| 187 | + * @param {number[]} data |
| 188 | + * @returns {number} |
| 189 | + */ |
| 190 | + _div: function(data) { |
| 191 | + if (data.length > 0) { |
| 192 | + return data.reduce((acc, curr) => acc / curr); |
| 193 | + } |
| 194 | + }, |
| 195 | + |
| 196 | + |
| 197 | + /** |
| 198 | + * Computes mean of a number array and returns the value. |
| 199 | + * |
| 200 | + * @private |
| 201 | + * @param {number[]} data |
| 202 | + * @returns {number} |
| 203 | + */ |
| 204 | + _mean: function(data) { |
| 205 | + if (data.length > 0) { |
| 206 | + return Arithmetic._sum(data) / data.length; |
| 207 | + } |
| 208 | + }, |
| 209 | + |
| 210 | + |
| 211 | + /** |
| 212 | + * Computes median of a number array and returns the value. |
| 213 | + * |
| 214 | + * @private |
| 215 | + * @param {number[]} data |
| 216 | + * @returns {number} |
| 217 | + */ |
| 218 | + _median: function (data) { |
| 219 | + if ((data.length % 2) === 0) { |
| 220 | + let first, second; |
| 221 | + data.sort(function(a, b){ |
| 222 | + return a - b; |
| 223 | + }); |
| 224 | + first = data[Math.floor(data.length / 2)]; |
| 225 | + second = data[Math.floor(data.length / 2) - 1]; |
| 226 | + return Arithmetic._mean([first, second]); |
| 227 | + } else { |
| 228 | + return data[Math.floor(data.length / 2)]; |
| 229 | + } |
| 230 | + }, |
| 231 | + |
| 232 | + |
| 233 | + /** |
| 234 | + * Computes standard deviation of a number array and returns the value. |
| 235 | + * |
| 236 | + * @private |
| 237 | + * @param {number[]} data |
| 238 | + * @returns {number} |
| 239 | + */ |
| 240 | + _stdDev: function (data) { |
| 241 | + if (data.length > 0) { |
| 242 | + let avg = Arithmetic._mean(data); |
| 243 | + let devSum = 0; |
| 244 | + for (let i = 0; i < data.length; i++) { |
| 245 | + devSum += (data[i] - avg) ** 2; |
| 246 | + } |
| 247 | + return Math.sqrt(devSum / data.length); |
| 248 | + } |
| 249 | + }, |
| 250 | +}; |
| 251 | + |
| 252 | +export default Arithmetic; |
0 commit comments