| 1 |
|
| 2 |
// IE 11 CustomEvent polyfill |
| 3 |
|
| 4 |
(function () { |
| 5 |
if ( typeof window.CustomEvent === "function" ) return false; //If not IE |
| 6 |
|
| 7 |
function CustomEvent ( event, params ) { |
| 8 |
params = params || { bubbles: false, cancelable: false, detail: undefined }; |
| 9 |
var evt = document.createEvent( 'CustomEvent' ); |
| 10 |
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); |
| 11 |
return evt; |
| 12 |
} |
| 13 |
|
| 14 |
CustomEvent.prototype = window.Event.prototype; |
| 15 |
|
| 16 |
window.CustomEvent = CustomEvent; |
| 17 |
})(); |
| 18 |
|
| 19 |
// IE 11 EventTarget polyfill |
| 20 |
(function () { |
| 21 |
if (typeof window.EventTarget === "function") return false; // If EventTarget exists |
| 22 |
|
| 23 |
function EventTarget() { |
| 24 |
this._listeners = {}; |
| 25 |
} |
| 26 |
|
| 27 |
EventTarget.prototype.addEventListener = function(type, callback) { |
| 28 |
if (!(type in this._listeners)) { |
| 29 |
this._listeners[type] = []; |
| 30 |
} |
| 31 |
this._listeners[type].push(callback); |
| 32 |
}; |
| 33 |
|
| 34 |
EventTarget.prototype.removeEventListener = function(type, callback) { |
| 35 |
if (!(type in this._listeners)) { |
| 36 |
return; |
| 37 |
} |
| 38 |
var stack = this._listeners[type]; |
| 39 |
for (var i = 0, l = stack.length; i < l; i++) { |
| 40 |
if (stack[i] === callback) { |
| 41 |
stack.splice(i, 1); |
| 42 |
return; |
| 43 |
} |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
EventTarget.prototype.dispatchEvent = function(event) { |
| 48 |
if (!(event.type in this._listeners)) { |
| 49 |
return true; |
| 50 |
} |
| 51 |
var stack = this._listeners[event.type].slice(); |
| 52 |
|
| 53 |
for (var i = 0, l = stack.length; i < l; i++) { |
| 54 |
stack[i].call(this, event); |
| 55 |
} |
| 56 |
return !event.defaultPrevented; |
| 57 |
}; |
| 58 |
|
| 59 |
window.EventTarget = EventTarget; |
| 60 |
})(); |
| 61 |
|
| 62 |
// IE 11 Array.isArray polyfill (just in case) |
| 63 |
if (!Array.isArray) { |
| 64 |
Array.isArray = function(arg) { |
| 65 |
return Object.prototype.toString.call(arg) === '[object Array]'; |
| 66 |
}; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/*! Native Promise Only |
| 71 |
v0.8.1 (c) Kyle Simpson |
| 72 |
MIT License: http://getify.mit-license.org |
| 73 |
*/ |
| 74 |
|
| 75 |
(function UMD(name,context,definition){ |
| 76 |
// special form of UMD for polyfilling across environments |
| 77 |
context[name] = context[name] || definition(); |
| 78 |
if (typeof module != "undefined" && module.exports) { module.exports = context[name]; } |
| 79 |
else if (typeof define == "function" && define.amd) { define(function $AMD$(){ return context[name]; }); } |
| 80 |
})("Promise",typeof global != "undefined" ? global : this,function DEF(){ |
| 81 |
/*jshint validthis:true */ |
| 82 |
"use strict"; |
| 83 |
|
| 84 |
var builtInProp, cycle, scheduling_queue, |
| 85 |
ToString = Object.prototype.toString, |
| 86 |
timer = (typeof setImmediate != "undefined") ? |
| 87 |
function timer(fn) { return setImmediate(fn); } : |
| 88 |
setTimeout |
| 89 |
; |
| 90 |
|
| 91 |
// dammit, IE8. |
| 92 |
try { |
| 93 |
Object.defineProperty({},"x",{}); |
| 94 |
builtInProp = function builtInProp(obj,name,val,config) { |
| 95 |
return Object.defineProperty(obj,name,{ |
| 96 |
value: val, |
| 97 |
writable: true, |
| 98 |
configurable: config !== false |
| 99 |
}); |
| 100 |
}; |
| 101 |
} |
| 102 |
catch (err) { |
| 103 |
builtInProp = function builtInProp(obj,name,val) { |
| 104 |
obj[name] = val; |
| 105 |
return obj; |
| 106 |
}; |
| 107 |
} |
| 108 |
|
| 109 |
// Note: using a queue instead of array for efficiency |
| 110 |
scheduling_queue = (function Queue() { |
| 111 |
var first, last, item; |
| 112 |
|
| 113 |
function Item(fn,self) { |
| 114 |
this.fn = fn; |
| 115 |
this.self = self; |
| 116 |
this.next = void 0; |
| 117 |
} |
| 118 |
|
| 119 |
return { |
| 120 |
add: function add(fn,self) { |
| 121 |
item = new Item(fn,self); |
| 122 |
if (last) { |
| 123 |
last.next = item; |
| 124 |
} |
| 125 |
else { |
| 126 |
first = item; |
| 127 |
} |
| 128 |
last = item; |
| 129 |
item = void 0; |
| 130 |
}, |
| 131 |
drain: function drain() { |
| 132 |
var f = first; |
| 133 |
first = last = cycle = void 0; |
| 134 |
|
| 135 |
while (f) { |
| 136 |
f.fn.call(f.self); |
| 137 |
f = f.next; |
| 138 |
} |
| 139 |
} |
| 140 |
}; |
| 141 |
})(); |
| 142 |
|
| 143 |
function schedule(fn,self) { |
| 144 |
scheduling_queue.add(fn,self); |
| 145 |
if (!cycle) { |
| 146 |
cycle = timer(scheduling_queue.drain); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// promise duck typing |
| 151 |
function isThenable(o) { |
| 152 |
var _then, o_type = typeof o; |
| 153 |
|
| 154 |
if (o != null && |
| 155 |
( |
| 156 |
o_type == "object" || o_type == "function" |
| 157 |
) |
| 158 |
) { |
| 159 |
_then = o.then; |
| 160 |
} |
| 161 |
return typeof _then == "function" ? _then : false; |
| 162 |
} |
| 163 |
|
| 164 |
function notify() { |
| 165 |
for (var i=0; i<this.chain.length; i++) { |
| 166 |
notifyIsolated( |
| 167 |
this, |
| 168 |
(this.state === 1) ? this.chain[i].success : this.chain[i].failure, |
| 169 |
this.chain[i] |
| 170 |
); |
| 171 |
} |
| 172 |
this.chain.length = 0; |
| 173 |
} |
| 174 |
|
| 175 |
// NOTE: This is a separate function to isolate |
| 176 |
// the `try..catch` so that other code can be |
| 177 |
// optimized better |
| 178 |
function notifyIsolated(self,cb,chain) { |
| 179 |
var ret, _then; |
| 180 |
try { |
| 181 |
if (cb === false) { |
| 182 |
chain.reject(self.msg); |
| 183 |
} |
| 184 |
else { |
| 185 |
if (cb === true) { |
| 186 |
ret = self.msg; |
| 187 |
} |
| 188 |
else { |
| 189 |
ret = cb.call(void 0,self.msg); |
| 190 |
} |
| 191 |
|
| 192 |
if (ret === chain.promise) { |
| 193 |
chain.reject(TypeError("Promise-chain cycle")); |
| 194 |
} |
| 195 |
else if (_then = isThenable(ret)) { |
| 196 |
_then.call(ret,chain.resolve,chain.reject); |
| 197 |
} |
| 198 |
else { |
| 199 |
chain.resolve(ret); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
catch (err) { |
| 204 |
chain.reject(err); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
function resolve(msg) { |
| 209 |
var _then, self = this; |
| 210 |
|
| 211 |
// already triggered? |
| 212 |
if (self.triggered) { return; } |
| 213 |
|
| 214 |
self.triggered = true; |
| 215 |
|
| 216 |
// unwrap |
| 217 |
if (self.def) { |
| 218 |
self = self.def; |
| 219 |
} |
| 220 |
|
| 221 |
try { |
| 222 |
if (_then = isThenable(msg)) { |
| 223 |
schedule(function(){ |
| 224 |
var def_wrapper = new MakeDefWrapper(self); |
| 225 |
try { |
| 226 |
_then.call(msg, |
| 227 |
function $resolve$(){ resolve.apply(def_wrapper,arguments); }, |
| 228 |
function $reject$(){ reject.apply(def_wrapper,arguments); } |
| 229 |
); |
| 230 |
} |
| 231 |
catch (err) { |
| 232 |
reject.call(def_wrapper,err); |
| 233 |
} |
| 234 |
}) |
| 235 |
} |
| 236 |
else { |
| 237 |
self.msg = msg; |
| 238 |
self.state = 1; |
| 239 |
if (self.chain.length > 0) { |
| 240 |
schedule(notify,self); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
catch (err) { |
| 245 |
reject.call(new MakeDefWrapper(self),err); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
function reject(msg) { |
| 250 |
var self = this; |
| 251 |
|
| 252 |
// already triggered? |
| 253 |
if (self.triggered) { return; } |
| 254 |
|
| 255 |
self.triggered = true; |
| 256 |
|
| 257 |
// unwrap |
| 258 |
if (self.def) { |
| 259 |
self = self.def; |
| 260 |
} |
| 261 |
|
| 262 |
self.msg = msg; |
| 263 |
self.state = 2; |
| 264 |
if (self.chain.length > 0) { |
| 265 |
schedule(notify,self); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
function iteratePromises(Constructor,arr,resolver,rejecter) { |
| 270 |
for (var idx=0; idx<arr.length; idx++) { |
| 271 |
(function IIFE(idx){ |
| 272 |
Constructor.resolve(arr[idx]) |
| 273 |
.then( |
| 274 |
function $resolver$(msg){ |
| 275 |
resolver(idx,msg); |
| 276 |
}, |
| 277 |
rejecter |
| 278 |
); |
| 279 |
})(idx); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
function MakeDefWrapper(self) { |
| 284 |
this.def = self; |
| 285 |
this.triggered = false; |
| 286 |
} |
| 287 |
|
| 288 |
function MakeDef(self) { |
| 289 |
this.promise = self; |
| 290 |
this.state = 0; |
| 291 |
this.triggered = false; |
| 292 |
this.chain = []; |
| 293 |
this.msg = void 0; |
| 294 |
} |
| 295 |
|
| 296 |
function Promise(executor) { |
| 297 |
if (typeof executor != "function") { |
| 298 |
throw TypeError("Not a function"); |
| 299 |
} |
| 300 |
|
| 301 |
if (this.__NPO__ !== 0) { |
| 302 |
throw TypeError("Not a promise"); |
| 303 |
} |
| 304 |
|
| 305 |
// instance shadowing the inherited "brand" |
| 306 |
// to signal an already "initialized" promise |
| 307 |
this.__NPO__ = 1; |
| 308 |
|
| 309 |
var def = new MakeDef(this); |
| 310 |
|
| 311 |
this["then"] = function then(success,failure) { |
| 312 |
var o = { |
| 313 |
success: typeof success == "function" ? success : true, |
| 314 |
failure: typeof failure == "function" ? failure : false |
| 315 |
}; |
| 316 |
// Note: `then(..)` itself can be borrowed to be used against |
| 317 |
// a different promise constructor for making the chained promise, |
| 318 |
// by substituting a different `this` binding. |
| 319 |
o.promise = new this.constructor(function extractChain(resolve,reject) { |
| 320 |
if (typeof resolve != "function" || typeof reject != "function") { |
| 321 |
throw TypeError("Not a function"); |
| 322 |
} |
| 323 |
|
| 324 |
o.resolve = resolve; |
| 325 |
o.reject = reject; |
| 326 |
}); |
| 327 |
def.chain.push(o); |
| 328 |
|
| 329 |
if (def.state !== 0) { |
| 330 |
schedule(notify,def); |
| 331 |
} |
| 332 |
|
| 333 |
return o.promise; |
| 334 |
}; |
| 335 |
this["catch"] = function $catch$(failure) { |
| 336 |
return this.then(void 0,failure); |
| 337 |
}; |
| 338 |
|
| 339 |
try { |
| 340 |
executor.call( |
| 341 |
void 0, |
| 342 |
function publicResolve(msg){ |
| 343 |
resolve.call(def,msg); |
| 344 |
}, |
| 345 |
function publicReject(msg) { |
| 346 |
reject.call(def,msg); |
| 347 |
} |
| 348 |
); |
| 349 |
} |
| 350 |
catch (err) { |
| 351 |
reject.call(def,err); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
var PromisePrototype = builtInProp({},"constructor",Promise, |
| 356 |
/*configurable=*/false |
| 357 |
); |
| 358 |
|
| 359 |
// Note: Android 4 cannot use `Object.defineProperty(..)` here |
| 360 |
Promise.prototype = PromisePrototype; |
| 361 |
|
| 362 |
// built-in "brand" to signal an "uninitialized" promise |
| 363 |
builtInProp(PromisePrototype,"__NPO__",0, |
| 364 |
/*configurable=*/false |
| 365 |
); |
| 366 |
|
| 367 |
builtInProp(Promise,"resolve",function Promise$resolve(msg) { |
| 368 |
var Constructor = this; |
| 369 |
|
| 370 |
// spec mandated checks |
| 371 |
// note: best "isPromise" check that's practical for now |
| 372 |
if (msg && typeof msg == "object" && msg.__NPO__ === 1) { |
| 373 |
return msg; |
| 374 |
} |
| 375 |
|
| 376 |
return new Constructor(function executor(resolve,reject){ |
| 377 |
if (typeof resolve != "function" || typeof reject != "function") { |
| 378 |
throw TypeError("Not a function"); |
| 379 |
} |
| 380 |
|
| 381 |
resolve(msg); |
| 382 |
}); |
| 383 |
}); |
| 384 |
|
| 385 |
builtInProp(Promise,"reject",function Promise$reject(msg) { |
| 386 |
return new this(function executor(resolve,reject){ |
| 387 |
if (typeof resolve != "function" || typeof reject != "function") { |
| 388 |
throw TypeError("Not a function"); |
| 389 |
} |
| 390 |
|
| 391 |
reject(msg); |
| 392 |
}); |
| 393 |
}); |
| 394 |
|
| 395 |
builtInProp(Promise,"all",function Promise$all(arr) { |
| 396 |
var Constructor = this; |
| 397 |
|
| 398 |
// spec mandated checks |
| 399 |
if (ToString.call(arr) != "[object Array]") { |
| 400 |
return Constructor.reject(TypeError("Not an array")); |
| 401 |
} |
| 402 |
if (arr.length === 0) { |
| 403 |
return Constructor.resolve([]); |
| 404 |
} |
| 405 |
|
| 406 |
return new Constructor(function executor(resolve,reject){ |
| 407 |
if (typeof resolve != "function" || typeof reject != "function") { |
| 408 |
throw TypeError("Not a function"); |
| 409 |
} |
| 410 |
|
| 411 |
var len = arr.length, msgs = Array(len), count = 0; |
| 412 |
|
| 413 |
iteratePromises(Constructor,arr,function resolver(idx,msg) { |
| 414 |
msgs[idx] = msg; |
| 415 |
if (++count === len) { |
| 416 |
resolve(msgs); |
| 417 |
} |
| 418 |
},reject); |
| 419 |
}); |
| 420 |
}); |
| 421 |
|
| 422 |
builtInProp(Promise,"race",function Promise$race(arr) { |
| 423 |
var Constructor = this; |
| 424 |
|
| 425 |
// spec mandated checks |
| 426 |
if (ToString.call(arr) != "[object Array]") { |
| 427 |
return Constructor.reject(TypeError("Not an array")); |
| 428 |
} |
| 429 |
|
| 430 |
return new Constructor(function executor(resolve,reject){ |
| 431 |
if (typeof resolve != "function" || typeof reject != "function") { |
| 432 |
throw TypeError("Not a function"); |
| 433 |
} |
| 434 |
|
| 435 |
iteratePromises(Constructor,arr,function resolver(idx,msg){ |
| 436 |
resolve(msg); |
| 437 |
},reject); |
| 438 |
}); |
| 439 |
}); |
| 440 |
|
| 441 |
return Promise; |
| 442 |
}); |
| 443 |
|
| 444 |
|
| 445 |
/*! |
| 446 |
* Symbol-ES6 v0.1.2 |
| 447 |
* ES6 Symbol polyfill in pure ES5. |
| 448 |
* |
| 449 |
* @license Copyright (c) 2017-2018 Rousan Ali, MIT License |
| 450 |
* |
| 451 |
* Codebase: https://github.com/rousan/symbol-es6 |
| 452 |
* Date: 28th Jan, 2018 |
| 453 |
*/ |
| 454 |
|
| 455 |
(function (global, factory) { |
| 456 |
|
| 457 |
"use strict"; |
| 458 |
|
| 459 |
if (typeof module === "object" && typeof module.exports === "object") { |
| 460 |
// For the environment like NodeJS, CommonJS etc where module or |
| 461 |
// module.exports objects are available |
| 462 |
module.exports = factory(global); |
| 463 |
} else { |
| 464 |
// For browser context, where global object is window |
| 465 |
factory(global); |
| 466 |
} |
| 467 |
|
| 468 |
/* window is for browser environment and global is for NodeJS environment */ |
| 469 |
})(typeof window !== "undefined" ? window : global, function (global) { |
| 470 |
|
| 471 |
"use strict"; |
| 472 |
|
| 473 |
var defineProperty = Object.defineProperty; |
| 474 |
|
| 475 |
var defineProperties = Object.defineProperties; |
| 476 |
|
| 477 |
var symbolHiddenCounter = 0; |
| 478 |
|
| 479 |
var globalSymbolRegistry = []; |
| 480 |
|
| 481 |
var slice = Array.prototype.slice; |
| 482 |
|
| 483 |
var ES6 = typeof global.ES6 === "object" ? global.ES6 : (global.ES6 = {}); |
| 484 |
|
| 485 |
var isArray = Array.isArray; |
| 486 |
|
| 487 |
var objectToString = Object.prototype.toString; |
| 488 |
|
| 489 |
var push = Array.prototype.push; |
| 490 |
|
| 491 |
var emptyFunction = function () {}; |
| 492 |
|
| 493 |
var simpleFunction = function (arg) { |
| 494 |
return arg; |
| 495 |
}; |
| 496 |
|
| 497 |
var isCallable = function (fn) { |
| 498 |
return typeof fn === 'function'; |
| 499 |
}; |
| 500 |
|
| 501 |
var isConstructor = function (fn) { |
| 502 |
return isCallable(fn); |
| 503 |
}; |
| 504 |
|
| 505 |
var Iterator = function () {}; |
| 506 |
|
| 507 |
var ArrayIterator = function ArrayIterator(array, flag) { |
| 508 |
this._array = array; |
| 509 |
this._flag = flag; |
| 510 |
this._nextIndex = 0; |
| 511 |
}; |
| 512 |
|
| 513 |
var StringIterator = function StringIterator(string, flag) { |
| 514 |
this._string = string; |
| 515 |
this._flag = flag; |
| 516 |
this._nextIndex = 0; |
| 517 |
}; |
| 518 |
|
| 519 |
var isES6Running = function() { |
| 520 |
return false; /* Now 'false' for testing purpose */ |
| 521 |
}; |
| 522 |
|
| 523 |
var isObject = function (value) { |
| 524 |
return value !== null && (typeof value === "object" || typeof value === "function"); |
| 525 |
}; |
| 526 |
|
| 527 |
var es6FunctionPrototypeHasInstanceSymbol = function (instance) { |
| 528 |
if (typeof this !== "function") |
| 529 |
return false; |
| 530 |
return instance instanceof this; |
| 531 |
}; |
| 532 |
|
| 533 |
var es6InstanceOfOperator = function (object, constructor) { |
| 534 |
if (!isObject(constructor)) |
| 535 |
throw new TypeError("Right-hand side of 'instanceof' is not an object"); |
| 536 |
|
| 537 |
var hasInstanceSymbolProp = constructor[Symbol.hasInstance]; |
| 538 |
if (typeof hasInstanceSymbolProp === "undefined") { |
| 539 |
return object instanceof constructor; |
| 540 |
} else if(typeof hasInstanceSymbolProp !== "function") { |
| 541 |
throw new TypeError(typeof hasInstanceSymbolProp + " is not a function"); |
| 542 |
} else { |
| 543 |
return hasInstanceSymbolProp.call(constructor, object); |
| 544 |
} |
| 545 |
}; |
| 546 |
|
| 547 |
// Generates name for a symbol instance and this name will be used as |
| 548 |
// property key for property symbols internally. |
| 549 |
var generateSymbolName = function (id) { |
| 550 |
return "@@_____" + id + "_____"; |
| 551 |
}; |
| 552 |
|
| 553 |
// Generates id for next Symbol instance |
| 554 |
var getNextSymbolId = function () { |
| 555 |
return symbolHiddenCounter++; |
| 556 |
}; |
| 557 |
|
| 558 |
var setupSymbolInternals = function (symbol, desc) { |
| 559 |
defineProperties(symbol, { |
| 560 |
_description: { |
| 561 |
value: desc |
| 562 |
}, |
| 563 |
_isSymbol: { |
| 564 |
value: true |
| 565 |
}, |
| 566 |
_id: { |
| 567 |
value: getNextSymbolId() |
| 568 |
} |
| 569 |
}); |
| 570 |
return symbol; |
| 571 |
}; |
| 572 |
|
| 573 |
var checkSymbolInternals = function (symbol) { |
| 574 |
return symbol._isSymbol === true && typeof symbol._id === "number" && typeof symbol._description === "string"; |
| 575 |
}; |
| 576 |
|
| 577 |
var isSymbol = function (symbol) { |
| 578 |
return symbol instanceof Symbol && checkSymbolInternals(symbol); |
| 579 |
}; |
| 580 |
|
| 581 |
var symbolFor = function (key) { |
| 582 |
key = String(key); |
| 583 |
var registryLength = globalSymbolRegistry.length, |
| 584 |
record, |
| 585 |
i = 0; |
| 586 |
|
| 587 |
for(; i<registryLength; ++i) { |
| 588 |
record = globalSymbolRegistry[i]; |
| 589 |
if (record.key === key) |
| 590 |
return record.symbol; |
| 591 |
} |
| 592 |
|
| 593 |
record = { |
| 594 |
key: key, |
| 595 |
symbol: Symbol(key) |
| 596 |
}; |
| 597 |
globalSymbolRegistry.push(record); |
| 598 |
return record.symbol; |
| 599 |
}; |
| 600 |
|
| 601 |
var symbolKeyFor = function (symbol) { |
| 602 |
if (!ES6.isSymbol(symbol)) |
| 603 |
throw new TypeError(String(symbol) + " is not a symbol"); |
| 604 |
var registryLength = globalSymbolRegistry.length, |
| 605 |
record, |
| 606 |
i = 0; |
| 607 |
|
| 608 |
for(; i<registryLength; ++i) { |
| 609 |
record = globalSymbolRegistry[i]; |
| 610 |
if (record.symbol === symbol) |
| 611 |
return record.key; |
| 612 |
} |
| 613 |
}; |
| 614 |
|
| 615 |
/* It affects array1 and appends array2 at the end of array1 */ |
| 616 |
var appendArray = function (array1, array2) { |
| 617 |
// Returns immediately if these are not array or not array-like objects |
| 618 |
if (!(typeof array1.length === "number" && array1.length >= 0 && typeof array2.length === "number" && array2.length >= 0)) |
| 619 |
return; |
| 620 |
var length1 = Math.floor(array1.length), |
| 621 |
length2 = Math.floor(array2.length), |
| 622 |
i = 0; |
| 623 |
|
| 624 |
array1.length = length1 + length2; |
| 625 |
for (; i<length2; ++i) |
| 626 |
if (array2.hasOwnProperty(i)) |
| 627 |
array1[length1 + i] = array2[i]; |
| 628 |
}; |
| 629 |
|
| 630 |
var es6ObjectPrototypeToString = function toString() { |
| 631 |
if (this === undefined || this === null) |
| 632 |
return objectToString.call(this); |
| 633 |
// Add support for @@toStringTag symbol |
| 634 |
if (typeof this[Symbol.toStringTag] === "string") |
| 635 |
return "[object " + this[Symbol.toStringTag] + "]"; |
| 636 |
else |
| 637 |
return objectToString.call(this); |
| 638 |
}; |
| 639 |
|
| 640 |
var es6ArrayPrototypeConcat = function concat() { |
| 641 |
if (this === undefined || this === null) |
| 642 |
throw new TypeError("Array.prototype.concat called on null or undefined"); |
| 643 |
|
| 644 |
// Boxing 'this' value to wrapper object |
| 645 |
var self = Object(this), |
| 646 |
targets = slice.call(arguments), |
| 647 |
outputs = []; // Later it may affected by Symbol |
| 648 |
|
| 649 |
targets.unshift(self); |
| 650 |
|
| 651 |
targets.forEach(function (target) { |
| 652 |
// If target is primitive then just push |
| 653 |
if (!isObject(target)) |
| 654 |
outputs.push(target); |
| 655 |
// Here Symbol.isConcatSpreadable support is added |
| 656 |
else if (typeof target[Symbol.isConcatSpreadable] !== "undefined") { |
| 657 |
if (target[Symbol.isConcatSpreadable]) { |
| 658 |
appendArray(outputs, target); |
| 659 |
} else { |
| 660 |
outputs.push(target); |
| 661 |
} |
| 662 |
} else if (isArray(target)) { |
| 663 |
appendArray(outputs, target); |
| 664 |
} else { |
| 665 |
outputs.push(target); |
| 666 |
} |
| 667 |
}); |
| 668 |
return outputs; |
| 669 |
}; |
| 670 |
|
| 671 |
var es6ForOfLoop = function (iterable, callback, thisArg) { |
| 672 |
callback = typeof callback !== "function" ? emptyFunction : callback; |
| 673 |
if (typeof iterable[Symbol.iterator] !== "function") |
| 674 |
throw new TypeError("Iterable[Symbol.iterator] is not a function"); |
| 675 |
var iterator = iterable[Symbol.iterator](), |
| 676 |
iterationResult; |
| 677 |
if (typeof iterator.next !== "function") |
| 678 |
throw new TypeError(".iterator.next is not a function"); |
| 679 |
while (true) { |
| 680 |
iterationResult = iterator.next(); |
| 681 |
if (!isObject(iterationResult)) |
| 682 |
throw new TypeError("Iterator result " + iterationResult + " is not an object"); |
| 683 |
if (iterationResult.done) |
| 684 |
break; |
| 685 |
callback.call(thisArg, iterationResult.value); |
| 686 |
} |
| 687 |
}; |
| 688 |
|
| 689 |
// Provides simple inheritance functionality |
| 690 |
var simpleInheritance = function (child, parent) { |
| 691 |
if (typeof child !== "function" || typeof parent !== "function") |
| 692 |
throw new TypeError("Child and Parent must be function type"); |
| 693 |
|
| 694 |
child.prototype = Object.create(parent.prototype); |
| 695 |
child.prototype.constructor = child; |
| 696 |
}; |
| 697 |
|
| 698 |
// Behaves as Symbol function in ES6, take description and returns an unique object, |
| 699 |
// but in ES6 this function returns 'symbol' primitive typed value. |
| 700 |
// Its type is 'object' not 'symbol'. |
| 701 |
// There is no wrapping in this case i.e. Object(sym) = sym. |
| 702 |
var Symbol = function Symbol(desc) { |
| 703 |
desc = typeof desc === "undefined" ? "" : String(desc); |
| 704 |
|
| 705 |
if(this instanceof Symbol) |
| 706 |
throw new TypeError("Symbol is not a constructor"); |
| 707 |
|
| 708 |
return setupSymbolInternals(Object.create(Symbol.prototype), desc); |
| 709 |
}; |
| 710 |
|
| 711 |
defineProperties(Symbol, { |
| 712 |
|
| 713 |
"for": { |
| 714 |
value: symbolFor, |
| 715 |
writable: true, |
| 716 |
configurable: true |
| 717 |
}, |
| 718 |
|
| 719 |
"keyFor": { |
| 720 |
value: symbolKeyFor, |
| 721 |
writable: true, |
| 722 |
configurable: true |
| 723 |
}, |
| 724 |
|
| 725 |
"hasInstance": { |
| 726 |
value: Symbol("Symbol.hasInstance") |
| 727 |
}, |
| 728 |
|
| 729 |
"isConcatSpreadable": { |
| 730 |
value: Symbol("Symbol.isConcatSpreadable") |
| 731 |
}, |
| 732 |
|
| 733 |
"iterator": { |
| 734 |
value: Symbol("Symbol.iterator") |
| 735 |
}, |
| 736 |
|
| 737 |
"toStringTag": { |
| 738 |
value: Symbol("Symbol.toStringTag") |
| 739 |
} |
| 740 |
}); |
| 741 |
|
| 742 |
// In ES6, this function returns like 'Symbol(<desc>)', but in this case |
| 743 |
// this function returns the symbol's internal name to work properly. |
| 744 |
Symbol.prototype.toString = function () { |
| 745 |
return generateSymbolName(this._id); |
| 746 |
}; |
| 747 |
|
| 748 |
// Returns itself but in ES6 It returns 'symbol' typed value. |
| 749 |
Symbol.prototype.valueOf = function () { |
| 750 |
return this; |
| 751 |
}; |
| 752 |
|
| 753 |
// Make Iterator like iterable |
| 754 |
defineProperty(Iterator.prototype, Symbol.iterator.toString(), { |
| 755 |
value: function () {return this;}, |
| 756 |
writable: true, |
| 757 |
configurable: true |
| 758 |
}); |
| 759 |
|
| 760 |
simpleInheritance(ArrayIterator, Iterator); |
| 761 |
|
| 762 |
simpleInheritance(StringIterator, Iterator); |
| 763 |
|
| 764 |
defineProperty(ArrayIterator.prototype, Symbol.toStringTag.toString(), { |
| 765 |
value: "Array Iterator", |
| 766 |
configurable: true |
| 767 |
}); |
| 768 |
|
| 769 |
defineProperty(StringIterator.prototype, Symbol.toStringTag.toString(), { |
| 770 |
value: "String Iterator", |
| 771 |
configurable: true |
| 772 |
}); |
| 773 |
|
| 774 |
// This iterator works on any Array or TypedArray or array-like objects |
| 775 |
ArrayIterator.prototype.next = function next() { |
| 776 |
if (!(this instanceof ArrayIterator)) |
| 777 |
throw new TypeError("Method Array Iterator.prototype.next called on incompatible receiver " + String(this)); |
| 778 |
|
| 779 |
var self = this, |
| 780 |
nextValue; |
| 781 |
|
| 782 |
if (self._nextIndex === -1) { |
| 783 |
return { |
| 784 |
done: true, |
| 785 |
value: undefined |
| 786 |
}; |
| 787 |
} |
| 788 |
|
| 789 |
if (!(typeof self._array.length === "number" && self._array.length >= 0)) { |
| 790 |
self._nextIndex = -1; |
| 791 |
return { |
| 792 |
done: true, |
| 793 |
value: undefined |
| 794 |
}; |
| 795 |
} |
| 796 |
|
| 797 |
// _flag = 1 for [index, value] |
| 798 |
// _flag = 2 for [value] |
| 799 |
// _flag = 3 for [index] |
| 800 |
if (self._nextIndex < Math.floor(self._array.length)) { |
| 801 |
if (self._flag === 1) |
| 802 |
nextValue = [self._nextIndex, self._array[self._nextIndex]]; |
| 803 |
else if (self._flag === 2) |
| 804 |
nextValue = self._array[self._nextIndex]; |
| 805 |
else if (self._flag === 3) |
| 806 |
nextValue = self._nextIndex; |
| 807 |
self._nextIndex++; |
| 808 |
return { |
| 809 |
done: false, |
| 810 |
value: nextValue |
| 811 |
}; |
| 812 |
} else { |
| 813 |
self._nextIndex = -1; |
| 814 |
return { |
| 815 |
done: true, |
| 816 |
value: undefined |
| 817 |
}; |
| 818 |
} |
| 819 |
}; |
| 820 |
|
| 821 |
StringIterator.prototype.next = function next() { |
| 822 |
if (!(this instanceof StringIterator)) |
| 823 |
throw new TypeError("Method String Iterator.prototype.next called on incompatible receiver " + String(this)); |
| 824 |
|
| 825 |
var self = this, |
| 826 |
stringObject = new String(this._string), |
| 827 |
nextValue; |
| 828 |
|
| 829 |
if (self._nextIndex === -1) { |
| 830 |
return { |
| 831 |
done: true, |
| 832 |
value: undefined |
| 833 |
}; |
| 834 |
} |
| 835 |
|
| 836 |
if (self._nextIndex < stringObject.length) { |
| 837 |
nextValue = stringObject[self._nextIndex]; |
| 838 |
self._nextIndex++; |
| 839 |
return { |
| 840 |
done: false, |
| 841 |
value: nextValue |
| 842 |
}; |
| 843 |
} else { |
| 844 |
self._nextIndex = -1; |
| 845 |
return { |
| 846 |
done: true, |
| 847 |
value: undefined |
| 848 |
}; |
| 849 |
} |
| 850 |
}; |
| 851 |
|
| 852 |
var es6ArrayPrototypeIteratorSymbol = function values() { |
| 853 |
if (this === undefined || this === null) |
| 854 |
throw new TypeError("Cannot convert undefined or null to object"); |
| 855 |
|
| 856 |
var self = Object(this); |
| 857 |
return new ArrayIterator(self, 2); |
| 858 |
}; |
| 859 |
|
| 860 |
var es6StringPrototypeIteratorSymbol = function values() { |
| 861 |
if (this === undefined || this === null) |
| 862 |
throw new TypeError("String.prototype[Symbol.iterator] called on null or undefined"); |
| 863 |
return new StringIterator(String(this), 0); |
| 864 |
}; |
| 865 |
|
| 866 |
var es6ArrayPrototypeEntries = function entries() { |
| 867 |
if (this === undefined || this === null) |
| 868 |
throw new TypeError("Cannot convert undefined or null to object"); |
| 869 |
|
| 870 |
var self = Object(this); |
| 871 |
return new ArrayIterator(self, 1); |
| 872 |
}; |
| 873 |
|
| 874 |
var es6ArrayPrototypeKeys = function keys() { |
| 875 |
if (this === undefined || this === null) |
| 876 |
throw new TypeError("Cannot convert undefined or null to object"); |
| 877 |
var self = Object(this); |
| 878 |
return new ArrayIterator(self, 3); |
| 879 |
}; |
| 880 |
|
| 881 |
var SpreadOperatorImpl = function (target, thisArg) { |
| 882 |
this._target = target; |
| 883 |
this._values = []; |
| 884 |
this._thisArg = thisArg; |
| 885 |
}; |
| 886 |
// All the arguments must be iterable |
| 887 |
SpreadOperatorImpl.prototype.spread = function () { |
| 888 |
var self = this; |
| 889 |
slice.call(arguments).forEach(function (iterable) { |
| 890 |
ES6.forOf(iterable, function (value) { |
| 891 |
self._values.push(value); |
| 892 |
}); |
| 893 |
}); |
| 894 |
return self; |
| 895 |
}; |
| 896 |
|
| 897 |
SpreadOperatorImpl.prototype.add = function () { |
| 898 |
var self = this; |
| 899 |
slice.call(arguments).forEach(function (value) { |
| 900 |
self._values.push(value); |
| 901 |
}); |
| 902 |
return self; |
| 903 |
}; |
| 904 |
|
| 905 |
SpreadOperatorImpl.prototype.call = function (thisArg) { |
| 906 |
if (typeof this._target !== "function") |
| 907 |
throw new TypeError("Target is not a function"); |
| 908 |
thisArg = arguments.length <= 0 ? this._thisArg : thisArg; |
| 909 |
return this._target.apply(thisArg, this._values); |
| 910 |
}; |
| 911 |
|
| 912 |
SpreadOperatorImpl.prototype.new = function () { |
| 913 |
if (typeof this._target !== "function") |
| 914 |
throw new TypeError("Target is not a constructor"); |
| 915 |
|
| 916 |
var temp, |
| 917 |
returnValue; |
| 918 |
temp = Object.create(this._target.prototype); |
| 919 |
returnValue = this._target.apply(temp, this._values); |
| 920 |
return isObject(returnValue) ? returnValue : temp; |
| 921 |
}; |
| 922 |
|
| 923 |
// Affects the target array |
| 924 |
SpreadOperatorImpl.prototype.array = function () { |
| 925 |
if (!isArray(this._target)) |
| 926 |
throw new TypeError("Target is not a array"); |
| 927 |
push.apply(this._target, this._values); |
| 928 |
return this._target; |
| 929 |
}; |
| 930 |
|
| 931 |
// Target must be Array or function |
| 932 |
var es6SpreadOperator = function spreadOperator(target, thisArg) { |
| 933 |
if (!(typeof target === "function" || isArray(target))) |
| 934 |
throw new TypeError("Spread operator only supports on array and function objects at this moment"); |
| 935 |
return new SpreadOperatorImpl(target, thisArg); |
| 936 |
}; |
| 937 |
|
| 938 |
var es6ArrayFrom = function from(arrayLike, mapFn, thisArg) { |
| 939 |
var constructor, |
| 940 |
i = 0, |
| 941 |
length, |
| 942 |
outputs; |
| 943 |
// Use the generic constructor |
| 944 |
constructor = !isConstructor(this) ? Array : this; |
| 945 |
if (arrayLike === undefined || arrayLike === null) |
| 946 |
throw new TypeError("Cannot convert undefined or null to object"); |
| 947 |
|
| 948 |
arrayLike = Object(arrayLike); |
| 949 |
if (mapFn === undefined) |
| 950 |
mapFn = simpleFunction; |
| 951 |
else if (!isCallable(mapFn)) |
| 952 |
throw new TypeError(mapFn + " is not a function"); |
| 953 |
|
| 954 |
if (typeof arrayLike[Symbol.iterator] === "undefined") { |
| 955 |
if (!(typeof arrayLike.length === "number" && arrayLike.length >= 0)) { |
| 956 |
outputs = new constructor(0); |
| 957 |
outputs.length = 0; |
| 958 |
return outputs; |
| 959 |
} |
| 960 |
length = Math.floor(arrayLike.length); |
| 961 |
outputs = new constructor(length); |
| 962 |
outputs.length = length; |
| 963 |
for(; i < length; ++i) |
| 964 |
outputs[i] = mapFn.call(thisArg, arrayLike[i]); |
| 965 |
} else { |
| 966 |
outputs = new constructor(); |
| 967 |
outputs.length = 0; |
| 968 |
ES6.forOf(arrayLike, function (value) { |
| 969 |
outputs.length++; |
| 970 |
outputs[outputs.length - 1] = mapFn.call(thisArg, value); |
| 971 |
}); |
| 972 |
} |
| 973 |
return outputs; |
| 974 |
}; |
| 975 |
|
| 976 |
// Export ES6 APIs and add all the patches to support Symbol in ES5 |
| 977 |
// If the running environment already supports ES6 then no patches will be applied, |
| 978 |
if (isES6Running()) |
| 979 |
return ES6; |
| 980 |
else { |
| 981 |
|
| 982 |
// Some ES6 APIs can't be implemented in pure ES5, so this 'ES6' object provides |
| 983 |
// some equivalent functionality of these features. |
| 984 |
defineProperties(ES6, { |
| 985 |
|
| 986 |
// Checks if a JS value is a symbol |
| 987 |
// It can be used as equivalent api in ES6: typeof symbol === 'symbol' |
| 988 |
isSymbol: { |
| 989 |
value: isSymbol, |
| 990 |
writable: true, |
| 991 |
configurable: true |
| 992 |
}, |
| 993 |
|
| 994 |
// Native ES5 'instanceof' operator does not support @@hasInstance symbol, |
| 995 |
// this method provides same functionality of ES6 'instanceof' operator. |
| 996 |
instanceOf: { |
| 997 |
value: es6InstanceOfOperator, |
| 998 |
writable: true, |
| 999 |
configurable: true |
| 1000 |
}, |
| 1001 |
|
| 1002 |
// This method behaves exactly same as ES6 for...of loop. |
| 1003 |
forOf: { |
| 1004 |
value: es6ForOfLoop, |
| 1005 |
writable: true, |
| 1006 |
configurable: true |
| 1007 |
}, |
| 1008 |
|
| 1009 |
// This method gives same functionality of the spread operator of ES6 |
| 1010 |
// It works on only functions and arrays. |
| 1011 |
// Limitation: You can't create array like this [...iterable, , , , 33] by this method, |
| 1012 |
// to achieve this you have to do like this [...iterable, undefined, undefined, undefined, 33] |
| 1013 |
spreadOperator: { |
| 1014 |
value: es6SpreadOperator, |
| 1015 |
writable: true, |
| 1016 |
configurable: true |
| 1017 |
} |
| 1018 |
}); |
| 1019 |
|
| 1020 |
defineProperty(global, "Symbol", { |
| 1021 |
value: Symbol, |
| 1022 |
writable: true, |
| 1023 |
configurable: true |
| 1024 |
}); |
| 1025 |
|
| 1026 |
defineProperty(Function.prototype, Symbol.hasInstance.toString(), { |
| 1027 |
value: es6FunctionPrototypeHasInstanceSymbol |
| 1028 |
}); |
| 1029 |
|
| 1030 |
defineProperty(Array.prototype, "concat", { |
| 1031 |
value: es6ArrayPrototypeConcat, |
| 1032 |
writable: true, |
| 1033 |
configurable: true |
| 1034 |
}); |
| 1035 |
|
| 1036 |
defineProperty(Object.prototype, "toString", { |
| 1037 |
value: es6ObjectPrototypeToString, |
| 1038 |
writable: true, |
| 1039 |
configurable: true |
| 1040 |
}); |
| 1041 |
|
| 1042 |
defineProperty(Array.prototype, Symbol.iterator.toString(), { |
| 1043 |
value: es6ArrayPrototypeIteratorSymbol, |
| 1044 |
writable: true, |
| 1045 |
configurable: true |
| 1046 |
}); |
| 1047 |
|
| 1048 |
defineProperty(Array, "from", { |
| 1049 |
value: es6ArrayFrom, |
| 1050 |
writable: true, |
| 1051 |
configurable: true |
| 1052 |
}); |
| 1053 |
|
| 1054 |
defineProperty(Array.prototype, "entries", { |
| 1055 |
value: es6ArrayPrototypeEntries, |
| 1056 |
writable: true, |
| 1057 |
configurable: true |
| 1058 |
}); |
| 1059 |
|
| 1060 |
defineProperty(Array.prototype, "keys", { |
| 1061 |
value: es6ArrayPrototypeKeys, |
| 1062 |
writable: true, |
| 1063 |
configurable: true |
| 1064 |
}); |
| 1065 |
|
| 1066 |
defineProperty(String.prototype, Symbol.iterator.toString(), { |
| 1067 |
value: es6StringPrototypeIteratorSymbol, |
| 1068 |
writable: true, |
| 1069 |
configurable: true |
| 1070 |
}); |
| 1071 |
} |
| 1072 |
|
| 1073 |
return ES6; |
| 1074 |
}); |