Skip to content

Commit d973942

Browse files
committed
fix: ensure RME is positive
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
1 parent f369ed3 commit d973942

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/utils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ export const formatNumber = (
146146
targetDigits: number,
147147
maxFractionDigits: number
148148
): string => {
149+
if (!Number.isFinite(x)) return String(x)
150+
149151
// Round large numbers to integers, but not to multiples of 10.
150152
// The actual number of significant digits may be more than `targetDigits`.
151153
if (Math.abs(x) >= 10 ** targetDigits) {
@@ -189,7 +191,8 @@ export const isPromiseLike = <T>(
189191
maybePromiseLike: unknown
190192
): maybePromiseLike is PromiseLike<T> =>
191193
maybePromiseLike !== null &&
192-
typeof maybePromiseLike === 'object' &&
194+
(typeof maybePromiseLike === 'object' ||
195+
typeof maybePromiseLike === 'function') &&
193196
typeof (maybePromiseLike as PromiseLike<T>).then === 'function'
194197

195198
type AsyncFunctionType<A extends unknown[], R> = (...args: A) => PromiseLike<R>
@@ -217,6 +220,9 @@ export const isFnAsyncResource = (fn: Fn | null | undefined): boolean => {
217220
if (isAsyncFunction(fn)) {
218221
return true
219222
}
223+
if (fn.length > 0) {
224+
return false
225+
}
220226
try {
221227
const fnCall = fn()
222228
const promiseLike = isPromiseLike(fnCall)
@@ -346,7 +352,8 @@ export const getStatisticsSorted = (samples: number[]): Statistics => {
346352
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing, @typescript-eslint/no-non-null-assertion
347353
const critical = tTable[(Math.round(df) || 1).toString()] || tTable.infinity!
348354
const moe = sem * critical
349-
const rme = (moe / mean) * 100
355+
const absMean = Math.abs(mean)
356+
const rme = absMean === 0 ? Number.POSITIVE_INFINITY : (moe / absMean) * 100
350357
const p50 = medianSorted(samples)
351358
return {
352359
aad: absoluteDeviation(samples, average, mean),

0 commit comments

Comments
 (0)