Skip to content

Commit d24575f

Browse files
authored
Remove es-abstract dependency. (#56)
1 parent b573654 commit d24575f

File tree

3 files changed

+79
-427
lines changed

3 files changed

+79
-427
lines changed

lib/ecmascript.ts

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const MathSign = Math.sign;
88
const MathTrunc = Math.trunc;
99
const NumberIsNaN = Number.isNaN;
1010
const NumberIsFinite = Number.isFinite;
11+
const NumberCtor = Number;
1112
const NumberMaxSafeInteger = Number.MAX_SAFE_INTEGER;
1213
const ObjectAssign = Object.assign;
1314
const ObjectCreate = Object.create;
@@ -17,13 +18,6 @@ const ReflectApply = Reflect.apply;
1718

1819
import { DEBUG } from './debug';
1920
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';
2721

2822
import { GetIntrinsic } from './intrinsicclass';
2923
import {
@@ -69,6 +63,53 @@ const YEAR_MIN = -271821;
6963
const YEAR_MAX = 275760;
7064
const BEFORE_FIRST_DST = bigInt(-388152).multiply(1e13); // 1847-01-01T00:00:00Z
7165

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+
72113
const ToIntegerThrowOnInfinity = (value) => {
73114
const integer = ES.ToInteger(value);
74115
if (!NumberIsFinite(integer)) {
@@ -77,7 +118,7 @@ const ToIntegerThrowOnInfinity = (value) => {
77118
return integer;
78119
};
79120

80-
const ToPositiveInteger = (value, property) => {
121+
const ToPositiveInteger = (value, property?: string) => {
81122
value = ToInteger(value);
82123
if (!NumberIsFinite(value)) {
83124
throw new RangeError('infinity is out of range');
@@ -154,9 +195,7 @@ import * as PARSE from './regex';
154195
const ES2020 = {
155196
IsInteger,
156197
ToInteger,
157-
ToLength,
158198
ToNumber,
159-
ToPrimitive,
160199
ToString,
161200
Type
162201
};
@@ -4121,7 +4160,13 @@ export const ES = ObjectAssign({}, ES2020, {
41214160
return arg;
41224161
}
41234162

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+
}
41254170
switch (typeof prim) {
41264171
case 'undefined':
41274172
case 'object':

0 commit comments

Comments
 (0)