polyfill_Array.js.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: polyfill/Array.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: polyfill/Array.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>// Licensed Materials - Property of IBM
  20. //
  21. // IBM Watson Analytics
  22. //
  23. // (C) Copyright IBM Corp. 2015, 2018
  24. //
  25. // US Government Users Restricted Rights - Use, duplication or
  26. // disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  27. ( function( Array, ObjectPolyfill, NMap, NSet )
  28. {
  29. "use strict";
  30. // This module uses bitwise operators to enforce unsigned ints or perform other optimizations.
  31. /*jshint bitwise:false*/
  32. var toObject = ObjectPolyfill.toObject,
  33. isCallable = ObjectPolyfill.isCallable,
  34. ensureCallable = ObjectPolyfill.ensureCallable;
  35. // Store reference to Object.prototype.toString for convenience and to (hopefully) grab the original before modification.
  36. var toString = Object.prototype.toString;
  37. /** @lends module:barejs/polyfill.Array */
  38. var stat = {},
  39. /** @lends module:barejs/polyfill.Array# */
  40. proto = {};
  41. /**
  42. * Method that performs the actual iteration
  43. * @memberof module:barejs/polyfill.Array~
  44. * @private
  45. */
  46. function iterate( _arrayLike, _callback, _thisArg, _logic )
  47. {
  48. var asString = ( "charAt" in _arrayLike ) &amp;&amp; ( "substr" in _arrayLike );
  49. for ( var i = 0, len = _arrayLike.length >>> 0, value, result; i &lt; len; ++i )
  50. {
  51. if ( asString || ( i in _arrayLike ) )
  52. {
  53. result = _callback.call( _thisArg, value = asString ? _arrayLike.charAt( i ) : _arrayLike[i], i, _arrayLike );
  54. if ( _logic( value, i, result ) === true )
  55. break;
  56. }
  57. }
  58. }
  59. /**
  60. * Polyfills for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array Array}.
  61. * @class module:barejs/polyfill.Array
  62. */
  63. /*istanbul ignore else: We test with __ES__ set to 3*/
  64. if ( __ES__ &lt; 5 )
  65. {
  66. /**
  67. * Enumerate all values in the array
  68. * @this {Array}
  69. * @param {function} _callback The callback to call for each value
  70. * @param {object} [_thisArg] Optional: the context in which the callback should be invoked
  71. */
  72. proto.forEach = function forEach( _callback/*, _thisArg (method has length 1)*/ )
  73. {
  74. iterate( toObject( this, forEach ), ensureCallable( _callback ), arguments[1], function() {/*no logic*/} );
  75. };
  76. /**
  77. * Check if callback returns true for every element
  78. * @this {Array}
  79. * @param {function} _callback The callback to test each value
  80. * @param {object} [_thisArg] Optional: the context in which the callback should be invoked
  81. * @returns {boolean} True if the callback returns true for each element, false otherwise.
  82. */
  83. proto.every = function every( _callback/*, _thisArg (method has length 1)*/ )
  84. {
  85. var result = true;
  86. iterate( toObject( this, every ), ensureCallable( _callback ), arguments[1], function( _v, _i, _r )
  87. {
  88. // If the callback returned a falsey value
  89. if ( !_r )
  90. {
  91. // The every statement doesn't hold true
  92. result = false;
  93. // Break execution
  94. return true;
  95. }
  96. } );
  97. return result;
  98. };
  99. /**
  100. * Check if callback returns true for any element
  101. * @this {Array}
  102. * @param {function} _callback The callback to test each value
  103. * @param {object} [_thisArg] Optional: the context in which the callback should be invoked
  104. * @returns {boolean} True if the callback returns true for at least one element, false otherwise.
  105. */
  106. proto.some = function some( _callback/*, _thisArg (method has length 1)*/ )
  107. {
  108. var result = false;
  109. iterate( toObject( this, some ), ensureCallable( _callback ), arguments[1], function( _v, _i, _r )
  110. {
  111. // If the callback returned a thruthy value, the some statement is true (shortcuted to return true for breaking)
  112. if ( _r )
  113. return ( result = true );
  114. } );
  115. return result;
  116. };
  117. /**
  118. * Creates a new array with only the elements matching the provided function.
  119. * @this {Array}
  120. * @param {function} _callback The callback to test each value.
  121. * @param {object} [_thisArg] Optional: the context in which the callback should be invoked
  122. * @returns {Array} A new array containing the result of callback per element.
  123. */
  124. proto.filter = function filter( _callback/*, _thisArg (method has length 1)*/ )
  125. {
  126. var result = [];
  127. iterate( toObject( this, filter ), ensureCallable( _callback ), arguments[1], function( _v, _i, _r )
  128. {
  129. // If the callback returned a thruthy value, add the value to the result
  130. if ( _r )
  131. result.push( _v );
  132. } );
  133. return result;
  134. };
  135. /**
  136. * Creates a new array with the results of calling a provided function on every element in this array.
  137. * @this {Array}
  138. * @param {function} _callback The callback to test each value
  139. * @param {object} [_thisArg] Optional: the context in which the callback should be invoked
  140. * @returns {Array} A new array containing the result of callback per element.
  141. */
  142. proto.map = function map( _callback/*, _thisArg (method has length 1)*/ )
  143. {
  144. var o = toObject( this, map ), result = new Array( o.length >>> 0 );
  145. iterate( o, ensureCallable( _callback ), arguments[1], function( _v, _i, _r )
  146. {
  147. result[_i] = _r;
  148. } );
  149. return result;
  150. };
  151. /**
  152. * Returns the first index at which a given element can be found in the array, or -1 if it is not present.
  153. * @this {Array}
  154. * @param {object} _searchElement Element to locate in the array.
  155. * @param {number} [_fromIndex=0] Optional: The index to start the search at. Default: 0
  156. * If the index is greater than or equal to the array's length, -1 is returned, which means
  157. * the array will not be searched. If the provided index value is a negative number, it is
  158. * taken as the offset from the end of the array. Note: if the provided index is negative,
  159. * the array is still searched from front to back. If the calculated index is less than 0,
  160. * then the whole array will be searched.
  161. * @returns {number} The first index at which a given element can be found in the array, or -1 if it is not present.
  162. */
  163. proto.indexOf = function indexOf( _searchElement/*, _fromIndex*/ )
  164. {
  165. var t = toObject( this, indexOf ), len = t.length >>> 0, i = 0;
  166. if ( len &lt; 1 )
  167. return -1;
  168. if ( arguments.length >= 2 )
  169. {
  170. if ( ( i = arguments[1] >> 0 ) &lt; 0 )
  171. i = Math.max( 0, len + i );
  172. }
  173. for ( ; i &lt; len; ++i )
  174. if ( ( i in t ) &amp;&amp; ( t[i] === _searchElement ) )
  175. return i;
  176. return -1;
  177. };
  178. /**
  179. * Returns the last index at which a given element can be found in the array, or -1 if it is not present.
  180. * The array is searched backwards, starting at fromIndex.
  181. * @this {Array}
  182. * @param {object} _searchElement Element to locate in the array.
  183. * @param {number} [_fromIndex=-1] Optional: The index at which to start searching backwards.
  184. * Defaults to the array's length - 1, i.e. the whole array will be searched. If the index is
  185. * greater than or equal to the length of the array, the whole array will be searched.
  186. * If negative, it is taken as the offset from the end of the array. Note that even when
  187. * the index is negative, the array is still searched from back to front. If the calculated
  188. * index is less than 0, -1 is returned, i.e. the array will not be searched.
  189. * @returns {number} The last index at which a given element can be found in the array, or -1 if it is not present.
  190. */
  191. proto.lastIndexOf = function lastIndexOf( _searchElement/*, _fromIndex*/ )
  192. {
  193. var t = toObject( this, lastIndexOf ), len = t.length >>> 0, i = len - 1;
  194. if ( len &lt; 1 )
  195. return -1;
  196. if ( arguments.length >= 2 )
  197. {
  198. if ( ( i = Math.min( i, arguments[1] >> 0 ) ) &lt; 0 )
  199. i += len;
  200. }
  201. for ( ; i >= 0; --i )
  202. if ( ( i in this ) &amp;&amp; ( this[i] === _searchElement ) )
  203. return i;
  204. return -1;
  205. };
  206. /**
  207. * The reduce() method applies a function against an accumulator and each value of the
  208. * array (from left-to-right) has to reduce it to a single value.
  209. * @this {Array}
  210. * @param {function} _callback The callback to call for each value, taking 4 arguments:
  211. * previousValue
  212. * The value previously returned in the last invocation of the callback, or initialValue, if supplied. (See below.)
  213. * currentValue
  214. * The current element being processed in the array.
  215. * index
  216. * The index of the current element being processed in the array.
  217. * array
  218. * The array reduce was called upon.
  219. * @param {object} [_initialValue] Optional: a value to pass to the first callback.
  220. */
  221. proto.reduce = function reduce( _callback/*, _initialValue*/ )
  222. {
  223. var t = toObject( this, reduce ), len = t.length >>> 0, i = ensureCallable( _callback, 0 ), value;
  224. if ( arguments.length >= 2 )
  225. {
  226. value = arguments[1];
  227. }
  228. else
  229. {
  230. while ( ( i &lt; len ) &amp;&amp; !( i in t ) )
  231. ++i;
  232. if ( i >= len )
  233. throw new TypeError( "Reduce of empty array with no initial value" );
  234. value = t[i++];
  235. }
  236. for (; i &lt; len; ++i)
  237. {
  238. if ( i in t )
  239. value = _callback( value, t[i], i, t );
  240. }
  241. return value;
  242. };
  243. /**
  244. * The reduceRight() method applies a function against an accumulator and each value of the
  245. * array (from right-to-left) has to reduce it to a single value.
  246. * @this {Array}
  247. * @param {function} _callback The callback to call for each value, taking 4 arguments:
  248. * previousValue
  249. * The value previously returned in the last invocation of the callback, or initialValue, if supplied. (See below.)
  250. * currentValue
  251. * The current element being processed in the array.
  252. * index
  253. * The index of the current element being processed in the array.
  254. * array
  255. * The array reduce was called upon.
  256. * @param {object} [_initialValue] Optional: a value to pass to the first callback.
  257. */
  258. proto.reduceRight = function reduceRight( _callback/*, _initialValue*/ )
  259. {
  260. var t = toObject( this, reduceRight ), len = t.length >>> 0, i = ensureCallable( _callback, len - 1 ), value;
  261. if ( arguments.length >= 2 )
  262. {
  263. value = arguments[1];
  264. }
  265. else
  266. {
  267. while ( ( i >= 0 ) &amp;&amp; !( i in t ) )
  268. --i;
  269. if ( i &lt; 0 )
  270. throw new TypeError( "Reduce of empty array with no initial value" );
  271. value = t[i--];
  272. }
  273. for (; i >= 0; --i)
  274. {
  275. if ( i in t )
  276. value = _callback( value, t[i], i, t );
  277. }
  278. return value;
  279. };
  280. /**
  281. * Check if an object is an array.
  282. * @param _arg The object to check.
  283. * @returns {boolean} true if an object is an array, false if it is not.
  284. */
  285. stat.isArray = function isArray( _arg )
  286. {
  287. return toString.call( _arg ) === "[object Array]";
  288. };
  289. // End of ES5 polyfill scope
  290. }
  291. /*istanbul ignore else: We test with __ES__ set to 3 */
  292. if ( __ES__ &lt; 6 )
  293. {
  294. /**
  295. * Find a value in the array
  296. * @param {function} _callback The callback to test each value in the array. If the value matches, it should return true.
  297. * @param {object} [_thisArg] Optional: the context in which the callback should be invoked
  298. * @returns the found value or undefined if not found.
  299. */
  300. proto.find = function find( _callback/*, _thisArg (method has length 1)*/ )
  301. {
  302. var result = void undefined;
  303. iterate( toObject( this, find ), ensureCallable( _callback ), arguments[1], function( _v, _i, _r )
  304. {
  305. // If the callback returned a thruthy value, the result is found
  306. if ( _r )
  307. {
  308. result = _v;
  309. // Break the loop
  310. return true;
  311. }
  312. } );
  313. return result;
  314. };
  315. /**
  316. * Find a value in the array
  317. * @param {function} _callback The callback to test each value in the array. If the value matches, it should return true.
  318. * @param {object} [_thisArg] Optional: the context in which the callback should be invoked
  319. * @returns {number} the found index or -1 if not found.
  320. */
  321. proto.findIndex = function findIndex( _callback/*, _thisArg (method has length 1)*/ )
  322. {
  323. var result = -1;
  324. iterate( toObject( this, findIndex ), ensureCallable( _callback ), arguments[1], function( _v, _i, _r )
  325. {
  326. // If the callback returned a thruthy value, the result is found
  327. if ( _r )
  328. {
  329. result = _i;
  330. // Break the loop
  331. return true;
  332. }
  333. } );
  334. return result;
  335. };
  336. /**
  337. * The fill() method fills all the elements of an array from a start index to an end index with a static value.
  338. * @param _value The value to set to each index
  339. * @param {number} [_start=0] Optional: the index to start filling (inclusive)
  340. * If _start is negative, it is treated as length + _start.
  341. * @param {number} [_end] Optional: the index at which to stop filling (exclusive)
  342. * If _end is negative, it is treated as length + _end.
  343. */
  344. proto.fill = function fill( _value/*, _start, _end*/ )
  345. {
  346. var t = toObject( this, fill ), len = t.length >>> 0, i = arguments[1] >> 0, end = arguments[2];
  347. if ( i &lt; 0 )
  348. i = Math.max( 0, i + len );
  349. if ( end === undefined )
  350. end = len;
  351. else if ( end &lt; 0 )
  352. end = Math.max( 0, end + len );
  353. for ( ; i &lt; end; ++i )
  354. t[i] = _value;
  355. return t;
  356. };
  357. /**
  358. * The Array.of() method creates a new Array instance with a variable number of arguments,
  359. * regardless of number or type of the arguments.
  360. * @param {...any} _value Any number of values that will be the content of the array.
  361. * @returns {Array} The created Array.
  362. */
  363. stat.of = function of()
  364. {
  365. var C = this;
  366. var args = arguments;
  367. var len = args.length;
  368. var result = isCallable( C ) ? Object( new C( len ) ) : new Array( len );
  369. for ( var i = 0; i &lt; len; ++i )
  370. result[i] = args[i];
  371. return result;
  372. };
  373. /**
  374. * The Array.from() method creates a new Array instance from an array-like or iterable object.
  375. * @param {object} _arrayLike An array-like or iterable object to convert to an array.
  376. * @param {function} [_mapFn] Optional. Map function to call on every element of the array.
  377. * @param {object} [_thisArg] Optional. Value to use as this when executing mapFn.
  378. * @returns {Array} The created Array.
  379. */
  380. stat.from = function from( _arrayLike/*, _mapFn, _thisArg*/ )
  381. {
  382. var C = this;
  383. var items = toObject( _arrayLike );
  384. var mapFn = typeof arguments[1] !== "undefined" ? ensureCallable( arguments[1] ) : null;
  385. var thisArg = arguments[2];
  386. // Iterators can't be emulated, but add specific logic for Map and Set so those are supported
  387. // Note: check both for a potential global and the polyfill separately:
  388. // NMap and Map MAY point to the same function, but don't have to in every environment (!).
  389. var isMap = items instanceof NMap;
  390. // Normalize to an Array, source. Chances are high we can just return this Array, or map it in place.
  391. var source;
  392. var i, len;
  393. if ( mapFn )
  394. {
  395. if ( thisArg === undefined )
  396. thisArg = null;
  397. else if ( thisArg !== null )
  398. thisArg = Object( thisArg );
  399. }
  400. var it = isCallable( items.next ) ? items : ObjectPolyfill.getIterator( items );
  401. if ( it )
  402. {
  403. source = [];
  404. for ( var cur = it.next(); !cur.done; cur = it.next() )
  405. source.push( cur.value );
  406. len = source.length;
  407. }
  408. // IE11's native map and set support forEach, but not iterators. Emulate using forEach.
  409. else if ( ( isMap || ( items instanceof NSet ) ) &amp;&amp; isCallable( items.forEach ) )
  410. {
  411. i = 0;
  412. len = Math.floor( items.size ) || 0;
  413. source = new Array( len );
  414. items.forEach( function( _value, _key )
  415. {
  416. var entry;
  417. if ( isMap )
  418. {
  419. entry = new Array( 2 );
  420. entry[0] = _key;
  421. entry[1] = _value;
  422. }
  423. else
  424. {
  425. entry = _value;
  426. }
  427. source[ i++ ] = entry;
  428. } );
  429. }
  430. if ( !source )
  431. {
  432. var asString = ( "charAt" in items ) &amp;&amp; ( "substr" in items );
  433. len = Math.floor( items.length ) || 0;
  434. source = new Array( len );
  435. for ( i = 0; i &lt; len; ++i )
  436. source[i] = asString ? items.charAt( i ) : items[i];
  437. }
  438. // We already have an Array (source), only create a new Object if we have a Constructor as context
  439. var result = isCallable( C ) &amp;&amp; C !== Array ? Object( new C( len ) ) : source;
  440. if ( mapFn || result !== source )
  441. {
  442. for ( i = 0; i &lt; len; ++i )
  443. result[i] = mapFn ? mapFn.call( thisArg, source[i], i ) : source[i];
  444. }
  445. return result;
  446. };
  447. // End of ES6 polyfill scope
  448. }
  449. /*istanbul ignore else: We test with __ES__ set to 3*/
  450. if ( __ES__ &lt; 7 )
  451. {
  452. /**
  453. * The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
  454. * The array is searched forwards, starting at fromIndex (defaults to 0).
  455. * @param {object} _searchElement Element to locate in the array.
  456. * @param {object} [_fromIndex=0] Optional: The index to start the search at. Default: 0
  457. * If the index is greater than or equal to the array's length, -1 is returned, which means
  458. * the array will not be searched. If the provided index value is a negative number, it is
  459. * taken as the offset from the end of the array. Note: if the provided index is negative,
  460. * the array is still searched from front to back. If the calculated index is less than 0,
  461. * then the whole array will be searched.
  462. * @returns {boolean} True if the element was found, false otherwise.
  463. */
  464. proto.includes = function includes( _searchElement/*, _fromIndex*/ )
  465. {
  466. var t = toObject( this, includes ), len = t.length >>> 0, i = 0;
  467. if ( len &lt; 1 )
  468. return false;
  469. if ( arguments.length > 1 )
  470. {
  471. if ( ( i = arguments[1] >> 0 ) &lt; 0 )
  472. i = Math.max( 0, len + i );
  473. }
  474. for ( ; i &lt; len; ++i )
  475. if ( ( t[i] === _searchElement ) || ( _searchElement !== _searchElement &amp;&amp; t[i] !== t[i] ) )
  476. return true;
  477. return false;
  478. };
  479. // End of ES7 polyfill scope
  480. }
  481. ObjectPolyfill.polyfill( Array, stat, proto, exports, "Array" );
  482. // End of module
  483. }( Array, require( "./Object" ), require( "../NMap" ), require( "../NSet" ) ) );
  484. </code></pre>
  485. </article>
  486. </section>
  487. </div>
  488. <nav>
  489. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-barejs.html">barejs</a></li><li><a href="module-barejs_polyfill.html">barejs/polyfill</a></li><li><a href="module-barejs_polyfill_Intl.html">barejs/polyfill/Intl</a></li></ul><h3>Classes</h3><ul><li><a href="module-barejs.decl.html">decl</a></li><li><a href="module-barejs.decl-Enum.html">Enum</a></li><li><a href="module-barejs.decl-Interface.html">Interface</a></li><li><a href="module-barejs.decl-SpecialType.html">SpecialType</a></li><li><a href="module-barejs.Destroyable.html">Destroyable</a></li><li><a href="module-barejs.EventArgs.html">EventArgs</a></li><li><a href="module-barejs.Evented.html">Evented</a></li><li><a href="module-barejs.Evented-EventedHandle.html">EventedHandle</a></li><li><a href="module-barejs.Exception.html">Exception</a></li><li><a href="module-barejs_polyfill.Array.html">Array</a></li><li><a href="module-barejs_polyfill.Date.html">Date</a></li><li><a href="module-barejs_polyfill.EntryStore.html">EntryStore</a></li><li><a href="module-barejs_polyfill.EntryStore.Iterator.html">Iterator</a></li><li><a href="module-barejs_polyfill.Function.html">Function</a></li><li><a href="module-barejs_polyfill.Map.html">Map</a></li><li><a href="module-barejs_polyfill.Map-MapIterator.html">MapIterator</a></li><li><a href="module-barejs_polyfill.Math.html">Math</a></li><li><a href="module-barejs_polyfill.Number.html">Number</a></li><li><a href="module-barejs_polyfill.Object.html">Object</a></li><li><a href="module-barejs_polyfill.Promise.html">Promise</a></li><li><a href="module-barejs_polyfill.Set.html">Set</a></li><li><a href="module-barejs_polyfill.Set-SetIterator.html">SetIterator</a></li><li><a href="module-barejs_polyfill.String.html">String</a></li><li><a href="module-barejs_polyfill.Symbol.html">Symbol</a></li><li><a href="module-barejs_polyfill.WeakMap.html">WeakMap</a></li><li><a href="module-barejs_polyfill.WeakSet.html">WeakSet</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat.html">DateTimeFormat</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat-DateTimeFormatOptions.html">DateTimeFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat.html">NumberFormat</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat-NumberFormatOptions.html">NumberFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl-Format.html">Format</a></li></ul>
  490. </nav>
  491. <br class="clear">
  492. <footer>
  493. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Oct 03 2018 15:59:33 GMT+0200 (W. Europe Daylight Time)
  494. </footer>
  495. <script> prettyPrint(); </script>
  496. <script src="scripts/linenumber.js"> </script>
  497. </body>
  498. </html>