s-array.js 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. /* globals document */
  2. var toStr = Object.prototype.toString;
  3. var canDistinguishSparseFromUndefined = 0 in [undefined]; // IE 6 - 8 have a bug where this returns false.
  4. var ifHasDenseUndefinedsIt = canDistinguishSparseFromUndefined ? it : xit;
  5. var undefinedIfNoSparseBug = canDistinguishSparseFromUndefined ? undefined : { valueOf: function () { return 0; } };
  6. var hasStrictMode = (function () {
  7. 'use strict';
  8. return !this;
  9. }());
  10. var ifHasStrictIt = hasStrictMode ? it : xit;
  11. describe('Array', function () {
  12. var testSubject;
  13. beforeEach(function () {
  14. testSubject = [2, 3, undefinedIfNoSparseBug, true, 'hej', null, false, 0];
  15. delete testSubject[1];
  16. });
  17. var createArrayLikeFromArray = function createArrayLikeFromArray(arr) {
  18. var o = {};
  19. Array.prototype.forEach.call(arr, function (e, i) {
  20. o[i] = e;
  21. });
  22. o.length = arr.length;
  23. return o;
  24. };
  25. describe('#forEach()', function () {
  26. var expected, actual;
  27. beforeEach(function () {
  28. expected = { 0: 2, 2: undefinedIfNoSparseBug, 3: true, 4: 'hej', 5: null, 6: false, 7: 0 };
  29. actual = {};
  30. });
  31. it('should pass the right parameters', function () {
  32. var callback = jasmine.createSpy('callback');
  33. var array = ['1'];
  34. array.forEach(callback);
  35. expect(callback).toHaveBeenCalledWith('1', 0, array);
  36. });
  37. it('should not affect elements added to the array after it has begun', function () {
  38. var arr = [1, 2, 3];
  39. var i = 0;
  40. arr.forEach(function (a) {
  41. i += 1;
  42. arr.push(a + 3);
  43. });
  44. expect(arr).toEqual([1, 2, 3, 4, 5, 6]);
  45. expect(i).toBe(3);
  46. });
  47. it('should set the right context when given none', function () {
  48. var context;
  49. [1].forEach(function () { context = this; });
  50. expect(context).toBe(function () { return this; }.call());
  51. });
  52. it('should iterate all', function () {
  53. testSubject.forEach(function (obj, index) {
  54. actual[index] = obj;
  55. });
  56. expect(actual).toExactlyMatch(expected);
  57. });
  58. it('should iterate all using a context', function () {
  59. var o = { a: actual };
  60. testSubject.forEach(function (obj, index) {
  61. this.a[index] = obj;
  62. }, o);
  63. expect(actual).toExactlyMatch(expected);
  64. });
  65. it('should iterate all in an array-like object', function () {
  66. var ts = createArrayLikeFromArray(testSubject);
  67. Array.prototype.forEach.call(ts, function (obj, index) {
  68. actual[index] = obj;
  69. });
  70. expect(actual).toExactlyMatch(expected);
  71. });
  72. it('should iterate all in an array-like object using a context', function () {
  73. var ts = createArrayLikeFromArray(testSubject);
  74. var o = { a: actual };
  75. Array.prototype.forEach.call(ts, function (obj, index) {
  76. this.a[index] = obj;
  77. }, o);
  78. expect(actual).toExactlyMatch(expected);
  79. });
  80. describe('strings', function () {
  81. var str = 'Hello, World!';
  82. it('should iterate all in a string', function () {
  83. actual = [];
  84. Array.prototype.forEach.call(str, function (item, index) {
  85. actual[index] = item;
  86. });
  87. expect(actual).toExactlyMatch(str.split(''));
  88. });
  89. it('should iterate all in a string using a context', function () {
  90. actual = [];
  91. var o = { a: actual };
  92. Array.prototype.forEach.call(str, function (item, index) {
  93. this.a[index] = item;
  94. }, o);
  95. expect(actual).toExactlyMatch(str.split(''));
  96. });
  97. });
  98. it('should have a boxed object as list argument of callback', function () {
  99. var listArg;
  100. Array.prototype.forEach.call('foo', function (item, index, list) {
  101. listArg = list;
  102. });
  103. expect(typeof listArg).toBe('object');
  104. expect(toStr.call(listArg)).toBe('[object String]');
  105. });
  106. ifHasStrictIt('does not autobox the content in strict mode', function () {
  107. var context;
  108. [1].forEach(function () {
  109. 'use strict';
  110. context = this;
  111. }, 'x');
  112. expect(typeof context).toBe('string');
  113. });
  114. });
  115. describe('#some()', function () {
  116. var actual, expected, numberOfRuns;
  117. beforeEach(function () {
  118. expected = { 0: 2, 2: undefinedIfNoSparseBug, 3: true };
  119. actual = {};
  120. numberOfRuns = 0;
  121. });
  122. it('should pass the correct values along to the callback', function () {
  123. var callback = jasmine.createSpy('callback');
  124. var array = ['1'];
  125. array.some(callback);
  126. expect(callback).toHaveBeenCalledWith('1', 0, array);
  127. });
  128. it('should not affect elements added to the array after it has begun', function () {
  129. var arr = [1, 2, 3];
  130. var i = 0;
  131. arr.some(function (a) {
  132. i += 1;
  133. arr.push(a + 3);
  134. return i > 3;
  135. });
  136. expect(arr).toEqual([1, 2, 3, 4, 5, 6]);
  137. expect(i).toBe(3);
  138. });
  139. it('should set the right context when given none', function () {
  140. /* eslint-disable array-callback-return */
  141. var context;
  142. [1].some(function () { context = this; });
  143. expect(context).toBe(function () { return this; }.call());
  144. });
  145. it('should return false if it runs to the end', function () {
  146. /* eslint-disable array-callback-return */
  147. actual = testSubject.some(function () {});
  148. expect(actual).toBeFalsy();
  149. });
  150. it('should return true if it is stopped somewhere', function () {
  151. actual = testSubject.some(function () { return true; });
  152. expect(actual).toBeTruthy();
  153. });
  154. it('should return false if there are no elements', function () {
  155. actual = [].some(function () { return true; });
  156. expect(actual).toBeFalsy();
  157. });
  158. it('should stop after 3 elements', function () {
  159. testSubject.some(function (obj, index) {
  160. actual[index] = obj;
  161. numberOfRuns += 1;
  162. return numberOfRuns === 3;
  163. });
  164. expect(actual).toExactlyMatch(expected);
  165. });
  166. it('should stop after 3 elements using a context', function () {
  167. var o = { a: actual };
  168. testSubject.some(function (obj, index) {
  169. this.a[index] = obj;
  170. numberOfRuns += 1;
  171. return numberOfRuns === 3;
  172. }, o);
  173. expect(actual).toExactlyMatch(expected);
  174. });
  175. it('should stop after 3 elements in an array-like object', function () {
  176. var ts = createArrayLikeFromArray(testSubject);
  177. Array.prototype.some.call(ts, function (obj, index) {
  178. actual[index] = obj;
  179. numberOfRuns += 1;
  180. return numberOfRuns === 3;
  181. });
  182. expect(actual).toExactlyMatch(expected);
  183. });
  184. it('should stop after 3 elements in an array-like object using a context', function () {
  185. var ts = createArrayLikeFromArray(testSubject);
  186. var o = { a: actual };
  187. Array.prototype.some.call(ts, function (obj, index) {
  188. this.a[index] = obj;
  189. numberOfRuns += 1;
  190. return numberOfRuns === 3;
  191. }, o);
  192. expect(actual).toExactlyMatch(expected);
  193. });
  194. it('should have a boxed object as list argument of callback', function () {
  195. var listArg;
  196. Array.prototype.some.call('foo', function (item, index, list) {
  197. listArg = list;
  198. });
  199. expect(typeof listArg).toBe('object');
  200. expect(toStr.call(listArg)).toBe('[object String]');
  201. });
  202. });
  203. describe('#every()', function () {
  204. var actual, expected, numberOfRuns;
  205. beforeEach(function () {
  206. expected = { 0: 2, 2: undefinedIfNoSparseBug, 3: true };
  207. actual = {};
  208. numberOfRuns = 0;
  209. });
  210. it('should pass the correct values along to the callback', function () {
  211. var callback = jasmine.createSpy('callback');
  212. var array = ['1'];
  213. array.every(callback);
  214. expect(callback).toHaveBeenCalledWith('1', 0, array);
  215. });
  216. it('should not affect elements added to the array after it has begun', function () {
  217. var arr = [1, 2, 3];
  218. var i = 0;
  219. arr.every(function (a) {
  220. i += 1;
  221. arr.push(a + 3);
  222. return i <= 3;
  223. });
  224. expect(arr).toEqual([1, 2, 3, 4, 5, 6]);
  225. expect(i).toBe(3);
  226. });
  227. it('should set the right context when given none', function () {
  228. /* eslint-disable array-callback-return */
  229. var context;
  230. [1].every(function () { context = this; });
  231. expect(context).toBe(function () { return this; }.call());
  232. });
  233. it('should return true if the array is empty', function () {
  234. actual = [].every(function () { return true; });
  235. expect(actual).toBeTruthy();
  236. actual = [].every(function () { return false; });
  237. expect(actual).toBeTruthy();
  238. });
  239. it('should return true if it runs to the end', function () {
  240. actual = [1, 2, 3].every(function () { return true; });
  241. expect(actual).toBeTruthy();
  242. });
  243. it('should return false if it is stopped before the end', function () {
  244. actual = [1, 2, 3].every(function () { return false; });
  245. expect(actual).toBeFalsy();
  246. });
  247. it('should return after 3 elements', function () {
  248. testSubject.every(function (obj, index) {
  249. actual[index] = obj;
  250. numberOfRuns += 1;
  251. return numberOfRuns !== 3;
  252. });
  253. expect(actual).toExactlyMatch(expected);
  254. });
  255. it('should stop after 3 elements using a context', function () {
  256. var o = { a: actual };
  257. testSubject.every(function (obj, index) {
  258. this.a[index] = obj;
  259. numberOfRuns += 1;
  260. return numberOfRuns !== 3;
  261. }, o);
  262. expect(actual).toExactlyMatch(expected);
  263. });
  264. it('should stop after 3 elements in an array-like object', function () {
  265. var ts = createArrayLikeFromArray(testSubject);
  266. Array.prototype.every.call(ts, function (obj, index) {
  267. actual[index] = obj;
  268. numberOfRuns += 1;
  269. return numberOfRuns !== 3;
  270. });
  271. expect(actual).toExactlyMatch(expected);
  272. });
  273. it('should stop after 3 elements in an array-like object using a context', function () {
  274. var ts = createArrayLikeFromArray(testSubject);
  275. var o = { a: actual };
  276. Array.prototype.every.call(ts, function (obj, index) {
  277. this.a[index] = obj;
  278. numberOfRuns += 1;
  279. return numberOfRuns !== 3;
  280. }, o);
  281. expect(actual).toExactlyMatch(expected);
  282. });
  283. it('should have a boxed object as list argument of callback', function () {
  284. var listArg;
  285. Array.prototype.every.call('foo', function (item, index, list) {
  286. listArg = list;
  287. });
  288. expect(typeof listArg).toBe('object');
  289. expect(toStr.call(listArg)).toBe('[object String]');
  290. });
  291. });
  292. describe('#indexOf()', function () {
  293. 'use strict';
  294. var actual, expected;
  295. beforeEach(function () {
  296. testSubject = [2, 3, undefinedIfNoSparseBug, true, 'hej', null, 2, false, 0];
  297. delete testSubject[1];
  298. });
  299. it('should find the element', function () {
  300. expected = 4;
  301. actual = testSubject.indexOf('hej');
  302. expect(actual).toBe(expected);
  303. });
  304. it('should not find the element', function () {
  305. expected = -1;
  306. actual = testSubject.indexOf('mus');
  307. expect(actual).toBe(expected);
  308. });
  309. ifHasDenseUndefinedsIt('should find undefined as well', function () {
  310. expected = -1;
  311. actual = testSubject.indexOf(undefined);
  312. expect(actual).not.toBe(expected);
  313. });
  314. ifHasDenseUndefinedsIt('should skip unset indexes', function () {
  315. expected = 2;
  316. actual = testSubject.indexOf(undefined);
  317. expect(actual).toBe(expected);
  318. });
  319. it('should use a strict test', function () {
  320. actual = testSubject.indexOf(null);
  321. expect(actual).toBe(5);
  322. actual = testSubject.indexOf('2');
  323. expect(actual).toBe(-1);
  324. });
  325. it('should skip the first if fromIndex is set', function () {
  326. expect(testSubject.indexOf(2, 2)).toBe(6);
  327. expect(testSubject.indexOf(2, 0)).toBe(0);
  328. expect(testSubject.indexOf(2, 6)).toBe(6);
  329. });
  330. it('should work with negative fromIndex', function () {
  331. expect(testSubject.indexOf(2, -3)).toBe(6);
  332. expect(testSubject.indexOf(2, -9)).toBe(0);
  333. });
  334. it('should work with fromIndex being greater than the length', function () {
  335. expect(testSubject.indexOf(0, 20)).toBe(-1);
  336. });
  337. it('should work with fromIndex being negative and greater than the length', function () {
  338. expect(testSubject.indexOf('hej', -20)).toBe(4);
  339. });
  340. describe('Array-like', function ArrayLike() {
  341. var indexOf = Array.prototype.indexOf;
  342. var testAL;
  343. beforeEach(function beforeEach() {
  344. testAL = {};
  345. testSubject = [2, 3, undefinedIfNoSparseBug, true, 'hej', null, 2, false, 0];
  346. testSubject.forEach(function (o, i) {
  347. testAL[i] = o;
  348. });
  349. testAL.length = testSubject.length;
  350. });
  351. it('should find the element (array-like)', function () {
  352. expected = 4;
  353. actual = indexOf.call(testAL, 'hej');
  354. expect(actual).toBe(expected);
  355. });
  356. it('should not find the element (array-like)', function () {
  357. expected = -1;
  358. actual = indexOf.call(testAL, 'mus');
  359. expect(actual).toBe(expected);
  360. });
  361. ifHasDenseUndefinedsIt('should find undefined as well (array-like)', function () {
  362. expected = -1;
  363. actual = indexOf.call(testAL, undefined);
  364. expect(actual).not.toBe(expected);
  365. });
  366. ifHasDenseUndefinedsIt('should skip unset indexes (array-like)', function () {
  367. expected = 2;
  368. actual = indexOf.call(testAL, undefined);
  369. expect(actual).toBe(expected);
  370. });
  371. it('should use a strict test (array-like)', function () {
  372. actual = Array.prototype.indexOf.call(testAL, null);
  373. expect(actual).toBe(5);
  374. actual = Array.prototype.indexOf.call(testAL, '2');
  375. expect(actual).toBe(-1);
  376. });
  377. it('should skip the first if fromIndex is set (array-like)', function () {
  378. expect(indexOf.call(testAL, 2, 2)).toBe(6);
  379. expect(indexOf.call(testAL, 2, 0)).toBe(0);
  380. expect(indexOf.call(testAL, 2, 6)).toBe(6);
  381. });
  382. it('should work with negative fromIndex (array-like)', function () {
  383. expect(indexOf.call(testAL, 2, -3)).toBe(6);
  384. expect(indexOf.call(testAL, 2, -9)).toBe(0);
  385. });
  386. it('should work with fromIndex being greater than the length (array-like)', function () {
  387. expect(indexOf.call(testAL, 0, 20)).toBe(-1);
  388. });
  389. it('should work with fromIndex being negative and greater than the length (array-like)', function () {
  390. expect(indexOf.call(testAL, 'hej', -20)).toBe(4);
  391. });
  392. });
  393. });
  394. describe('#lastIndexOf()', function () {
  395. 'use strict';
  396. var actual, expected;
  397. beforeEach(function () {
  398. testSubject = [2, 3, undefinedIfNoSparseBug, true, 'hej', null, 2, 3, false, 0];
  399. delete testSubject[1];
  400. delete testSubject[7];
  401. });
  402. describe('Array', function () {
  403. it('should find the element', function () {
  404. expected = 4;
  405. actual = testSubject.lastIndexOf('hej');
  406. expect(actual).toBe(expected);
  407. });
  408. it('should not find the element', function () {
  409. expected = -1;
  410. actual = testSubject.lastIndexOf('mus');
  411. expect(actual).toBe(expected);
  412. });
  413. ifHasDenseUndefinedsIt('should find undefined as well', function () {
  414. expected = -1;
  415. actual = testSubject.lastIndexOf(undefined);
  416. expect(actual).not.toBe(expected);
  417. });
  418. ifHasDenseUndefinedsIt('should skip unset indexes', function () {
  419. expected = 2;
  420. actual = testSubject.lastIndexOf(undefined);
  421. expect(actual).toBe(expected);
  422. });
  423. it('should use a strict test', function () {
  424. actual = testSubject.lastIndexOf(null);
  425. expect(actual).toBe(5);
  426. actual = testSubject.lastIndexOf('2');
  427. expect(actual).toBe(-1);
  428. });
  429. it('should skip the first if fromIndex is set', function () {
  430. expect(testSubject.lastIndexOf(2, 2)).toBe(0);
  431. expect(testSubject.lastIndexOf(2, 0)).toBe(0);
  432. expect(testSubject.lastIndexOf(2, 6)).toBe(6);
  433. });
  434. it('should work with negative fromIndex', function () {
  435. expect(testSubject.lastIndexOf(2, -3)).toBe(6);
  436. expect(testSubject.lastIndexOf(2, -9)).toBe(0);
  437. });
  438. it('should work with fromIndex being greater than the length', function () {
  439. expect(testSubject.lastIndexOf(2, 20)).toBe(6);
  440. });
  441. it('should work with fromIndex being negative and greater than the length', function () {
  442. expect(testSubject.lastIndexOf(2, -20)).toBe(-1);
  443. });
  444. });
  445. describe('Array like', function () {
  446. var lastIndexOf = Array.prototype.lastIndexOf;
  447. var testAL;
  448. beforeEach(function () {
  449. testAL = {};
  450. testSubject.forEach(function (o, i) {
  451. testAL[i] = o;
  452. });
  453. testAL.length = testSubject.length;
  454. });
  455. it('should find the element (array-like)', function () {
  456. expected = 4;
  457. actual = lastIndexOf.call(testAL, 'hej');
  458. expect(actual).toBe(expected);
  459. });
  460. it('should not find the element (array-like)', function () {
  461. expected = -1;
  462. actual = lastIndexOf.call(testAL, 'mus');
  463. expect(actual).toBe(expected);
  464. });
  465. ifHasDenseUndefinedsIt('should find undefined as well (array-like)', function () {
  466. expected = -1;
  467. actual = lastIndexOf.call(testAL, undefined);
  468. expect(actual).not.toBe(expected);
  469. });
  470. ifHasDenseUndefinedsIt('should skip unset indexes (array-like)', function () {
  471. expected = 2;
  472. actual = lastIndexOf.call(testAL, undefined);
  473. expect(actual).toBe(expected);
  474. });
  475. it('should use a strict test (array-like)', function () {
  476. actual = lastIndexOf.call(testAL, null);
  477. expect(actual).toBe(5);
  478. actual = lastIndexOf.call(testAL, '2');
  479. expect(actual).toBe(-1);
  480. });
  481. it('should skip the first if fromIndex is set', function () {
  482. expect(lastIndexOf.call(testAL, 2, 2)).toBe(0);
  483. expect(lastIndexOf.call(testAL, 2, 0)).toBe(0);
  484. expect(lastIndexOf.call(testAL, 2, 6)).toBe(6);
  485. });
  486. it('should work with negative fromIndex', function () {
  487. expect(lastIndexOf.call(testAL, 2, -3)).toBe(6);
  488. expect(lastIndexOf.call(testAL, 2, -9)).toBe(0);
  489. });
  490. it('should work with fromIndex being greater than the length', function () {
  491. expect(lastIndexOf.call(testAL, 2, 20)).toBe(6);
  492. });
  493. it('should work with fromIndex being negative and greater than the length', function () {
  494. expect(lastIndexOf.call(testAL, 2, -20)).toBe(-1);
  495. });
  496. });
  497. });
  498. describe('#filter()', function () {
  499. var filteredArray;
  500. var callback = function callback(o, i) {
  501. return i !== 3 && i !== 5;
  502. };
  503. beforeEach(function () {
  504. testSubject = [2, 3, undefinedIfNoSparseBug, true, 'hej', 3, null, false, 0];
  505. delete testSubject[1];
  506. filteredArray = [2, undefinedIfNoSparseBug, 'hej', null, false, 0];
  507. });
  508. describe('Array object', function () {
  509. it('should call the callback with the proper arguments', function () {
  510. var predicate = jasmine.createSpy('predicate');
  511. var arr = ['1'];
  512. arr.filter(predicate);
  513. expect(predicate).toHaveBeenCalledWith('1', 0, arr);
  514. });
  515. it('should not affect elements added to the array after it has begun', function () {
  516. var arr = [1, 2, 3];
  517. var i = 0;
  518. arr.filter(function (a) {
  519. i += 1;
  520. if (i <= 4) {
  521. arr.push(a + 3);
  522. }
  523. return true;
  524. });
  525. expect(arr).toEqual([1, 2, 3, 4, 5, 6]);
  526. expect(i).toBe(3);
  527. });
  528. ifHasDenseUndefinedsIt('should skip unset values', function () {
  529. var passedValues = {};
  530. testSubject = [1, 2, 3, 4];
  531. delete testSubject[1];
  532. testSubject.filter(function (o, i) {
  533. passedValues[i] = o;
  534. return true;
  535. });
  536. expect(passedValues).toExactlyMatch(testSubject);
  537. });
  538. it('should pass the right context to the filter', function () {
  539. var passedValues = {};
  540. testSubject = [1, 2, 3, 4];
  541. delete testSubject[1];
  542. testSubject.filter(function (o, i) {
  543. this[i] = o;
  544. return true;
  545. }, passedValues);
  546. expect(passedValues).toExactlyMatch(testSubject);
  547. });
  548. it('should set the right context when given none', function () {
  549. /* eslint-disable array-callback-return */
  550. var context;
  551. [1].filter(function () { context = this; });
  552. expect(context).toBe(function () { return this; }.call());
  553. });
  554. it('should remove only the values for which the callback returns false', function () {
  555. var result = testSubject.filter(callback);
  556. expect(result).toExactlyMatch(filteredArray);
  557. });
  558. it('should leave the original array untouched', function () {
  559. var copy = testSubject.slice();
  560. expect(copy).toNotBe(testSubject);
  561. testSubject.filter(callback);
  562. expect(testSubject).toExactlyMatch(copy);
  563. });
  564. it('should not be affected by same-index mutation', function () {
  565. var results = [1, 2, 3].filter(function (value, index, array) {
  566. array[index] = 'a'; // eslint-disable-line no-param-reassign
  567. return true;
  568. });
  569. expect(results).toEqual([1, 2, 3]);
  570. });
  571. });
  572. describe('Array like', function () {
  573. beforeEach(function () {
  574. testSubject = createArrayLikeFromArray(testSubject);
  575. });
  576. it('should call the predicate with the proper arguments', function () {
  577. var predicate = jasmine.createSpy('predicate');
  578. var arr = createArrayLikeFromArray(['1']);
  579. Array.prototype.filter.call(arr, predicate);
  580. expect(predicate).toHaveBeenCalledWith('1', 0, arr);
  581. });
  582. it('should not affect elements added to the array after it has begun', function () {
  583. var arr = createArrayLikeFromArray([1, 2, 3]);
  584. var i = 0;
  585. Array.prototype.filter.call(arr, function (a) {
  586. i += 1;
  587. if (i <= 4) {
  588. arr[i + 2] = a + 3;
  589. arr.length += 1;
  590. }
  591. return true;
  592. });
  593. expect(Array.prototype.slice.call(arr)).toEqual([1, 2, 3, 4, 5, 6]);
  594. expect(i).toBe(3);
  595. });
  596. it('should skip non-set values', function () {
  597. var passedValues = createArrayLikeFromArray([]);
  598. testSubject = createArrayLikeFromArray([1, 2, 3, 4]);
  599. delete testSubject[1];
  600. Array.prototype.filter.call(testSubject, function (o, i) {
  601. passedValues[i] = o;
  602. passedValues.length = i + 1;
  603. return true;
  604. });
  605. expect(passedValues).toEqual(testSubject);
  606. });
  607. it('should set the right context when given none', function () {
  608. var context;
  609. Array.prototype.filter.call(createArrayLikeFromArray([1]), function () { context = this; }, undefined);
  610. expect(context).toBe(function () { return this; }.call());
  611. });
  612. it('should pass the right context to the filter', function () {
  613. var passedValues = {};
  614. testSubject = createArrayLikeFromArray([1, 2, 3, 4]);
  615. delete testSubject[1];
  616. Array.prototype.filter.call(testSubject, function (o, i) {
  617. this[i] = o;
  618. this.length = i + 1;
  619. return true;
  620. }, passedValues);
  621. expect(passedValues).toEqual(testSubject);
  622. });
  623. it('should remove only the values for which the callback returns false', function () {
  624. var result = Array.prototype.filter.call(testSubject, callback);
  625. expect(result).toExactlyMatch(filteredArray);
  626. });
  627. it('should leave the original array untouched', function () {
  628. var copy = createArrayLikeFromArray(testSubject);
  629. Array.prototype.filter.call(testSubject, callback);
  630. expect(testSubject).toExactlyMatch(copy);
  631. });
  632. });
  633. it('should have a boxed object as list argument of callback', function () {
  634. var actual;
  635. Array.prototype.filter.call('foo', function (item, index, list) {
  636. actual = list;
  637. });
  638. expect(typeof actual).toBe('object');
  639. expect(toStr.call(actual)).toBe('[object String]');
  640. });
  641. });
  642. describe('#map()', function () {
  643. var callback;
  644. beforeEach(function () {
  645. var i = 0;
  646. callback = function () {
  647. return i++;
  648. };
  649. });
  650. describe('Array object', function () {
  651. it('should call mapper with the right parameters', function () {
  652. var mapper = jasmine.createSpy('mapper');
  653. var array = [1];
  654. array.map(mapper);
  655. expect(mapper).toHaveBeenCalledWith(1, 0, array);
  656. });
  657. it('should set the context correctly', function () {
  658. var context = {};
  659. testSubject.map(function (o, i) {
  660. this[i] = o;
  661. }, context);
  662. expect(context).toExactlyMatch(testSubject);
  663. });
  664. it('should set the right context when given none', function () {
  665. var context;
  666. [1].map(function () { context = this; });
  667. expect(context).toBe(function () { return this; }.call());
  668. });
  669. it('should not change the array it is called on', function () {
  670. var copy = testSubject.slice();
  671. testSubject.map(callback);
  672. expect(testSubject).toExactlyMatch(copy);
  673. });
  674. it('should only run for the number of objects in the array when it started', function () {
  675. var arr = [1, 2, 3];
  676. var i = 0;
  677. arr.map(function (o) {
  678. arr.push(o + 3);
  679. i += 1;
  680. return o;
  681. });
  682. expect(arr).toExactlyMatch([1, 2, 3, 4, 5, 6]);
  683. expect(i).toBe(3);
  684. });
  685. it('should properly translate the values as according to the callback', function () {
  686. var result = testSubject.map(callback);
  687. var expected = [0, 0, 1, 2, 3, 4, 5, 6];
  688. delete expected[1];
  689. expect(result).toExactlyMatch(expected);
  690. });
  691. it('should skip non-existing values', function () {
  692. var array = [1, 2, 3, 4];
  693. var i = 0;
  694. delete array[2];
  695. array.map(function () {
  696. i += 1;
  697. });
  698. expect(i).toBe(3);
  699. });
  700. });
  701. describe('Array-like', function () {
  702. beforeEach(function () {
  703. testSubject = createArrayLikeFromArray(testSubject);
  704. });
  705. it('should call mapper with the right parameters', function () {
  706. var mapper = jasmine.createSpy('mapper');
  707. var array = createArrayLikeFromArray([1]);
  708. Array.prototype.map.call(array, mapper);
  709. expect(mapper).toHaveBeenCalledWith(1, 0, array);
  710. });
  711. it('should set the context correctly', function () {
  712. var context = {};
  713. Array.prototype.map.call(testSubject, function (o, i) {
  714. this[i] = o;
  715. this.length = i + 1;
  716. }, context);
  717. expect(context).toEqual(testSubject);
  718. });
  719. it('should set the right context when given none', function () {
  720. var context;
  721. Array.prototype.map.call(createArrayLikeFromArray([1]), function () { context = this; });
  722. expect(context).toBe(function () { return this; }.call());
  723. });
  724. it('should not change the array it is called on', function () {
  725. var copy = createArrayLikeFromArray(testSubject);
  726. Array.prototype.map.call(testSubject, callback);
  727. expect(testSubject).toExactlyMatch(copy);
  728. });
  729. it('should only run for the number of objects in the array when it started', function () {
  730. var arr = createArrayLikeFromArray([1, 2, 3]);
  731. var i = 0;
  732. Array.prototype.map.call(arr, function (o) {
  733. Array.prototype.push.call(arr, o + 3);
  734. i += 1;
  735. return o;
  736. });
  737. expect(Array.prototype.slice.call(arr)).toEqual([1, 2, 3, 4, 5, 6]);
  738. expect(i).toBe(3);
  739. });
  740. it('should properly translate the values as according to the callback', function () {
  741. var result = Array.prototype.map.call(testSubject, callback);
  742. var expected = [0, 0, 1, 2, 3, 4, 5, 6];
  743. delete expected[1];
  744. expect(result).toExactlyMatch(expected);
  745. });
  746. it('should skip non-existing values', function () {
  747. var array = createArrayLikeFromArray([1, 2, 3, 4]);
  748. var i = 0;
  749. delete array[2];
  750. Array.prototype.map.call(array, function () {
  751. i += 1;
  752. });
  753. expect(i).toBe(3);
  754. });
  755. });
  756. it('should have a boxed object as list argument of callback', function () {
  757. var actual;
  758. Array.prototype.map.call('foo', function (item, index, list) {
  759. actual = list;
  760. });
  761. expect(typeof actual).toBe('object');
  762. expect(toStr.call(actual)).toBe('[object String]');
  763. });
  764. });
  765. describe('#reduce()', function () {
  766. beforeEach(function () {
  767. testSubject = [1, 2, 3];
  768. });
  769. describe('Array', function () {
  770. it('should pass the correct arguments to the callback', function () {
  771. var spy = jasmine.createSpy().andReturn(0);
  772. testSubject.reduce(spy);
  773. expect(spy.calls[0].args).toExactlyMatch([1, 2, 1, testSubject]);
  774. });
  775. it('should start with the right initialValue', function () {
  776. var spy = jasmine.createSpy().andReturn(0);
  777. testSubject.reduce(spy, 0);
  778. expect(spy.calls[0].args).toExactlyMatch([0, 1, 0, testSubject]);
  779. });
  780. it('should not affect elements added to the array after it has begun', function () {
  781. var arr = [1, 2, 3];
  782. var i = 0;
  783. arr.reduce(function (a, b) {
  784. i += 1;
  785. if (i <= 4) {
  786. arr.push(a + 3);
  787. }
  788. return b;
  789. });
  790. expect(arr).toEqual([1, 2, 3, 4, 5]);
  791. expect(i).toBe(2);
  792. });
  793. it('should work as expected for empty arrays', function () {
  794. var spy = jasmine.createSpy();
  795. expect(function () {
  796. [].reduce(spy);
  797. }).toThrow();
  798. expect(spy).not.toHaveBeenCalled();
  799. });
  800. it('should throw correctly if no callback is given', function () {
  801. expect(function () {
  802. testSubject.reduce();
  803. }).toThrow();
  804. });
  805. it('should return the expected result', function () {
  806. expect(testSubject.reduce(function (a, b) {
  807. return String(a || '') + String(b || '');
  808. })).toBe(testSubject.join(''));
  809. });
  810. it('should not directly affect the passed array', function () {
  811. var copy = testSubject.slice();
  812. testSubject.reduce(function (a, b) {
  813. return a + b;
  814. });
  815. expect(testSubject).toEqual(copy);
  816. });
  817. it('should skip non-set values', function () {
  818. delete testSubject[1];
  819. var visited = {};
  820. testSubject.reduce(function (a, b) {
  821. if (a) { visited[a] = true; }
  822. if (b) { visited[b] = true; }
  823. return 0;
  824. });
  825. expect(visited).toEqual({ 1: true, 3: true });
  826. });
  827. it('should have the right length', function () {
  828. expect(testSubject.reduce.length).toBe(1);
  829. });
  830. });
  831. describe('Array-like objects', function () {
  832. beforeEach(function () {
  833. testSubject = createArrayLikeFromArray(testSubject);
  834. testSubject.reduce = Array.prototype.reduce;
  835. });
  836. it('should pass the correct arguments to the callback', function () {
  837. var spy = jasmine.createSpy().andReturn(0);
  838. testSubject.reduce(spy);
  839. expect(spy.calls[0].args).toExactlyMatch([1, 2, 1, testSubject]);
  840. });
  841. it('should start with the right initialValue', function () {
  842. var spy = jasmine.createSpy().andReturn(0);
  843. testSubject.reduce(spy, 0);
  844. expect(spy.calls[0].args).toExactlyMatch([0, 1, 0, testSubject]);
  845. });
  846. it('should not affect elements added to the array after it has begun', function () {
  847. var arr = createArrayLikeFromArray([1, 2, 3]);
  848. var i = 0;
  849. Array.prototype.reduce.call(arr, function (a, b) {
  850. i += 1;
  851. if (i <= 4) {
  852. arr[i + 2] = a + 3;
  853. }
  854. return b;
  855. });
  856. expect(arr).toEqual({
  857. 0: 1,
  858. 1: 2,
  859. 2: 3,
  860. 3: 4,
  861. 4: 5,
  862. length: 3
  863. });
  864. expect(i).toBe(2);
  865. });
  866. it('should work as expected for empty arrays', function () {
  867. var spy = jasmine.createSpy();
  868. expect(function () {
  869. Array.prototype.reduce.call({ length: 0 }, spy);
  870. }).toThrow();
  871. expect(spy).not.toHaveBeenCalled();
  872. });
  873. it('should throw correctly if no callback is given', function () {
  874. expect(function () {
  875. testSubject.reduce();
  876. }).toThrow();
  877. });
  878. it('should return the expected result', function () {
  879. expect(testSubject.reduce(function (a, b) {
  880. return String(a || '') + String(b || '');
  881. })).toBe('123');
  882. });
  883. it('should not directly affect the passed array', function () {
  884. var copy = createArrayLikeFromArray(testSubject);
  885. testSubject.reduce(function (a, b) {
  886. return a + b;
  887. });
  888. delete testSubject.reduce;
  889. expect(testSubject).toEqual(copy);
  890. });
  891. it('should skip non-set values', function () {
  892. delete testSubject[1];
  893. var visited = {};
  894. testSubject.reduce(function (a, b) {
  895. if (a) { visited[a] = true; }
  896. if (b) { visited[b] = true; }
  897. return 0;
  898. });
  899. expect(visited).toEqual({ 1: true, 3: true });
  900. });
  901. it('should have the right length', function () {
  902. expect(testSubject.reduce.length).toBe(1);
  903. });
  904. });
  905. it('should have a boxed object as list argument of callback', function () {
  906. var actual;
  907. Array.prototype.reduce.call('foo', function (accumulator, item, index, list) {
  908. actual = list;
  909. });
  910. expect(typeof actual).toBe('object');
  911. expect(toStr.call(actual)).toBe('[object String]');
  912. });
  913. });
  914. describe('#reduceRight()', function () {
  915. beforeEach(function () {
  916. testSubject = [1, 2, 3];
  917. });
  918. describe('Array', function () {
  919. it('should pass the correct arguments to the callback', function () {
  920. var spy = jasmine.createSpy().andReturn(0);
  921. testSubject.reduceRight(spy);
  922. expect(spy.calls[0].args).toExactlyMatch([3, 2, 1, testSubject]);
  923. });
  924. it('should start with the right initialValue', function () {
  925. var spy = jasmine.createSpy().andReturn(0);
  926. testSubject.reduceRight(spy, 0);
  927. expect(spy.calls[0].args).toExactlyMatch([0, 3, 2, testSubject]);
  928. });
  929. it('should not affect elements added to the array after it has begun', function () {
  930. var arr = [1, 2, 3];
  931. var i = 0;
  932. arr.reduceRight(function (a, b) {
  933. i += 1;
  934. if (i <= 4) {
  935. arr.push(a + 3);
  936. }
  937. return b;
  938. });
  939. expect(arr).toEqual([1, 2, 3, 6, 5]);
  940. expect(i).toBe(2);
  941. });
  942. it('should work as expected for empty arrays', function () {
  943. var spy = jasmine.createSpy();
  944. expect(function () {
  945. [].reduceRight(spy);
  946. }).toThrow();
  947. expect(spy).not.toHaveBeenCalled();
  948. });
  949. it('should work as expected for empty arrays with an initial value', function () {
  950. var spy = jasmine.createSpy();
  951. var result;
  952. result = [].reduceRight(spy, '');
  953. expect(spy).not.toHaveBeenCalled();
  954. expect(result).toBe('');
  955. });
  956. it('should throw correctly if no callback is given', function () {
  957. expect(function () {
  958. testSubject.reduceRight();
  959. }).toThrow();
  960. });
  961. it('should return the expected result', function () {
  962. expect(testSubject.reduceRight(function (a, b) {
  963. return String(a || '') + String(b || '');
  964. })).toBe('321');
  965. });
  966. it('should not directly affect the passed array', function () {
  967. var copy = testSubject.slice();
  968. testSubject.reduceRight(function (a, b) {
  969. return a + b;
  970. });
  971. expect(testSubject).toEqual(copy);
  972. });
  973. it('should skip non-set values', function () {
  974. delete testSubject[1];
  975. var visited = {};
  976. testSubject.reduceRight(function (a, b) {
  977. if (a) { visited[a] = true; }
  978. if (b) { visited[b] = true; }
  979. return 0;
  980. });
  981. expect(visited).toEqual({ 1: true, 3: true });
  982. });
  983. it('should have the right length', function () {
  984. expect(testSubject.reduceRight.length).toBe(1);
  985. });
  986. });
  987. describe('Array-like objects', function () {
  988. beforeEach(function () {
  989. testSubject = createArrayLikeFromArray(testSubject);
  990. testSubject.reduceRight = Array.prototype.reduceRight;
  991. });
  992. it('should pass the correct arguments to the callback', function () {
  993. var spy = jasmine.createSpy().andReturn(0);
  994. testSubject.reduceRight(spy);
  995. expect(spy.calls[0].args).toExactlyMatch([3, 2, 1, testSubject]);
  996. });
  997. it('should start with the right initialValue', function () {
  998. var spy = jasmine.createSpy().andReturn(0);
  999. testSubject.reduceRight(spy, 0);
  1000. expect(spy.calls[0].args).toExactlyMatch([0, 3, 2, testSubject]);
  1001. });
  1002. it('should not affect elements added to the array after it has begun', function () {
  1003. var arr = createArrayLikeFromArray([1, 2, 3]);
  1004. var i = 0;
  1005. Array.prototype.reduceRight.call(arr, function (a, b) {
  1006. i += 1;
  1007. if (i <= 4) {
  1008. arr[i + 2] = a + 3;
  1009. }
  1010. return b;
  1011. });
  1012. expect(arr).toEqual({
  1013. 0: 1,
  1014. 1: 2,
  1015. 2: 3,
  1016. 3: 6,
  1017. 4: 5,
  1018. length: 3 // does not get updated on property assignment
  1019. });
  1020. expect(i).toBe(2);
  1021. });
  1022. it('should work as expected for empty arrays', function () {
  1023. var spy = jasmine.createSpy();
  1024. expect(function () {
  1025. Array.prototype.reduceRight.call({ length: 0 }, spy);
  1026. }).toThrow();
  1027. expect(spy).not.toHaveBeenCalled();
  1028. });
  1029. it('should throw correctly if no callback is given', function () {
  1030. expect(function () {
  1031. testSubject.reduceRight();
  1032. }).toThrow();
  1033. });
  1034. it('should return the expected result', function () {
  1035. expect(testSubject.reduceRight(function (a, b) {
  1036. return String(a || '') + String(b || '');
  1037. })).toBe('321');
  1038. });
  1039. it('should not directly affect the passed array', function () {
  1040. var copy = createArrayLikeFromArray(testSubject);
  1041. testSubject.reduceRight(function (a, b) {
  1042. return a + b;
  1043. });
  1044. delete testSubject.reduceRight;
  1045. expect(testSubject).toEqual(copy);
  1046. });
  1047. it('should skip non-set values', function () {
  1048. delete testSubject[1];
  1049. var visited = {};
  1050. testSubject.reduceRight(function (a, b) {
  1051. if (a) { visited[a] = true; }
  1052. if (b) { visited[b] = true; }
  1053. return 0;
  1054. });
  1055. expect(visited).toEqual({ 1: true, 3: true });
  1056. });
  1057. it('should have the right length', function () {
  1058. expect(testSubject.reduceRight.length).toBe(1);
  1059. });
  1060. });
  1061. it('should have a boxed object as list argument of callback', function () {
  1062. var actual;
  1063. Array.prototype.reduceRight.call('foo', function (accumulator, item, index, list) {
  1064. actual = list;
  1065. });
  1066. expect(typeof actual).toBe('object');
  1067. expect(toStr.call(actual)).toBe('[object String]');
  1068. });
  1069. });
  1070. describe('.isArray()', function () {
  1071. it('should be true for Array', function () {
  1072. expect(Array.isArray([])).toBe(true);
  1073. });
  1074. it('should be false for primitives', function () {
  1075. var primitives = [
  1076. 'foo',
  1077. true,
  1078. false,
  1079. 42,
  1080. 0,
  1081. -0,
  1082. NaN,
  1083. Infinity,
  1084. -Infinity
  1085. ];
  1086. primitives.forEach(function (v) {
  1087. expect(Array.isArray(v)).toBe(false);
  1088. });
  1089. });
  1090. it('should fail for other objects', function () {
  1091. var objects = [
  1092. {},
  1093. /foo/,
  1094. arguments
  1095. ];
  1096. if (Object.create) {
  1097. objects.push(Object.create(null));
  1098. }
  1099. objects.forEach(function (v) {
  1100. expect(Array.isArray(v)).toBe(false);
  1101. });
  1102. });
  1103. if (typeof document !== 'undefined') {
  1104. it('should be false for an HTML element', function () {
  1105. var el = document.getElementsByTagName('div');
  1106. expect(Array.isArray(el)).toBe(false);
  1107. });
  1108. }
  1109. });
  1110. describe('#shift()', function () {
  1111. it('works on arrays', function () {
  1112. var arr = [1, 2];
  1113. var result = arr.shift();
  1114. expect(result).toBe(1);
  1115. expect(arr.length).toBe(1);
  1116. expect(Object.prototype.hasOwnProperty.call(arr, 0)).toBe(true);
  1117. expect(Object.prototype.hasOwnProperty.call(arr, 1)).toBe(false);
  1118. expect(arr[0]).toBe(2);
  1119. expect(arr[1]).toBeUndefined();
  1120. });
  1121. it('is generic', function () {
  1122. var obj = { 0: 1, 1: 2, length: 2 };
  1123. var result = Array.prototype.shift.call(obj);
  1124. expect(result).toBe(1);
  1125. expect(obj.length).toBe(1);
  1126. expect(Object.prototype.hasOwnProperty.call(obj, 0)).toBe(true);
  1127. expect(Object.prototype.hasOwnProperty.call(obj, 1)).toBe(false);
  1128. expect(obj[0]).toBe(2);
  1129. expect(obj[1]).toBeUndefined();
  1130. });
  1131. });
  1132. describe('#unshift()', function () {
  1133. it('should return length', function () {
  1134. expect([].unshift(0)).toBe(1);
  1135. });
  1136. it('works on arrays', function () {
  1137. var arr = [1];
  1138. var result = arr.unshift(undefined);
  1139. expect(result).toBe(2);
  1140. expect(arr.length).toBe(2);
  1141. expect(Object.prototype.hasOwnProperty.call(arr, 0)).toBe(true);
  1142. expect(Object.prototype.hasOwnProperty.call(arr, 1)).toBe(true);
  1143. expect(arr[0]).toBeUndefined();
  1144. expect(arr[1]).toBe(1);
  1145. });
  1146. it('is generic', function () {
  1147. var obj = { 0: 1, length: 1 };
  1148. var result = Array.prototype.unshift.call(obj, undefined);
  1149. expect(result).toBe(2);
  1150. expect(obj.length).toBe(2);
  1151. expect(Object.prototype.hasOwnProperty.call(obj, 0)).toBe(true);
  1152. expect(Object.prototype.hasOwnProperty.call(obj, 1)).toBe(true);
  1153. expect(obj[0]).toBeUndefined();
  1154. expect(obj[1]).toBe(1);
  1155. });
  1156. });
  1157. describe('#splice()', function () {
  1158. var b = ['b'];
  1159. var a = [1, 'a', b];
  1160. var test;
  1161. var makeArray = function (l, givenPrefix) {
  1162. var prefix = givenPrefix || '';
  1163. var length = l;
  1164. var arr = [];
  1165. while (length--) {
  1166. arr.unshift(prefix + Array(length + 1).join(' ') + length);
  1167. }
  1168. return arr;
  1169. };
  1170. beforeEach(function () {
  1171. test = a.slice(0);
  1172. });
  1173. it('has the right length', function () {
  1174. expect(Array.prototype.splice.length).toBe(2);
  1175. });
  1176. /* This test is disabled, because ES6 normalizes actual
  1177. * browser behavior, contradicting ES5.
  1178. */
  1179. xit('treats undefined deleteCount as 0', function () {
  1180. expect(test.splice(0).length).toBe(0);
  1181. expect(test.splice(0)).toEqual(test.splice(0, 0));
  1182. });
  1183. // ES6 introduced a proper default value
  1184. it('defaults deleteCount to length - start if there is only 1 argument', function () {
  1185. expect([0, 1, 2].splice(0).length).toBe(3);
  1186. expect([0, 1, 2].splice(1).length).toBe(2);
  1187. });
  1188. it('basic implementation test 1', function () {
  1189. expect(test.splice(0, 0)).toEqual([]);
  1190. });
  1191. it('basic implementation test 2', function () {
  1192. test.splice(0, 2);
  1193. expect(test).toEqual([b]);
  1194. });
  1195. it('should return right result 1', function () {
  1196. var array = [];
  1197. array.splice(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
  1198. array.splice(1, 0, 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20', 'F21', 'F22', 'F23', 'F24', 'F25', 'F26');
  1199. array.splice(5, 0, 'XXX');
  1200. expect(array).toEqual([1, 'F1', 'F2', 'F3', 'F4', 'XXX', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20', 'F21', 'F22', 'F23', 'F24', 'F25', 'F26', 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]);
  1201. });
  1202. it('should return right result 2', function () {
  1203. var array = makeArray(6);
  1204. array.splice(array.length - 1, 1, '');
  1205. array.splice(0, 1, 1, 2, 3, 4);
  1206. array.splice(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45);
  1207. array.splice(4, 0, '99999999999999');
  1208. expect(array).toEqual([1, 2, 3, 4, '99999999999999', 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 1, 2, 3, 4, ' 1', ' 2', ' 3', ' 4', '']);
  1209. });
  1210. it('should return right result 3', function () {
  1211. var array = [1, 2, 3];
  1212. array.splice(0, array.length);
  1213. array.splice(0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  1214. array.splice(1, 1, 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20', 'F21', 'F22', 'F23', 'F24', 'F25', 'F26');
  1215. array.splice(5, 1, 'YYY', 'XXX');
  1216. array.splice(0, 1);
  1217. array.splice(0, 2);
  1218. array.pop();
  1219. array.push.apply(array, makeArray(10, '-'));
  1220. array.splice(array.length - 2, 10);
  1221. array.splice();
  1222. array.splice(1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  1223. array.splice(1, 1, 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20', 'F21', 'F22', 'F23', 'F24', 'F25', 'F26', 1, 23, 4, 5, 6, 7, 8);
  1224. array.splice(30, 10);
  1225. array.splice(30, 1);
  1226. array.splice(30, 0);
  1227. array.splice(2, 5, 1, 2, 3, 'P', 'LLL', 'CCC', 'YYY', 'XXX');
  1228. array.push(1, 2, 3, 4, 5, 6);
  1229. array.splice(1, 6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 8, 9);
  1230. array.splice(3, 7);
  1231. array.unshift(7, 8, 9, 10, 11);
  1232. array.pop();
  1233. array.splice(5, 2);
  1234. array.pop();
  1235. array.unshift.apply(array, makeArray(8, '~'));
  1236. array.pop();
  1237. array.splice(3, 1, 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20', 'F21', 'F22', 'F23', 'F24', 'F25', 'F26', 1, 23, 4, 5, 6, 7, 8);
  1238. array.splice(4, 5, 'P', 'LLL', 'CCC', 'YYY', 'XXX');
  1239. expect(array).toEqual(['~0', '~ 1', '~ 2', 'F1', 'P', 'LLL', 'CCC', 'YYY', 'XXX', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20', 'F21', 'F22', 'F23', 'F24', 'F25', 'F26', 1, 23, 4, 5, 6, 7, 8, '~ 4', '~ 5', '~ 6', '~ 7', 7, 8, 9, 10, 11, 2, 4, 5, 6, 7, 8, 9, 'CCC', 'YYY', 'XXX', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20', 'F21', 'F22', 'F23', 'F24', 'F25', 'F26', 1, 23, 4, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'YYY', 'XXX', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20', 'F21', 'F22', 'F23', 'F24', 'F25', 'F26', 3, 4, 5, 6, 7, 8, 9, '-0', '- 1', '- 2', '- 3', '- 4', '- 5', '- 6', '- 7', 1, 2, 3]);
  1240. });
  1241. it('should do nothing if method called with no arguments', function () {
  1242. expect(test.splice()).toEqual([]);
  1243. expect(test).toEqual(a);
  1244. });
  1245. it('should set first argument to 0 if first argument is set but undefined', function () {
  1246. var test2 = test.slice(0);
  1247. expect(test.splice(void 0, 2)).toEqual(test2.splice(0, 2));
  1248. expect(test).toEqual(test2);
  1249. });
  1250. it('should work with objects - adding 1', function () {
  1251. var obj = {};
  1252. Array.prototype.splice.call(obj, 0, 0, 1, 2, 3);
  1253. expect(obj.length).toBe(3);
  1254. });
  1255. it('should work with objects - adding 2', function () {
  1256. var obj = { 0: 1, length: 1 };
  1257. Array.prototype.splice.call(obj, 1, 0, 2, 3);
  1258. expect(obj.length).toBe(3);
  1259. });
  1260. it('should work with objects - removing', function () {
  1261. var obj = { 0: 1, 1: 2, 2: 3, length: 3 };
  1262. Array.prototype.splice.call(obj, 0, 3);
  1263. expect(obj.length).toBe(0);
  1264. });
  1265. it('should work with objects - replacing', function () {
  1266. var obj = { 0: 99, length: 1 };
  1267. Array.prototype.splice.call(obj, 0, 1, 1, 2, 3);
  1268. expect(obj.length).toBe(3);
  1269. expect(obj[0]).toBe(1);
  1270. });
  1271. ifHasDenseUndefinedsIt('should not break on sparse arrays in Opera', function () {
  1272. // test from https://github.com/wikimedia/VisualEditor/blob/d468b00311e69c2095b9da360c5745153342a5c3/src/ve.utils.js#L182-L196
  1273. var n = 256;
  1274. var arr = [];
  1275. arr[n] = 'a';
  1276. arr.splice(n + 1, 0, 'b');
  1277. expect(arr[n]).toBe('a');
  1278. });
  1279. ifHasDenseUndefinedsIt('should not break on sparse arrays in Safari 7/8', function () {
  1280. // test from https://github.com/wikimedia/VisualEditor/blob/d468b00311e69c2095b9da360c5745153342a5c3/src/ve.utils.js#L182-L196
  1281. var justFine = new Array(1e5 - 1);
  1282. justFine[10] = 'x';
  1283. var tooBig = new Array(1e5);
  1284. tooBig[8] = 'x';
  1285. justFine.splice(1, 1);
  1286. expect(8 in justFine).toBe(false);
  1287. expect(justFine.indexOf('x')).toBe(9);
  1288. tooBig.splice(1, 1);
  1289. expect(6 in tooBig).toBe(false);
  1290. expect(tooBig.indexOf('x')).toBe(7);
  1291. });
  1292. });
  1293. describe('#join()', function () {
  1294. it('defaults to a comma separator when none is provided', function () {
  1295. expect([1, 2].join()).toBe('1,2');
  1296. });
  1297. it('defaults to a comma separator when undefined is provided', function () {
  1298. expect([1, 2].join(undefined)).toBe('1,2');
  1299. });
  1300. it('works, extended', function () {
  1301. expect([].join()).toBe('');
  1302. expect([undefined].join()).toBe('');
  1303. expect([undefined, undefined].join()).toBe(',');
  1304. expect([null, null].join()).toBe(',');
  1305. expect([undefined, undefined].join('|')).toBe('|');
  1306. expect([null, null].join('|')).toBe('|');
  1307. expect([1, 2, 3].join('|')).toBe('1|2|3');
  1308. expect([1, 2, 3].join(null)).toBe('1null2null3');
  1309. expect([1, 2, 3].join({})).toBe('1[object Object]2[object Object]3');
  1310. expect([1, 2, 3].join('')).toBe('123');
  1311. });
  1312. it('is generic', function () {
  1313. var obj = { 0: 1, 1: 2, 2: 3, 3: 4, length: 3 };
  1314. expect(Array.prototype.join.call(obj, ',')).toBe('1,2,3');
  1315. });
  1316. it('works with a string literal', function () {
  1317. var str = '123';
  1318. expect(Array.prototype.join.call(str, ',')).toBe('1,2,3');
  1319. });
  1320. it('works with `arguments`', function () {
  1321. var args = (function () { return arguments; }(1, 2, 3));
  1322. expect(Array.prototype.join.call(args, ',')).toBe('1,2,3');
  1323. });
  1324. });
  1325. describe('#push()', function () {
  1326. it('works on arrays', function () {
  1327. var arr = [];
  1328. var result = arr.push(undefined);
  1329. expect(result).toBe(1);
  1330. expect(arr.length).toBe(1);
  1331. expect(Object.prototype.hasOwnProperty.call(arr, 0)).toBe(true);
  1332. expect(arr[0]).toBeUndefined();
  1333. });
  1334. it('is generic', function () {
  1335. var obj = {};
  1336. var result = Array.prototype.push.call(obj, undefined);
  1337. expect(result).toBe(1);
  1338. expect(obj.length).toBe(1);
  1339. expect(Object.prototype.hasOwnProperty.call(obj, 0)).toBe(true);
  1340. expect(obj[0]).toBeUndefined();
  1341. });
  1342. });
  1343. describe('#pop()', function () {
  1344. it('works on arrays', function () {
  1345. var arr = [1, 2, 3];
  1346. var result = arr.pop();
  1347. expect(result).toBe(3);
  1348. expect(arr.length).toBe(2);
  1349. expect(Object.prototype.hasOwnProperty.call(arr, 2)).toBe(false);
  1350. expect(arr[2]).toBeUndefined();
  1351. });
  1352. it('is generic', function () {
  1353. var obj = { 0: 1, 1: 2, 2: 3, length: 3 };
  1354. var result = Array.prototype.pop.call(obj);
  1355. expect(result).toBe(3);
  1356. expect(obj.length).toBe(2);
  1357. expect(Object.prototype.hasOwnProperty.call(obj, 2)).toBe(false);
  1358. expect(obj[2]).toBeUndefined();
  1359. });
  1360. });
  1361. describe('#slice()', function () {
  1362. it('works on arrays', function () {
  1363. var arr = [1, 2, 3, 4];
  1364. var result = arr.slice(1, 3);
  1365. expect(result).toEqual([2, 3]);
  1366. });
  1367. it('is generic', function () {
  1368. var obj = { 0: 1, 1: 2, 2: 3, 3: 4, length: 4 };
  1369. var result = Array.prototype.slice.call(obj, 1, 3);
  1370. expect(result).toEqual([2, 3]);
  1371. });
  1372. it('works with `arguments`', function () {
  1373. var obj = (function () {
  1374. return arguments;
  1375. }(1, 2, 3, 4));
  1376. var result = Array.prototype.slice.call(obj, 1, 3);
  1377. expect(result).toEqual([2, 3]);
  1378. });
  1379. it('boxed string access', function () {
  1380. var obj = '1234';
  1381. var result = Array.prototype.slice.call(obj, 1, 3);
  1382. expect(result).toEqual(['2', '3']);
  1383. });
  1384. if (typeof document !== 'undefined') {
  1385. it('should be able to slice a NodeList', function () {
  1386. var nodes = document.getElementsByTagName('div');
  1387. expect(Array.isArray(Array.prototype.slice.call(nodes))).toBe(true);
  1388. });
  1389. }
  1390. });
  1391. describe('#sort()', function () {
  1392. describe('usage', function () {
  1393. it('requires a function or undefined as first argument', function () {
  1394. var actual = [1, 2];
  1395. expect(actual.sort()).toBe(actual);
  1396. expect(actual.sort(undefined)).toBe(actual);
  1397. expect(actual.sort(function () { return 0; })).toBe(actual);
  1398. });
  1399. it('requires a non-function or non-undefined to throw a `TypeError`', function () {
  1400. expect(function () { [1, 2].sort(null); }).toThrow();
  1401. expect(function () { [1, 2].sort(1); }).toThrow();
  1402. expect(function () { [1, 2].sort(''); }).toThrow();
  1403. expect(function () { [1, 2].sort(true); }).toThrow();
  1404. expect(function () { [1, 2].sort({}); }).toThrow();
  1405. expect(function () { [1, 2].sort([]); }).toThrow();
  1406. expect(function () { [1, 2].sort(new Date()); }).toThrow();
  1407. expect(function () { [1, 2].sort(/pattern/); }).toThrow();
  1408. });
  1409. });
  1410. describe('ascending', function () {
  1411. it('[5,2,4,6,1,3] should result in [1,2,3,4,5,6]', function () {
  1412. var actual = [5, 2, 4, 6, 1, 3];
  1413. var expected = [1, 2, 3, 4, 5, 6];
  1414. actual.sort();
  1415. expect(actual).toEqual(expected);
  1416. });
  1417. it('[5,2,2,6,1,3] should result in [1,2,2,3,5,6]', function () {
  1418. var actual = [5, 2, 2, 6, 1, 3];
  1419. var expected = [1, 2, 2, 3, 5, 6];
  1420. actual.sort();
  1421. expect(actual).toEqual(expected);
  1422. });
  1423. it('[0,0,0,0,0,1] should result in [0,0,0,0,0,1]', function () {
  1424. var actual = [0, 0, 0, 0, 0, 1];
  1425. var expected = [0, 0, 0, 0, 0, 1];
  1426. actual.sort();
  1427. expect(actual).toEqual(expected);
  1428. });
  1429. it('[0,0,0,0,0,-1] should result in [-1,0,0,0,0,0]', function () {
  1430. var actual = [0, 0, 0, 0, 0, -1];
  1431. var expected = [-1, 0, 0, 0, 0, 0];
  1432. actual.sort();
  1433. expect(actual).toEqual(expected);
  1434. });
  1435. it('[f,e,d,a,c,b] should result in [a,b,c,d,e,f]', function () {
  1436. var actual = ['f', 'e', 'd', 'a', 'c', 'b'];
  1437. var expected = ['a', 'b', 'c', 'd', 'e', 'f'];
  1438. actual.sort();
  1439. expect(actual).toEqual(expected);
  1440. });
  1441. it('[f,e,d,,,,a,c,b] should result in [a,b,c,d,e,f,,,]', function () {
  1442. var actual = ['f', 'e', 'd', 1, 2, 'a', 'c', 'b'];
  1443. delete actual[3];
  1444. delete actual[4];
  1445. var expected = ['a', 'b', 'c', 'd', 'e', 'f'];
  1446. expected.length = 8;
  1447. actual.sort();
  1448. expect(actual).toEqual(expected);
  1449. });
  1450. it('[f,e,d,,null,,a,c,b] should result in [a,b,c,d,e,f,null,,,]', function () {
  1451. var actual = ['f', 'e', 'd', 1, null, 2, 'a', 'c', 'b'];
  1452. delete actual[3];
  1453. delete actual[5];
  1454. var expected = ['a', 'b', 'c', 'd', 'e', 'f', null];
  1455. expected.length = 9;
  1456. actual.sort();
  1457. expect(actual).toEqual(expected);
  1458. });
  1459. it('[f,e,d,,null,undefined,a,c,b] should result in [a,b,c,d,e,f,null,undefined,,]', function () {
  1460. var actual = ['f', 'e', 'd', 1, null, undefined, 'a', 'c', 'b'];
  1461. delete actual[3];
  1462. var expected = ['a', 'b', 'c', 'd', 'e', 'f', null, undefined];
  1463. expected.length = 9;
  1464. actual.sort();
  1465. expect(actual).toEqual(expected);
  1466. });
  1467. it('[] should result in []', function () {
  1468. var actual = [];
  1469. var expected = [];
  1470. actual.sort();
  1471. expect(actual).toEqual(expected);
  1472. });
  1473. it('[1] should result in [1]', function () {
  1474. var actual = [1];
  1475. var expected = [1];
  1476. actual.sort();
  1477. expect(actual).toEqual(expected);
  1478. });
  1479. it('result should find only greater or equal values', function () {
  1480. var actual = [];
  1481. var i;
  1482. for (i = 0; i < 100; i += 1) {
  1483. actual.push(('00' + (Math.random() * 100).toFixed(0)).slice(-3));
  1484. }
  1485. actual.sort();
  1486. for (i = 0; i < actual.length - 1; i += 1) {
  1487. expect(actual[i] <= actual[i + 1]).toBe(true);
  1488. }
  1489. });
  1490. });
  1491. describe('descending', function () {
  1492. var descending = function (left, right) {
  1493. var leftS = String(left);
  1494. var rightS = String(right);
  1495. if (leftS === rightS) {
  1496. return +0;
  1497. }
  1498. if (leftS < rightS) {
  1499. return 1;
  1500. }
  1501. return -1;
  1502. };
  1503. it('[5,2,4,6,1,3] should result in [6,5,4,3,2,1]', function () {
  1504. var actual = [5, 2, 4, 6, 1, 3];
  1505. var expected = [6, 5, 4, 3, 2, 1];
  1506. actual.sort(descending);
  1507. expect(actual).toEqual(expected);
  1508. });
  1509. it('[5,2,2,6,1,3] should result in [6,5,4,2,2,1]', function () {
  1510. var actual = [5, 2, 2, 6, 1, 3];
  1511. var expected = [6, 5, 3, 2, 2, 1];
  1512. actual.sort(descending);
  1513. expect(actual).toEqual(expected);
  1514. });
  1515. it('[0,0,0,0,0,1] should result in [1,0,0,0,0,0]', function () {
  1516. var actual = [0, 0, 0, 0, 0, 1];
  1517. var expected = [1, 0, 0, 0, 0, 0];
  1518. actual.sort(descending);
  1519. expect(actual).toEqual(expected);
  1520. });
  1521. it('[0,0,0,0,0,-1] should result in [0,0,0,0,0,-1]', function () {
  1522. var actual = [0, 0, 0, 0, 0, -1];
  1523. var expected = [0, 0, 0, 0, 0, -1];
  1524. actual.sort(descending);
  1525. expect(actual).toEqual(expected);
  1526. });
  1527. it('[f,e,d,a,c,b] should result in [f,e,d,c,b,a]', function () {
  1528. var actual = ['f', 'e', 'd', 'a', 'c', 'b'];
  1529. var expected = ['f', 'e', 'd', 'c', 'b', 'a'];
  1530. actual.sort(descending);
  1531. expect(actual).toEqual(expected);
  1532. });
  1533. it('[f,e,d,,,a,c,b] should result in [f,e,d,c,b,a,,,]', function () {
  1534. var actual = ['f', 'e', 'd', 1, 2, 'a', 'c', 'b'];
  1535. delete actual[3];
  1536. delete actual[4];
  1537. var expected = ['f', 'e', 'd', 'c', 'b', 'a'];
  1538. expected.length = 8;
  1539. actual.sort(descending);
  1540. expect(actual).toEqual(expected);
  1541. });
  1542. it('[f,e,d,,null,,a,c,b] should result in [null,f,e,d,c,b,a,,,]', function () {
  1543. var actual = ['f', 'e', 'd', 1, null, 2, 'a', 'c', 'b'];
  1544. delete actual[3];
  1545. delete actual[5];
  1546. var expected = [null, 'f', 'e', 'd', 'c', 'b', 'a'];
  1547. expected.length = 9;
  1548. actual.sort(descending);
  1549. expect(actual).toEqual(expected);
  1550. });
  1551. it('[f,e,d,undefined,null,,a,c,b] should result in [null,f,e,d,c,b,a,undefined,,]', function () {
  1552. var actual = ['f', 'e', 'd', undefined, null, 2, 'a', 'c', 'b'];
  1553. delete actual[5];
  1554. var expected = [null, 'f', 'e', 'd', 'c', 'b', 'a', undefined];
  1555. expected.length = 9;
  1556. actual.sort(descending);
  1557. expect(actual).toEqual(expected);
  1558. });
  1559. it('[] should result in []', function () {
  1560. var actual = [];
  1561. var expected = [];
  1562. actual.sort(descending);
  1563. expect(actual).toEqual(expected);
  1564. });
  1565. it('[1] should result in [1]', function () {
  1566. var actual = [1];
  1567. var expected = [1];
  1568. actual.sort(descending);
  1569. expect(actual).toEqual(expected);
  1570. });
  1571. it('result should find only lesser or equal values', function () {
  1572. var actual = [];
  1573. var i;
  1574. for (i = 0; i < 100; i += 1) {
  1575. actual.push(('00' + (Math.random() * 100).toFixed(0)).slice(-3));
  1576. }
  1577. actual.sort(descending);
  1578. for (i = 0; i < actual.length - 1; i += 1) {
  1579. expect(actual[i] >= actual[i + 1]).toBe(true);
  1580. }
  1581. });
  1582. });
  1583. describe('returned value', function () {
  1584. it('should be the source object', function () {
  1585. var actual = [1, 3, 2];
  1586. expect(actual.sort()).toBe(actual);
  1587. });
  1588. });
  1589. describe('when used generically', function () {
  1590. var descending = function (left, right) {
  1591. var leftS = String(left);
  1592. var rightS = String(right);
  1593. if (leftS === rightS) {
  1594. return +0;
  1595. }
  1596. if (leftS < rightS) {
  1597. return 1;
  1598. }
  1599. return -1;
  1600. };
  1601. var args = function () {
  1602. return arguments;
  1603. };
  1604. it('should not sort objects without length', function () {
  1605. var actual = {
  1606. 0: 5,
  1607. 1: 2,
  1608. 2: 4,
  1609. 3: 6,
  1610. 4: 1,
  1611. 5: 3
  1612. };
  1613. var expected = {
  1614. 0: 5,
  1615. 1: 2,
  1616. 2: 4,
  1617. 3: 6,
  1618. 4: 1,
  1619. 5: 3
  1620. };
  1621. Array.prototype.sort.call(actual);
  1622. expect(actual).toEqual(expected);
  1623. Array.prototype.sort.call(actual, descending);
  1624. expect(actual).toEqual(expected);
  1625. });
  1626. it('should sort objects ascending with length', function () {
  1627. var actual = {
  1628. 0: 5,
  1629. 1: 2,
  1630. 2: 4,
  1631. 3: 6,
  1632. 4: 1,
  1633. 5: 3,
  1634. length: 6
  1635. };
  1636. var expected = {
  1637. 0: 1,
  1638. 1: 2,
  1639. 2: 3,
  1640. 3: 4,
  1641. 4: 5,
  1642. 5: 6,
  1643. length: 6
  1644. };
  1645. Array.prototype.sort.call(actual);
  1646. expect(actual).toEqual(expected);
  1647. });
  1648. it('should sort objects descending with length', function () {
  1649. var actual = {
  1650. 0: 5,
  1651. 1: 2,
  1652. 2: 4,
  1653. 3: 6,
  1654. 4: 1,
  1655. 5: 3,
  1656. length: 6
  1657. };
  1658. var expected = {
  1659. 0: 6,
  1660. 1: 5,
  1661. 2: 4,
  1662. 3: 3,
  1663. 4: 2,
  1664. 5: 1,
  1665. length: 6
  1666. };
  1667. Array.prototype.sort.call(actual, descending);
  1668. expect(actual).toEqual(expected);
  1669. });
  1670. it('should sort objects descending with mixed content types and with length', function () {
  1671. var actual = {
  1672. 0: 5,
  1673. 1: 2,
  1674. 2: 4,
  1675. 4: null,
  1676. 6: 1,
  1677. 7: 3,
  1678. length: 8
  1679. };
  1680. var expected = {
  1681. 0: null,
  1682. 1: 5,
  1683. 2: 4,
  1684. 3: 3,
  1685. 4: 2,
  1686. 5: 1,
  1687. length: 8
  1688. };
  1689. Array.prototype.sort.call(actual, descending);
  1690. expect(actual).toEqual(expected);
  1691. });
  1692. it('should sort `arguments` object ascending', function () {
  1693. var actual = args(5, 2, 4, 6, 1, 3);
  1694. var expected = args(1, 2, 3, 4, 5, 6);
  1695. Array.prototype.sort.call(actual);
  1696. expect(actual).toEqual(expected);
  1697. });
  1698. it('should sort `arguments` object ascending with mixed content types', function () {
  1699. var actual = args(5, 2, 4, null, 1, 3);
  1700. var expected = args(1, 2, 3, 4, 5, null);
  1701. Array.prototype.sort.call(actual);
  1702. expect(actual).toEqual(expected);
  1703. });
  1704. it('should sort `arguments` object descending', function () {
  1705. var actual = args(5, 2, 4, 6, 1, 3);
  1706. var expected = args(6, 5, 4, 3, 2, 1);
  1707. Array.prototype.sort.call(actual, descending);
  1708. expect(actual).toEqual(expected);
  1709. });
  1710. it('should sort `arguments` object descending with mixed content types', function () {
  1711. var actual = args(5, 2, 4, null, 1, 3);
  1712. var expected = args(null, 5, 4, 3, 2, 1);
  1713. Array.prototype.sort.call(actual, descending);
  1714. expect(actual).toEqual(expected);
  1715. });
  1716. });
  1717. });
  1718. });