test-fun.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. 'use strict';
  2. import test from 'tape-six';
  3. import {streamToArray, delay} from './helpers.mjs';
  4. import chain, {none, finalValue, many} from '../src/index.js';
  5. import fromIterable from '../src/utils/fromIterable.js';
  6. import fun from '../src/fun.js';
  7. test.asPromise('fun: smoke test', (t, resolve) => {
  8. const output = [],
  9. c = chain([
  10. fromIterable([1, 2, 3]),
  11. fun(
  12. x => x * x,
  13. x => 2 * x + 1
  14. ),
  15. streamToArray(output)
  16. ]);
  17. c.on('end', () => {
  18. t.deepEqual(output, [3, 9, 19]);
  19. resolve();
  20. });
  21. });
  22. test.asPromise('fun: final', (t, resolve) => {
  23. const output = [],
  24. c = chain([
  25. fromIterable([1, 2, 3]),
  26. fun(
  27. x => x * x,
  28. x => finalValue(x),
  29. x => 2 * x + 1
  30. ),
  31. streamToArray(output)
  32. ]);
  33. c.on('end', () => {
  34. t.deepEqual(output, [1, 4, 9]);
  35. resolve();
  36. });
  37. });
  38. test.asPromise('fun: nothing', (t, resolve) => {
  39. const output = [],
  40. c = chain([
  41. fromIterable([1, 2, 3]),
  42. fun(
  43. x => x * x,
  44. () => none,
  45. x => 2 * x + 1
  46. ),
  47. streamToArray(output)
  48. ]);
  49. c.on('end', () => {
  50. t.deepEqual(output, []);
  51. resolve();
  52. });
  53. });
  54. test.asPromise('fun: empty', (t, resolve) => {
  55. const output = [],
  56. c = chain([fromIterable([1, 2, 3]), x => x * x, fun(), streamToArray(output)]);
  57. c.on('end', () => {
  58. t.deepEqual(output, [1, 4, 9]);
  59. resolve();
  60. });
  61. });
  62. test.asPromise('fun: async', (t, resolve) => {
  63. const output = [],
  64. c = chain([
  65. fromIterable([1, 2, 3]),
  66. fun(
  67. delay(x => x * x),
  68. x => 2 * x + 1
  69. ),
  70. streamToArray(output)
  71. ]);
  72. c.on('end', () => {
  73. t.deepEqual(output, [3, 9, 19]);
  74. resolve();
  75. });
  76. });
  77. test.asPromise('fun: generator', (t, resolve) => {
  78. const output = [],
  79. c = chain([
  80. fromIterable([1, 2, 3]),
  81. fun(
  82. x => x * x,
  83. function* (x) {
  84. yield x;
  85. yield x + 1;
  86. yield x + 2;
  87. },
  88. x => 2 * x + 1
  89. ),
  90. streamToArray(output)
  91. ]);
  92. c.on('end', () => {
  93. t.deepEqual(output, [3, 5, 7, 9, 11, 13, 19, 21, 23]);
  94. resolve();
  95. });
  96. });
  97. test.asPromise('fun: many', (t, resolve) => {
  98. const output = [],
  99. c = chain([
  100. fromIterable([1, 2, 3]),
  101. fun(
  102. x => x * x,
  103. x => many([x, x + 1, x + 2]),
  104. x => 2 * x + 1
  105. ),
  106. streamToArray(output)
  107. ]);
  108. c.on('end', () => {
  109. t.deepEqual(output, [3, 5, 7, 9, 11, 13, 19, 21, 23]);
  110. resolve();
  111. });
  112. });
  113. test.asPromise('fun: combined', (t, resolve) => {
  114. const output = [],
  115. c = chain([
  116. fromIterable([1, 2]),
  117. fun(
  118. delay(x => -x),
  119. x => many([x, x * 10]),
  120. function* (x) {
  121. yield x;
  122. yield x - 1;
  123. },
  124. x => -x
  125. ),
  126. streamToArray(output)
  127. ]);
  128. c.on('end', () => {
  129. t.deepEqual(output, [1, 2, 10, 11, 2, 3, 20, 21]);
  130. resolve();
  131. });
  132. });
  133. test.asPromise('fun: combined final', (t, resolve) => {
  134. const output = [],
  135. c = chain([
  136. fromIterable([1, 2]),
  137. fun(
  138. delay(x => -x),
  139. x => many([x, x * 10]),
  140. function* (x) {
  141. yield x;
  142. yield finalValue(x - 1);
  143. },
  144. x => -x
  145. ),
  146. streamToArray(output)
  147. ]);
  148. c.on('end', () => {
  149. t.deepEqual(output, [1, -2, 10, -11, 2, -3, 20, -21]);
  150. resolve();
  151. });
  152. });
  153. test.asPromise('fun: as fun', (t, resolve) => {
  154. const output = [],
  155. c = chain([
  156. fromIterable([1, 2]),
  157. fun(
  158. delay(x => -x),
  159. x => many([x, x * 10]),
  160. function* (x) {
  161. yield x;
  162. yield finalValue(x - 1);
  163. },
  164. x => -x
  165. ),
  166. streamToArray(output)
  167. ]);
  168. c.on('end', () => {
  169. t.deepEqual(output, [1, -2, 10, -11, 2, -3, 20, -21]);
  170. resolve();
  171. });
  172. });