symbol.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. define([], function() {
  2. /*!
  3. * Symbol-ES6 v0.1.2
  4. * ES6 Symbol polyfill in pure ES5.
  5. *
  6. * OL MODIFICATIONS:
  7. * Added isImplemented function to conditionally polyfill only if Symbol isn't
  8. * supported. Taken from https://github.com/medikoo/es6-symbol
  9. *
  10. * @license Copyright (c) 2017-2018 Rousan Ali, MIT License
  11. *
  12. * Codebase: https://github.com/rousan/symbol-es6
  13. * Date: 28th Jan, 2018
  14. */
  15. (function (global, factory) {
  16. "use strict";
  17. if (typeof module === "object" && typeof module.exports === "object") {
  18. // For the environment like NodeJS, CommonJS etc where module or
  19. // module.exports objects are available
  20. module.exports = factory(global);
  21. } else {
  22. // For browser context, where global object is window
  23. factory(global);
  24. }
  25. /* window is for browser environment and global is for NodeJS environment */
  26. })(typeof window !== "undefined" ? window : global, function (global) {
  27. "use strict";
  28. function isImplemented() {
  29. var symbol;
  30. if (typeof Symbol !== 'function') return false;
  31. symbol = Symbol('test symbol');
  32. try { String(symbol); } catch (e) { return false; }
  33. var validTypes = { object: true, symbol: true };
  34. // Return 'true' also for polyfills
  35. if (!validTypes[typeof Symbol.iterator]) return false;
  36. if (!validTypes[typeof Symbol.toPrimitive]) return false;
  37. if (!validTypes[typeof Symbol.toStringTag]) return false;
  38. return true;
  39. };
  40. if (!isImplemented()) {
  41. !(function (global, factory) {
  42. "use strict";
  43. "object" == typeof module && "object" == typeof module.exports
  44. ? (module.exports = factory(global))
  45. : factory(global);
  46. })("undefined" != typeof window ? window : global, function (global) {
  47. "use strict";
  48. var defineProperty = Object.defineProperty,
  49. defineProperties = Object.defineProperties,
  50. symbolHiddenCounter = 0,
  51. globalSymbolRegistry = [],
  52. slice = Array.prototype.slice,
  53. ES6 = "object" == typeof global.ES6 ? global.ES6 : (global.ES6 = {}),
  54. isArray = Array.isArray,
  55. objectToString = Object.prototype.toString,
  56. push = Array.prototype.push,
  57. emptyFunction = function () { },
  58. simpleFunction = function (arg) {
  59. return arg;
  60. },
  61. isCallable = function (fn) {
  62. return "function" == typeof fn;
  63. },
  64. Iterator = function () { },
  65. ArrayIterator = function (array, flag) {
  66. (this._array = array), (this._flag = flag), (this._nextIndex = 0);
  67. },
  68. StringIterator = function (string, flag) {
  69. (this._string = string), (this._flag = flag), (this._nextIndex = 0);
  70. },
  71. isObject = function (value) {
  72. return (
  73. null !== value &&
  74. ("object" == typeof value || "function" == typeof value)
  75. );
  76. },
  77. setupSymbolInternals = function (symbol, desc) {
  78. return (
  79. defineProperties(symbol, {
  80. _description: { value: desc },
  81. _isSymbol: { value: !0 },
  82. _id: { value: symbolHiddenCounter++ }
  83. }),
  84. symbol
  85. );
  86. },
  87. appendArray = function (array1, array2) {
  88. if (
  89. "number" == typeof array1.length &&
  90. array1.length >= 0 &&
  91. "number" == typeof array2.length &&
  92. array2.length >= 0
  93. ) {
  94. var length1 = Math.floor(array1.length),
  95. length2 = Math.floor(array2.length),
  96. i = 0;
  97. for (array1.length = length1 + length2; i < length2; ++i)
  98. array2.hasOwnProperty(i) && (array1[length1 + i] = array2[i]);
  99. }
  100. },
  101. simpleInheritance = function (child, parent) {
  102. if ("function" != typeof child || "function" != typeof parent)
  103. throw new TypeError("Child and Parent must be function type");
  104. (child.prototype = Object.create(parent.prototype)),
  105. (child.prototype.constructor = child);
  106. },
  107. Symbol = function Symbol(desc) {
  108. if (
  109. ((desc = void 0 === desc ? "" : String(desc)), this instanceof Symbol)
  110. )
  111. throw new TypeError("Symbol is not a constructor");
  112. return setupSymbolInternals(Object.create(Symbol.prototype), desc);
  113. };
  114. defineProperties(Symbol, {
  115. for: {
  116. value: function (key) {
  117. key = String(key);
  118. for (
  119. var record, registryLength = globalSymbolRegistry.length, i = 0;
  120. i < registryLength;
  121. ++i
  122. )
  123. if ((record = globalSymbolRegistry[i]).key === key)
  124. return record.symbol;
  125. return (
  126. (record = { key: key, symbol: Symbol(key) }),
  127. globalSymbolRegistry.push(record),
  128. record.symbol
  129. );
  130. },
  131. writable: !0,
  132. configurable: !0
  133. },
  134. keyFor: {
  135. value: function (symbol) {
  136. if (!ES6.isSymbol(symbol))
  137. throw new TypeError(String(symbol) + " is not a symbol");
  138. for (
  139. var record, registryLength = globalSymbolRegistry.length, i = 0;
  140. i < registryLength;
  141. ++i
  142. )
  143. if ((record = globalSymbolRegistry[i]).symbol === symbol)
  144. return record.key;
  145. },
  146. writable: !0,
  147. configurable: !0
  148. },
  149. hasInstance: { value: Symbol("Symbol.hasInstance") },
  150. isConcatSpreadable: { value: Symbol("Symbol.isConcatSpreadable") },
  151. iterator: { value: Symbol("Symbol.iterator") },
  152. toStringTag: { value: Symbol("Symbol.toStringTag") }
  153. }),
  154. (Symbol.prototype.toString = function () {
  155. return "@@_____" + this._id + "_____";
  156. }),
  157. (Symbol.prototype.valueOf = function () {
  158. return this;
  159. }),
  160. defineProperty(Iterator.prototype, Symbol.iterator.toString(), {
  161. value: function () {
  162. return this;
  163. },
  164. writable: !0,
  165. configurable: !0
  166. }),
  167. simpleInheritance(ArrayIterator, Iterator),
  168. simpleInheritance(StringIterator, Iterator),
  169. defineProperty(ArrayIterator.prototype, Symbol.toStringTag.toString(), {
  170. value: "Array Iterator",
  171. configurable: !0
  172. }),
  173. defineProperty(StringIterator.prototype, Symbol.toStringTag.toString(), {
  174. value: "String Iterator",
  175. configurable: !0
  176. }),
  177. (ArrayIterator.prototype.next = function () {
  178. if (!(this instanceof ArrayIterator))
  179. throw new TypeError(
  180. "Method Array Iterator.prototype.next called on incompatible receiver " +
  181. String(this)
  182. );
  183. var nextValue;
  184. return -1 === this._nextIndex
  185. ? { done: !0, value: void 0 }
  186. : "number" == typeof this._array.length &&
  187. this._array.length >= 0 &&
  188. this._nextIndex < Math.floor(this._array.length)
  189. ? (1 === this._flag
  190. ? (nextValue = [this._nextIndex, this._array[this._nextIndex]])
  191. : 2 === this._flag
  192. ? (nextValue = this._array[this._nextIndex])
  193. : 3 === this._flag && (nextValue = this._nextIndex),
  194. this._nextIndex++ ,
  195. { done: !1, value: nextValue })
  196. : ((this._nextIndex = -1), { done: !0, value: void 0 });
  197. }),
  198. (StringIterator.prototype.next = function () {
  199. if (!(this instanceof StringIterator))
  200. throw new TypeError(
  201. "Method String Iterator.prototype.next called on incompatible receiver " +
  202. String(this)
  203. );
  204. var nextValue,
  205. stringObject = new String(this._string);
  206. return -1 === this._nextIndex
  207. ? { done: !0, value: void 0 }
  208. : this._nextIndex < stringObject.length
  209. ? ((nextValue = stringObject[this._nextIndex]),
  210. this._nextIndex++ ,
  211. { done: !1, value: nextValue })
  212. : ((this._nextIndex = -1), { done: !0, value: void 0 });
  213. });
  214. var SpreadOperatorImpl = function (target, thisArg) {
  215. (this._target = target), (this._values = []), (this._thisArg = thisArg);
  216. };
  217. (SpreadOperatorImpl.prototype.spread = function () {
  218. var self = this;
  219. return (
  220. slice.call(arguments).forEach(function (iterable) {
  221. ES6.forOf(iterable, function (value) {
  222. self._values.push(value);
  223. });
  224. }),
  225. self
  226. );
  227. }),
  228. (SpreadOperatorImpl.prototype.add = function () {
  229. var self = this;
  230. return (
  231. slice.call(arguments).forEach(function (value) {
  232. self._values.push(value);
  233. }),
  234. self
  235. );
  236. }),
  237. (SpreadOperatorImpl.prototype.call = function (thisArg) {
  238. if ("function" != typeof this._target)
  239. throw new TypeError("Target is not a function");
  240. return (
  241. (thisArg = arguments.length <= 0 ? this._thisArg : thisArg),
  242. this._target.apply(thisArg, this._values)
  243. );
  244. }),
  245. (SpreadOperatorImpl.prototype.new = function () {
  246. if ("function" != typeof this._target)
  247. throw new TypeError("Target is not a constructor");
  248. var temp, returnValue;
  249. return (
  250. (temp = Object.create(this._target.prototype)),
  251. (returnValue = this._target.apply(temp, this._values)),
  252. isObject(returnValue) ? returnValue : temp
  253. );
  254. }),
  255. (SpreadOperatorImpl.prototype.array = function () {
  256. if (!isArray(this._target)) throw new TypeError("Target is not a array");
  257. return push.apply(this._target, this._values), this._target;
  258. });
  259. return (
  260. defineProperties(ES6, {
  261. isSymbol: {
  262. value: function (symbol) {
  263. return (
  264. symbol instanceof Symbol &&
  265. (function (symbol) {
  266. return (
  267. !0 === symbol._isSymbol &&
  268. "number" == typeof symbol._id &&
  269. "string" == typeof symbol._description
  270. );
  271. })(symbol)
  272. );
  273. },
  274. writable: !0,
  275. configurable: !0
  276. },
  277. instanceOf: {
  278. value: function (object, constructor) {
  279. if (!isObject(constructor))
  280. throw new TypeError(
  281. "Right-hand side of 'instanceof' is not an object"
  282. );
  283. var hasInstanceSymbolProp = constructor[Symbol.hasInstance];
  284. if (void 0 === hasInstanceSymbolProp)
  285. return object instanceof constructor;
  286. if ("function" != typeof hasInstanceSymbolProp)
  287. throw new TypeError(
  288. typeof hasInstanceSymbolProp + " is not a function"
  289. );
  290. return hasInstanceSymbolProp.call(constructor, object);
  291. },
  292. writable: !0,
  293. configurable: !0
  294. },
  295. forOf: {
  296. value: function (iterable, callback, thisArg) {
  297. if (
  298. ((callback =
  299. "function" != typeof callback ? emptyFunction : callback),
  300. "function" != typeof iterable[Symbol.iterator])
  301. )
  302. throw new TypeError("Iterable[Symbol.iterator] is not a function");
  303. var iterationResult,
  304. iterator = iterable[Symbol.iterator]();
  305. if ("function" != typeof iterator.next)
  306. throw new TypeError(".iterator.next is not a function");
  307. for (; ;) {
  308. if (
  309. ((iterationResult = iterator.next()), !isObject(iterationResult))
  310. )
  311. throw new TypeError(
  312. "Iterator result " + iterationResult + " is not an object"
  313. );
  314. if (iterationResult.done) break;
  315. callback.call(thisArg, iterationResult.value);
  316. }
  317. },
  318. writable: !0,
  319. configurable: !0
  320. },
  321. spreadOperator: {
  322. value: function (target, thisArg) {
  323. if ("function" != typeof target && !isArray(target))
  324. throw new TypeError(
  325. "Spread operator only supports on array and function objects at this moment"
  326. );
  327. return new SpreadOperatorImpl(target, thisArg);
  328. },
  329. writable: !0,
  330. configurable: !0
  331. }
  332. }),
  333. defineProperty(global, "Symbol", {
  334. value: Symbol,
  335. writable: !0,
  336. configurable: !0
  337. }),
  338. defineProperty(Function.prototype, Symbol.hasInstance.toString(), {
  339. value: function (instance) {
  340. return "function" == typeof this && instance instanceof this;
  341. }
  342. }),
  343. defineProperty(Array.prototype, "concat", {
  344. value: function () {
  345. if (void 0 === this || null === this)
  346. throw new TypeError(
  347. "Array.prototype.concat called on null or undefined"
  348. );
  349. var self = Object(this),
  350. targets = slice.call(arguments),
  351. outputs = [];
  352. return (
  353. targets.unshift(self),
  354. targets.forEach(function (target) {
  355. isObject(target)
  356. ? void 0 !== target[Symbol.isConcatSpreadable]
  357. ? target[Symbol.isConcatSpreadable]
  358. ? appendArray(outputs, target)
  359. : outputs.push(target)
  360. : isArray(target)
  361. ? appendArray(outputs, target)
  362. : outputs.push(target)
  363. : outputs.push(target);
  364. }),
  365. outputs
  366. );
  367. },
  368. writable: !0,
  369. configurable: !0
  370. }),
  371. defineProperty(Object.prototype, "toString", {
  372. value: function () {
  373. return void 0 === this || null === this
  374. ? objectToString.call(this)
  375. : "string" == typeof this[Symbol.toStringTag]
  376. ? "[object " + this[Symbol.toStringTag] + "]"
  377. : objectToString.call(this);
  378. },
  379. writable: !0,
  380. configurable: !0
  381. }),
  382. defineProperty(Array.prototype, Symbol.iterator.toString(), {
  383. value: function () {
  384. if (void 0 === this || null === this)
  385. throw new TypeError("Cannot convert undefined or null to object");
  386. var self = Object(this);
  387. return new ArrayIterator(self, 2);
  388. },
  389. writable: !0,
  390. configurable: !0
  391. }),
  392. defineProperty(Array, "from", {
  393. value: function (arrayLike, mapFn, thisArg) {
  394. var constructor,
  395. length,
  396. outputs,
  397. i = 0;
  398. if (
  399. ((constructor = isCallable(this) ? this : Array),
  400. void 0 === arrayLike || null === arrayLike)
  401. )
  402. throw new TypeError("Cannot convert undefined or null to object");
  403. if (((arrayLike = Object(arrayLike)), void 0 === mapFn))
  404. mapFn = simpleFunction;
  405. else if (!isCallable(mapFn))
  406. throw new TypeError(mapFn + " is not a function");
  407. if (void 0 === arrayLike[Symbol.iterator]) {
  408. if (!("number" == typeof arrayLike.length && arrayLike.length >= 0))
  409. return ((outputs = new constructor(0)).length = 0), outputs;
  410. for (
  411. length = Math.floor(arrayLike.length),
  412. (outputs = new constructor(length)).length = length;
  413. i < length;
  414. ++i
  415. )
  416. outputs[i] = mapFn.call(thisArg, arrayLike[i]);
  417. } else
  418. ((outputs = new constructor()).length = 0),
  419. ES6.forOf(arrayLike, function (value) {
  420. outputs.length++ ,
  421. (outputs[outputs.length - 1] = mapFn.call(thisArg, value));
  422. });
  423. return outputs;
  424. },
  425. writable: !0,
  426. configurable: !0
  427. }),
  428. defineProperty(Array.prototype, "entries", {
  429. value: function () {
  430. if (void 0 === this || null === this)
  431. throw new TypeError("Cannot convert undefined or null to object");
  432. var self = Object(this);
  433. return new ArrayIterator(self, 1);
  434. },
  435. writable: !0,
  436. configurable: !0
  437. }),
  438. defineProperty(Array.prototype, "keys", {
  439. value: function () {
  440. if (void 0 === this || null === this)
  441. throw new TypeError("Cannot convert undefined or null to object");
  442. var self = Object(this);
  443. return new ArrayIterator(self, 3);
  444. },
  445. writable: !0,
  446. configurable: !0
  447. }),
  448. defineProperty(String.prototype, Symbol.iterator.toString(), {
  449. value: function () {
  450. if (void 0 === this || null === this)
  451. throw new TypeError(
  452. "String.prototype[Symbol.iterator] called on null or undefined"
  453. );
  454. return new StringIterator(String(this), 0);
  455. },
  456. writable: !0,
  457. configurable: !0
  458. }),
  459. ES6
  460. );
  461. });
  462. }
  463. });
  464. });