Skip to content

Commit 39d00d6

Browse files
committed
numbers: Drop boxed numbers support, simplify .identify() checks, clarify round-trip test (ref #771)
1 parent eb5cb5b commit 39d00d6

7 files changed

Lines changed: 77 additions & 31 deletions

File tree

‎src/tag/scalar/float_core.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,20 @@ const floatCoreTag = defineScalarTag('tag:yaml.org,2002:float', {
5050
// ('.inf'/'.nan' start with '.').
5151
implicitFirstChars: ['-', '+', '.', ...'0123456789'],
5252
resolve: resolveYamlFloat,
53-
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
54-
// Also claim integer-valued numbers that stringify in exponential notation
55-
// (>= 1e21), since their `!!int` text would be invalid; `represent` emits a
56-
// valid float form for them.
57-
(object % 1 !== 0 || Object.is(object, -0) || /e/i.test(object.toString(10))),
53+
identify: (object) =>
54+
// No ancient boxed numbers support
55+
typeof object === 'number' &&
56+
(
57+
// We land here all numbers, not handled (declined) by !!int `.identify`
58+
// The same condition as for !!int, but reversed.
59+
60+
// Filter out integers...
61+
!Number.isInteger(object) ||
62+
// but allow negative zero
63+
Object.is(object, -0) ||
64+
// and integers with exponential form
65+
object.toString(10).indexOf('e') >= 0
66+
),
5867
represent: representYamlFloat
5968
})
6069

‎src/tag/scalar/float_json.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,20 @@ const floatJsonTag = defineScalarTag('tag:yaml.org,2002:float', {
5656
// Superset of source.charAt(0) over all matched inputs: optional '-' or digit.
5757
implicitFirstChars: ['-', ...'0123456789'],
5858
resolve: resolveYamlFloat,
59-
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
60-
// Also claim integer-valued numbers that stringify in exponential notation
61-
// (>= 1e21), since their `!!int` text would be invalid; `represent` emits a
62-
// valid float form for them.
63-
(object % 1 !== 0 || Object.is(object, -0) || /e/i.test(object.toString(10))),
59+
identify: (object) =>
60+
// No ancient boxed numbers support
61+
typeof object === 'number' &&
62+
(
63+
// We land here all numbers, not handled (declined) by !!int `.identify`
64+
// The same condition as for !!int, but reversed.
65+
66+
// Filter out integers...
67+
!Number.isInteger(object) ||
68+
// but allow negative zero
69+
Object.is(object, -0) ||
70+
// and integers with exponential form
71+
object.toString(10).indexOf('e') >= 0
72+
),
6473
represent: representYamlFloat
6574
})
6675

‎src/tag/scalar/float_yaml11.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,20 @@ const floatYaml11Tag = defineScalarTag('tag:yaml.org,2002:float', {
5757
// ('.inf'/'.nan' start with '.').
5858
implicitFirstChars: ['-', '+', '.', ...'0123456789'],
5959
resolve: resolveYamlFloat,
60-
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
61-
// Also claim integer-valued numbers that stringify in exponential notation
62-
// (>= 1e21), since their `!!int` text would be invalid; `represent` emits a
63-
// valid float form for them.
64-
(object % 1 !== 0 || Object.is(object, -0) || /e/i.test(object.toString(10))),
60+
identify: (object) =>
61+
// No ancient boxed numbers support
62+
typeof object === 'number' &&
63+
(
64+
// We land here all numbers, not handled (declined) by !!int `.identify`
65+
// The same condition as for !!int, but reversed.
66+
67+
// Filter out integers...
68+
!Number.isInteger(object) ||
69+
// but allow negative zero
70+
Object.is(object, -0) ||
71+
// and integers with exponential form
72+
object.toString(10).indexOf('e') >= 0
73+
),
6574
represent: representYamlFloat
6675
})
6776

‎src/tag/scalar/int_core.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ const intCoreTag = defineScalarTag('tag:yaml.org,2002:int', {
5353
// Superset of source.charAt(0) over all matched inputs: optional sign + decimal digit.
5454
implicitFirstChars: ['-', '+', ...'0123456789'],
5555
resolve: resolveYamlInteger,
56-
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
57-
// Large integers (>= 1e21) stringify in exponential notation, which is not
58-
// valid `!!int` text. Reject them here so they fall through to the float tag.
59-
(object % 1 === 0 && !Object.is(object, -0) && !/e/i.test(object.toString(10))),
56+
identify: (object) =>
57+
// No ancient boxed numbers support
58+
Number.isInteger(object) &&
59+
// Negative zero => !!float
60+
!Object.is(object, -0) &&
61+
// Exponential form => !!float, round-trip for !!int 1e21 will be broken
62+
object.toString(10).indexOf('e') < 0,
6063
represent: (object: number) => object.toString(10)
6164
})
6265

‎src/tag/scalar/int_json.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ const intJsonTag = defineScalarTag('tag:yaml.org,2002:int', {
4848
// Superset of source.charAt(0) over all matched inputs: optional '-' or digit.
4949
implicitFirstChars: ['-', ...'0123456789'],
5050
resolve: resolveYamlInteger,
51-
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
52-
// Large integers (>= 1e21) stringify in exponential notation, which is not
53-
// valid `!!int` text. Reject them here so they fall through to the float tag.
54-
(object % 1 === 0 && !Object.is(object, -0) && !/e/i.test(object.toString(10))),
51+
identify: (object) =>
52+
// No ancient boxed numbers support
53+
Number.isInteger(object) &&
54+
// Negative zero => !!float
55+
!Object.is(object, -0) &&
56+
// Exponential form => !!float, round-trip for !!int 1e21 will be broken
57+
object.toString(10).indexOf('e') < 0,
5558
represent: (object: number) => object.toString(10)
5659
})
5760

‎src/tag/scalar/int_yaml11.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ const intYaml11Tag = defineScalarTag('tag:yaml.org,2002:int', {
4747
// Superset of source.charAt(0) over all matched inputs: optional sign + decimal digit.
4848
implicitFirstChars: ['-', '+', ...'0123456789'],
4949
resolve: resolveYamlInteger,
50-
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
51-
// Large integers (>= 1e21) stringify in exponential notation, which is not
52-
// valid `!!int` text. Reject them here so they fall through to the float tag.
53-
(object % 1 === 0 && !Object.is(object, -0) && !/e/i.test(object.toString(10))),
50+
identify: (object) =>
51+
// No ancient boxed numbers support
52+
Number.isInteger(object) &&
53+
// Negative zero => !!float
54+
!Object.is(object, -0) &&
55+
// Exponential form => !!float, round-trip for !!int 1e21 will be broken
56+
object.toString(10).indexOf('e') < 0,
5457
represent: (object: number) => object.toString(10)
5558
})
5659

‎test/core/tags/int.test.mjs‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ describe('tags/int', () => {
4343
// Integers at or above 1e21 stringify in exponential notation
4444
// ('1e+21'), which is not valid `!!int` text. They must round-trip
4545
// through the float tag rather than being dumped as `!!int '1e+21'`.
46-
const large = [1e21, 1.5e21, -1e21, 1e100, Number.MAX_VALUE]
4746

48-
for (const value of large) {
49-
assert.strictEqual(load(dump(value, { schema }), { schema }), value)
50-
}
47+
// Should round-trip as !!int
48+
assert.strictEqual(dump(1e20, { schema }), '100000000000000000000\n')
49+
assert.strictEqual(load(dump(1e20, { schema }), { schema }), 1e20)
50+
assert.strictEqual(dump(-1e20, { schema }), '-100000000000000000000\n')
51+
assert.strictEqual(load(dump(-1e20, { schema }), { schema }), -1e20)
52+
53+
// Should round-trip as !!float
54+
assert.strictEqual(dump(1e21, { schema }), '1.e+21\n')
55+
assert.strictEqual(load(dump(1e21, { schema }), { schema }), 1e21)
56+
assert.strictEqual(dump(-1e21, { schema }), '-1.e+21\n')
57+
assert.strictEqual(load(dump(-1e21, { schema }), { schema }), -1e21)
58+
59+
assert.strictEqual(load(dump(Number.MAX_VALUE, { schema }), { schema }), Number.MAX_VALUE)
60+
assert.strictEqual(load(dump(-Number.MAX_VALUE, { schema }), { schema }), -Number.MAX_VALUE)
5161
})
5262

5363
it(`${name} fail explicit tag`, () => {

0 commit comments

Comments
 (0)