Skip to content

Commit 422802c

Browse files
committed
apply suggestions from review
1 parent f02e40c commit 422802c

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

packages/core-js/internals/structured-clone.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
/* eslint-disable es/no-map -- safe */
2-
/* eslint-disable es/no-set -- safe */
3-
'use const';
4-
var isSymbol = require('./is-symbol');
5-
var toObject = require('./to-object');
6-
var getOwnPropertyNames = require('./object-get-own-property-names');
7-
var classof = require('./classof');
1+
'use strict';
2+
var isSymbol = require('../internals/is-symbol');
3+
var getOwnPropertyNames = require('../internals/object-get-own-property-names');
4+
var classof = require('../internals/classof');
5+
var getBuiltin = require('../internals/get-built-in');
6+
var isObject = require('../internals/is-object');
7+
var has = require('../internals/has');
8+
9+
var Set = getBuiltin('Set');
10+
var Map = getBuiltin('Map');
811

912
function createDataCloneError(message) {
1013
if (typeof DOMException === 'function') {
@@ -21,18 +24,19 @@ function createDataCloneError(message) {
2124
*/
2225
module.exports = function structuredCloneInternal(weakmap, value) {
2326
if (isSymbol(value)) throw createDataCloneError('Symbols are not cloneable');
24-
if (typeof value !== 'function' && typeof value !== 'object') return value;
27+
if (!isObject(value)) return value;
2528
if (value === null) return null;
2629
if (weakmap.has(value)) return weakmap.get(value); // effectively preserves circular references
2730

2831
var cloned, i, deep;
32+
var type = classof(value);
2933

30-
switch (classof(value)) {
34+
switch (type) {
3135
case 'Boolean':
3236
case 'BigInt':
3337
case 'Number':
3438
case 'String':
35-
cloned = toObject(value.valueOf());
39+
cloned = Object(value.valueOf());
3640
break;
3741
case 'Date':
3842
cloned = new Date(value.valueOf());
@@ -67,12 +71,12 @@ module.exports = function structuredCloneInternal(weakmap, value) {
6771
deep = true;
6872
break;
6973
default:
70-
throw createDataCloneError('Uncloneable type: ' + classof(value));
74+
throw createDataCloneError('Uncloneable type: ' + type);
7175
}
7276

7377
weakmap.set(value, cloned);
7478

75-
if (deep) switch (classof(value)) {
79+
if (deep) switch (type) {
7680
case 'Map':
7781
value.forEach(function (v, k) {
7882
cloned.set(structuredCloneInternal(weakmap, k), structuredCloneInternal(weakmap, v));
@@ -86,14 +90,13 @@ module.exports = function structuredCloneInternal(weakmap, value) {
8690
case 'Error':
8791
// Attempt to clone the stack.
8892
if (
89-
!Object.prototype.hasOwnProperty.call(value, 'stack') && // Chrome, Safari
90-
!Object.prototype.hasOwnProperty.call(Error.prototype, 'stack') // Firefox
93+
!has(value, 'stack') && // Chrome, Safari
94+
!has(Error.prototype, 'stack') // Firefox
9195
) break;
9296
try {
9397
cloned.stack = structuredCloneInternal(weakmap, value.stack);
94-
} catch (error) {
95-
if (classof(error) === 'TypeError') return cloned; // Stack cloning not avaliable.
96-
throw error; // Unexpected error while cloning.
98+
} catch (_) {
99+
return cloned; // Stack cloning not avaliable.
97100
}
98101
break;
99102
case 'Array':

packages/core-js/web/structured-clone.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
1+
require('../modules/es.weak-map');
2+
require('../modules/es.map');
3+
require('../modules/es.set');
24
require('../modules/web.structured-clone');
35
var path = require('../internals/path');
46

tests/compat/tests.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,9 @@ GLOBAL.tests = {
15461546
'web.queue-microtask': function () {
15471547
return Object.getOwnPropertyDescriptor(GLOBAL, 'queueMicrotask').value;
15481548
},
1549+
'web.structured-clone': function () {
1550+
return typeof structuredClone === 'function';
1551+
},
15491552
'web.timers': function () {
15501553
return !/MSIE .\./.test(USERAGENT);
15511554
},
@@ -1554,7 +1557,4 @@ GLOBAL.tests = {
15541557
return URL.prototype.toJSON;
15551558
}],
15561559
'web.url-search-params': URL_AND_URL_SEARCH_PARAMS_SUPPORT,
1557-
'web.structured-clone': function () {
1558-
return typeof structuredClone === 'function';
1559-
}
15601560
};

0 commit comments

Comments
 (0)