Skip to content

Commit

Permalink
Add internal Type helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 30, 2014
1 parent ebfa2e3 commit 98d0a7b
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@
var ArrayIterator; // make our implementation private

var Symbol = globals.Symbol || {};
var isSymbol = function (sym) {
/*jshint notypeof: true */
return typeof globals.Symbol === 'function' && typeof sym === 'symbol';
/*jshint notypeof: false */
var Type = {
string: function (x) { return _toString(x) === '[object String]'; },
regex: function (x) { return _toString(x) === '[object RegExp]'; },
symbol: function (x) {
/*jshint notypeof: true */
return typeof globals.Symbol === 'function' && typeof x === 'symbol';

This comment has been minimized.

Copy link
@sebmck

sebmck Dec 14, 2014

This breaks symbol polyfills such as es6-symbol.

This comment has been minimized.

Copy link
@ljharb

ljharb Dec 14, 2014

Author Collaborator

Symbols can't be polyfilled, since it'd require the ability to store objects as keys. I appreciate what that is trying to do, but if the es6-shim included it, it would be in the sham and not the shim, since it comes with caveats.

In other words, anything that fails this test is not a true polyfill, and is supposed to break.

/*jshint notypeof: false */
}
};

var defineProperty = function (object, name, value, force) {
Expand Down Expand Up @@ -129,7 +133,7 @@
// work properly with each other, even though we don't have full Iterator
// support. That is, `Array.from(map.keys())` will work, but we don't
// pretend to export a "real" Iterator interface.
var $iterator$ = isSymbol(Symbol.iterator) ? Symbol.iterator : '_es6-shim iterator_';
var $iterator$ = Type.symbol(Symbol.iterator) ? Symbol.iterator : '_es6-shim iterator_';
// Firefox ships a partial implementation using the name @@iterator.
// https://bugzilla.mozilla.org/show_bug.cgi?id=907077#c14
// So use that name if we detect it.
Expand All @@ -141,7 +145,7 @@
var o = {};
o[$iterator$] = impl;
defineProperties(prototype, o);
if (!prototype[$iterator$] && isSymbol($iterator$)) {
if (!prototype[$iterator$] && Type.symbol($iterator$)) {
// implementations are buggy when $iterator$ is a Symbol
prototype[$iterator$] = impl;
}
Expand Down Expand Up @@ -506,7 +510,7 @@

startsWith: function (searchStr) {
var thisStr = String(ES.CheckObjectCoercible(this));
if (_toString(searchStr) === '[object RegExp]') {
if (Type.regex(searchStr)) {
throw new TypeError('Cannot call method "startsWith" with a regex');
}
searchStr = String(searchStr);
Expand All @@ -517,7 +521,7 @@

endsWith: function (searchStr) {
var thisStr = String(ES.CheckObjectCoercible(this));
if (_toString(searchStr) === '[object RegExp]') {
if (Type.regex(searchStr)) {
throw new TypeError('Cannot call method "endsWith" with a regex');
}
searchStr = String(searchStr);
Expand Down Expand Up @@ -824,7 +828,7 @@
defineProperties(Array.prototype, {
values: Array.prototype[$iterator$]
});
if (isSymbol(Symbol.unscopables)) {
if (Type.symbol(Symbol.unscopables)) {
Array.prototype[Symbol.unscopables].values = true;
}
}
Expand Down

0 comments on commit 98d0a7b

Please sign in to comment.