Array.from.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. test("length is 1", () => {
  2. expect(Array.from).toHaveLength(1);
  3. });
  4. describe("normal behavior", () => {
  5. test("empty array, no mapFn", () => {
  6. const a = Array.from([]);
  7. expect(a instanceof Array).toBeTrue();
  8. expect(a).toHaveLength(0);
  9. });
  10. test("empty array, with mapFn, no thisArg", () => {
  11. const a = Array.from([], n => n);
  12. expect(a instanceof Array).toBeTrue();
  13. expect(a).toHaveLength(0);
  14. });
  15. test("empty array, with mapFn, with thisArg", () => {
  16. const a = Array.from(
  17. [],
  18. function (n) {
  19. return n + this.value;
  20. },
  21. { value: 100 }
  22. );
  23. expect(a instanceof Array).toBeTrue();
  24. expect(a).toHaveLength(0);
  25. });
  26. test("empty string, no mapFn", () => {
  27. const a = Array.from("");
  28. expect(a instanceof Array).toBeTrue();
  29. expect(a).toHaveLength(0);
  30. });
  31. test("empty string, with mapFn, no thisArg", () => {
  32. const a = Array.from("", n => n);
  33. expect(a instanceof Array).toBeTrue();
  34. expect(a).toHaveLength(0);
  35. });
  36. test("empty string, with mapFn, with thisArg", () => {
  37. const a = Array.from(
  38. "",
  39. function (n) {
  40. return n + this.value;
  41. },
  42. { value: 100 }
  43. );
  44. expect(a instanceof Array).toBeTrue();
  45. expect(a).toHaveLength(0);
  46. });
  47. test("non-empty array, no mapFn", () => {
  48. const a = Array.from([5, 8, 1]);
  49. expect(a instanceof Array).toBeTrue();
  50. expect(a).toHaveLength(3);
  51. expect(a[0]).toBe(5);
  52. expect(a[1]).toBe(8);
  53. expect(a[2]).toBe(1);
  54. });
  55. test("non-empty array, with mapFn, no thisArg", () => {
  56. const a = Array.from([5, 8, 1], (n, i) => n - i);
  57. expect(a instanceof Array).toBeTrue();
  58. expect(a).toHaveLength(3);
  59. expect(a[0]).toBe(5);
  60. expect(a[1]).toBe(7);
  61. expect(a[2]).toBe(-1);
  62. });
  63. test("non-empty array, with mapFn, with thisArg", () => {
  64. const a = Array.from(
  65. [5, 8, 1],
  66. function (n, i) {
  67. return n - i + this.value;
  68. },
  69. { value: 100 }
  70. );
  71. expect(a instanceof Array).toBeTrue();
  72. expect(a).toHaveLength(3);
  73. expect(a[0]).toBe(105);
  74. expect(a[1]).toBe(107);
  75. expect(a[2]).toBe(99);
  76. });
  77. test("non-empty string, no mapFn", () => {
  78. const a = Array.from("what");
  79. expect(a instanceof Array).toBeTrue();
  80. expect(a).toHaveLength(4);
  81. expect(a[0]).toBe("w");
  82. expect(a[1]).toBe("h");
  83. expect(a[2]).toBe("a");
  84. expect(a[3]).toBe("t");
  85. });
  86. test("non-empty string, with mapFn, no thisArg", () => {
  87. const a = Array.from("what", (n, i) => n + n + i);
  88. expect(a instanceof Array).toBeTrue();
  89. expect(a).toHaveLength(4);
  90. expect(a[0]).toBe("ww0");
  91. expect(a[1]).toBe("hh1");
  92. expect(a[2]).toBe("aa2");
  93. expect(a[3]).toBe("tt3");
  94. });
  95. test("non-empty string, with mapFn, with thisArg", () => {
  96. const a = Array.from(
  97. "what",
  98. function (n, i) {
  99. return n + i + this.value;
  100. },
  101. { value: "a" }
  102. );
  103. expect(a instanceof Array).toBeTrue();
  104. expect(a).toHaveLength(4);
  105. expect(a[0]).toBe("w0a");
  106. expect(a[1]).toBe("h1a");
  107. expect(a[2]).toBe("a2a");
  108. expect(a[3]).toBe("t3a");
  109. });
  110. test("shallow array copy, no mapFn", () => {
  111. const a = [1, 2, 3];
  112. const b = Array.from([a]);
  113. expect(b instanceof Array).toBeTrue();
  114. expect(b).toHaveLength(1);
  115. b[0][0] = 4;
  116. expect(a[0]).toBe(4);
  117. });
  118. test("shallow array copy, with mapFn, no thisArg", () => {
  119. const a = [1, 2, 3];
  120. const b = Array.from([a], n => n.map(n => n + 2));
  121. expect(b instanceof Array).toBeTrue();
  122. expect(b).toHaveLength(1);
  123. b[0][0] = 10;
  124. expect(a[0]).toBe(1);
  125. expect(b[0][0]).toBe(10);
  126. expect(b[0][1]).toBe(4);
  127. expect(b[0][2]).toBe(5);
  128. });
  129. test("shallow array copy, with mapFn, with thisArg", () => {
  130. const a = [1, 2, 3];
  131. const b = Array.from(
  132. [a],
  133. function (n, i) {
  134. return n.map(n => n + 2 + i + this.value);
  135. },
  136. { value: 100 }
  137. );
  138. expect(b instanceof Array).toBeTrue();
  139. expect(b).toHaveLength(1);
  140. b[0][0] = 10;
  141. expect(a[0]).toBe(1);
  142. expect(b[0][0]).toBe(10);
  143. expect(b[0][1]).toBe(104);
  144. expect(b[0][2]).toBe(105);
  145. });
  146. const rangeIterator = function (begin, end) {
  147. return {
  148. [Symbol.iterator]() {
  149. let value = begin - 1;
  150. return {
  151. next() {
  152. if (value < end) {
  153. value += 1;
  154. }
  155. return { value: value, done: value >= end };
  156. },
  157. };
  158. },
  159. };
  160. };
  161. test("from iterator, no mapFn", () => {
  162. const a = Array.from(rangeIterator(8, 10));
  163. expect(a instanceof Array).toBeTrue();
  164. expect(a).toHaveLength(2);
  165. expect(a[0]).toBe(8);
  166. expect(a[1]).toBe(9);
  167. });
  168. test("from iterator, with mapFn, no thisArg", () => {
  169. const a = Array.from(rangeIterator(8, 10), n => --n);
  170. expect(a instanceof Array).toBeTrue();
  171. expect(a).toHaveLength(2);
  172. expect(a[0]).toBe(7);
  173. expect(a[1]).toBe(8);
  174. });
  175. test("from iterator, with mapFn, with thisArg", () => {
  176. const a = Array.from(
  177. rangeIterator(8, 10),
  178. function (n, i) {
  179. return n + i + this.value;
  180. },
  181. { value: 100 }
  182. );
  183. expect(a instanceof Array).toBeTrue();
  184. expect(a).toHaveLength(2);
  185. expect(a[0]).toBe(108);
  186. expect(a[1]).toBe(110);
  187. });
  188. });