From 39dc947409c7df19ac4f9502df8cb0d4a1b2829a Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Sat, 27 Jan 2018 13:57:53 -0600 Subject: [PATCH] util: add bigint formatting to util.inspect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/18412 Reviewed-By: Anna Henningsen Reviewed-By: Joyee Cheung Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Luigi Pinca Reviewed-By: Evan Lucas Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Сковорода Никита Андреевич Reviewed-By: Tobias Nießen --- lib/util.js | 4 ++++ test/parallel/test-util-inspect-bigint.js | 10 ++++++++++ 2 files changed, 14 insertions(+) create mode 100644 test/parallel/test-util-inspect-bigint.js diff --git a/lib/util.js b/lib/util.js index 0f0ed408ba4cc1..4525792b2ec4c4 100644 --- a/lib/util.js +++ b/lib/util.js @@ -342,6 +342,7 @@ inspect.colors = Object.assign(Object.create(null), { inspect.styles = Object.assign(Object.create(null), { 'special': 'cyan', 'number': 'yellow', + 'bigint': 'yellow', 'boolean': 'yellow', 'undefined': 'grey', 'null': 'bold', @@ -650,6 +651,9 @@ function formatPrimitive(fn, value, ctx) { } if (typeof value === 'number') return formatNumber(fn, value); + // eslint-disable-next-line valid-typeof + if (typeof value === 'bigint') + return fn(`${value}n`, 'bigint'); if (typeof value === 'boolean') return fn(`${value}`, 'boolean'); if (typeof value === 'undefined') diff --git a/test/parallel/test-util-inspect-bigint.js b/test/parallel/test-util-inspect-bigint.js new file mode 100644 index 00000000000000..cb50c1f6982460 --- /dev/null +++ b/test/parallel/test-util-inspect-bigint.js @@ -0,0 +1,10 @@ +'use strict'; + +// Flags: --harmony-bigint + +require('../common'); +const assert = require('assert'); + +const { inspect } = require('util'); + +assert.strictEqual(inspect(1n), '1n');