diff --git a/components/stats.js b/components/stats.js index 8960ab78e25600a9bc1ec3f7ddf55ff784feeb23..4021fdee0af44ab2fb229e565988014ba4a4d99f 100644 --- a/components/stats.js +++ b/components/stats.js @@ -1,62 +1,51 @@ import React, { Component } from 'react' import { Text, + View, ScrollView } from 'react-native' import { LocalDate, ChronoUnit } from 'js-joda' import styles from '../styles/index' import cycleModule from '../lib/cycle' -import getPeriodInfo from '../lib/period-length' +import getCycleInfo from '../lib/cycle-length' export default class Stats extends Component { - constructor(props) { - super(props) - const allMensesStarts = cycleModule().getAllMensesStarts() - this.test = allMensesStarts - this.state = { - text: determineStatsText(allMensesStarts) - } - } - render() { + const allMensesStarts = cycleModule().getAllMensesStarts() + const statsText = determineStatsText(allMensesStarts) return ( <ScrollView> - <Text style={styles.welcome}>{this.state.text}</Text> + <View> + <Text style={styles.stats}>{statsText}</Text> + </View> </ScrollView> ) } } function getCycleLength(cycleStartDates) { - const periodLengths = [] + const cycleLengths = [] for (let i = 0; i < cycleStartDates.length - 1; i++) { - const nextPeriodStart = LocalDate.parse(cycleStartDates[i]) - const periodStart = LocalDate.parse(cycleStartDates[i + 1]) - periodLengths.push(periodStart.until(nextPeriodStart, ChronoUnit.DAYS)) + const nextCycleStart = LocalDate.parse(cycleStartDates[i]) + const cycleStart = LocalDate.parse(cycleStartDates[i + 1]) + cycleLengths.push(cycleStart.until(nextCycleStart, ChronoUnit.DAYS)) } - return periodLengths + return cycleLengths } function determineStatsText(allMensesStarts) { - const emptyStats = 'At least one completed period is needed to present you with stats here.' + const emptyStats = 'At least one completed cycle is needed to present you with stats here.' if (allMensesStarts.length < 2) { return emptyStats } else { const cycleLengths = getCycleLength(allMensesStarts) const numberOfCycles = cycleLengths.length - const periodInfo = getPeriodInfo(cycleLengths) if (numberOfCycles === 1) { - return `You have documented one period of ${cycleLengths[0]} days.` - } else { - const statsText = `Stats are based on ${numberOfCycles} completed - periods.\n\n - Average period length: ${periodInfo.mean} days\n\n - shortest period: ${periodInfo.minimum} days\n - longest period: ${periodInfo.maximum} days\n - median length (meaning 50% of periods are of this length or shorter): - ${periodInfo.median} days\n - standard deviation: ${periodInfo.stdDeviation}` - return statsText + return `You have documented one cycle of ${cycleLengths[0]} days.` } + const cycleInfo = getCycleInfo(cycleLengths) + const statsText = `Stats are based on ${numberOfCycles} completed cycles.\n\n\ + Average cycle length: ${cycleInfo.mean} days\n\nShortest cycle: ${cycleInfo.minimum} days\nLongest cycle: ${cycleInfo.maximum} days\nMedian length (meaning 50% of cycles are of this length or shorter): ${cycleInfo.median} days\nStandard deviation: ${cycleInfo.stdDeviation}` + return statsText } } \ No newline at end of file diff --git a/lib/period-length.js b/lib/cycle-length.js similarity index 67% rename from lib/period-length.js rename to lib/cycle-length.js index e62f076ef09aa0a555c62219fbd3a33c5e90baba..6f8feb56f54f4ecd70bafe7fe9648133314178cb 100644 --- a/lib/period-length.js +++ b/lib/cycle-length.js @@ -1,38 +1,38 @@ import assert from 'assert' -export default function getPeriodLengthStats(cycleLengths) { +export default function getCycleLengthStats(cycleLengths) { throwIfArgsAreNotInRequiredFormat(cycleLengths) - const periodLengthStats = {} + const cycleLengthStats = {} const sortedCycleLengths = cycleLengths.sort((a, b) => { return a - b }) - periodLengthStats.minimum = sortedCycleLengths[0] - periodLengthStats.maximum = sortedCycleLengths[cycleLengths.length - 1] - periodLengthStats.mean = Math.round( + cycleLengthStats.minimum = sortedCycleLengths[0] + cycleLengthStats.maximum = sortedCycleLengths[cycleLengths.length - 1] + cycleLengthStats.mean = Math.round( cycleLengths.reduce(getSum) / cycleLengths.length * 100 ) / 100 // median if (cycleLengths.length % 2 == 1) { - periodLengthStats.median = sortedCycleLengths[ + cycleLengthStats.median = sortedCycleLengths[ (cycleLengths.length + 1) / 2 - 1 ] } else { const middle = cycleLengths.length / 2 - periodLengthStats.median = (sortedCycleLengths[middle - 1] + + 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 - periodLengthStats.mean, 2) + return Math.pow(cycleLength - cycleLengthStats.mean, 2) }).reduce(getSum) - periodLengthStats.stdDeviation = Math.round( + cycleLengthStats.stdDeviation = Math.round( Math.sqrt(sumOfSquares / (cycleLengths.length - 1 )) * 100 ) / 100 } else { - periodLengthStats.stdDeviation = null + cycleLengthStats.stdDeviation = null } - return periodLengthStats + return cycleLengthStats } function getSum(total, num) { diff --git a/styles/index.js b/styles/index.js index 1b09eb4c37ec79d41f94ab2a9a4e117ec1f9eb6a..fde1700a6900a5120d887c67c0830a20068bbf71 100644 --- a/styles/index.js +++ b/styles/index.js @@ -82,5 +82,11 @@ export default StyleSheet.create({ marginTop: 15, marginLeft: 'auto', marginRight: 'auto' + }, + stats: { + fontSize: 18, + margin: 30, + textAlign: 'left', + textAlignVertical: 'center' } }) \ No newline at end of file diff --git a/test/period-length.spec.js b/test/cycle-length.spec.js similarity index 65% rename from test/period-length.spec.js rename to test/cycle-length.spec.js index f10fb1854441b9d3578b7c6ed7384ab357269817..043cf96d34a355993549b1c400e99bb509645d63 100644 --- a/test/period-length.spec.js +++ b/test/cycle-length.spec.js @@ -1,14 +1,14 @@ import chai from 'chai' import { AssertionError } from 'assert' -import periodInfo from '../lib/period-length' +import cycleInfo from '../lib/cycle-length' const expect = chai.expect -describe('getPeriodLengthStats', () => { +describe('getCycleLengthStats', () => { it('works for a simple odd-numbered array', () => { - const periodLengths = [99, 5, 1, 2, 100] - const result = periodInfo(periodLengths) + const cycleLengths = [99, 5, 1, 2, 100] + const result = cycleInfo(cycleLengths) const expectedResult = { minimum: 1, maximum: 100, @@ -20,8 +20,8 @@ describe('getPeriodLengthStats', () => { }) it('works for a simple even-numbered array', () => { - const periodLengths = [4, 1, 15, 2, 20, 5] - const result = periodInfo(periodLengths) + const cycleLengths = [4, 1, 15, 2, 20, 5] + const result = cycleInfo(cycleLengths) const expectedResult = { minimum: 1, maximum: 20, @@ -32,8 +32,8 @@ describe('getPeriodLengthStats', () => { expect(result).to.eql(expectedResult) }) it('works for an one-element array', () => { - const periodLengths = [42] - const result = periodInfo(periodLengths) + const cycleLengths = [42] + const result = cycleInfo(cycleLengths) const expectedResult = { minimum: 42, maximum: 42, @@ -45,20 +45,20 @@ describe('getPeriodLengthStats', () => { }) describe('when args are wrong', () => { it('throws when arg object is an empty array', () => { - const periodLengths = [] - expect(() => periodInfo(periodLengths).to.throw(AssertionError)) + const cycleLengths = [] + expect(() => cycleInfo(cycleLengths).to.throw(AssertionError)) }) it('throws when arg object is not in right format', () => { const wrongObject = { hello: 'world' } - expect(() => periodInfo(wrongObject).to.throw(AssertionError)) + expect(() => cycleInfo(wrongObject).to.throw(AssertionError)) }) it('throws when arg array contains a string', () => { const wrongElement = [4, 1, 15, '2', 20, 5] - expect(() => periodInfo(wrongElement).to.throw(AssertionError)) + expect(() => cycleInfo(wrongElement).to.throw(AssertionError)) }) it('throws when arg array contains a NaN', () => { const wrongElement = [4, 1, 15, NaN, 20, 5] - expect(() => periodInfo(wrongElement).to.throw(AssertionError)) + expect(() => cycleInfo(wrongElement).to.throw(AssertionError)) }) }) }) \ No newline at end of file