MCPcopy Create free account
hub / github.com/josdejong/mathjs / _format

Function _format

src/utils/string.js:63–112  ·  view source on GitHub ↗
(value, options)

Source from the content-addressed store, hash-verified

61}
62
63function _format (value, options) {
64 if (typeof value === 'number') {
65 return formatNumber(value, options)
66 }
67
68 if (isBigNumber(value)) {
69 return formatBigNumber(value, options)
70 }
71
72 // note: we use unsafe duck-typing here to check for Fractions, this is
73 // ok here since we're only invoking toString or concatenating its values
74 if (looksLikeFraction(value)) {
75 if (!options || options.fraction !== 'decimal') {
76 // output as ratio, like '1/3'
77 return `${value.s * value.n}/${value.d}`
78 } else {
79 // output as decimal, like '0.(3)'
80 return value.toString()
81 }
82 }
83
84 if (Array.isArray(value)) {
85 return formatArray(value, options)
86 }
87
88 if (isString(value)) {
89 return stringify(value)
90 }
91
92 if (typeof value === 'function') {
93 return value.syntax ? String(value.syntax) : 'function'
94 }
95
96 if (value && typeof value === 'object') {
97 if (typeof value.format === 'function') {
98 return value.format(options)
99 } else if (value && value.toString(options) !== {}.toString()) {
100 // this object has a non-native toString method, use that one
101 return value.toString(options)
102 } else {
103 const entries = Object.keys(value).map(key => {
104 return stringify(key) + ': ' + format(value[key], options)
105 })
106
107 return '{' + entries.join(', ') + '}'
108 }
109 }
110
111 return String(value)
112}
113
114/**
115 * Stringify a value into a string enclosed in double quotes.

Callers 2

formatFunction · 0.85
formatFunction · 0.85

Calls 10

isBigNumberFunction · 0.90
isStringFunction · 0.90
formatFunction · 0.90
looksLikeFractionFunction · 0.85
formatArrayFunction · 0.85
stringifyFunction · 0.85
toStringMethod · 0.65
formatMethod · 0.65
mapMethod · 0.65
keysMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…