free-functions.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. describe("Basic functions", () => {
  2. const workbook = createWorkbook();
  3. const sheet = createSheet(workbook, "Sheet 1");
  4. sheet.makeCurrent();
  5. sheet.setCell("A", 0, "0");
  6. sheet.setCell("A", 1, "1");
  7. sheet.setCell("A", 2, "2");
  8. test("select", () => {
  9. expect(select).toBeDefined();
  10. expect(select(true, 1, 2)).toBe(1);
  11. expect(select(false, 1, 2)).toBe(2);
  12. });
  13. test("choose", () => {
  14. expect(choose).toBeDefined();
  15. expect(choose(0, 1, 2, 3)).toBe(1);
  16. expect(choose(1, 1, 2, 3)).toBe(2);
  17. expect(choose(3, 1, 2, 3)).toBeUndefined();
  18. expect(choose(-1, 1, 2, 3)).toBeUndefined();
  19. });
  20. test("now", () => {
  21. expect(now).toBeDefined();
  22. expect(now()).toBeInstanceOf(Date);
  23. });
  24. test("randRange", () => {
  25. expect(randRange).toBeDefined();
  26. });
  27. test("integer", () => {
  28. expect(integer).toBeDefined();
  29. expect(integer("0")).toEqual(0);
  30. expect(integer("32")).toEqual(32);
  31. });
  32. test("sheet", () => {
  33. expect(globalThis.sheet).toBeDefined();
  34. expect(globalThis.sheet("Sheet 1")).toBe(sheet);
  35. expect(globalThis.sheet("Not a sheet")).toBeUndefined();
  36. });
  37. test("reduce", () => {
  38. expect(reduce).toBeDefined();
  39. expect(reduce(acc => acc + 1, 0, [1, 2, 3, 4])).toEqual(4);
  40. expect(reduce(acc => acc + 1, 0, [])).toEqual(0);
  41. expect(reduce((acc, x) => acc + "|" + x.toString(), 0, R`A0:A2`)).toEqual("0|0|1|2");
  42. expect(reduce((acc, x) => acc + "|" + x.toString(), 0, R`A0:A0`)).toEqual("0|0");
  43. });
  44. test("numericReduce", () => {
  45. expect(numericReduce).toBeDefined();
  46. expect(numericReduce(acc => acc + 1, 0, [1, 2, 3, 4])).toEqual(4);
  47. expect(numericReduce(acc => acc + 1, 0, [])).toEqual(0);
  48. expect(numericReduce((acc, x) => acc + x, 19, R`A0:A2`)).toEqual(22);
  49. expect(numericReduce(acc => acc + 1, 3, R`A0:A0`)).toEqual(4);
  50. });
  51. test("numericResolve", () => {
  52. expect(numericResolve).toBeDefined();
  53. expect(numericResolve(["0", "1", "2"])).toEqual([0, 1, 2]);
  54. expect(numericResolve([])).toEqual([]);
  55. expect(numericResolve(R`A0:A2`)).toEqual([0, 1, 2]);
  56. expect(numericResolve(R`A0:A0`)).toEqual([0]);
  57. });
  58. test("resolve", () => {
  59. expect(resolve).toBeDefined();
  60. expect(resolve(["A", "B", "C"])).toEqual(["A", "B", "C"]);
  61. expect(resolve([])).toEqual([]);
  62. expect(resolve(R`A0:A2`)).toEqual(["0", "1", "2"]);
  63. expect(resolve(R`A0:A0`)).toEqual(["0"]);
  64. });
  65. });
  66. describe("Statistics", () => {
  67. const workbook = createWorkbook();
  68. const sheet = createSheet(workbook, "Sheet 1");
  69. sheet.makeCurrent();
  70. for (let i = 0; i < 10; ++i) sheet.setCell("A", i, `${i}`);
  71. for (let i = 0; i < 10; ++i) sheet.setCell("B", i, `${i * i}`);
  72. test("sum", () => {
  73. expect(sum).toBeDefined();
  74. expect(sum(R`A0:A9`)).toEqual(45);
  75. });
  76. test("sumIf", () => {
  77. expect(sumIf).toBeDefined();
  78. expect(sumIf(x => !Number.isNaN(x), R`A0:A10`)).toEqual(45);
  79. });
  80. test("count", () => {
  81. expect(count).toBeDefined();
  82. expect(count(R`A0:A9`)).toEqual(10);
  83. });
  84. test("countIf", () => {
  85. expect(countIf).toBeDefined();
  86. expect(countIf(x => x, R`A0:A10`)).toEqual(10);
  87. });
  88. test("average", () => {
  89. expect(average).toBeDefined();
  90. expect(average(R`A0:A9`)).toEqual(4.5);
  91. });
  92. test("averageIf", () => {
  93. expect(averageIf).toBeDefined();
  94. expect(averageIf(x => !Number.isNaN(x), R`A0:A10`)).toEqual(4.5);
  95. });
  96. test("minIf", () => {
  97. expect(minIf).toBeDefined();
  98. expect(minIf(x => x > 25, R`B0:B9`)).toEqual(36);
  99. });
  100. test("min", () => {
  101. expect(min).toBeDefined();
  102. expect(min(R`B0:B9`)).toEqual(0);
  103. });
  104. test("maxIf", () => {
  105. expect(maxIf).toBeDefined();
  106. expect(maxIf(x => x > 25, R`B0:B9`)).toEqual(81);
  107. });
  108. test("max", () => {
  109. expect(max).toBeDefined();
  110. expect(max(R`B0:B9`)).toEqual(81);
  111. });
  112. test("sumProductIf", () => {
  113. expect(sumProductIf).toBeDefined();
  114. expect(sumProductIf((a, b) => b > 25, R`A0:A9`, R`B0:B9`)).toEqual(1800);
  115. });
  116. test("sumProduct", () => {
  117. expect(sumProduct).toBeDefined();
  118. expect(sumProduct(R`A0:A9`, R`B0:B9`)).toEqual(2025);
  119. });
  120. test("median", () => {
  121. expect(median).toBeDefined();
  122. expect(median(R`A0:A9`)).toEqual(4.5);
  123. expect(median(R`A0:A2`)).toEqual(1);
  124. });
  125. test("variance", () => {
  126. expect(variance).toBeDefined();
  127. expect(variance(R`A0:A0`)).toEqual(0);
  128. expect(variance(R`A0:A9`)).toEqual(82.5);
  129. });
  130. test("mode", () => {
  131. expect(mode).toBeDefined();
  132. expect(mode(R`A0:A0`.union(R`A0:A0`).union(R`A1:A9`))).toEqual(0);
  133. });
  134. test("stddev", () => {
  135. expect(stddev).toBeDefined();
  136. expect(stddev(R`A0:A0`)).toEqual(0);
  137. expect(stddev(R`A0:A9`)).toEqual(Math.sqrt(82.5));
  138. });
  139. });
  140. describe("Lookup", () => {
  141. const workbook = createWorkbook();
  142. const sheet = createSheet(workbook, "Sheet 1");
  143. sheet.makeCurrent();
  144. for (let i = 0; i < 10; ++i) {
  145. sheet.setCell("A", i, `${i}`);
  146. sheet.setCell("B", i, `B${i}`);
  147. }
  148. sheet.focusCell("A", 0);
  149. test("row", () => {
  150. expect(row()).toEqual(0);
  151. });
  152. test("column", () => {
  153. expect(column()).toEqual("A");
  154. });
  155. test("lookup", () => {
  156. expect(lookup).toBeDefined();
  157. // Note: String ordering.
  158. expect(lookup("2", R`A0:A9`, R`B0:B9`)).toEqual("B2");
  159. expect(lookup("20", R`A0:A9`, R`B0:B9`)).toBeUndefined();
  160. expect(lookup("80", R`A0:A9`, R`B0:B9`, undefined, "nextlargest")).toEqual("B9");
  161. });
  162. test("reflookup", () => {
  163. expect(reflookup).toBeDefined();
  164. // Note: String ordering.
  165. expect(reflookup("2", R`A0:A9`, R`B0:B9`).name).toEqual("B2");
  166. expect(reflookup("20", R`A0:A9`, R`B0:B9`)).toEqual(here());
  167. expect(reflookup("80", R`A0:A9`, R`B0:B9`, undefined, "nextlargest").name).toEqual("B9");
  168. });
  169. });