es5.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. 'use strict';
  2. var $ = require('./$')
  3. , $export = require('./$.export')
  4. , DESCRIPTORS = require('./$.descriptors')
  5. , createDesc = require('./$.property-desc')
  6. , html = require('./$.html')
  7. , cel = require('./$.dom-create')
  8. , has = require('./$.has')
  9. , cof = require('./$.cof')
  10. , invoke = require('./$.invoke')
  11. , fails = require('./$.fails')
  12. , anObject = require('./$.an-object')
  13. , aFunction = require('./$.a-function')
  14. , isObject = require('./$.is-object')
  15. , toObject = require('./$.to-object')
  16. , toIObject = require('./$.to-iobject')
  17. , toInteger = require('./$.to-integer')
  18. , toIndex = require('./$.to-index')
  19. , toLength = require('./$.to-length')
  20. , IObject = require('./$.iobject')
  21. , IE_PROTO = require('./$.uid')('__proto__')
  22. , createArrayMethod = require('./$.array-methods')
  23. , arrayIndexOf = require('./$.array-includes')(false)
  24. , ObjectProto = Object.prototype
  25. , ArrayProto = Array.prototype
  26. , arraySlice = ArrayProto.slice
  27. , arrayJoin = ArrayProto.join
  28. , defineProperty = $.setDesc
  29. , getOwnDescriptor = $.getDesc
  30. , defineProperties = $.setDescs
  31. , factories = {}
  32. , IE8_DOM_DEFINE;
  33. if(!DESCRIPTORS){
  34. IE8_DOM_DEFINE = !fails(function(){
  35. return defineProperty(cel('div'), 'a', {get: function(){ return 7; }}).a != 7;
  36. });
  37. $.setDesc = function(O, P, Attributes){
  38. if(IE8_DOM_DEFINE)try {
  39. return defineProperty(O, P, Attributes);
  40. } catch(e){ /* empty */ }
  41. if('get' in Attributes || 'set' in Attributes)throw TypeError('Accessors not supported!');
  42. if('value' in Attributes)anObject(O)[P] = Attributes.value;
  43. return O;
  44. };
  45. $.getDesc = function(O, P){
  46. if(IE8_DOM_DEFINE)try {
  47. return getOwnDescriptor(O, P);
  48. } catch(e){ /* empty */ }
  49. if(has(O, P))return createDesc(!ObjectProto.propertyIsEnumerable.call(O, P), O[P]);
  50. };
  51. $.setDescs = defineProperties = function(O, Properties){
  52. anObject(O);
  53. var keys = $.getKeys(Properties)
  54. , length = keys.length
  55. , i = 0
  56. , P;
  57. while(length > i)$.setDesc(O, P = keys[i++], Properties[P]);
  58. return O;
  59. };
  60. }
  61. $export($export.S + $export.F * !DESCRIPTORS, 'Object', {
  62. // 19.1.2.6 / 15.2.3.3 Object.getOwnPropertyDescriptor(O, P)
  63. getOwnPropertyDescriptor: $.getDesc,
  64. // 19.1.2.4 / 15.2.3.6 Object.defineProperty(O, P, Attributes)
  65. defineProperty: $.setDesc,
  66. // 19.1.2.3 / 15.2.3.7 Object.defineProperties(O, Properties)
  67. defineProperties: defineProperties
  68. });
  69. // IE 8- don't enum bug keys
  70. var keys1 = ('constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,' +
  71. 'toLocaleString,toString,valueOf').split(',')
  72. // Additional keys for getOwnPropertyNames
  73. , keys2 = keys1.concat('length', 'prototype')
  74. , keysLen1 = keys1.length;
  75. // Create object with `null` prototype: use iframe Object with cleared prototype
  76. var createDict = function(){
  77. // Thrash, waste and sodomy: IE GC bug
  78. var iframe = cel('iframe')
  79. , i = keysLen1
  80. , gt = '>'
  81. , iframeDocument;
  82. iframe.style.display = 'none';
  83. html.appendChild(iframe);
  84. iframe.src = 'javascript:'; // eslint-disable-line no-script-url
  85. // createDict = iframe.contentWindow.Object;
  86. // html.removeChild(iframe);
  87. iframeDocument = iframe.contentWindow.document;
  88. iframeDocument.open();
  89. iframeDocument.write('<script>document.F=Object</script' + gt);
  90. iframeDocument.close();
  91. createDict = iframeDocument.F;
  92. while(i--)delete createDict.prototype[keys1[i]];
  93. return createDict();
  94. };
  95. var createGetKeys = function(names, length){
  96. return function(object){
  97. var O = toIObject(object)
  98. , i = 0
  99. , result = []
  100. , key;
  101. for(key in O)if(key != IE_PROTO)has(O, key) && result.push(key);
  102. // Don't enum bug & hidden keys
  103. while(length > i)if(has(O, key = names[i++])){
  104. ~arrayIndexOf(result, key) || result.push(key);
  105. }
  106. return result;
  107. };
  108. };
  109. var Empty = function(){};
  110. $export($export.S, 'Object', {
  111. // 19.1.2.9 / 15.2.3.2 Object.getPrototypeOf(O)
  112. getPrototypeOf: $.getProto = $.getProto || function(O){
  113. O = toObject(O);
  114. if(has(O, IE_PROTO))return O[IE_PROTO];
  115. if(typeof O.constructor == 'function' && O instanceof O.constructor){
  116. return O.constructor.prototype;
  117. } return O instanceof Object ? ObjectProto : null;
  118. },
  119. // 19.1.2.7 / 15.2.3.4 Object.getOwnPropertyNames(O)
  120. getOwnPropertyNames: $.getNames = $.getNames || createGetKeys(keys2, keys2.length, true),
  121. // 19.1.2.2 / 15.2.3.5 Object.create(O [, Properties])
  122. create: $.create = $.create || function(O, /*?*/Properties){
  123. var result;
  124. if(O !== null){
  125. Empty.prototype = anObject(O);
  126. result = new Empty();
  127. Empty.prototype = null;
  128. // add "__proto__" for Object.getPrototypeOf shim
  129. result[IE_PROTO] = O;
  130. } else result = createDict();
  131. return Properties === undefined ? result : defineProperties(result, Properties);
  132. },
  133. // 19.1.2.14 / 15.2.3.14 Object.keys(O)
  134. keys: $.getKeys = $.getKeys || createGetKeys(keys1, keysLen1, false)
  135. });
  136. var construct = function(F, len, args){
  137. if(!(len in factories)){
  138. for(var n = [], i = 0; i < len; i++)n[i] = 'a[' + i + ']';
  139. factories[len] = Function('F,a', 'return new F(' + n.join(',') + ')');
  140. }
  141. return factories[len](F, args);
  142. };
  143. // 19.2.3.2 / 15.3.4.5 Function.prototype.bind(thisArg, args...)
  144. $export($export.P, 'Function', {
  145. bind: function bind(that /*, args... */){
  146. var fn = aFunction(this)
  147. , partArgs = arraySlice.call(arguments, 1);
  148. var bound = function(/* args... */){
  149. var args = partArgs.concat(arraySlice.call(arguments));
  150. return this instanceof bound ? construct(fn, args.length, args) : invoke(fn, args, that);
  151. };
  152. if(isObject(fn.prototype))bound.prototype = fn.prototype;
  153. return bound;
  154. }
  155. });
  156. // fallback for not array-like ES3 strings and DOM objects
  157. $export($export.P + $export.F * fails(function(){
  158. if(html)arraySlice.call(html);
  159. }), 'Array', {
  160. slice: function(begin, end){
  161. var len = toLength(this.length)
  162. , klass = cof(this);
  163. end = end === undefined ? len : end;
  164. if(klass == 'Array')return arraySlice.call(this, begin, end);
  165. var start = toIndex(begin, len)
  166. , upTo = toIndex(end, len)
  167. , size = toLength(upTo - start)
  168. , cloned = Array(size)
  169. , i = 0;
  170. for(; i < size; i++)cloned[i] = klass == 'String'
  171. ? this.charAt(start + i)
  172. : this[start + i];
  173. return cloned;
  174. }
  175. });
  176. $export($export.P + $export.F * (IObject != Object), 'Array', {
  177. join: function join(separator){
  178. return arrayJoin.call(IObject(this), separator === undefined ? ',' : separator);
  179. }
  180. });
  181. // 22.1.2.2 / 15.4.3.2 Array.isArray(arg)
  182. $export($export.S, 'Array', {isArray: require('./$.is-array')});
  183. var createArrayReduce = function(isRight){
  184. return function(callbackfn, memo){
  185. aFunction(callbackfn);
  186. var O = IObject(this)
  187. , length = toLength(O.length)
  188. , index = isRight ? length - 1 : 0
  189. , i = isRight ? -1 : 1;
  190. if(arguments.length < 2)for(;;){
  191. if(index in O){
  192. memo = O[index];
  193. index += i;
  194. break;
  195. }
  196. index += i;
  197. if(isRight ? index < 0 : length <= index){
  198. throw TypeError('Reduce of empty array with no initial value');
  199. }
  200. }
  201. for(;isRight ? index >= 0 : length > index; index += i)if(index in O){
  202. memo = callbackfn(memo, O[index], index, this);
  203. }
  204. return memo;
  205. };
  206. };
  207. var methodize = function($fn){
  208. return function(arg1/*, arg2 = undefined */){
  209. return $fn(this, arg1, arguments[1]);
  210. };
  211. };
  212. $export($export.P, 'Array', {
  213. // 22.1.3.10 / 15.4.4.18 Array.prototype.forEach(callbackfn [, thisArg])
  214. forEach: $.each = $.each || methodize(createArrayMethod(0)),
  215. // 22.1.3.15 / 15.4.4.19 Array.prototype.map(callbackfn [, thisArg])
  216. map: methodize(createArrayMethod(1)),
  217. // 22.1.3.7 / 15.4.4.20 Array.prototype.filter(callbackfn [, thisArg])
  218. filter: methodize(createArrayMethod(2)),
  219. // 22.1.3.23 / 15.4.4.17 Array.prototype.some(callbackfn [, thisArg])
  220. some: methodize(createArrayMethod(3)),
  221. // 22.1.3.5 / 15.4.4.16 Array.prototype.every(callbackfn [, thisArg])
  222. every: methodize(createArrayMethod(4)),
  223. // 22.1.3.18 / 15.4.4.21 Array.prototype.reduce(callbackfn [, initialValue])
  224. reduce: createArrayReduce(false),
  225. // 22.1.3.19 / 15.4.4.22 Array.prototype.reduceRight(callbackfn [, initialValue])
  226. reduceRight: createArrayReduce(true),
  227. // 22.1.3.11 / 15.4.4.14 Array.prototype.indexOf(searchElement [, fromIndex])
  228. indexOf: methodize(arrayIndexOf),
  229. // 22.1.3.14 / 15.4.4.15 Array.prototype.lastIndexOf(searchElement [, fromIndex])
  230. lastIndexOf: function(el, fromIndex /* = @[*-1] */){
  231. var O = toIObject(this)
  232. , length = toLength(O.length)
  233. , index = length - 1;
  234. if(arguments.length > 1)index = Math.min(index, toInteger(fromIndex));
  235. if(index < 0)index = toLength(length + index);
  236. for(;index >= 0; index--)if(index in O)if(O[index] === el)return index;
  237. return -1;
  238. }
  239. });
  240. // 20.3.3.1 / 15.9.4.4 Date.now()
  241. $export($export.S, 'Date', {now: function(){ return +new Date; }});
  242. var lz = function(num){
  243. return num > 9 ? num : '0' + num;
  244. };
  245. // 20.3.4.36 / 15.9.5.43 Date.prototype.toISOString()
  246. // PhantomJS / old WebKit has a broken implementations
  247. $export($export.P + $export.F * (fails(function(){
  248. return new Date(-5e13 - 1).toISOString() != '0385-07-25T07:06:39.999Z';
  249. }) || !fails(function(){
  250. new Date(NaN).toISOString();
  251. })), 'Date', {
  252. toISOString: function toISOString(){
  253. if(!isFinite(this))throw RangeError('Invalid time value');
  254. var d = this
  255. , y = d.getUTCFullYear()
  256. , m = d.getUTCMilliseconds()
  257. , s = y < 0 ? '-' : y > 9999 ? '+' : '';
  258. return s + ('00000' + Math.abs(y)).slice(s ? -6 : -4) +
  259. '-' + lz(d.getUTCMonth() + 1) + '-' + lz(d.getUTCDate()) +
  260. 'T' + lz(d.getUTCHours()) + ':' + lz(d.getUTCMinutes()) +
  261. ':' + lz(d.getUTCSeconds()) + '.' + (m > 99 ? m : '0' + lz(m)) + 'Z';
  262. }
  263. });