(v0: any, units: Unit[], decimal: number)
| 278 | |
| 279 | // v0: string or number |
| 280 | export const formatUnit = (v0: any, units: Unit[], decimal: number) => { |
| 281 | if (units.length == 1 && units[0].operator == "x" && units[0].rhs == 0) { |
| 282 | // string format unit |
| 283 | const v = replaceWithVariables(units[0].unit, { |
| 284 | [VariableCurrentValue]: v0 |
| 285 | }) |
| 286 | return v |
| 287 | } |
| 288 | |
| 289 | if (units.length > 0 && units[0].operator == "=") { |
| 290 | // enum unit |
| 291 | for (var i = 0; i < units.length; i++) { |
| 292 | const unit = units[i] |
| 293 | if (unit.rhs == v0) { |
| 294 | return replaceWithVariables(unit.unit, { |
| 295 | [VariableCurrentValue]: v0 |
| 296 | }) |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | if (!isNumber(v0)) { |
| 301 | if (units.length == 0) { |
| 302 | return v0 |
| 303 | } else { |
| 304 | return v0 + units[0].unit |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | let v = v0 |
| 309 | if (v0 < 0) { |
| 310 | v = -1 * v0 |
| 311 | } |
| 312 | |
| 313 | if (isEmpty(units)) { |
| 314 | return v0 < 0 ? round(v, decimal) * -1 : round(v, decimal) |
| 315 | } |
| 316 | |
| 317 | if (v == 0) { |
| 318 | return v |
| 319 | } |
| 320 | |
| 321 | let index = 0; |
| 322 | let min; |
| 323 | // we need to find the min calc value that is greater than 1 |
| 324 | for (var i = 0; i < units.length; i++) { |
| 325 | const unit = units[i] |
| 326 | const res = calcValue(v, unit) |
| 327 | if (res >= 1) { |
| 328 | if (!min) { |
| 329 | min = res |
| 330 | index = i |
| 331 | continue |
| 332 | } |
| 333 | if (res < min) { |
| 334 | min = res |
| 335 | index = i |
| 336 | continue |
| 337 | } |
no test coverage detected