GeezerVison is showing github.com — util: support BigInt in util.format · nodejs/node@384221e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -198,8 +198,8 @@ Each placeholder token is replaced with the converted value from the | 198 | 198 | corresponding argument. Supported placeholders are: | 199 | 199 |
| 200 | 200 | * `%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`. | 203 | 203 | * `%f` - Floating point value. | 204 | 204 | * `%j` - JSON. Replaced with the string `'[Circular]'` if the argument | 205 | 205 | contains circular references. | |
Collapse file Copy file name to clipboardExpand all lines: lib/util.js +14-2Lines changed: 14 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -103,7 +103,13 @@ function formatWithOptions(inspectOptions, f) { | 103 | 103 | tempStr = tryStringify(arguments[a++]); | 104 | 104 | break; | 105 | 105 | 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 | + } | 107 | 113 | break; | 108 | 114 | case 79: // 'O' | 109 | 115 | tempStr = inspect(arguments[a++], inspectOptions); | @@ -119,7 +125,13 @@ function formatWithOptions(inspectOptions, f) { | 119 | 125 | break; | 120 | 126 | } | 121 | 127 | 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 | + } | 123 | 135 | break; | 124 | 136 | case 102: // 'f' | 125 | 137 | tempStr = `${parseFloat(arguments[a++])}`; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-util-format.js +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -68,6 +68,18 @@ assert.strictEqual(util.format('%d', -0.5), '-0.5'); | 68 | 68 | assert.strictEqual(util.format('%d', ''), '0'); | 69 | 69 | assert.strictEqual(util.format('%d %d', 42, 43), '42 43'); | 70 | 70 | 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 | +); | 71 | 83 |
| 72 | 84 | // Integer format specifier | 73 | 85 | assert.strictEqual(util.format('%i'), '%i'); | @@ -80,6 +92,28 @@ assert.strictEqual(util.format('%i', -0.5), '0'); | 80 | 92 | assert.strictEqual(util.format('%i', ''), 'NaN'); | 81 | 93 | assert.strictEqual(util.format('%i %i', 42, 43), '42 43'); | 82 | 94 | 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 | +); | 83 | 117 |
| 84 | 118 | // Float format specifier | 85 | 119 | assert.strictEqual(util.format('%f'), '%f'); | |
|