s-array.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. describe('Array', function() {
  2. var testSubject;
  3. beforeEach(function() {
  4. testSubject = [2, 3, undefined, true, 'hej', null, false, 0];
  5. delete testSubject[1];
  6. });
  7. function createArrayLikeFromArray(arr) {
  8. var o = {};
  9. Array.prototype.forEach.call(arr, function(e, i) {
  10. o[i]=e;
  11. });
  12. o.length = arr.length;
  13. return o;
  14. };
  15. describe('forEach', function() {
  16. "use strict";
  17. var expected, actual;
  18. beforeEach(function() {
  19. expected = {0:2, 2: undefined, 3:true, 4: 'hej', 5:null, 6:false, 7:0 };
  20. actual = {};
  21. });
  22. it('should pass the right parameters', function() {
  23. var callback = jasmine.createSpy('callback'),
  24. array = ['1'];
  25. array.forEach(callback);
  26. expect(callback).toHaveBeenCalledWith('1', 0, array);
  27. });
  28. it('should not affect elements added to the array after it has begun', function() {
  29. var arr = [1,2,3],
  30. i = 0;
  31. arr.forEach(function(a) {
  32. i++;
  33. arr.push(a+3);
  34. });
  35. expect(arr).toEqual([1,2,3,4,5,6]);
  36. expect(i).toBe(3);
  37. });
  38. it('should set the right context when given none', function() {
  39. var context;
  40. [1].forEach(function() {context = this;});
  41. expect(context).toBe(function() {return this}.call());
  42. });
  43. it('should iterate all', function() {
  44. testSubject.forEach(function(obj, index) {
  45. actual[index] = obj;
  46. });
  47. expect(actual).toExactlyMatch(expected);
  48. });
  49. it('should iterate all using a context', function() {
  50. var o = { a: actual };
  51. testSubject.forEach(function(obj, index) {
  52. this.a[index] = obj;
  53. }, o);
  54. expect(actual).toExactlyMatch(expected);
  55. });
  56. it('should iterate all in an array-like object', function() {
  57. var ts = createArrayLikeFromArray(testSubject);
  58. Array.prototype.forEach.call(ts, function(obj, index) {
  59. actual[index] = obj;
  60. });
  61. expect(actual).toExactlyMatch(expected);
  62. });
  63. it('should iterate all in an array-like object using a context', function() {
  64. var ts = createArrayLikeFromArray(testSubject),
  65. o = { a: actual };
  66. Array.prototype.forEach.call(ts, function(obj, index) {
  67. this.a[index] = obj;
  68. }, o);
  69. expect(actual).toExactlyMatch(expected);
  70. });
  71. describe('strings', function() {
  72. var str = 'Hello, World!',
  73. toString = Object.prototype.toString;
  74. it('should iterate all in a string', function() {
  75. actual = [];
  76. Array.prototype.forEach.call(str, function(item, index) {
  77. actual[index] = item;
  78. });
  79. expect(actual).toExactlyMatch(str.split(''));
  80. });
  81. it('should iterate all in a string using a context', function() {
  82. actual = [];
  83. var o = { a: actual };
  84. Array.prototype.forEach.call(str, function(item, index) {
  85. this.a[index] = item;
  86. }, o);
  87. expect(actual).toExactlyMatch(str.split(''));
  88. });
  89. it('should have String object for third argument of callback', function() {
  90. Array.prototype.forEach.call(str, function(item, index, obj) {
  91. actual = obj;
  92. });
  93. expect(typeof actual).toBe("object");
  94. expect(toString.call(actual)).toBe("[object String]");
  95. });
  96. });
  97. });
  98. describe('some', function() {
  99. var actual, expected, numberOfRuns;
  100. beforeEach(function() {
  101. expected = {0:2, 2: undefined, 3:true };
  102. actual = {};
  103. numberOfRuns = 0;
  104. });
  105. it('should pass the correct values along to the callback', function() {
  106. var callback = jasmine.createSpy('callback');
  107. var array = ['1'];
  108. array.some(callback);
  109. expect(callback).toHaveBeenCalledWith('1', 0, array);
  110. });
  111. it('should not affect elements added to the array after it has begun', function() {
  112. var arr = [1,2,3],
  113. i = 0;
  114. arr.some(function(a) {
  115. i++;
  116. arr.push(a+3);
  117. return i > 3;
  118. });
  119. expect(arr).toEqual([1,2,3,4,5,6]);
  120. expect(i).toBe(3);
  121. });
  122. it('should set the right context when given none', function() {
  123. var context;
  124. [1].some(function() {context = this;});
  125. expect(context).toBe(function() {return this}.call());
  126. });
  127. it('should return false if it runs to the end', function() {
  128. actual = testSubject.some(function() {});
  129. expect(actual).toBeFalsy();
  130. });
  131. it('should return true if it is stopped somewhere', function() {
  132. actual = testSubject.some(function() { return true; });
  133. expect(actual).toBeTruthy();
  134. });
  135. it('should return false if there are no elements', function() {
  136. actual = [].some(function() { return true; });
  137. expect(actual).toBeFalsy();
  138. });
  139. it('should stop after 3 elements', function() {
  140. testSubject.some(function(obj, index) {
  141. actual[index] = obj;
  142. numberOfRuns += 1;
  143. if(numberOfRuns == 3) {
  144. return true;
  145. }
  146. return false;
  147. });
  148. expect(actual).toExactlyMatch(expected);
  149. });
  150. it('should stop after 3 elements using a context', function() {
  151. var o = { a: actual };
  152. testSubject.some(function(obj, index) {
  153. this.a[index] = obj;
  154. numberOfRuns += 1;
  155. if(numberOfRuns == 3) {
  156. return true;
  157. }
  158. return false;
  159. }, o);
  160. expect(actual).toExactlyMatch(expected);
  161. });
  162. it('should stop after 3 elements in an array-like object', function() {
  163. var ts = createArrayLikeFromArray(testSubject);
  164. Array.prototype.some.call(ts, function(obj, index) {
  165. actual[index] = obj;
  166. numberOfRuns += 1;
  167. if(numberOfRuns == 3) {
  168. return true;
  169. }
  170. return false;
  171. });
  172. expect(actual).toExactlyMatch(expected);
  173. });
  174. it('should stop after 3 elements in an array-like object using a context', function() {
  175. var ts = createArrayLikeFromArray(testSubject);
  176. var o = { a: actual };
  177. Array.prototype.some.call(ts, function(obj, index) {
  178. this.a[index] = obj;
  179. numberOfRuns += 1;
  180. if(numberOfRuns == 3) {
  181. return true;
  182. }
  183. return false;
  184. }, o);
  185. expect(actual).toExactlyMatch(expected);
  186. });
  187. });
  188. describe('every', function() {
  189. var actual, expected, numberOfRuns;
  190. beforeEach(function() {
  191. expected = {0:2, 2: undefined, 3:true };
  192. actual = {};
  193. numberOfRuns = 0;
  194. });
  195. it('should pass the correct values along to the callback', function() {
  196. var callback = jasmine.createSpy('callback');
  197. var array = ['1'];
  198. array.every(callback);
  199. expect(callback).toHaveBeenCalledWith('1', 0, array);
  200. });
  201. it('should not affect elements added to the array after it has begun', function() {
  202. var arr = [1,2,3],
  203. i = 0;
  204. arr.every(function(a) {
  205. i++;
  206. arr.push(a+3);
  207. return i <= 3;
  208. });
  209. expect(arr).toEqual([1,2,3,4,5,6]);
  210. expect(i).toBe(3);
  211. });
  212. it('should set the right context when given none', function() {
  213. var context;
  214. [1].every(function() {context = this;});
  215. expect(context).toBe(function() {return this}.call());
  216. });
  217. it('should return true if the array is empty', function() {
  218. actual = [].every(function() { return true; });
  219. expect(actual).toBeTruthy();
  220. actual = [].every(function() { return false; });
  221. expect(actual).toBeTruthy();
  222. });
  223. it('should return true if it runs to the end', function() {
  224. actual = [1,2,3].every(function() { return true; });
  225. expect(actual).toBeTruthy();
  226. });
  227. it('should return false if it is stopped before the end', function() {
  228. actual = [1,2,3].every(function() { return false; });
  229. expect(actual).toBeFalsy();
  230. });
  231. it('should return after 3 elements', function() {
  232. testSubject.every(function(obj, index) {
  233. actual[index] = obj;
  234. numberOfRuns += 1;
  235. if(numberOfRuns == 3) {
  236. return false;
  237. }
  238. return true;
  239. });
  240. expect(actual).toExactlyMatch(expected);
  241. });
  242. it('should stop after 3 elements using a context', function() {
  243. var o = { a: actual };
  244. testSubject.every(function(obj, index) {
  245. this.a[index] = obj;
  246. numberOfRuns += 1;
  247. if(numberOfRuns == 3) {
  248. return false;
  249. }
  250. return true;
  251. }, o);
  252. expect(actual).toExactlyMatch(expected);
  253. });
  254. it('should stop after 3 elements in an array-like object', function() {
  255. var ts = createArrayLikeFromArray(testSubject);
  256. Array.prototype.every.call(ts, function(obj, index) {
  257. actual[index] = obj;
  258. numberOfRuns += 1;
  259. if(numberOfRuns == 3) {
  260. return false;
  261. }
  262. return true;
  263. });
  264. expect(actual).toExactlyMatch(expected);
  265. });
  266. it('should stop after 3 elements in an array-like object using a context', function() {
  267. var ts = createArrayLikeFromArray(testSubject);
  268. var o = { a: actual };
  269. Array.prototype.every.call(ts, function(obj, index) {
  270. this.a[index] = obj;
  271. numberOfRuns += 1;
  272. if(numberOfRuns == 3) {
  273. return false;
  274. }
  275. return true;
  276. }, o);
  277. expect(actual).toExactlyMatch(expected);
  278. });
  279. });
  280. describe('indexOf', function() {
  281. "use strict";
  282. var actual, expected, testSubject;
  283. beforeEach(function() {
  284. testSubject = [2, 3, undefined, true, 'hej', null, 2, false, 0];
  285. delete testSubject[1];
  286. });
  287. it('should find the element', function() {
  288. expected = 4;
  289. actual = testSubject.indexOf('hej');
  290. expect(actual).toEqual(expected);
  291. });
  292. it('should not find the element', function() {
  293. expected = -1;
  294. actual = testSubject.indexOf('mus');
  295. expect(actual).toEqual(expected);
  296. });
  297. it('should find undefined as well', function() {
  298. expected = -1;
  299. actual = testSubject.indexOf(undefined);
  300. expect(actual).not.toEqual(expected);
  301. });
  302. it('should skip unset indexes', function() {
  303. expected = 2;
  304. actual = testSubject.indexOf(undefined);
  305. expect(actual).toEqual(expected);
  306. });
  307. it('should use a strict test', function() {
  308. actual = testSubject.indexOf(null);
  309. expect(actual).toEqual(5);
  310. actual = testSubject.indexOf('2');
  311. expect(actual).toEqual(-1);
  312. });
  313. it('should skip the first if fromIndex is set', function() {
  314. expect(testSubject.indexOf(2, 2)).toEqual(6);
  315. expect(testSubject.indexOf(2, 0)).toEqual(0);
  316. expect(testSubject.indexOf(2, 6)).toEqual(6);
  317. });
  318. it('should work with negative fromIndex', function() {
  319. expect(testSubject.indexOf(2, -3)).toEqual(6);
  320. expect(testSubject.indexOf(2, -9)).toEqual(0);
  321. });
  322. it('should work with fromIndex being greater than the length', function() {
  323. expect(testSubject.indexOf(0, 20)).toEqual(-1);
  324. });
  325. it('should work with fromIndex being negative and greater than the length', function() {
  326. expect(testSubject.indexOf('hej', -20)).toEqual(4);
  327. });
  328. describe('Array-like', function ArrayLike() {
  329. var indexOf = Array.prototype.indexOf,
  330. testAL;
  331. beforeEach(function beforeEach() {
  332. testAL = {};
  333. testSubject = [2, 3, undefined, true, 'hej', null, 2, false, 0];
  334. testSubject.forEach(function (o,i) {
  335. testAL[i] = o;
  336. });
  337. testAL.length = testSubject.length;
  338. });
  339. it('should find the element (array-like)', function() {
  340. expected = 4;
  341. actual = indexOf.call(testAL, 'hej');
  342. expect(actual).toEqual(expected);
  343. });
  344. it('should not find the element (array-like)', function() {
  345. expected = -1;
  346. actual = indexOf.call(testAL, 'mus');
  347. expect(actual).toEqual(expected);
  348. });
  349. it('should find undefined as well (array-like)', function() {
  350. expected = -1;
  351. actual = indexOf.call(testAL, undefined);
  352. expect(actual).not.toEqual(expected);
  353. });
  354. it('should skip unset indexes (array-like)', function() {
  355. expected = 2;
  356. actual = indexOf.call(testAL, undefined);
  357. expect(actual).toEqual(expected);
  358. });
  359. it('should use a strict test (array-like)', function() {
  360. actual = Array.prototype.indexOf.call(testAL, null);
  361. expect(actual).toEqual(5);
  362. actual = Array.prototype.indexOf.call(testAL, '2');
  363. expect(actual).toEqual(-1);
  364. });
  365. it('should skip the first if fromIndex is set (array-like)', function() {
  366. expect(indexOf.call(testAL, 2, 2)).toEqual(6);
  367. expect(indexOf.call(testAL, 2, 0)).toEqual(0);
  368. expect(indexOf.call(testAL, 2, 6)).toEqual(6);
  369. });
  370. it('should work with negative fromIndex (array-like)', function() {
  371. expect(indexOf.call(testAL, 2, -3)).toEqual(6);
  372. expect(indexOf.call(testAL, 2, -9)).toEqual(0);
  373. });
  374. it('should work with fromIndex being greater than the length (array-like)', function() {
  375. expect(indexOf.call(testAL, 0, 20)).toEqual(-1);
  376. });
  377. it('should work with fromIndex being negative and greater than the length (array-like)', function() {
  378. expect(indexOf.call(testAL, 'hej', -20)).toEqual(4);
  379. });
  380. });
  381. });
  382. describe('lastIndexOf', function() {
  383. "use strict";
  384. var actual, expected, testSubject, testAL;
  385. beforeEach(function() {
  386. testSubject = [2, 3, undefined, true, 'hej', null, 2, 3, false, 0];
  387. delete testSubject[1];
  388. delete testSubject[7];
  389. });
  390. describe('Array', function() {
  391. it('should find the element', function() {
  392. expected = 4;
  393. actual = testSubject.lastIndexOf('hej');
  394. expect(actual).toEqual(expected);
  395. });
  396. it('should not find the element', function() {
  397. expected = -1;
  398. actual = testSubject.lastIndexOf('mus');
  399. expect(actual).toEqual(expected);
  400. });
  401. it('should find undefined as well', function() {
  402. expected = -1;
  403. actual = testSubject.lastIndexOf(undefined);
  404. expect(actual).not.toEqual(expected);
  405. });
  406. it('should skip unset indexes', function() {
  407. expected = 2;
  408. actual = testSubject.lastIndexOf(undefined);
  409. expect(actual).toEqual(expected);
  410. });
  411. it('should use a strict test', function() {
  412. actual = testSubject.lastIndexOf(null);
  413. expect(actual).toEqual(5);
  414. actual = testSubject.lastIndexOf('2');
  415. expect(actual).toEqual(-1);
  416. });
  417. it('should skip the first if fromIndex is set', function() {
  418. expect(testSubject.lastIndexOf(2, 2)).toEqual(0);
  419. expect(testSubject.lastIndexOf(2, 0)).toEqual(0);
  420. expect(testSubject.lastIndexOf(2, 6)).toEqual(6);
  421. });
  422. it('should work with negative fromIndex', function() {
  423. expect(testSubject.lastIndexOf(2, -3)).toEqual(6);
  424. expect(testSubject.lastIndexOf(2, -9)).toEqual(0);
  425. });
  426. it('should work with fromIndex being greater than the length', function() {
  427. expect(testSubject.lastIndexOf(2, 20)).toEqual(6);
  428. });
  429. it('should work with fromIndex being negative and greater than the length', function() {
  430. expect(testSubject.lastIndexOf(2, -20)).toEqual(-1);
  431. });
  432. });
  433. describe('Array like', function() {
  434. var lastIndexOf = Array.prototype.lastIndexOf,
  435. testAL;
  436. beforeEach(function() {
  437. testAL = {};
  438. testSubject.forEach(function (o,i) {
  439. testAL[i] = o;
  440. });
  441. testAL.length = testSubject.length;
  442. });
  443. it('should find the element (array-like)', function() {
  444. expected = 4;
  445. actual = lastIndexOf.call(testAL, 'hej');
  446. expect(actual).toEqual(expected);
  447. });
  448. it('should not find the element (array-like)', function() {
  449. expected = -1;
  450. actual = lastIndexOf.call(testAL, 'mus');
  451. expect(actual).toEqual(expected);
  452. });
  453. it('should find undefined as well (array-like)', function() {
  454. expected = -1;
  455. actual = lastIndexOf.call(testAL, undefined);
  456. expect(actual).not.toEqual(expected);
  457. });
  458. it('should skip unset indexes (array-like)', function() {
  459. expected = 2;
  460. actual = lastIndexOf.call(testAL, undefined);
  461. expect(actual).toEqual(expected);
  462. });
  463. it('should use a strict test (array-like)', function() {
  464. actual = lastIndexOf.call(testAL, null);
  465. expect(actual).toEqual(5);
  466. actual = lastIndexOf.call(testAL, '2');
  467. expect(actual).toEqual(-1);
  468. });
  469. it('should skip the first if fromIndex is set', function() {
  470. expect(lastIndexOf.call(testAL, 2, 2)).toEqual(0);
  471. expect(lastIndexOf.call(testAL, 2, 0)).toEqual(0);
  472. expect(lastIndexOf.call(testAL, 2, 6)).toEqual(6);
  473. });
  474. it('should work with negative fromIndex', function() {
  475. expect(lastIndexOf.call(testAL, 2, -3)).toEqual(6);
  476. expect(lastIndexOf.call(testAL, 2, -9)).toEqual(0);
  477. });
  478. it('should work with fromIndex being greater than the length', function() {
  479. expect(lastIndexOf.call(testAL, 2, 20)).toEqual(6);
  480. });
  481. it('should work with fromIndex being negative and greater than the length', function() {
  482. expect(lastIndexOf.call(testAL, 2, -20)).toEqual(-1);
  483. });
  484. });
  485. });
  486. describe('filter', function() {
  487. var filteredArray,
  488. callback = function callback(o, i, arr) {
  489. return (
  490. i != 3 && i != 5
  491. );
  492. };
  493. beforeEach(function() {
  494. testSubject = [2, 3, undefined, true, 'hej', 3, null, false, 0];
  495. delete testSubject[1];
  496. filteredArray = [2, undefined, 'hej', null, false, 0];
  497. });
  498. describe('Array object', function() {
  499. it('should call the callback with the proper arguments', function() {
  500. var callback = jasmine.createSpy('callback'),
  501. arr = ['1'];
  502. arr.filter(callback);
  503. expect(callback).toHaveBeenCalledWith('1', 0, arr);
  504. });
  505. it('should not affect elements added to the array after it has begun', function() {
  506. var arr = [1,2,3],
  507. i = 0;
  508. arr.filter(function(a) {
  509. i++;
  510. if(i <= 4) {
  511. arr.push(a+3);
  512. }
  513. return true;
  514. });
  515. expect(arr).toEqual([1,2,3,4,5,6]);
  516. expect(i).toBe(3);
  517. });
  518. it('should skip non-set values', function() {
  519. var passedValues = {};
  520. testSubject = [1,2,3,4];
  521. delete testSubject[1];
  522. testSubject.filter(function(o, i) {
  523. passedValues[i] = o;
  524. return true;
  525. });
  526. expect(passedValues).toExactlyMatch(testSubject);
  527. });
  528. it('should pass the right context to the filter', function() {
  529. var passedValues = {};
  530. testSubject = [1,2,3,4];
  531. delete testSubject[1];
  532. testSubject.filter(function(o, i) {
  533. this[i] = o;
  534. return true;
  535. }, passedValues);
  536. expect(passedValues).toExactlyMatch(testSubject);
  537. });
  538. it('should set the right context when given none', function() {
  539. var context;
  540. [1].filter(function() {context = this;});
  541. expect(context).toBe(function() {return this}.call());
  542. });
  543. it('should remove only the values for which the callback returns false', function() {
  544. var result = testSubject.filter(callback);
  545. expect(result).toExactlyMatch(filteredArray);
  546. });
  547. it('should leave the original array untouched', function() {
  548. var copy = testSubject.slice();
  549. testSubject.filter(callback);
  550. expect(testSubject).toExactlyMatch(copy);
  551. });
  552. it('should not be affected by same-index mutation', function () {
  553. var results = [1, 2, 3]
  554. .filter(function (value, index, array) {
  555. array[index] = 'a';
  556. return true;
  557. });
  558. expect(results).toEqual([1, 2, 3]);
  559. });
  560. });
  561. describe('Array like', function() {
  562. beforeEach(function() {
  563. testSubject = createArrayLikeFromArray(testSubject);
  564. });
  565. it('should call the callback with the proper arguments', function() {
  566. var callback = jasmine.createSpy('callback'),
  567. arr = createArrayLikeFromArray(['1']);
  568. Array.prototype.filter.call(arr, callback);
  569. expect(callback).toHaveBeenCalledWith('1', 0, arr);
  570. });
  571. it('should not affect elements added to the array after it has begun', function() {
  572. var arr = createArrayLikeFromArray([1,2,3]),
  573. i = 0;
  574. Array.prototype.filter.call(arr, function(a) {
  575. i++;
  576. if(i <= 4) {
  577. arr[i+2] = a+3;
  578. }
  579. return true;
  580. });
  581. delete arr.length;
  582. expect(arr).toExactlyMatch([1,2,3,4,5,6]);
  583. expect(i).toBe(3);
  584. });
  585. it('should skip non-set values', function() {
  586. var passedValues = {};
  587. testSubject = createArrayLikeFromArray([1,2,3,4]);
  588. delete testSubject[1];
  589. Array.prototype.filter.call(testSubject, function(o, i) {
  590. passedValues[i] = o;
  591. return true;
  592. });
  593. delete testSubject.length;
  594. expect(passedValues).toExactlyMatch(testSubject);
  595. });
  596. it('should set the right context when given none', function() {
  597. var context;
  598. Array.prototype.filter.call(createArrayLikeFromArray([1]), function() {context = this;}, undefined);
  599. expect(context).toBe(function() {return this}.call());
  600. });
  601. it('should pass the right context to the filter', function() {
  602. var passedValues = {};
  603. testSubject = createArrayLikeFromArray([1,2,3,4]);
  604. delete testSubject[1];
  605. Array.prototype.filter.call(testSubject, function(o, i) {
  606. this[i] = o;
  607. return true;
  608. }, passedValues);
  609. delete testSubject.length;
  610. expect(passedValues).toExactlyMatch(testSubject);
  611. });
  612. it('should remove only the values for which the callback returns false', function() {
  613. var result = Array.prototype.filter.call(testSubject, callback);
  614. expect(result).toExactlyMatch(filteredArray);
  615. });
  616. it('should leave the original array untouched', function() {
  617. var copy = createArrayLikeFromArray(testSubject);
  618. Array.prototype.filter.call(testSubject, callback);
  619. expect(testSubject).toExactlyMatch(copy);
  620. });
  621. });
  622. });
  623. describe('map', function() {
  624. var callback;
  625. beforeEach(function() {
  626. var i = 0;
  627. callback = function() {
  628. return i++;
  629. };
  630. });
  631. describe('Array object', function() {
  632. it('should call callback with the right parameters', function() {
  633. var callback = jasmine.createSpy('callback'),
  634. array = [1];
  635. array.map(callback);
  636. expect(callback).toHaveBeenCalledWith(1, 0, array);
  637. });
  638. it('should set the context correctly', function() {
  639. var context = {};
  640. testSubject.map(function(o,i) {
  641. this[i] = o;
  642. }, context);
  643. expect(context).toExactlyMatch(testSubject);
  644. });
  645. it('should set the right context when given none', function() {
  646. var context;
  647. [1].map(function() {context = this;});
  648. expect(context).toBe(function() {return this}.call());
  649. });
  650. it('should not change the array it is called on', function() {
  651. var copy = testSubject.slice();
  652. testSubject.map(callback);
  653. expect(testSubject).toExactlyMatch(copy);
  654. });
  655. it('should only run for the number of objects in the array when it started', function() {
  656. var arr = [1,2,3],
  657. i = 0;
  658. arr.map(function(o) {
  659. arr.push(o+3);
  660. i++;
  661. return o;
  662. });
  663. expect(arr).toExactlyMatch([1,2,3,4,5,6]);
  664. expect(i).toBe(3);
  665. });
  666. it('should properly translate the values as according to the callback', function() {
  667. var result = testSubject.map(callback),
  668. expected = [0,0,1,2,3,4,5,6];
  669. delete expected[1];
  670. expect(result).toExactlyMatch(expected);
  671. });
  672. it('should skip non-existing values', function() {
  673. var array = [1,2,3,4],
  674. i = 0;
  675. delete array[2];
  676. array.map(function() {
  677. i++;
  678. });
  679. expect(i).toBe(3);
  680. });
  681. });
  682. describe('Array-like', function() {
  683. beforeEach(function() {
  684. testSubject = createArrayLikeFromArray(testSubject);
  685. });
  686. it('should call callback with the right parameters', function() {
  687. var callback = jasmine.createSpy('callback'),
  688. array = createArrayLikeFromArray([1]);
  689. Array.prototype.map.call(array, callback);
  690. expect(callback).toHaveBeenCalledWith(1, 0, array);
  691. });
  692. it('should set the context correctly', function() {
  693. var context = {};
  694. Array.prototype.map.call(testSubject, function(o,i) {
  695. this[i] = o;
  696. }, context);
  697. delete testSubject.length;
  698. expect(context).toExactlyMatch(testSubject);
  699. });
  700. it('should set the right context when given none', function() {
  701. var context;
  702. Array.prototype.map.call(createArrayLikeFromArray([1]), function() {context = this;});
  703. expect(context).toBe(function() {return this}.call());
  704. });
  705. it('should not change the array it is called on', function() {
  706. var copy = createArrayLikeFromArray(testSubject);
  707. Array.prototype.map.call(testSubject, callback);
  708. expect(testSubject).toExactlyMatch(copy);
  709. });
  710. it('should only run for the number of objects in the array when it started', function() {
  711. var arr = createArrayLikeFromArray([1,2,3]),
  712. i = 0;
  713. Array.prototype.map.call(arr, function(o) {
  714. Array.prototype.push.call(arr, o+3);
  715. i++;
  716. return o;
  717. });
  718. delete arr.length;
  719. expect(arr).toExactlyMatch([1,2,3,4,5,6]);
  720. expect(i).toBe(3);
  721. });
  722. it('should properly translate the values as according to the callback', function() {
  723. var result = Array.prototype.map.call(testSubject, callback),
  724. expected = [0,0,1,2,3,4,5,6];
  725. delete expected[1];
  726. expect(result).toExactlyMatch(expected);
  727. });
  728. it('should skip non-existing values', function() {
  729. var array = createArrayLikeFromArray([1,2,3,4]),
  730. i = 0;
  731. delete array[2];
  732. Array.prototype.map.call(array, function() {
  733. i++;
  734. });
  735. expect(i).toBe(3);
  736. });
  737. });
  738. });
  739. describe('reduce', function() {
  740. beforeEach(function() {
  741. testSubject = [1,2,3];
  742. });
  743. describe('Array', function() {
  744. it('should pass the correct arguments to the callback', function() {
  745. var spy = jasmine.createSpy().andReturn(0);
  746. testSubject.reduce(spy);
  747. expect(spy.calls[0].args).toExactlyMatch([1, 2, 1, testSubject]);
  748. });
  749. it('should start with the right initialValue', function() {
  750. var spy = jasmine.createSpy().andReturn(0);
  751. testSubject.reduce(spy, 0);
  752. expect(spy.calls[0].args).toExactlyMatch([0, 1, 0, testSubject]);
  753. });
  754. it('should not affect elements added to the array after it has begun', function() {
  755. var arr = [1,2,3],
  756. i = 0;
  757. arr.reduce(function(a, b) {
  758. i++;
  759. if(i <= 4) {
  760. arr.push(a+3);
  761. };
  762. return b;
  763. });
  764. expect(arr).toEqual([1,2,3,4,5]);
  765. expect(i).toBe(2);
  766. });
  767. it('should work as expected for empty arrays', function() {
  768. var spy = jasmine.createSpy();
  769. expect(function() {
  770. [].reduce(spy);
  771. }).toThrow();
  772. expect(spy).not.toHaveBeenCalled();
  773. });
  774. it('should throw correctly if no callback is given', function() {
  775. expect(function() {
  776. testSubject.reduce();
  777. }).toThrow();
  778. });
  779. it('should return the expected result', function() {
  780. expect(testSubject.reduce(function(a,b) {
  781. return (a||'').toString()+(b||'').toString();
  782. })).toEqual(testSubject.join(''));
  783. });
  784. it('should not directly affect the passed array', function() {
  785. var copy = testSubject.slice();
  786. testSubject.reduce(function(a,b) {
  787. return a+b;
  788. });
  789. expect(testSubject).toEqual(copy);
  790. });
  791. it('should skip non-set values', function() {
  792. delete testSubject[1];
  793. var visited = {};
  794. testSubject.reduce(function(a,b) {
  795. if(a)
  796. visited[a] = true;
  797. if(b)
  798. visited[b] = true;
  799. return 0;
  800. });
  801. expect(visited).toEqual({ '1': true, '3': true });
  802. });
  803. it('should have the right length', function() {
  804. expect(testSubject.reduce.length).toBe(1);
  805. });
  806. });
  807. describe('Array-like objects', function() {
  808. beforeEach(function() {
  809. testSubject = createArrayLikeFromArray(testSubject);
  810. testSubject.reduce = Array.prototype.reduce;
  811. });
  812. it('should pass the correct arguments to the callback', function() {
  813. var spy = jasmine.createSpy().andReturn(0);
  814. testSubject.reduce(spy);
  815. expect(spy.calls[0].args).toExactlyMatch([1, 2, 1, testSubject]);
  816. });
  817. it('should start with the right initialValue', function() {
  818. var spy = jasmine.createSpy().andReturn(0);
  819. testSubject.reduce(spy, 0);
  820. expect(spy.calls[0].args).toExactlyMatch([0, 1, 0, testSubject]);
  821. });
  822. it('should not affect elements added to the array after it has begun', function() {
  823. var arr = createArrayLikeFromArray([1,2,3]),
  824. i = 0;
  825. Array.prototype.reduce.call(arr, function(a, b) {
  826. i++;
  827. if(i <= 4) {
  828. arr[i+2] = a+3;
  829. };
  830. return b;
  831. });
  832. expect(arr).toEqual({
  833. 0: 1,
  834. 1: 2,
  835. 2: 3,
  836. 3: 4,
  837. 4: 5,
  838. length: 3
  839. });
  840. expect(i).toBe(2);
  841. });
  842. it('should work as expected for empty arrays', function() {
  843. var spy = jasmine.createSpy();
  844. expect(function() {
  845. Array.prototype.reduce.call({length: 0}, spy);
  846. }).toThrow();
  847. expect(spy).not.toHaveBeenCalled();
  848. });
  849. it('should throw correctly if no callback is given', function() {
  850. expect(function() {
  851. testSubject.reduce();
  852. }).toThrow();
  853. });
  854. it('should return the expected result', function() {
  855. expect(testSubject.reduce(function(a,b) {
  856. return (a||'').toString()+(b||'').toString();
  857. })).toEqual('123');
  858. });
  859. it('should not directly affect the passed array', function() {
  860. var copy = createArrayLikeFromArray(testSubject);
  861. testSubject.reduce(function(a,b) {
  862. return a+b;
  863. });
  864. delete(testSubject.reduce);
  865. expect(testSubject).toEqual(copy);
  866. });
  867. it('should skip non-set values', function() {
  868. delete testSubject[1];
  869. var visited = {};
  870. testSubject.reduce(function(a,b) {
  871. if(a)
  872. visited[a] = true;
  873. if(b)
  874. visited[b] = true;
  875. return 0;
  876. });
  877. expect(visited).toEqual({ '1': true, '3': true });
  878. });
  879. it('should have the right length', function() {
  880. expect(testSubject.reduce.length).toBe(1);
  881. });
  882. });
  883. });
  884. describe('reduceRight', function() {
  885. beforeEach(function() {
  886. testSubject = [1,2,3];
  887. });
  888. describe('Array', function() {
  889. it('should pass the correct arguments to the callback', function() {
  890. var spy = jasmine.createSpy().andReturn(0);
  891. testSubject.reduceRight(spy);
  892. expect(spy.calls[0].args).toExactlyMatch([3, 2, 1, testSubject]);
  893. });
  894. it('should start with the right initialValue', function() {
  895. var spy = jasmine.createSpy().andReturn(0);
  896. testSubject.reduceRight(spy, 0);
  897. expect(spy.calls[0].args).toExactlyMatch([0, 3, 2, testSubject]);
  898. });
  899. it('should not affect elements added to the array after it has begun', function() {
  900. var arr = [1,2,3],
  901. i = 0;
  902. arr.reduceRight(function(a, b) {
  903. i++;
  904. if(i <= 4) {
  905. arr.push(a+3);
  906. };
  907. return b;
  908. });
  909. expect(arr).toEqual([1,2,3,6,5]);
  910. expect(i).toBe(2);
  911. });
  912. it('should work as expected for empty arrays', function() {
  913. var spy = jasmine.createSpy();
  914. expect(function() {
  915. [].reduceRight(spy);
  916. }).toThrow();
  917. expect(spy).not.toHaveBeenCalled();
  918. });
  919. it('should throw correctly if no callback is given', function() {
  920. expect(function() {
  921. testSubject.reduceRight();
  922. }).toThrow();
  923. });
  924. it('should return the expected result', function() {
  925. expect(testSubject.reduceRight(function(a,b) {
  926. return (a||'').toString()+(b||'').toString();
  927. })).toEqual('321');
  928. });
  929. it('should not directly affect the passed array', function() {
  930. var copy = testSubject.slice();
  931. testSubject.reduceRight(function(a,b) {
  932. return a+b;
  933. });
  934. expect(testSubject).toEqual(copy);
  935. });
  936. it('should skip non-set values', function() {
  937. delete testSubject[1];
  938. var visited = {};
  939. testSubject.reduceRight(function(a,b) {
  940. if(a)
  941. visited[a] = true;
  942. if(b)
  943. visited[b] = true;
  944. return 0;
  945. });
  946. expect(visited).toEqual({ '1': true, '3': true });
  947. });
  948. it('should have the right length', function() {
  949. expect(testSubject.reduceRight.length).toBe(1);
  950. });
  951. });
  952. describe('Array-like objects', function() {
  953. beforeEach(function() {
  954. testSubject = createArrayLikeFromArray(testSubject);
  955. testSubject.reduceRight = Array.prototype.reduceRight;
  956. });
  957. it('should pass the correct arguments to the callback', function() {
  958. var spy = jasmine.createSpy().andReturn(0);
  959. testSubject.reduceRight(spy);
  960. expect(spy.calls[0].args).toExactlyMatch([3, 2, 1, testSubject]);
  961. });
  962. it('should start with the right initialValue', function() {
  963. var spy = jasmine.createSpy().andReturn(0);
  964. testSubject.reduceRight(spy, 0);
  965. expect(spy.calls[0].args).toExactlyMatch([0, 3, 2, testSubject]);
  966. });
  967. it('should not affect elements added to the array after it has begun', function() {
  968. var arr = createArrayLikeFromArray([1,2,3]),
  969. i = 0;
  970. Array.prototype.reduceRight.call(arr, function(a, b) {
  971. i++;
  972. if(i <= 4) {
  973. arr[i+2] = a+3;
  974. };
  975. return b;
  976. });
  977. expect(arr).toEqual({
  978. 0: 1,
  979. 1: 2,
  980. 2: 3,
  981. 3: 6,
  982. 4: 5,
  983. length: 3 // does not get updated on property assignment
  984. });
  985. expect(i).toBe(2);
  986. });
  987. it('should work as expected for empty arrays', function() {
  988. var spy = jasmine.createSpy();
  989. expect(function() {
  990. Array.prototype.reduceRight.call({length:0}, spy);
  991. }).toThrow();
  992. expect(spy).not.toHaveBeenCalled();
  993. });
  994. it('should throw correctly if no callback is given', function() {
  995. expect(function() {
  996. testSubject.reduceRight();
  997. }).toThrow();
  998. });
  999. it('should return the expected result', function() {
  1000. expect(testSubject.reduceRight(function(a,b) {
  1001. return (a||'').toString()+(b||'').toString();
  1002. })).toEqual('321');
  1003. });
  1004. it('should not directly affect the passed array', function() {
  1005. var copy = createArrayLikeFromArray(testSubject);
  1006. testSubject.reduceRight(function(a,b) {
  1007. return a+b;
  1008. });
  1009. delete(testSubject.reduceRight);
  1010. expect(testSubject).toEqual(copy);
  1011. });
  1012. it('should skip non-set values', function() {
  1013. delete testSubject[1];
  1014. var visited = {};
  1015. testSubject.reduceRight(function(a,b) {
  1016. if(a)
  1017. visited[a] = true;
  1018. if(b)
  1019. visited[b] = true;
  1020. return 0;
  1021. });
  1022. expect(visited).toEqual({ '1': true, '3': true });
  1023. });
  1024. it('should have the right length', function() {
  1025. expect(testSubject.reduceRight.length).toBe(1);
  1026. });
  1027. });
  1028. });
  1029. describe('isArray', function () {
  1030. it('should work for Array', function () {
  1031. var ret = Array.isArray([]);
  1032. expect(ret).toBe(true);
  1033. });
  1034. it('should fail for other objects', function () {
  1035. var objects = [
  1036. "someString",
  1037. true,
  1038. false,
  1039. 42,
  1040. 0,
  1041. {},
  1042. Object.create(null),
  1043. /foo/,
  1044. arguments,
  1045. document.getElementsByTagName("div")
  1046. ];
  1047. objects.forEach(function (v) {
  1048. expect(Array.isArray(v)).toBe(false);
  1049. });
  1050. });
  1051. });
  1052. describe('splice', function () {
  1053. var b = ["b"],
  1054. a = [1, "a", b],
  1055. test;
  1056. beforeEach(function() {
  1057. test = a.slice(0);
  1058. });
  1059. it('basic implementation test 1', function () {
  1060. expect(test.splice(0)).toEqual(a);
  1061. });
  1062. it('basic implementation test 2', function () {
  1063. test.splice(0, 2);
  1064. expect(test).toEqual([b]);
  1065. });
  1066. it('should do nothing if method called with no arguments', function () {
  1067. expect(test.splice()).toEqual([]);
  1068. expect(test).toEqual(a);
  1069. });
  1070. //TODO:: Is this realy TRUE behavior?
  1071. it('should set first argument to 0 if first argument is set but undefined', function () {
  1072. var test2 = test.slice(0);
  1073. expect(test.splice(void 0, 2)).toEqual(test2.splice(0, 2));
  1074. expect(test).toEqual(test2);
  1075. });
  1076. it('should deleted and return all items after "start" when second argument is undefined', function () {
  1077. expect(test.splice(0)).toEqual(a);
  1078. expect(test).toEqual([]);
  1079. });
  1080. it('should deleted and return all items after "start" when second argument is undefined', function () {
  1081. expect(test.splice(2)).toEqual([b]);
  1082. expect(test).toEqual([1, "a"]);
  1083. });
  1084. it('runshould have the right length', function () {
  1085. expect(test.splice.length).toBe(2);
  1086. });
  1087. });
  1088. });