Skip to content

Commit

Permalink
fix bug where valueToString function would return undefined (JSON.str…
Browse files Browse the repository at this point in the history
…iingify(undefined) === undefined)
  • Loading branch information
trxcllnt committed Jan 9, 2019
1 parent 4b58bde commit 62578b9
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion js/src/util/pretty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@
// specific language governing permissions and limitations
// under the License.

/** @ignore */ const undf = void (0);

/** @ignore */
export function valueToString(x: any) {
return typeof x === 'string' ? `"${x}"` : ArrayBuffer.isView(x) ? `[${x}]` : JSON.stringify(x);
if (x === null) return 'null';
if (x === undf) return 'undefined';
if (typeof x === 'string') { return `"${x}"`; }
// If [Symbol.toPrimitive] is implemented (like in BN)
// use it instead of JSON.stringify(). This ensures we
// print BigInts, Decimals, and Binary in their native
// representation
if (typeof x[Symbol.toPrimitive] === 'function') {
return x[Symbol.toPrimitive]('string');
}
return ArrayBuffer.isView(x) ? `[${x}]` : JSON.stringify(x);
}

0 comments on commit 62578b9

Please sign in to comment.