Skip to content

Commit

Permalink
add own toString method to instances with @@toStringTag in non-global…
Browse files Browse the repository at this point in the history
… version, fixes #199
  • Loading branch information
zloirock committed Mar 27, 2018
1 parent af131e8 commit a1839d3
Show file tree
Hide file tree
Showing 26 changed files with 63 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/core-js-pure/override/internals/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = function (NAME, wrapper, common, IS_MAP, IS_WEAK) {
});
}

setToStringTag(C, NAME);
setToStringTag(C, NAME, false, true);

exported[NAME] = C;
$export({ global: true, wrap: true, forced: true }, exported);
Expand Down
16 changes: 16 additions & 0 deletions packages/core-js-pure/override/internals/set-to-string-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var defineProperty = require('../internals/object-define-property').f;
var hide = require('../internals/hide');
var has = require('../internals/has');
var TO_STRING_TAG = require('../internals/well-known-symbol')('toStringTag');
var toString = require('../internals/object-to-string');
var METHOD_REQUIRED = toString !== ({}).toString;

module.exports = function (it, TAG, STATIC, SET_METHOD) {
if (it) {
var target = STATIC ? it : it.prototype;
if (!has(target, TO_STRING_TAG)) {
defineProperty(target, TO_STRING_TAG, { configurable: true, value: TAG });
}
if (SET_METHOD && METHOD_REQUIRED) hide(target, 'toString', toString);
}
};
2 changes: 1 addition & 1 deletion packages/core-js/internals/create-iterator-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ require('../internals/hide')(IteratorPrototype, ITERATOR, function () { return t

module.exports = function (Constructor, NAME, next) {
Constructor.prototype = create(IteratorPrototype, { next: createPropertyDescriptor(1, next) });
setToStringTag(Constructor, NAME + ' Iterator');
setToStringTag(Constructor, NAME + ' Iterator', false, true);
return Constructor;
};
2 changes: 1 addition & 1 deletion packages/core-js/internals/define-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = function (Base, NAME, Constructor, next, DEFAULT, IS_SET, FORCE
IteratorPrototype = getPrototypeOf($anyNative.call(new Base()));
if (IteratorPrototype !== Object.prototype && IteratorPrototype.next) {
// Set @@toStringTag to native iterators
setToStringTag(IteratorPrototype, TAG, true);
setToStringTag(IteratorPrototype, TAG, true, true);
// fix for some old engines
if (!IS_PURE && !has(IteratorPrototype, ITERATOR)) hide(IteratorPrototype, ITERATOR, returnThis);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/core-js/internals/object-to-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
var classof = require('../internals/classof');
var TO_STRING_TAG = require('../internals/well-known-symbol')('toStringTag');
var test = {};

test[TO_STRING_TAG] = 'z';

// `Object.prototype.toString` method implementation
// https://tc39.github.io/ecma262/#sec-object.prototype.tostring
module.exports = String(test) !== '[object z]' ? function toString() {
return '[object ' + classof(this) + ']';
} : test.toString;
6 changes: 3 additions & 3 deletions packages/core-js/internals/set-to-string-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ var defineProperty = require('../internals/object-define-property').f;
var has = require('../internals/has');
var TO_STRING_TAG = require('../internals/well-known-symbol')('toStringTag');

module.exports = function (it, tag, stat) {
if (it && !has(it = stat ? it : it.prototype, TO_STRING_TAG)) {
defineProperty(it, TO_STRING_TAG, { configurable: true, value: tag });
module.exports = function (it, TAG, STATIC) {
if (it && !has(it = STATIC ? it : it.prototype, TO_STRING_TAG)) {
defineProperty(it, TO_STRING_TAG, { configurable: true, value: TAG });
}
};
12 changes: 3 additions & 9 deletions packages/core-js/modules/es.object.to-string.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
'use strict';
var classof = require('../internals/classof');
var TO_STRING_TAG = require('../internals/well-known-symbol')('toStringTag');
var test = {};

test[TO_STRING_TAG] = 'z';
var toString = require('../internals/object-to-string');

// `Object.prototype.toString` method
// https://tc39.github.io/ecma262/#sec-object.prototype.tostring
if (String(test) !== '[object z]') {
require('../internals/redefine')(Object.prototype, 'toString', function toString() {
return '[object ' + classof(this) + ']';
}, true);
if (toString !== ({}).toString) {
require('../internals/redefine')(Object.prototype, 'toString', toString, true);
}
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ if (!USE_NATIVE) {
}

$export({ global: true, wrap: true, forced: !USE_NATIVE }, { Promise: $Promise });
require('../internals/set-to-string-tag')($Promise, PROMISE);
require('../internals/set-to-string-tag')($Promise, PROMISE, false, true);
require('../internals/set-species')(PROMISE);
Wrapper = require('../internals/path')[PROMISE];

Expand Down
1 change: 1 addition & 0 deletions tests/pure/es.array.iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ QUnit.test('Array#@@iterator', assert => {
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'Array Iterator');
assert.strictEqual(String(iterator), '[object Array Iterator]');
assert.deepEqual(iterator.next(), {
value: 'q',
done: false,
Expand Down
2 changes: 2 additions & 0 deletions tests/pure/es.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ QUnit.test('Map & -0', assert => {

QUnit.test('Map#@@toStringTag', assert => {
assert.strictEqual(Map.prototype[Symbol.toStringTag], 'Map', 'Map::@@toStringTag is `Map`');
assert.strictEqual(String(new Map()), '[object Map]', 'correct stringification');
});

QUnit.test('Map Iterator', assert => {
Expand Down Expand Up @@ -382,6 +383,7 @@ QUnit.test('Map#@@iterator', assert => {
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'Map Iterator');
assert.strictEqual(String(iterator), '[object Map Iterator]');
assert.deepEqual(iterator.next(), {
value: ['a', 'q'],
done: false,
Expand Down
1 change: 1 addition & 0 deletions tests/pure/es.promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ QUnit.test('Promise#catch', assert => {

QUnit.test('Promise#@@toStringTag', assert => {
assert.ok(Promise.prototype[Symbol.toStringTag] === 'Promise', 'Promise::@@toStringTag is `Promise`');
assert.strictEqual(String(new Promise(() => { /* empty */ })), '[object Promise]', 'correct stringification');
});

QUnit.test('Promise.all', assert => {
Expand Down
2 changes: 2 additions & 0 deletions tests/pure/es.set.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ QUnit.test('Set & -0', assert => {

QUnit.test('Set#@@toStringTag', assert => {
assert.strictEqual(Set.prototype[Symbol.toStringTag], 'Set', 'Set::@@toStringTag is `Set`');
assert.strictEqual(String(new Set()), '[object Set]', 'correct stringification');
});

QUnit.test('Set Iterator', assert => {
Expand Down Expand Up @@ -376,6 +377,7 @@ QUnit.test('Set#@@iterator', assert => {
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'Set Iterator');
assert.strictEqual(String(iterator), '[object Set Iterator]');
assert.deepEqual(iterator.next(), {
value: 'q',
done: false,
Expand Down
1 change: 1 addition & 0 deletions tests/pure/es.string.iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ QUnit.test('String#@@iterator', assert => {
let iterator = getIterator('qwe');
assert.isIterator(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'String Iterator');
assert.strictEqual(String(iterator), '[object String Iterator]');
assert.deepEqual(iterator.next(), {
value: 'q',
done: false,
Expand Down
1 change: 1 addition & 0 deletions tests/pure/es.weak-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,5 @@ QUnit.test('WeakMap#set', assert => {

QUnit.test('WeakMap#@@toStringTag', assert => {
assert.strictEqual(WeakMap.prototype[Symbol.toStringTag], 'WeakMap', 'WeakMap::@@toStringTag is `WeakMap`');
assert.strictEqual(String(new WeakMap()), '[object WeakMap]', 'correct stringification');
});
1 change: 1 addition & 0 deletions tests/pure/es.weak-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ QUnit.test('WeakSet#has', assert => {

QUnit.test('WeakSet::@@toStringTag', assert => {
assert.strictEqual(WeakSet.prototype[Symbol.toStringTag], 'WeakSet', 'WeakSet::@@toStringTag is `WeakSet`');
assert.strictEqual(String(new WeakSet()), '[object WeakSet]', 'correct stringification');
});
1 change: 1 addition & 0 deletions tests/pure/esnext.string.code-points.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ QUnit.test('String#codePoints', assert => {
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'String Iterator');
assert.strictEqual(String(iterator), '[object String Iterator]');
assert.deepEqual(iterator.next(), {
value: 113,
done: false,
Expand Down
3 changes: 3 additions & 0 deletions tests/pure/esnext.string.match-all.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { STRICT } from '../helpers/constants';

import matchAll from 'core-js-pure/features/string/match-all';
import Symbol from 'core-js-pure/features/symbol';
import assign from 'core-js-pure/features/object/assign';

QUnit.test('String#matchAll', assert => {
Expand Down Expand Up @@ -41,6 +42,8 @@ QUnit.test('String#matchAll', assert => {
let iterator = matchAll('1111a2b3cccc', /(\d)(\D)/g);
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'RegExp String Iterator');
assert.strictEqual(String(iterator), '[object RegExp String Iterator]');
assert.deepEqual(iterator.next(), {
value: assign(['1a', '1', 'a'], {
input: '1111a2b3cccc',
Expand Down
1 change: 1 addition & 0 deletions tests/tests/es.array.iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ QUnit.test('Array#keys', assert => {
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'Array Iterator');
assert.strictEqual(String(iterator), '[object Array Iterator]');
assert.deepEqual(iterator.next(), {
value: 0,
done: false,
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/es.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ QUnit.test('Map & -0', assert => {

QUnit.test('Map#@@toStringTag', assert => {
assert.strictEqual(Map.prototype[Symbol.toStringTag], 'Map', 'Map::@@toStringTag is `Map`');
assert.strictEqual(String(new Map()), '[object Map]', 'correct stringification');
});

QUnit.test('Map Iterator', assert => {
Expand Down Expand Up @@ -438,6 +439,7 @@ QUnit.test('Map#@@iterator', assert => {
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'Map Iterator');
assert.strictEqual(String(iterator), '[object Map Iterator]');
assert.deepEqual(iterator.next(), {
value: ['a', 'q'],
done: false,
Expand Down
1 change: 1 addition & 0 deletions tests/tests/es.promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ QUnit.test('Promise#catch', assert => {

QUnit.test('Promise#@@toStringTag', assert => {
assert.ok(Promise.prototype[Symbol.toStringTag] === 'Promise', 'Promise::@@toStringTag is `Promise`');
assert.strictEqual(String(new Promise(() => { /* empty */ })), '[object Promise]', 'correct stringification');
});

QUnit.test('Promise.all', assert => {
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/es.set.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ QUnit.test('Set & -0', assert => {

QUnit.test('Set#@@toStringTag', assert => {
assert.strictEqual(Set.prototype[Symbol.toStringTag], 'Set', 'Set::@@toStringTag is `Set`');
assert.strictEqual(String(new Set()), '[object Set]', 'correct stringification');
});

QUnit.test('Set Iterator', assert => {
Expand Down Expand Up @@ -419,6 +420,7 @@ QUnit.test('Set#@@iterator', assert => {
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'Set Iterator');
assert.strictEqual(String(iterator), '[object Set Iterator]');
assert.deepEqual(iterator.next(), {
value: 'q',
done: false,
Expand Down
1 change: 1 addition & 0 deletions tests/tests/es.string.iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ QUnit.test('String#@@iterator', assert => {
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'String Iterator');
assert.strictEqual(String(iterator), '[object String Iterator]');
assert.deepEqual(iterator.next(), {
value: 'q',
done: false,
Expand Down
1 change: 1 addition & 0 deletions tests/tests/es.weak-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ QUnit.test('WeakMap#set', assert => {

QUnit.test('WeakMap#@@toStringTag', assert => {
assert.strictEqual(WeakMap.prototype[Symbol.toStringTag], 'WeakMap', 'WeakMap::@@toStringTag is `WeakMap`');
assert.strictEqual(String(new WeakMap()), '[object WeakMap]', 'correct stringification');
});
1 change: 1 addition & 0 deletions tests/tests/es.weak-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ QUnit.test('WeakSet#has', assert => {

QUnit.test('WeakSet::@@toStringTag', assert => {
assert.strictEqual(WeakSet.prototype[Symbol.toStringTag], 'WeakSet', 'WeakSet::@@toStringTag is `WeakSet`');
assert.strictEqual(String(new WeakSet()), '[object WeakSet]', 'correct stringification');
});
1 change: 1 addition & 0 deletions tests/tests/esnext.string.code-points.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ QUnit.test('String#codePoints', assert => {
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'String Iterator');
assert.strictEqual(String(iterator), '[object String Iterator]');
assert.deepEqual(iterator.next(), {
value: 113,
done: false,
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/esnext.string.match-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ QUnit.test('String#matchAll', assert => {
let iterator = '1111a2b3cccc'.matchAll(/(\d)(\D)/g);
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.strictEqual(iterator[Symbol.toStringTag], 'RegExp String Iterator');
assert.strictEqual(String(iterator), '[object RegExp String Iterator]');
assert.deepEqual(iterator.next(), {
value: assign(['1a', '1', 'a'], {
input: '1111a2b3cccc',
Expand Down

0 comments on commit a1839d3

Please sign in to comment.