Skip to content

Commit

Permalink
fix export logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Dec 10, 2022
1 parent c18eea3 commit 23776a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
49 changes: 26 additions & 23 deletions packages/core-js/modules/es.number.constructor.js
Expand Up @@ -3,9 +3,9 @@ var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var DESCRIPTORS = require('../internals/descriptors');
var global = require('../internals/global');
var path = require('../internals/path');
var uncurryThis = require('../internals/function-uncurry-this');
var isForced = require('../internals/is-forced');
var defineBuiltIn = require('../internals/define-built-in');
var hasOwn = require('../internals/has-own-property');
var inheritIfRequired = require('../internals/inherit-if-required');
var isPrototypeOf = require('../internals/object-is-prototype-of');
Expand All @@ -20,6 +20,7 @@ var trim = require('../internals/string-trim').trim;

var NUMBER = 'Number';
var NativeNumber = global[NUMBER];
var PureNumberNamespace = path[NUMBER];
var NumberPrototype = NativeNumber.prototype;
var TypeError = global.TypeError;
var arraySlice = uncurryThis(''.slice);
Expand Down Expand Up @@ -62,36 +63,38 @@ var toNumber = function (argument) {
} return +it;
};

var FORCED = isForced(NUMBER, !NativeNumber(' 0o1') || !NativeNumber('0b1') || NativeNumber('+0x1'));

// `Number` constructor
// https://tc39.es/ecma262/#sec-number-constructor
var shouldForce = !NativeNumber(' 0o1') || !NativeNumber('0b1') || NativeNumber('+0x1');
if (IS_PURE || isForced(NUMBER, shouldForce)) {
var NumberWrapper = function Number(value) {
var n = arguments.length < 1 ? 0 : NativeNumber(toNumeric(value));
var dummy = this;
// check on 1..constructor(foo) case
return isPrototypeOf(NumberPrototype, dummy) && fails(function () { thisNumberValue(dummy); })
? inheritIfRequired(Object(n), dummy, NumberWrapper) : n;
};
for (var keys = DESCRIPTORS ? getOwnPropertyNames(NativeNumber) : (
var NumberWrapper = function Number(value) {
var n = arguments.length < 1 ? 0 : NativeNumber(toNumeric(value));
var dummy = this;
// check on 1..constructor(foo) case
return isPrototypeOf(NumberPrototype, dummy) && fails(function () { thisNumberValue(dummy); })
? inheritIfRequired(Object(n), dummy, NumberWrapper) : n;
};
NumberWrapper.prototype = NumberPrototype;
if (FORCED && !IS_PURE) NumberPrototype.constructor = NumberWrapper;

$({ global: true, constructor: true, wrap: true, forced: FORCED }, {
Number: NumberWrapper
});

var copyStaticProperties = function (target, source) {
for (var keys = DESCRIPTORS ? getOwnPropertyNames(source) : (
// ES3:
'MAX_VALUE,MIN_VALUE,NaN,NEGATIVE_INFINITY,POSITIVE_INFINITY,' +
// ES2015 (in case, if modules with ES2015 Number statics required before):
'EPSILON,MAX_SAFE_INTEGER,MIN_SAFE_INTEGER,isFinite,isInteger,isNaN,isSafeInteger,parseFloat,parseInt,' +
// ESNext
'fromString,range'
).split(','), j = 0, key; keys.length > j; j++) {
if (hasOwn(NativeNumber, key = keys[j]) && !hasOwn(NumberWrapper, key)) {
defineProperty(NumberWrapper, key, getOwnPropertyDescriptor(NativeNumber, key));
if (hasOwn(source, key = keys[j]) && !hasOwn(target, key)) {
defineProperty(target, key, getOwnPropertyDescriptor(source, key));
}
}
NumberWrapper.prototype = NumberPrototype;
if (IS_PURE) {
var source = {};
source[NUMBER] = NumberWrapper;
$({ global: true, forced: shouldForce }, source);
} else {
NumberPrototype.constructor = NumberWrapper;
defineBuiltIn(global, NUMBER, NumberWrapper, { constructor: true });
}
}
};

if (IS_PURE && PureNumberNamespace) copyStaticProperties(path[NUMBER], PureNumberNamespace);
if (FORCED || IS_PURE) copyStaticProperties(path[NUMBER], NativeNumber);
11 changes: 1 addition & 10 deletions tests/unit-pure/es.number.constructor.js
@@ -1,5 +1,4 @@
import { nativeSubclass } from '../helpers/helpers';
import { globalThis } from 'core-js-pure';
import globalThis from 'core-js-pure/es/global-this';
import create from 'core-js-pure/es/object/create';
import Number from 'core-js-pure/es/number';
import Symbol from 'core-js-pure/es/symbol';
Expand All @@ -20,8 +19,6 @@ function getCheck(assert) {
QUnit.test('Number constructor: regression', assert => {
const check = getCheck(assert);
assert.isFunction(Number);
assert.arity(Number, 1);
assert.name(Number, 'Number');
assert.same(Number.prototype.constructor, NativeNumber);
assert.same(1.0.constructor, NativeNumber);
const constants = ['MAX_VALUE', 'MIN_VALUE', 'NaN', 'NEGATIVE_INFINITY', 'POSITIVE_INFINITY'];
Expand Down Expand Up @@ -181,12 +178,6 @@ QUnit.test('Number constructor: regression', assert => {
check(`${ whitespaces }0X42`, 66);
check(`0X42${ whitespaces }`, 66);
check(`${ whitespaces }0X42${ whitespaces }`, 66);
if (nativeSubclass) {
const Subclass = nativeSubclass(Number);
assert.true(new Subclass() instanceof Subclass, 'correct subclassing with native classes #1');
assert.true(new Subclass() instanceof Number, 'correct subclassing with native classes #2');
assert.same(new Subclass(1).toFixed(2), '1.00', 'correct subclassing with native classes #3');
}
});

QUnit.test('Number constructor: binary', assert => {
Expand Down

0 comments on commit 23776a6

Please sign in to comment.