@@ -7,6 +7,7 @@ import * as TestState from "./test-state";
77import * as Numbers from "@monkeytype/util/numbers" ;
88import { CompletedEvent , IncompleteTest } from "@monkeytype/schemas/results" ;
99import { isFunboxActiveWithProperty } from "./funbox/list" ;
10+ import * as CustomText from "./custom-text" ;
1011
1112type CharCount = {
1213 spaces : number ;
@@ -144,14 +145,17 @@ export function calculateTestSeconds(now?: number): number {
144145 }
145146}
146147
147- export function calculateWpmAndRaw ( withDecimalPoints ?: true ) : {
148+ export function calculateWpmAndRaw (
149+ withDecimalPoints ?: true ,
150+ final = false
151+ ) : {
148152 wpm : number ;
149153 raw : number ;
150154} {
151155 const testSeconds = calculateTestSeconds (
152156 TestState . isActive ? performance . now ( ) : end
153157 ) ;
154- const chars = countChars ( ) ;
158+ const chars = countChars ( final ) ;
155159 const wpm = Numbers . roundTo2 (
156160 ( ( chars . correctWordChars + chars . correctSpaces ) * ( 60 / testSeconds ) ) / 5
157161 ) ;
@@ -280,7 +284,7 @@ function getTargetWords(): string[] {
280284 return targetWords ;
281285}
282286
283- function countChars ( ) : CharCount {
287+ function countChars ( final = false ) : CharCount {
284288 let correctWordChars = 0 ;
285289 let correctChars = 0 ;
286290 let incorrectChars = 0 ;
@@ -343,7 +347,13 @@ function countChars(): CharCount {
343347 }
344348 correctChars += toAdd . correct ;
345349 incorrectChars += toAdd . incorrect ;
346- if ( i === inputWords . length - 1 ) {
350+
351+ const isTimedTest =
352+ Config . mode === "time" ||
353+ ( Config . mode === "custom" && CustomText . getLimit ( ) . mode === "time" ) ;
354+ const shouldCountPartialLastWord = ! final || ( final && isTimedTest ) ;
355+
356+ if ( i === inputWords . length - 1 && shouldCountPartialLastWord ) {
347357 //last word - check if it was all correct - add to correct word chars
348358 if ( toAdd . incorrect === 0 ) correctWordChars += toAdd . correct ;
349359 } else {
@@ -370,7 +380,7 @@ function countChars(): CharCount {
370380 } ;
371381}
372382
373- export function calculateStats ( ) : Stats {
383+ export function calculateFinalStats ( ) : Stats {
374384 console . debug ( "Calculating result stats" ) ;
375385 let testSeconds = calculateTestSeconds ( ) ;
376386 console . debug (
@@ -398,8 +408,10 @@ export function calculateStats(): Stats {
398408 testSeconds
399409 ) ;
400410 }
401- const chars = countChars ( ) ;
402- const { wpm, raw } = calculateWpmAndRaw ( true ) ;
411+
412+ //todo: this counts chars twice - once here and once in calculateWpmAndRaw
413+ const chars = countChars ( true ) ;
414+ const { wpm, raw } = calculateWpmAndRaw ( true , true ) ;
403415 const acc = Numbers . roundTo2 ( calculateAccuracy ( ) ) ;
404416 const ret = {
405417 wpm : isNaN ( wpm ) ? 0 : wpm ,
0 commit comments