Skip to content

Commit 3bb8f40

Browse files
committed
feat: write parsing for float values
1 parent c57a755 commit 3bb8f40

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/time.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ const timeRepo = (algos, args, runs, id) => {
8080
return { id, sortedTimes, args, runs }
8181
}
8282

83+
const MAX_ARRAY = 50
84+
const MAX_STRING = 50
85+
const MAX_DECIMALS = 3
86+
8387
/**
8488
* Builds a locale string with type information.
8589
*
@@ -89,23 +93,29 @@ const timeRepo = (algos, args, runs, id) => {
8993
const toLocaleString = (val) => {
9094
// Parse array
9195
if (Array.isArray(val)) {
92-
if (val.length > 50) {
96+
if (val.length > MAX_ARRAY) {
9397
return `[array of length ${val.length}]`
9498
} else {
9599
return `[${val.toLocaleString()}]`
96100
}
97101
}
98102

99-
// Parse string
100103
const valType = typeof val
104+
105+
// Parse string
101106
if (valType === 'string') {
102-
if (val.length > 50) {
107+
if (val.length > MAX_STRING) {
103108
return `'string of length ${val.length}'`
104109
} else {
105110
return `'${val.toLocaleString()}'`
106111
}
107112
}
108113

114+
// Parse number
115+
if (valType === 'number' && !Number.isInteger(val)) {
116+
return val.toFixed(MAX_DECIMALS).toLocaleString()
117+
}
118+
109119
return val.toLocaleString()
110120
}
111121

@@ -123,7 +133,7 @@ const parseResult = ({ id, sortedTimes, args, runs }) => {
123133
`${id}\nargument list: ${strArgs}\nnumber of runs: ${strRuns}\n\n`
124134

125135
for (let idx = 0; idx < sortedTimes.length; idx++) {
126-
const left = `${idx + 1}: ${sortedTimes[idx][1].toFixed(3)} ms - `
136+
const left = `${idx + 1}: ${toLocaleString(sortedTimes[idx][1])} ms - `
127137
const right = `${sortedTimes[idx][0]}\n`
128138
parsedResult += `${left}${right}`
129139
}

0 commit comments

Comments
 (0)