@@ -8,6 +8,7 @@ const MathSign = Math.sign;
8
8
const MathTrunc = Math . trunc ;
9
9
const NumberIsNaN = Number . isNaN ;
10
10
const NumberIsFinite = Number . isFinite ;
11
+ const NumberCtor = Number ;
11
12
const NumberMaxSafeInteger = Number . MAX_SAFE_INTEGER ;
12
13
const ObjectAssign = Object . assign ;
13
14
const ObjectCreate = Object . create ;
@@ -17,13 +18,6 @@ const ReflectApply = Reflect.apply;
17
18
18
19
import { DEBUG } from './debug' ;
19
20
import bigInt from 'big-integer' ;
20
- import IsInteger from 'es-abstract/2020/IsInteger.js' ;
21
- import ToInteger from 'es-abstract/2020/ToInteger.js' ;
22
- import ToLength from 'es-abstract/2020/ToLength.js' ;
23
- import ToNumber from 'es-abstract/2020/ToNumber.js' ;
24
- import ToPrimitive from 'es-abstract/2020/ToPrimitive.js' ;
25
- import ToString from 'es-abstract/2020/ToString.js' ;
26
- import Type from 'es-abstract/2020/Type.js' ;
27
21
28
22
import { GetIntrinsic } from './intrinsicclass' ;
29
23
import {
@@ -69,6 +63,53 @@ const YEAR_MIN = -271821;
69
63
const YEAR_MAX = 275760 ;
70
64
const BEFORE_FIRST_DST = bigInt ( - 388152 ) . multiply ( 1e13 ) ; // 1847-01-01T00:00:00Z
71
65
66
+ function IsInteger ( value : unknown ) : value is number {
67
+ if ( typeof value !== 'number' || ! NumberIsFinite ( value ) ) return false ;
68
+ const abs = MathAbs ( value ) ;
69
+ return MathFloor ( abs ) === abs ;
70
+ }
71
+
72
+ function Type ( value : unknown ) : string {
73
+ if ( value === null ) return 'Null' ;
74
+ switch ( typeof value ) {
75
+ case 'symbol' :
76
+ return 'Symbol' ;
77
+ case 'bigint' :
78
+ return 'BigInt' ;
79
+ case 'undefined' :
80
+ return 'undefined' ;
81
+ case 'function' :
82
+ case 'object' :
83
+ return 'Object' ;
84
+ case 'number' :
85
+ return 'Number' ;
86
+ case 'boolean' :
87
+ return 'Boolean' ;
88
+ case 'string' :
89
+ return 'String' ;
90
+ }
91
+ }
92
+
93
+ function ToNumber ( value : unknown ) : number {
94
+ if ( typeof value === 'bigint' ) throw new TypeError ( 'Cannot convert BigInt to number' ) ;
95
+ return NumberCtor ( value ) ;
96
+ }
97
+
98
+ function ToInteger ( value : unknown ) : number {
99
+ const num = ToNumber ( value ) ;
100
+ if ( NumberIsNaN ( num ) ) return 0 ;
101
+ const integer = MathTrunc ( num ) ;
102
+ if ( num === 0 ) return 0 ;
103
+ return integer ;
104
+ }
105
+
106
+ function ToString ( value : unknown ) : string {
107
+ if ( typeof value === 'symbol' ) {
108
+ throw new TypeError ( 'Cannot convert a Symbol value to a String' ) ;
109
+ }
110
+ return String ( value ) ;
111
+ }
112
+
72
113
const ToIntegerThrowOnInfinity = ( value ) => {
73
114
const integer = ES . ToInteger ( value ) ;
74
115
if ( ! NumberIsFinite ( integer ) ) {
@@ -77,7 +118,7 @@ const ToIntegerThrowOnInfinity = (value) => {
77
118
return integer ;
78
119
} ;
79
120
80
- const ToPositiveInteger = ( value , property ) => {
121
+ const ToPositiveInteger = ( value , property ?: string ) => {
81
122
value = ToInteger ( value ) ;
82
123
if ( ! NumberIsFinite ( value ) ) {
83
124
throw new RangeError ( 'infinity is out of range' ) ;
@@ -154,9 +195,7 @@ import * as PARSE from './regex';
154
195
const ES2020 = {
155
196
IsInteger,
156
197
ToInteger,
157
- ToLength,
158
198
ToNumber,
159
- ToPrimitive,
160
199
ToString,
161
200
Type
162
201
} ;
@@ -4121,7 +4160,13 @@ export const ES = ObjectAssign({}, ES2020, {
4121
4160
return arg ;
4122
4161
}
4123
4162
4124
- const prim = ES . ToPrimitive ( arg , Number ) ;
4163
+ let prim = arg ;
4164
+ if ( typeof arg === 'object' ) {
4165
+ const toPrimFn = arg [ Symbol . toPrimitive ] ;
4166
+ if ( toPrimFn && typeof toPrimFn === 'function' ) {
4167
+ prim = ReflectApply ( toPrimFn , arg , [ 'number' ] ) ;
4168
+ }
4169
+ }
4125
4170
switch ( typeof prim ) {
4126
4171
case 'undefined' :
4127
4172
case 'object' :
0 commit comments