ami/public/polyfill/symbol.js

12 lines
10 KiB
JavaScript
Raw Normal View History

/*!
* Symbol-ES6 v0.1.2
* ES6 Symbol polyfill in pure ES5.
*
* @license Copyright (c) 2017-2018 Rousan Ali, MIT License
*
* Codebase: https://github.com/rousan/symbol-es6
* Date: 28th Jan, 2018
*/
!function(global,factory){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=factory(global):factory(global)}("undefined"!=typeof window?window:global,function(global){"use strict";var defineProperty=Object.defineProperty,defineProperties=Object.defineProperties,symbolHiddenCounter=0,globalSymbolRegistry=[],slice=Array.prototype.slice,ES6="object"==typeof global.ES6?global.ES6:global.ES6={},isArray=Array.isArray,objectToString=Object.prototype.toString,push=Array.prototype.push,emptyFunction=function(){},simpleFunction=function(arg){return arg},isCallable=function(fn){return"function"==typeof fn},Iterator=function(){},ArrayIterator=function(array,flag){this._array=array,this._flag=flag,this._nextIndex=0},StringIterator=function(string,flag){this._string=string,this._flag=flag,this._nextIndex=0},isObject=function(value){return null!==value&&("object"==typeof value||"function"==typeof value)},setupSymbolInternals=function(symbol,desc){return defineProperties(symbol,{_description:{value:desc},_isSymbol:{value:!0},_id:{value:symbolHiddenCounter++}}),symbol},appendArray=function(array1,array2){if("number"==typeof array1.length&&array1.length>=0&&"number"==typeof array2.length&&array2.length>=0){var length1=Math.floor(array1.length),length2=Math.floor(array2.length),i=0;for(array1.length=length1+length2;i<length2;++i)array2.hasOwnProperty(i)&&(array1[length1+i]=array2[i])}},simpleInheritance=function(child,parent){if("function"!=typeof child||"function"!=typeof parent)throw new TypeError("Child and Parent must be function type");child.prototype=Object.create(parent.prototype),child.prototype.constructor=child},Symbol=function Symbol(desc){if(desc=void 0===desc?"":String(desc),this instanceof Symbol)throw new TypeError("Symbol is not a constructor");return setupSymbolInternals(Object.create(Symbol.prototype),desc)};defineProperties(Symbol,{for:{value:function(key){key=String(key);for(var record,registryLength=globalSymbolRegistry.length,i=0;i<registryLength;++i)if((record=globalSymbolRegistry[i]).key===key)return record.symbol;return record={key:key,symbol:Symbol(key)},globalSymbolRegistry.push(record),record.symbol},writable:!0,configurable:!0},keyFor:{value:function(symbol){if(!ES6.isSymbol(symbol))throw new TypeError(String(symbol)+" is not a symbol");for(var record,registryLength=globalSymbolRegistry.length,i=0;i<registryLength;++i)if((record=globalSymbolRegistry[i]).symbol===symbol)return record.key},writable:!0,configurable:!0},hasInstance:{value:Symbol("Symbol.hasInstance")},isConcatSpreadable:{value:Symbol("Symbol.isConcatSpreadable")},iterator:{value:Symbol("Symbol.iterator")},toStringTag:{value:Symbol("Symbol.toStringTag")}}),Symbol.prototype.toString=function(){return"@@_____"+this._id+"_____"},Symbol.prototype.valueOf=function(){return this},defineProperty(Iterator.prototype,Symbol.iterator.toString(),{value:function(){return this},writable:!0,configurable:!0}),simpleInheritance(ArrayIterator,Iterator),simpleInheritance(StringIterator,Iterator),defineProperty(ArrayIterator.prototype,Symbol.toStringTag.toString(),{value:"Array Iterator",configurable:!0}),defineProperty(StringIterator.prototype,Symbol.toStringTag.toString(),{value:"String Iterator",configurable:!0}),ArrayIterator.prototype.next=function(){if(!(this instanceof ArrayIterator))throw new TypeError("Method Array Iterator.prototype.next called on incompatible receiver "+String(this));var nextValue;return-1===this._nextIndex?{done:!0,value:void 0}:"number"==typeof this._array.length&&this._array.length>=0&&this._nextIndex<Math.floor(this._array.length)?(1===this._flag?nextValue=[this._nextIndex,this._array[this._nextIndex]]:2===this._flag?nextValue=this._array[this._nextIndex]:3===this._flag&&(nextValue=this._nextIndex),this._nextIndex++,{done:!1,value:nextValue}):(this._nextIndex=-1,{done:!0,value:void 0})},StringIterator.prototype.next=function(){if(!(this instanceof StringIterator))throw new TypeError("Method String Iterator.prototype.next called on incompatible receiver "+String(this));var nextValue,stringObject=new Str