(value, options)
| 61 | } |
| 62 | |
| 63 | function _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. |
no test coverage detected
searching dependent graphs…