Merge branch 'chore/retire-assert' into 'main'

Chore/retire assert

See merge request bloodyhealth/drip!426
This commit is contained in:
Sofiya Tepikin
2022-07-27 18:06:12 +00:00
4 changed files with 574 additions and 127 deletions
+21 -22
View File
@@ -1,5 +1,3 @@
import assert from 'assert'
export function getCycleLengthStats(cycleLengths) {
throwIfArgsAreNotInRequiredFormat(cycleLengths)
const cycleLengthStats = {}
@@ -8,27 +6,27 @@ export function getCycleLengthStats(cycleLengths) {
})
cycleLengthStats.minimum = sortedCycleLengths[0]
cycleLengthStats.maximum = sortedCycleLengths[cycleLengths.length - 1]
cycleLengthStats.mean = Math.round(
cycleLengths.reduce(getSum) / cycleLengths.length * 100
) / 100
cycleLengthStats.mean =
Math.round((cycleLengths.reduce(getSum) / cycleLengths.length) * 100) / 100
// median
if (cycleLengths.length % 2 == 1) {
cycleLengthStats.median = sortedCycleLengths[
(cycleLengths.length + 1) / 2 - 1
]
cycleLengthStats.median =
sortedCycleLengths[(cycleLengths.length + 1) / 2 - 1]
} else {
const middle = cycleLengths.length / 2
cycleLengthStats.median = (sortedCycleLengths[middle - 1] +
sortedCycleLengths[middle]) / 2
cycleLengthStats.median =
(sortedCycleLengths[middle - 1] + sortedCycleLengths[middle]) / 2
}
// corrected standard deviation (based on unbiased sample variance)
if (cycleLengths.length > 1) {
const sumOfSquares = cycleLengths.map(cycleLength => {
return Math.pow(cycleLength - cycleLengthStats.mean, 2)
}).reduce(getSum)
cycleLengthStats.stdDeviation = Math.round(
Math.sqrt(sumOfSquares / (cycleLengths.length - 1 )) * 100
) / 100
const sumOfSquares = cycleLengths
.map((cycleLength) => {
return Math.pow(cycleLength - cycleLengthStats.mean, 2)
})
.reduce(getSum)
cycleLengthStats.stdDeviation =
Math.round(Math.sqrt(sumOfSquares / (cycleLengths.length - 1)) * 100) /
100
} else {
cycleLengthStats.stdDeviation = null
}
@@ -40,10 +38,11 @@ function getSum(total, num) {
}
function throwIfArgsAreNotInRequiredFormat(cycleLengths) {
assert.ok(Array.isArray(cycleLengths), 'Input should be an array.')
assert.ok(cycleLengths.length > 0, 'Input array should not be empty.')
cycleLengths.forEach(cycleLength => {
assert.equal(typeof cycleLength, 'number', 'Elements in the array should be of type number.')
assert.ok(!isNaN(cycleLength), 'Elements of array should not be NaN.')
if (!Array.isArray(cycleLengths)) throw new Error('Input should be an array.')
if (cycleLengths.length < 1)
throw new Error('Input array should not be empty.')
cycleLengths.forEach((cycleLength) => {
if (typeof cycleLength !== 'number' || Number.isNaN(cycleLength))
throw new Error('Elements in the array should be of type number.')
})
}
}