GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util: support BigInt in util.format · nodejs/node@384221e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • util.md
  • lib
    • util.js
  • test/parallel
    • test-util-format.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/util.md‎

Copy file name to clipboardExpand all lines: doc/api/util.md
+2-2Lines changed: 2 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ Each placeholder token is replaced with the converted value from the
198198
corresponding argument. Supported placeholders are:
199199

200200
* `%s` - `String`.
201-
* `%d` - `Number` (integer or floating point value).
202-
* `%i` - Integer.
201+
* `%d` - `Number` (integer or floating point value) or `BigInt`.
202+
* `%i` - Integer or `BigInt`.
203203
* `%f` - Floating point value.
204204
* `%j` - JSON. Replaced with the string `'[Circular]'` if the argument
205205
contains circular references.
Collapse file

‎lib/util.js‎

Copy file name to clipboardExpand all lines: lib/util.js
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ function formatWithOptions(inspectOptions, f) {
103103
tempStr = tryStringify(arguments[a++]);
104104
break;
105105
case 100: // 'd'
106-
tempStr = `${Number(arguments[a++])}`;
106+
const tempNum = arguments[a++];
107+
// eslint-disable-next-line valid-typeof
108+
if (typeof tempNum === 'bigint') {
109+
tempStr = `${tempNum}n`;
110+
} else {
111+
tempStr = `${Number(tempNum)}`;
112+
}
107113
break;
108114
case 79: // 'O'
109115
tempStr = inspect(arguments[a++], inspectOptions);
@@ -119,7 +125,13 @@ function formatWithOptions(inspectOptions, f) {
119125
break;
120126
}
121127
case 105: // 'i'
122-
tempStr = `${parseInt(arguments[a++])}`;
128+
const tempInteger = arguments[a++];
129+
// eslint-disable-next-line valid-typeof
130+
if (typeof tempInteger === 'bigint') {
131+
tempStr = `${tempInteger}n`;
132+
} else {
133+
tempStr = `${parseInt(tempInteger)}`;
134+
}
123135
break;
124136
case 102: // 'f'
125137
tempStr = `${parseFloat(arguments[a++])}`;
Collapse file

‎test/parallel/test-util-format.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-format.js
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ assert.strictEqual(util.format('%d', -0.5), '-0.5');
6868
assert.strictEqual(util.format('%d', ''), '0');
6969
assert.strictEqual(util.format('%d %d', 42, 43), '42 43');
7070
assert.strictEqual(util.format('%d %d', 42), '42 %d');
71+
assert.strictEqual(
72+
util.format('%d', 1180591620717411303424),
73+
'1.1805916207174113e+21'
74+
);
75+
assert.strictEqual(
76+
util.format('%d', 1180591620717411303424n),
77+
'1180591620717411303424n'
78+
);
79+
assert.strictEqual(
80+
util.format('%d %d', 1180591620717411303424n, 12345678901234567890123n),
81+
'1180591620717411303424n 12345678901234567890123n'
82+
);
7183

7284
// Integer format specifier
7385
assert.strictEqual(util.format('%i'), '%i');
@@ -80,6 +92,28 @@ assert.strictEqual(util.format('%i', -0.5), '0');
8092
assert.strictEqual(util.format('%i', ''), 'NaN');
8193
assert.strictEqual(util.format('%i %i', 42, 43), '42 43');
8294
assert.strictEqual(util.format('%i %i', 42), '42 %i');
95+
assert.strictEqual(
96+
util.format('%i', 1180591620717411303424),
97+
'1'
98+
);
99+
assert.strictEqual(
100+
util.format('%i', 1180591620717411303424n),
101+
'1180591620717411303424n'
102+
);
103+
assert.strictEqual(
104+
util.format('%i %i', 1180591620717411303424n, 12345678901234567890123n),
105+
'1180591620717411303424n 12345678901234567890123n'
106+
);
107+
108+
assert.strictEqual(
109+
util.format('%d %i', 1180591620717411303424n, 12345678901234567890123n),
110+
'1180591620717411303424n 12345678901234567890123n'
111+
);
112+
113+
assert.strictEqual(
114+
util.format('%i %d', 1180591620717411303424n, 12345678901234567890123n),
115+
'1180591620717411303424n 12345678901234567890123n'
116+
);
83117

84118
// Float format specifier
85119
assert.strictEqual(util.format('%f'), '%f');

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.