MCPcopy
hub / github.com/josdejong/mathjs / formatBest

Function formatBest

src/type/unit/Unit.js:1195–1216  ·  view source on GitHub ↗

* Helper function to normalize a unit for conversion and formatting * @param {Unit} unit The unit to be normalized * @return {Object} Object with normalized unit and value * @private

(unit, options = {})

Source from the content-addressed store, hash-verified

1193 * @private
1194 */
1195 function formatBest (unit, options = {}) {
1196 // Simplfy the unit list, unless it is valueless or was created directly in the
1197 // constructor or as the result of to or toSI
1198 const simp = unit.skipAutomaticSimplification || unit.value === null
1199 ? unit.clone()
1200 : unit.simplify()
1201
1202 // Apply some custom logic for handling VA and VAR. The goal is to express the value of the unit as a real value, if possible. Otherwise, use a real-valued unit instead of a complex-valued one.
1203 handleVAandVARUnits(simp)
1204 // Now apply the best prefix
1205 // Units must have only one unit and not have the fixPrefix flag set
1206 applyBestPrefixIfNeeded(simp, options.offset)
1207
1208 const value = simp._denormalize(simp.value)
1209 const valueStr = (simp.value !== null) ? format(value, options || {}) : ''
1210 const unitStr = simp.formatUnits()
1211 return {
1212 simp,
1213 valueStr,
1214 unitStr
1215 }
1216 }
1217
1218 /**
1219 * Helper to handle VA and VAR units

Callers 1

Unit.jsFile · 0.85

Calls 6

handleVAandVARUnitsFunction · 0.85
applyBestPrefixIfNeededFunction · 0.85
formatUnitsMethod · 0.80
cloneMethod · 0.65
simplifyMethod · 0.65
formatFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…