aigle.js

  1. 'use strict';
  2. const { AigleCore, AigleProxy } = require('aigle-core');
  3. const Queue = require('./internal/queue');
  4. const invokeAsync = require('./internal/async');
  5. const {
  6. VERSION,
  7. INTERNAL,
  8. PENDING,
  9. UNHANDLED,
  10. errorObj,
  11. call0,
  12. callResolve,
  13. callReject,
  14. callReceiver,
  15. printWarning,
  16. } = require('./internal/util');
  17. let stackTraces = false;
  18. class Aigle extends AigleCore {
  19. /**
  20. * Create a new promise instance. It is same as native Promise.
  21. * It has three states, `pending`, `fulfilled` and `rejected`, the first state is `pending`.
  22. * The passed function has `resolve` and `reject` as the arguments,
  23. * if `reject` is called or an error is caused, the state goes to `rejected` and then the error is thrown to next `catch`.
  24. * If request is success and `resolve` is called, `then` or next task is called.
  25. * @param {Function} executor
  26. * @example
  27. * return new Promise((resolve, reject) => {
  28. * fs.readFile('filepath', (err, data) => {
  29. * if (err) {
  30. * return reject(err);
  31. * }
  32. * resolve(data);
  33. * });
  34. * })
  35. * .then(data => ...)
  36. * .catch(err => ...);
  37. */
  38. constructor(executor) {
  39. super();
  40. this._resolved = 0;
  41. this._value = undefined;
  42. this._key = undefined;
  43. this._receiver = undefined;
  44. this._onFulfilled = undefined;
  45. this._onRejected = undefined;
  46. this._receivers = undefined;
  47. if (executor === INTERNAL) {
  48. return;
  49. }
  50. this._execute(executor);
  51. }
  52. /**
  53. * @param {Function} onFulfilled
  54. * @param {Function} [onRejected]
  55. * @return {Aigle} Returns an Aigle instance
  56. */
  57. then(onFulfilled, onRejected) {
  58. return addAigle(this, new Aigle(INTERNAL), onFulfilled, onRejected);
  59. }
  60. /**
  61. * @param {Object|Function} onRejected
  62. * @return {Aigle} Returns an Aigle instance
  63. * @example
  64. * return Aigle.reject(new TypeError('error'))
  65. * .catch(TypeError, error => console.log(error));
  66. */
  67. catch(onRejected) {
  68. if (arguments.length > 1) {
  69. let l = arguments.length;
  70. onRejected = arguments[--l];
  71. if (typeof onRejected === 'function') {
  72. const errorTypes = Array(l);
  73. while (l--) {
  74. errorTypes[l] = arguments[l];
  75. }
  76. onRejected = createOnRejected(errorTypes, onRejected);
  77. }
  78. }
  79. return addAigle(this, new Aigle(INTERNAL), undefined, onRejected);
  80. }
  81. /**
  82. * @param {Function} handler
  83. * @return {Aigle} Returns an Aigle instance
  84. */
  85. finally(handler) {
  86. handler = typeof handler !== 'function' ? handler : createFinallyHandler(this, handler);
  87. return addAigle(this, new Aigle(INTERNAL), handler, handler);
  88. }
  89. /**
  90. * @return {string}
  91. */
  92. toString() {
  93. return '[object Promise]';
  94. }
  95. /**
  96. * @return {boolean}
  97. */
  98. isPending() {
  99. return this._resolved === 0;
  100. }
  101. /**
  102. * @return {boolean}
  103. */
  104. isFulfilled() {
  105. return this._resolved === 1;
  106. }
  107. /**
  108. * @return {boolean}
  109. */
  110. isRejected() {
  111. return this._resolved === 2;
  112. }
  113. /**
  114. * @return {boolean}
  115. */
  116. isCancelled() {
  117. return this._value instanceof CancellationError;
  118. }
  119. /**
  120. * @return {*}
  121. */
  122. value() {
  123. return this._resolved === 1 ? this._value : undefined;
  124. }
  125. /**
  126. * @return {*}
  127. */
  128. reason() {
  129. return this._resolved === 2 ? this._value : undefined;
  130. }
  131. /**
  132. * @example
  133. * const { CancellationError } = Aigle;
  134. * let cancelled = false;
  135. * const promise = new Aigle((resolve, reject, onCancel) => {
  136. * setTimeout(resolve, 30, 'resolved');
  137. * onCancel(() => cancelled = true);
  138. * });
  139. * promise.cancel();
  140. * promise.catch(error => {
  141. * console.log(error instanceof CancellationError); // true
  142. * console.log(cancelled); // true
  143. * });
  144. */
  145. cancel() {
  146. if (this._execute === execute || this._resolved !== 0) {
  147. return;
  148. }
  149. const { _onCancelQueue } = this;
  150. if (_onCancelQueue) {
  151. let i = -1;
  152. const { array } = _onCancelQueue;
  153. this._onCancelQueue = undefined;
  154. while (++i < _onCancelQueue.length) {
  155. array[i]();
  156. }
  157. }
  158. this._resolved = 2;
  159. this._value = new CancellationError('late cancellation observer');
  160. if (this._parent) {
  161. this._parent.cancel();
  162. }
  163. }
  164. suppressUnhandledRejections() {
  165. this._receiver = INTERNAL;
  166. }
  167. /**
  168. * @param {Function} handler
  169. * @return {Aigle} Returns an Aigle instance
  170. * @example
  171. * const array = [1, 2, 3];
  172. * Aigle.resolve(array)
  173. * .spread((arg1, arg2, arg3) => {
  174. * console.log(arg1, arg2, arg3); // 1, 2, 3
  175. * });
  176. *
  177. * @example
  178. * const string = '123';
  179. * Aigle.resolve(string)
  180. * .spread((arg1, arg2, arg3) => {
  181. * console.log(arg1, arg2, arg3); // 1, 2, 3
  182. * });
  183. */
  184. spread(handler) {
  185. return addReceiver(this, new Spread(handler));
  186. }
  187. /**
  188. * `Aigle#all` will execute [`Aigle.all`](https://suguru03.github.io/aigle/docs/global.html#all) using a previous promise value.
  189. * The value will be assigned as the first argument to [`Aigle.all`](https://suguru03.github.io/aigle/docs/global.html#all).
  190. * @return {Aigle} Returns an Aigle instance
  191. * @example
  192. * const order = [];
  193. * const makeDelay = (num, delay) => {
  194. * return Aigle.delay(delay)
  195. * .then(() => {
  196. * order.push(num);
  197. * return num;
  198. * });
  199. * };
  200. * Aigle.resolve([
  201. * makeDelay(1, 30),
  202. * makeDelay(2, 20),
  203. * makeDelay(3, 10)
  204. * ])
  205. * .all()
  206. * .then(array => {
  207. * console.log(array); // [1, 2, 3];
  208. * console.log(order); // [3, 2, 1];
  209. * });
  210. */
  211. all() {
  212. return addProxy(this, All);
  213. }
  214. allSettled() {
  215. return addProxy(this, AllSettled);
  216. }
  217. /**
  218. * @return {Aigle} Returns an Aigle instance
  219. * @example
  220. * Aigle.resolve([
  221. * new Aigle(resolve => setTimeout(() => resolve(1), 30)),
  222. * new Aigle(resolve => setTimeout(() => resolve(2), 20)),
  223. * new Aigle(resolve => setTimeout(() => resolve(3), 10))
  224. * ])
  225. * .race()
  226. * .then(value => console.log(value)); // 3
  227. */
  228. race() {
  229. return addProxy(this, Race);
  230. }
  231. /**
  232. * `Aigle#props` will execute [`Aigle.props`](https://suguru03.github.io/aigle/docs/global.html#props) using a previous promise value.
  233. * The value will be assigned as the first argument to [`Aigle.props`](https://suguru03.github.io/aigle/docs/global.html#props).
  234. * @return {Aigle} Returns an Aigle instance
  235. * @example
  236. * const order = [];
  237. * const makeDelay = (num, delay) => {
  238. * return Aigle.delay(delay)
  239. * .then(() => {
  240. * order.push(num);
  241. * return num;
  242. * });
  243. * };
  244. * Aigle.resolve({
  245. * a: makeDelay(1, 30),
  246. * b: makeDelay(2, 20),
  247. * c: makeDelay(3, 10)
  248. * })
  249. * .props()
  250. * .then(object => {
  251. * console.log(object); // { a: 1, b: 2, c: 3 }
  252. * console.log(order); // [3, 2, 1]
  253. * });
  254. */
  255. props() {
  256. return addProxy(this, Props);
  257. }
  258. /**
  259. * `Aigle#parallel` will execute [`Aigle.parallel`](https://suguru03.github.io/aigle/docs/global.html#parallel) using a previous promise value.
  260. * The value will be assigned as the first argument to [`Aigle.parallel`](https://suguru03.github.io/aigle/docs/global.html#parallel).
  261. * @example
  262. * Aigle.resolve([
  263. * () => Aigle.delay(30, 1),
  264. * Aigle.delay(20, 2),
  265. * 3
  266. * ])
  267. * .parallel()
  268. * .then(array => {
  269. * console.log(array); // [1, 2, 3]
  270. * });
  271. *
  272. * @example
  273. * Aigle.resolve({
  274. * a: () => Aigle.delay(30, 1),
  275. * b: Aigle.delay(20, 2),
  276. * c: 3
  277. * })
  278. * .parallel()
  279. * .then(object => {
  280. * console.log(object); // { a: 1, b: 2, c: 3 }
  281. * });
  282. */
  283. parallel() {
  284. return addProxy(this, Parallel);
  285. }
  286. /**
  287. * `Aigle#series` has the same functionality as [`Aigle#parallel`](https://suguru03.github.io/aigle/docs/Aigle.html#parallel)
  288. * and it works in series.
  289. * @example
  290. * Aigle.resolve([
  291. * () => Aigle.delay(30, 1),
  292. * Aigle.delay(20, 2),
  293. * 3
  294. * ])
  295. * .series()
  296. * .then(array => {
  297. * console.log(array); // [1, 2, 3]
  298. * });
  299. *
  300. * @example
  301. * Aigle.resolve({
  302. * a: () => Aigle.delay(30, 1),
  303. * b: Aigle.delay(20, 2),
  304. * c: 3
  305. * })
  306. * .series()
  307. * .then(object => {
  308. * console.log(object); // { a: 1, b: 2, c: 3 }
  309. * });
  310. */
  311. series() {
  312. return addProxy(this, Series);
  313. }
  314. /**
  315. * `Aigle#parallelLimit` has the same functionality as [`Aigle#parallel`](https://suguru03.github.io/aigle/docs/Aigle.html#parallel)
  316. * and it works with concurrency.
  317. * @param {number} [limit=8]
  318. * @example
  319. * Aigle.resolve([
  320. * () => Aigle.delay(30, 1),
  321. * Aigle.delay(20, 2),
  322. * 3
  323. * ])
  324. * .parallelLimit()
  325. * .then(array => {
  326. * console.log(array); // [1, 2, 3]
  327. * });
  328. *
  329. * @example
  330. * Aigle.resolve({
  331. * a: () => Aigle.delay(30, 1),
  332. * b: Aigle.delay(20, 2),
  333. * c: 3
  334. * })
  335. * .parallelLimit(2)
  336. * .then(object => {
  337. * console.log(object); // { a: 1, b: 2, c: 3 }
  338. * });
  339. */
  340. parallelLimit(limit) {
  341. return addProxy(this, ParallelLimit, limit);
  342. }
  343. /**
  344. * `Aigle#each` will execute [`Aigle.each`](https://suguru03.github.io/aigle/docs/global.html#each) using a previous promise value and a defined iterator.
  345. * The value will be assigned as the first argument to [`Aigle.each`](https://suguru03.github.io/aigle/docs/global.html#each) and
  346. * the iterator will be assigned as the second argument.
  347. * @param {Function} iterator
  348. * @example
  349. * const order = [];
  350. * const collection = [1, 4, 2];
  351. * const iterator = (num, value, collection) => {
  352. * return Aigle.delay(num * 10)
  353. * .then(() => order.push(num));
  354. * };
  355. * return Aigle.resolve(collection)
  356. * .each(iterator)
  357. * .then(value => {
  358. * console.log(value); // undefined
  359. * console.log(order); // [1, 2, 4];
  360. * });
  361. *
  362. * @example
  363. * const order = [];
  364. * const collection = { a: 1, b: 4, c: 2 };
  365. * const iterator = (num, key, collection) => {
  366. * return Aigle.delay(num * 10)
  367. * .then(() => order.push(num));
  368. * };
  369. * return Aigle.resolve(collection)
  370. * .each(iterator)
  371. * .then(value => {
  372. * console.log(value); // undefined
  373. * console.log(order); // [1, 2, 4];
  374. * });
  375. *
  376. * @example
  377. * const order = [];
  378. * const collection = [1, 4, 2];
  379. * const iterator = (num, value, collection) => {
  380. * return Aigle.delay(num * 10)
  381. * .then(() => {
  382. * order.push(num);
  383. * return num !== 2; // break
  384. * });
  385. * };
  386. * return Aigle.resolve(collection)
  387. * .each(iterator)
  388. * .then(value => {
  389. * console.log(value); // undefined
  390. * console.log(order); // [1, 2];
  391. * });
  392. */
  393. each(iterator) {
  394. return addProxy(this, Each, iterator);
  395. }
  396. /**
  397. * @alias each
  398. * @param {Function} iterator
  399. */
  400. forEach(iterator) {
  401. return addProxy(this, Each, iterator);
  402. }
  403. /**
  404. * `Aigle#eachSeries` is almost the same as [`Aigle#each`](https://suguru03.github.io/aigle/docs/Aigle.html#each), but it will work in series.
  405. * @param {Function} iterator
  406. * @example
  407. * const order = [];
  408. * const collection = [1, 4, 2];
  409. * const iterator = (num, index, collection) => {
  410. * return Aigle.delay(num * 10)
  411. * .then(() => order.push(num));
  412. * };
  413. * Aigle.resolve(collection)
  414. * .eachSeries(iterator)
  415. * .then(value => {
  416. * console.log(value); // undefined
  417. * console.log(order); // [1, 4, 2];
  418. * });
  419. *
  420. * @example
  421. * const order = [];
  422. * const collection = { a: 1, b: 4, c: 2 };
  423. * const iterator = (num, index, collection) => {
  424. * return Aigle.delay(num * 10)
  425. * .then(() => order.push(num));
  426. * };
  427. * Aigle.resolve(collection)
  428. * .eachSeries(iterator)
  429. * .then(value => {
  430. * console.log(value); // undefined
  431. * console.log(order); // [1, 4, 2];
  432. * });
  433. *
  434. * @example
  435. * const order = [];
  436. * const collection = [1, 4, 2];
  437. * const iterator = num => {
  438. * return Aigle.delay(num * 10)
  439. * .then(() => {
  440. * order.push(num);
  441. * return num !== 4; // break
  442. * });
  443. * };
  444. * Aigle.resolve(collection)
  445. * .eachSeries(iterator)
  446. * .then(value => {
  447. * console.log(value); // undefined
  448. * console.log(order); // [1, 4];
  449. * });
  450. */
  451. eachSeries(iterator) {
  452. return addProxy(this, EachSeries, iterator);
  453. }
  454. /**
  455. * @alias eachSeries
  456. * @param {Function} iterator
  457. */
  458. forEachSeries(iterator) {
  459. return addProxy(this, EachSeries, iterator);
  460. }
  461. /**
  462. * `Aigle#eachLimit` is almost the same as [`Aigle.each`](https://suguru03.github.io/aigle/docs/Aigle.html#each) and
  463. * [`Aigle.eachSeries`](https://suguru03.github.io/aigle/docs/Aigle.html#eachSeries), but it will work with concurrency.
  464. * @param {number} [limit=8]
  465. * @param {Function} iterator
  466. * @return {Aigle} Returns an Aigle instance
  467. * @example
  468. * const collection = [1, 5, 3, 4, 2];
  469. * return Aigle.resolve(collection)
  470. * .eachLimit(2, (num, index, collection) => {
  471. * return new Aigle(resolve => setTimeout(() => {
  472. * console.log(num); // 1, 3, 5, 2, 4
  473. * resolve(num);
  474. * }, num * 10));
  475. * });
  476. *
  477. * @example
  478. * const collection = [1, 5, 3, 4, 2];
  479. * return Aigle.resolve(collection)
  480. * .eachLimit((num, index, collection) => {
  481. * return new Aigle(resolve => setTimeout(() => {
  482. * console.log(num); // 1, 2, 3, 4, 5
  483. * resolve(num);
  484. * }, num * 10));
  485. * });
  486. */
  487. eachLimit(limit, iterator) {
  488. return addProxy(this, EachLimit, limit, iterator);
  489. }
  490. /**
  491. * @alias eachLimit
  492. * @param {number} [limit=8]
  493. * @param {Function} iterator
  494. */
  495. forEachLimit(limit, iterator) {
  496. return addProxy(this, EachLimit, limit, iterator);
  497. }
  498. /**
  499. * `Aigle#map` will execute [`Aigle.map`](https://suguru03.github.io/aigle/docs/global.html#map) using a previous promise value and a defined iterator.
  500. * The value will be assigned as the first argument to [`Aigle.map`](https://suguru03.github.io/aigle/docs/global.html#map) and
  501. * the iterator will be assigned as the second argument.
  502. * @param {Function|string} iterator - if you define string, you can use shorthand which is similar to lodash
  503. * @return {Aigle} Returns an Aigle instance
  504. * @example
  505. * const order = [];
  506. * const collection = [1, 4, 2];
  507. * const iterator = num => {
  508. * return Aigle.delay(num * 10)
  509. * .then(() => {
  510. * order.push(num);
  511. * return num * 2;
  512. * });
  513. * };
  514. * Aigle.resolve(collection)
  515. * .map(iterator)
  516. * .then(array => {
  517. * console.log(array); // [2, 8, 4]
  518. * console.log(order); // [1, 2, 4]
  519. * });
  520. *
  521. * @example
  522. * const order = [];
  523. * const collection = { a: 1, b: 4, c: 2 };
  524. * const iterator = num => {
  525. * return Aigle.delay(num * 10)
  526. * .then(() => {
  527. * order.push(num);
  528. * return num * 2;
  529. * });
  530. * };
  531. * Aigle.resolve(collection)
  532. * .map(iterator)
  533. * .then(array => {
  534. * console.log(array); // [2, 8, 4]
  535. * console.log(order); // [1, 2, 4]
  536. * });
  537. *
  538. * @example
  539. * const collection = [{
  540. * uid: 1, name: 'test1'
  541. * }, {
  542. * uid: 4, name: 'test4'
  543. * }, {
  544. * uid: 2, name: 'test2'
  545. * }];
  546. * Aigle.resolve(collection)
  547. * .map('uid')
  548. * .then(uids => console.log(uids)); // [1, 4, 2]
  549. *
  550. * @example
  551. * const collection = {
  552. * task1: { uid: 1, name: 'test1' },
  553. * task2: { uid: 4, name: 'test4' },
  554. * task3: { uid: 2, name: 'test2' }
  555. * }];
  556. * Aigle.resolve(collection)
  557. * .map('uid')
  558. * .then(uids => console.log(uids)); // [1, 4, 2]
  559. */
  560. map(iterator) {
  561. return addProxy(this, Map, iterator);
  562. }
  563. /**
  564. * `Aigle#mapSeries` is almost the same as [`Aigle#map`](https://suguru03.github.io/aigle/docs/Aigle.html#map), but it will work in series.
  565. * @param {Function} iterator
  566. * @return {Aigle} Returns an Aigle instance
  567. * @example
  568. * const order = [];
  569. * const collection = [1, 4, 2];
  570. * const iterator = num => {
  571. * return Aigle.delay(num * 10)
  572. * .then(() => {
  573. * order.push(num);
  574. * return num * 2;
  575. * });
  576. * };
  577. * Aigle.resolve(collection)
  578. * .mapSeries(iterator)
  579. * .then(array => {
  580. * console.log(array); // [2, 8, 4]
  581. * console.log(order); // [1, 4, 2]
  582. * });
  583. *
  584. * @example
  585. * const order = [];
  586. * const collection = { a: 1, b: 4, c: 2 };
  587. * const iterator = num => {
  588. * return Aigle.delay(num * 10)
  589. * .then(() => {
  590. * order.push(num);
  591. * return num * 2;
  592. * });
  593. * };
  594. * Aigle.resolve(collection)
  595. * .mapSeries(iterator)
  596. * .then(array => {
  597. * console.log(array); // [2, 8, 4]
  598. * console.log(order); // [1, 4, 2]
  599. * });
  600. */
  601. mapSeries(iterator) {
  602. return addProxy(this, MapSeries, iterator);
  603. }
  604. /**
  605. * `Aigle#mapLimit` is almost the same as [`Aigle#map`](https://suguru03.github.io/aigle/docs/Aigle.html#map)
  606. * and [`Aigle#mapSeries`](https://suguru03.github.io/aigle/docs/Aigle.html#mapSeries)), but it will work with concurrency.
  607. * @param {integer} [limit=8]
  608. * @param {Function} iterator
  609. * @return {Aigle} Returns an Aigle instance
  610. * @example
  611. * const order = [];
  612. * const collection = [1, 5, 3, 4, 2];
  613. * const iterator = (num, index) => {
  614. * return Aigle.delay(num * 10)
  615. * .then(() => {
  616. * order.push(num);
  617. * return num * 2;
  618. * });
  619. * };
  620. * Aigle.resolve(collection)
  621. * .mapLimit(2, iterator)
  622. * .then(array => {
  623. * console.log(array); // [2, 10, 6, 8, 4];
  624. * console.log(order); // [1, 3, 5, 2, 4];
  625. * });
  626. *
  627. * @example
  628. * const order = [];
  629. * const collection = {
  630. * task1: 1,
  631. * task2: 5,
  632. * task3: 3,
  633. * task4: 4,
  634. * task5: 2
  635. * };
  636. * const iterator = (num, key) => {
  637. * return Aigle.delay(num * 10)
  638. * .then(() => {
  639. * order.push(num);
  640. * return num * 2;
  641. * });
  642. * };
  643. * Aigle.resolve(collection)
  644. * .mapLimit(2, iterator)
  645. * .then(array => {
  646. * console.log(array); // [2, 10, 6, 8, 4];
  647. * console.log(order); // [1, 3, 5, 2, 4];
  648. * });
  649. *
  650. * @example
  651. * const order = [];
  652. * const collection = [1, 5, 3, 4, 2];
  653. * const iterator = num => {
  654. * return Aigle.delay(num * 10)
  655. * .then(() => {
  656. * order.push(num);
  657. * return num * 2;
  658. * });
  659. * };
  660. * Aigle.resolve(collection)
  661. * .mapLimit(iterator)
  662. * .then(array => {
  663. * console.log(array); // [2, 10, 6, 8, 4];
  664. * console.log(order); // [1, 2, 3, 4, 5];
  665. * });
  666. */
  667. mapLimit(limit, iterator) {
  668. return addProxy(this, MapLimit, limit, iterator);
  669. }
  670. /**
  671. * `Aigle#mapValues` will execute [`Aigle.mapValues`](https://suguru03.github.io/aigle/docs/global.html#mapValues) using a previous promise value and a defined iterator.
  672. * The value will be assigned as the first argument to [`Aigle.mapValues`](https://suguru03.github.io/aigle/docs/global.html#mapValues) and
  673. * the iterator will be assigned as the second argument.
  674. * @param {Function|string} iterator - if you define string, you can use shorthand which is similar to lodash
  675. * @return {Aigle} Returns an Aigle instance
  676. * @example
  677. * const order = [];
  678. * const collection = [1, 4, 2];
  679. * const iterator = num => {
  680. * return Aigle.delay(num * 10)
  681. * .then(() => {
  682. * order.push(num);
  683. * return num * 2;
  684. * });
  685. * };
  686. * Aigle.resolve(collection)
  687. * .mapValues(iterator)
  688. * .then(object => {
  689. * console.log(object); // { '0': 2, '1': 8, '2': 4 }
  690. * console.log(order); // [1, 2, 4]
  691. * });
  692. *
  693. * @example
  694. * const order = [];
  695. * const collection = { a: 1, b: 4, c: 2 };
  696. * const iterator = num => {
  697. * return Aigle.delay(num * 10)
  698. * .then(() => {
  699. * order.push(num);
  700. * return num * 2;
  701. * });
  702. * };
  703. * Aigle.resolve(collection)
  704. * .mapValues(iterator)
  705. * .then(object => {
  706. * console.log(object); // { a: 2, b: 8, c: 4 }
  707. * console.log(order); // [1, 2, 4]
  708. * });
  709. *
  710. * @example
  711. * const collection = [{
  712. * uid: 1, name: 'test1'
  713. * }, {
  714. * uid: 4, name: 'test4'
  715. * }, {
  716. * uid: 2, name: 'test2'
  717. * }];
  718. * Aigle.resolve(collection)
  719. * .mapValues('uid')
  720. * .then(uids => console.log(uids)); // { '0': 1, '1': 4, '2': 2 }
  721. *
  722. * @example
  723. * const collection = {
  724. * task1: { uid: 1, name: 'test1' },
  725. * task2: { uid: 4, name: 'test4' },
  726. * task3: { uid: 2, name: 'test2' }
  727. * }];
  728. * Aigle.resolve(collection)
  729. * .mapValues('uid')
  730. * .then(uids => console.log(uids)); // { task1: 1, task2: 4, task3: 2 }
  731. */
  732. mapValues(iterator) {
  733. return addProxy(this, MapValues, iterator);
  734. }
  735. /**
  736. * `Aigle#mapValuesSeries` is almost the same as [`Aigle#mapValues`](https://suguru03.github.io/aigle/docs/Aigle.html#mapValues), but it will work in series.
  737. * @param {Function} iterator
  738. * @return {Aigle} Returns an Aigle instance
  739. * @example
  740. * const order = [];
  741. * const collection = [1, 4, 2];
  742. * const iterator = num => {
  743. * return Aigle.delay(num * 10)
  744. * .then(() => {
  745. * order.push(num);
  746. * return num * 2;
  747. * });
  748. * };
  749. * Aigle.resolve(collection)
  750. * .mapValuesSeries(iterator)
  751. * .then(object => {
  752. * console.log(object); // { '0': 2, '1': 8, '2': 4 }
  753. * console.log(order); // [1, 4, 2]
  754. * });
  755. *
  756. * @example
  757. * const order = [];
  758. * const collection = { a: 1, b: 4, c: 2 };
  759. * const iterator = num => {
  760. * return Aigle.delay(num * 10)
  761. * .then(() => {
  762. * order.push(num);
  763. * return num * 2;
  764. * });
  765. * };
  766. * Aigle.resolve(collection)
  767. * .mapValuesSeries(iterator)
  768. * .then(object => {
  769. * console.log(object); // { a: 2, b: 8, c: 4 }
  770. * console.log(order); // [1, 4, 2]
  771. * });
  772. */
  773. mapValuesSeries(iterator) {
  774. return addProxy(this, MapValuesSeries, iterator);
  775. }
  776. /**
  777. * `Aigle#mapValuesLimit` is almost the same as [`Aigle#mapValues`](https://suguru03.github.io/aigle/docs/Aigle.html#mapValues)
  778. * and [`Aigle#mapValuesSeries`](https://suguru03.github.io/aigle/docs/Aigle.html#mapValuesSeries)), but it will work with concurrency.
  779. * @param {number} [limit=8]
  780. * @param {Function} iterator
  781. * @return {Aigle} Returns an Aigle instance
  782. * @example
  783. * const order = [];
  784. * const collection = [1, 5, 3, 4, 2];
  785. * const iterator = (num, index) => {
  786. * return Aigle.delay(num * 10)
  787. * .then(() => {
  788. * order.push(num);
  789. * return num * 2;
  790. * });
  791. * };
  792. * Aigle.resolve(collection)
  793. * .mapValuesLimit(2, iterator)
  794. * .then(object => {
  795. * console.log(object); // { '0': 2, '1': 10, '2': 6, '3': 8, '4': 4 }
  796. * console.log(order); // [1, 3, 5, 2, 4]
  797. * });
  798. *
  799. * @example
  800. * const order = [];
  801. * const collection = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  802. * const iterator = (num, key) => {
  803. * return Aigle.delay(num * 10)
  804. * .then(() => {
  805. * order.push(num);
  806. * return num * 2;
  807. * });
  808. * };
  809. * Aigle.resolve(collection)
  810. * .mapValuesLimit(2, iterator)
  811. * .then(object => {
  812. * console.log(object); // { a: 2, b: 10, c: 6, d: 8, e: 4 }
  813. * console.log(order); // [1, 3, 5, 2, 4]
  814. * });
  815. *
  816. * @example
  817. * const order = [];
  818. * const collection = [1, 5, 3, 4, 2];
  819. * const iterator = num => {
  820. * return Aigle.delay(num * 10)
  821. * .then(() => {
  822. * order.push(num);
  823. * return num * 2;
  824. * });
  825. * };
  826. * Aigle.resolve(collection)
  827. * .mapValuesLimit(iterator)
  828. * .then(object => {
  829. * console.log(object); // { '0': 2, '1': 10, '2': 6, '3': 8, '4': 4 }
  830. * console.log(order); // [1, 2, 3, 4, 5]
  831. * });
  832. */
  833. mapValuesLimit(limit, iterator) {
  834. return addProxy(this, MapValuesLimit, limit, iterator);
  835. }
  836. /**
  837. * `Aigle#filter` will execute [`Aigle.filter`](https://suguru03.github.io/aigle/docs/global.html#filter) using a previous promise value and a defined iterator.
  838. * The value will be assigned as the first argument to [`Aigle.filter`](https://suguru03.github.io/aigle/docs/global.html#filter) and
  839. * the iterator will be assigned as the second argument.
  840. * @param {Function|Array|Object|string} iterator
  841. * @return {Aigle} Returns an Aigle instance
  842. * @example
  843. * const order = [];
  844. * const collection = [1, 4, 2];
  845. * const iterator = (num, index) => {
  846. * return Aigle.delay(num * 10)
  847. * .then(() => {
  848. * order.push(num);
  849. * return num % 2;
  850. * });
  851. * };
  852. * Aigle.resolve(collection)
  853. * .filter(iterator)
  854. * .then(array => {
  855. * console.log(array); // [1];
  856. * console.log(order); // [1, 2, 4];
  857. * });
  858. *
  859. * @example
  860. * const order = [];
  861. * const collection = { a: 1, b: 4, c: 2 };
  862. * const iterator = (num, key) => {
  863. * return Aigle.delay(num * 10)
  864. * .then(() => {
  865. * order.push(num);
  866. * return num % 2;
  867. * });
  868. * };
  869. * Aigle.resolve(collection)
  870. * .filter(iterator)
  871. * .then(array => {
  872. * console.log(array); // [1];
  873. * console.log(order); // [1, 2, 4];
  874. * });
  875. *
  876. * @example
  877. * const order = [];
  878. * const collection = [{
  879. * name: 'bargey', active: false
  880. * }, {
  881. * name: 'fread', active: true
  882. * }];
  883. * Aigle.resolve(collection)
  884. * .filter('active')
  885. * .then(array => {
  886. * console.log(array); // [{ name: 'fread', active: true }]
  887. * });
  888. *
  889. * @example
  890. * const order = [];
  891. * const collection = [{
  892. * name: 'bargey', active: false
  893. * }, {
  894. * name: 'fread', active: true
  895. * }];
  896. * Aigle.resolve(collection)
  897. * .filter(['name', 'fread'])
  898. * .then(array => {
  899. * console.log(array); // [{ name: 'fread', active: true }]
  900. * });
  901. *
  902. * @example
  903. * const order = [];
  904. * const collection = [{
  905. * name: 'bargey', active: false
  906. * }, {
  907. * name: 'fread', active: true
  908. * }];
  909. * Aigle.resolve(collection)
  910. * .filter({ name: 'fread', active: true })
  911. * .then(array => {
  912. * console.log(array); // [{ name: 'fread', active: true }]
  913. * });
  914. */
  915. filter(iterator) {
  916. return addProxy(this, Filter, iterator);
  917. }
  918. /**
  919. * `Aigle#filterSeries` is almost the same as [`Aigle#filter`](https://suguru03.github.io/aigle/docs/Aigle.html#filter), but it will work in series.
  920. * @param {Function} iterator
  921. * @return {Aigle} Returns an Aigle instance
  922. * @example
  923. * const order = [];
  924. * const collection = [1, 4, 2];
  925. * const iterator = (num, index) => {
  926. * return Aigle.delay(num * 10)
  927. * .then(() => {
  928. * order.push(num);
  929. * return num % 2;
  930. * });
  931. * };
  932. * Aigle.resolve(collection)
  933. * .filterSeries(iterator)
  934. * .then(array => {
  935. * console.log(array); // [1];
  936. * console.log(order); // [1, 4, 2];
  937. * });
  938. *
  939. * @example
  940. * const order = [];
  941. * const collection = { a: 1, b: 4, c: 2 };
  942. * const iterator = (num, key) => {
  943. * return Aigle.delay(num * 10)
  944. * .then(() => {
  945. * order.push(num);
  946. * return num % 2;
  947. * });
  948. * };
  949. * Aigle.resolve(collection)
  950. * .filterSeries(iterator)
  951. * .then(array => {
  952. * console.log(array); // [1];
  953. * console.log(order); // [1, 4, 2];
  954. * });
  955. */
  956. filterSeries(iterator) {
  957. return addProxy(this, FilterSeries, iterator);
  958. }
  959. /**
  960. * `Aigle#filterLimit` is almost the same as [`Aigle#filter`](https://suguru03.github.io/aigle/docs/Aigle.html#filter)
  961. * and [`Aigle#filterSeries`](https://suguru03.github.io/aigle/docs/Aigle.html#filterSeries)), but it will work with concurrency.
  962. * @param {integer} [limit=8]
  963. * @param {Function} iterator
  964. * @return {Aigle} Returns an Aigle instance
  965. * @example
  966. * const order = [];
  967. * const collection = [1, 5, 3, 4, 2];
  968. * const iterator = (num, index) => {
  969. * return Aigle.delay(num * 10)
  970. * .then(() => {
  971. * order.push(num);
  972. * return num % 2;
  973. * });
  974. * };
  975. * Aigle.resolve(collection)
  976. * .filterLimit(2, iterator)
  977. * .then(array => {
  978. * console.log(array); // [1, 5, 3];
  979. * console.log(order); // [1, 3, 5, 2, 4];
  980. * });
  981. *
  982. * @example
  983. * const order = [];
  984. * const collection = {
  985. * task1: 1,
  986. * task2: 5,
  987. * task3: 3,
  988. * task4: 4,
  989. * task5: 2
  990. * };
  991. * const iterator = (num, key) => {
  992. * return Aigle.delay(num * 10)
  993. * .then(() => {
  994. * order.push(num);
  995. * return num % 2;
  996. * });
  997. * };
  998. * Aigle.resolve(collection)
  999. * .filterLimit(2, iterator)
  1000. * .then(array => {
  1001. * console.log(array); // [1, 5, 3];
  1002. * console.log(order); // [1, 3, 5, 2, 4];
  1003. * });
  1004. *
  1005. * @example
  1006. * const order = [];
  1007. * const collection = [1, 5, 3, 4, 2];
  1008. * const iterator = num => {
  1009. * return Aigle.delay(num * 10)
  1010. * .then(() => {
  1011. * order.push(num);
  1012. * return num % 2;
  1013. * });
  1014. * };
  1015. * Aigle.resolve(collection)
  1016. * .filterLimit(iterator)
  1017. * .then(array => {
  1018. * console.log(array); // [1, 5, 3];
  1019. * console.log(order); // [1, 2, 3, 4, 5];
  1020. * });
  1021. */
  1022. filterLimit(limit, iterator) {
  1023. return addProxy(this, FilterLimit, limit, iterator);
  1024. }
  1025. /**
  1026. * @param {Function|string} iterator
  1027. * @return {Aigle} Returns an Aigle instance
  1028. * @example
  1029. * const order = [];
  1030. * const collection = [1, 4, 2];
  1031. * const iterator = (num, index) => {
  1032. * return Aigle.delay(num * 10)
  1033. * .then(() => {
  1034. * order.push(num);
  1035. * return num % 2;
  1036. * });
  1037. * };
  1038. * Aigle.resolve(collection)
  1039. * .reject(iterator)
  1040. * .then(array => {
  1041. * console.log(array); // [4, 2];
  1042. * console.log(order); // [1, 2, 4];
  1043. * });
  1044. *
  1045. * @example
  1046. * const order = [];
  1047. * const collection = { a: 1, b: 4, c: 2 };
  1048. * const iterator = (num, key) => {
  1049. * return Aigle.delay(num * 10)
  1050. * .then(() => {
  1051. * order.push(num);
  1052. * return num % 2;
  1053. * });
  1054. * };
  1055. * Aigle.resolve(collection)
  1056. * .reject(iterator)
  1057. * .then(array => {
  1058. * console.log(array); // [4, 2];
  1059. * console.log(order); // [1, 2, 4];
  1060. * });
  1061. *
  1062. * @example
  1063. * const order = [];
  1064. * const collection = [{
  1065. * name: 'bargey', active: false
  1066. * }, {
  1067. * name: 'fread', active: true
  1068. * }];
  1069. * Aigle.collection(collection)
  1070. * .reject('active')
  1071. * .then(array => {
  1072. * console.log(array); // [{ name: 'fread', active: false }]
  1073. * });
  1074. *
  1075. * @example
  1076. * const order = [];
  1077. * const collection = [{
  1078. * name: 'bargey', active: false
  1079. * }, {
  1080. * name: 'fread', active: true
  1081. * }];
  1082. * Aigle.collection(collection)
  1083. * .reject(['name', 'bargey'])
  1084. * .then(array => {
  1085. * console.log(array); // [{ name: 'fread', active: false }]
  1086. * });
  1087. *
  1088. * @example
  1089. * const order = [];
  1090. * const collection = [{
  1091. * name: 'bargey', active: false
  1092. * }, {
  1093. * name: 'fread', active: true
  1094. * }];
  1095. * Aigle.collection(collection)
  1096. * .reject({ name: 'bargey', active: false })
  1097. * .then(array => {
  1098. * console.log(array); // [{ name: 'fread', active: false }]
  1099. * });
  1100. */
  1101. reject(iterator) {
  1102. return addProxy(this, Reject, iterator);
  1103. }
  1104. /**
  1105. * @param {Function} iterator
  1106. * @return {Aigle} Returns an Aigle instance
  1107. * @example
  1108. * const order = [];
  1109. * const collection = [1, 4, 2];
  1110. * const iterator = (num, index) => {
  1111. * return Aigle.delay(num * 10)
  1112. * .then(() => {
  1113. * order.push(num);
  1114. * return num % 2;
  1115. * });
  1116. * };
  1117. * Aigle.resolve(collection)
  1118. * .rejectSeries(iterator)
  1119. * .then(array => {
  1120. * console.log(array); // [4, 2];
  1121. * console.log(order); // [1, 4, 2];
  1122. * });
  1123. *
  1124. * @example
  1125. * const order = [];
  1126. * const collection = { a: 1, b: 4, c: 2 };
  1127. * const iterator = (num, key) => {
  1128. * return Aigle.delay(num * 10)
  1129. * .then(() => {
  1130. * order.push(num);
  1131. * return num % 2;
  1132. * });
  1133. * };
  1134. * Aigle.resolve(collection)
  1135. * .rejectSeries(iterator)
  1136. * .then(array => {
  1137. * console.log(array); // [4, 2];
  1138. * console.log(order); // [1, 4, 2];
  1139. * });
  1140. */
  1141. rejectSeries(iterator) {
  1142. return addProxy(this, RejectSeries, iterator);
  1143. }
  1144. /**
  1145. * @param {number} [limit=8]
  1146. * @param {Function} iterator
  1147. * @return {Aigle} Returns an Aigle instance
  1148. * @example
  1149. * const order = [];
  1150. * const collection = [1, 5, 3, 4, 2];
  1151. * const iterator = (num, index) => {
  1152. * return Aigle.delay(num * 10)
  1153. * .then(() => {
  1154. * order.push(num);
  1155. * return num % 2;
  1156. * });
  1157. * };
  1158. * Aigle.resolve(collection)
  1159. * .rejectLimit(2, iterator)
  1160. * .then(array => {
  1161. * console.log(array); // [4, 2]
  1162. * console.log(order); // [1, 3, 5, 2, 4]
  1163. * });
  1164. *
  1165. * @example
  1166. * const order = [];
  1167. * const collection = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  1168. * const iterator = (num, key) => {
  1169. * return Aigle.delay(num * 10)
  1170. * .then(() => {
  1171. * order.push(num);
  1172. * return num % 2;
  1173. * });
  1174. * };
  1175. * Aigle.resolve(collection)
  1176. * .rejectLimit(2, iterator)
  1177. * .then(array => {
  1178. * console.log(array); // [4, 2]
  1179. * console.log(order); // [1, 3, 5, 2, 4]
  1180. * });
  1181. *
  1182. * @example
  1183. * const order = [];
  1184. * const collection = [1, 5, 3, 4, 2];
  1185. * const iterator = num => {
  1186. * return Aigle.delay(num * 10)
  1187. * .then(() => {
  1188. * order.push(num);
  1189. * return num % 2;
  1190. * });
  1191. * };
  1192. * Aigle.resolve(collection)
  1193. * .rejectLimit(iterator)
  1194. * .then(array => {
  1195. * console.log(array); // [4, 2]
  1196. * console.log(order); // [1, 2, 3, 4, 5]
  1197. * });
  1198. */
  1199. rejectLimit(limit, iterator) {
  1200. return addProxy(this, RejectLimit, limit, iterator);
  1201. }
  1202. /**
  1203. * `Aigle#find` will execute [`Aigle.find`](https://suguru03.github.io/aigle/docs/global.html#find) using a previous promise value and a defined iterator.
  1204. * The value will be assigned as the first argument to [`Aigle.find`](https://suguru03.github.io/aigle/docs/global.html#find) and
  1205. * the iterator will be assigned as the second argument.
  1206. * @param {Function|Array|Object|string} iterator
  1207. * @return {Aigle} Returns an Aigle instance
  1208. * @example
  1209. * const order = [];
  1210. * const collection = [1, 4, 2];
  1211. * const iterator = num => {
  1212. * return Aigle.delay(num * 10)
  1213. * .then(() => {
  1214. * order.push(num);
  1215. * return num % 2 === 0;
  1216. * });
  1217. * };
  1218. * Aigle.resolve(collection)
  1219. * .find(iterator)
  1220. * .then(value => {
  1221. * console.log(value); // 2
  1222. * console.log(order); // [1, 2];
  1223. * });
  1224. *
  1225. * @example
  1226. * const order = [];
  1227. * const collection = { a: 1, b: 4, c: 2 };
  1228. * const iterator = num => {
  1229. * return Aigle.delay(num * 10)
  1230. * .then(() => {
  1231. * order.push(num);
  1232. * return num % 2 === 0;
  1233. * });
  1234. * };
  1235. * Aigle.resolve(collection)
  1236. * .find(iterator)
  1237. * .then(value => {
  1238. * console.log(value); // 2
  1239. * console.log(order); // [1, 2];
  1240. * });
  1241. *
  1242. * @example
  1243. * const order = [];
  1244. * const collection = [1, 4, 2];
  1245. * const iterator = num => {
  1246. * return Aigle.delay(num * 10)
  1247. * .then(() => {
  1248. * order.push(num);
  1249. * return false;
  1250. * });
  1251. * };
  1252. * Aigle.resolve(collection)
  1253. * .find(iterator)
  1254. * .then(value => {
  1255. * console.log(value); // undefined
  1256. * console.log(order); // [1, 2, 4];
  1257. * });
  1258. *
  1259. * @example
  1260. * const collection = [{
  1261. * name: 'bargey', active: false
  1262. * }, {
  1263. * name: 'fread', active: true
  1264. * }];
  1265. * Aigle.resolve(collection)
  1266. * .find('active')
  1267. * .then(object => {
  1268. * console.log(object); // { name: 'fread', active: true }
  1269. * });
  1270. *
  1271. * @example
  1272. * const collection = [{
  1273. * name: 'bargey', active: false
  1274. * }, {
  1275. * name: 'fread', active: true
  1276. * }];
  1277. * Aigle.resolve(collection)
  1278. * .find(['name', 'fread'])
  1279. * .then(object => {
  1280. * console.log(object); // { name: 'fread', active: true }
  1281. * });
  1282. *
  1283. * @example
  1284. * const collection = [{
  1285. * name: 'bargey', active: false
  1286. * }, {
  1287. * name: 'fread', active: true
  1288. * }];
  1289. * Aigle.resolve(collection)
  1290. * .find({ name: 'fread', active: true })
  1291. * .then(object => {
  1292. * console.log(object); // { name: 'fread', active: true }
  1293. * });
  1294. */
  1295. find(iterator) {
  1296. return addProxy(this, Find, iterator);
  1297. }
  1298. /**
  1299. * `Aigle#findSeries` is almost the same as [`Aigle#find`](https://suguru03.github.io/aigle/docs/Aigle.html#find), but it will work in series.
  1300. * @param {Function} iterator
  1301. * @return {Aigle} Returns an Aigle instance
  1302. * @example
  1303. * const order = [];
  1304. * const collection = [1, 4, 2];
  1305. * const iterator = num => {
  1306. * return Aigle.delay(num * 10)
  1307. * .then(() => {
  1308. * order.push(num);
  1309. * return num % 2 === 0;
  1310. * });
  1311. * };
  1312. * Aigle.resolve(collection)
  1313. * .findSeries(iterator)
  1314. * .then(value => {
  1315. * console.log(value); // 4
  1316. * console.log(order); // [1, 4];
  1317. * });
  1318. *
  1319. * @example
  1320. * const order = [];
  1321. * const collection = { a: 1, b: 4, c: 2 };
  1322. * const iterator = num => {
  1323. * return Aigle.delay(num * 10)
  1324. * .then(() => {
  1325. * order.push(num);
  1326. * return num % 2 === 0;
  1327. * });
  1328. * };
  1329. * Aigle.resolve(collection)
  1330. * .findSeries(iterator)
  1331. * .then(value => {
  1332. * console.log(value); // 4
  1333. * console.log(order); // [1, 4];
  1334. * });
  1335. *
  1336. * @example
  1337. * const order = [];
  1338. * const collection = [1, 4, 2];
  1339. * const iterator = num => {
  1340. * return Aigle.delay(num * 10)
  1341. * .then(() => {
  1342. * order.push(num);
  1343. * return false;
  1344. * });
  1345. * };
  1346. * Aigle.resolve(collection)
  1347. * .findSeries(iterator)
  1348. * .then(value => {
  1349. * console.log(value); // undefined
  1350. * console.log(order); // [1, 4, 2];
  1351. * });
  1352. */
  1353. findSeries(iterator) {
  1354. return addProxy(this, FindSeries, iterator);
  1355. }
  1356. /**
  1357. * `Aigle#findLimit` is almost the same as [`Aigle#find`](https://suguru03.github.io/aigle/docs/Aigle.html#find)
  1358. * and [`Aigle#findSeries`](https://suguru03.github.io/aigle/docs/Aigle.html#findSeries)), but it will work with concurrency.
  1359. * @param {integer} [limit=8]
  1360. * @param {Function} iterator
  1361. * @return {Aigle} Returns an Aigle instance
  1362. * @example
  1363. * const order = [];
  1364. * const collection = [1, 5, 3, 4, 2];
  1365. * const iterator = (num, index) => {
  1366. * return Aigle.delay(num * 10)
  1367. * .then(() => {
  1368. * order.push(num);
  1369. * return num % 2 === 0;
  1370. * });
  1371. * };
  1372. * Aigle.resolve(collection)
  1373. * .findLimit(2, iterator)
  1374. * .then(value => {
  1375. * console.log(value); // 2
  1376. * console.log(order); // [1, 3, 5, 2];
  1377. * });
  1378. *
  1379. * @example
  1380. * const order = [];
  1381. * const collection = {
  1382. * task1: 1,
  1383. * task2: 5,
  1384. * task3: 3,
  1385. * task4: 4,
  1386. * task5: 2
  1387. * };
  1388. * const iterator = (num, key) => {
  1389. * return Aigle.delay(num * 10)
  1390. * .then(() => {
  1391. * order.push(num);
  1392. * return num % 2 === 0;
  1393. * });
  1394. * };
  1395. * Aigle.resolve(collection)
  1396. * .findLimit(2, iterator)
  1397. * .then(value => {
  1398. * console.log(value); // 2
  1399. * console.log(order); // [1, 3, 5, 2];
  1400. * });
  1401. *
  1402. * @example
  1403. * const order = [];
  1404. * const collection = [1, 5, 3, 4, 2];
  1405. * const iterator = num => {
  1406. * return Aigle.delay(num * 10)
  1407. * .then(() => {
  1408. * order.push(num);
  1409. * return num % 2 === 0;
  1410. * });
  1411. * };
  1412. * Aigle.resolve(collection)
  1413. * .findLimit(2, iterator)
  1414. * .then(value => {
  1415. * console.log(value); // 2
  1416. * console.log(order); // [1, 2];
  1417. * });
  1418. */
  1419. findLimit(limit, iterator) {
  1420. return addProxy(this, FindLimit, limit, iterator);
  1421. }
  1422. /**
  1423. * `Aigle#findIndex` will execute [`Aigle.findIndex`](https://suguru03.github.io/aigle/docs/global.html#findIndex) using a previous promise value and a defined iterator.
  1424. * The value will be assigned as the first argument to [`Aigle.findIndex`](https://suguru03.github.io/aigle/docs/global.html#findIndex) and
  1425. * the iterator will be assigned as the second argument.
  1426. * @param {Function|Array|Object|string} iterator
  1427. * @return {Aigle} Returns an Aigle instance
  1428. * @example
  1429. * const order = [];
  1430. * const collection = [1, 4, 2];
  1431. * const iterator = num => {
  1432. * return Aigle.delay(num * 10)
  1433. * .then(() => {
  1434. * order.push(num);
  1435. * return num % 2 === 0;
  1436. * });
  1437. * };
  1438. * Aigle.resolve(collection)
  1439. * .findIndex(iterator)
  1440. * .then(index => {
  1441. * console.log(index); // 2
  1442. * console.log(order); // [1, 2];
  1443. * });
  1444. *
  1445. * @example
  1446. * const order = [];
  1447. * const collection = [1, 4, 2];
  1448. * const iterator = num => {
  1449. * return Aigle.delay(num * 10)
  1450. * .then(() => {
  1451. * order.push(num);
  1452. * return false;
  1453. * });
  1454. * };
  1455. * Aigle.resolve(collection)
  1456. * .findIndex(iterator)
  1457. * .then(index => {
  1458. * console.log(index); // -1
  1459. * console.log(order); // [1, 2, 4];
  1460. * });
  1461. *
  1462. * @example
  1463. * const collection = [{
  1464. * name: 'bargey', active: false
  1465. * }, {
  1466. * name: 'fread', active: true
  1467. * }];
  1468. * Aigle.resolve(collection)
  1469. * .findIndex('active')
  1470. * .then(index => {
  1471. * console.log(index); // 1
  1472. * });
  1473. *
  1474. * @example
  1475. * const collection = [{
  1476. * name: 'bargey', active: false
  1477. * }, {
  1478. * name: 'fread', active: true
  1479. * }];
  1480. * Aigle.resolve(collection)
  1481. * .findIndex(['name', 'fread'])
  1482. * .then(index => {
  1483. * console.log(index); // 1
  1484. * });
  1485. *
  1486. * @example
  1487. * const collection = [{
  1488. * name: 'bargey', active: false
  1489. * }, {
  1490. * name: 'fread', active: true
  1491. * }];
  1492. * Aigle.resolve(collection)
  1493. * .findIndex({ name: 'fread', active: true })
  1494. * .then(index => {
  1495. * console.log(index); // 1
  1496. * });
  1497. */
  1498. findIndex(iterator) {
  1499. return addProxy(this, FindIndex, iterator);
  1500. }
  1501. /**
  1502. * `Aigle#findIndexSeries` is almost the same as [`Aigle#findIndex`](https://suguru03.github.io/aigle/docs/Aigle.html#findIndex), but it will work in series.
  1503. * @param {Function} iterator
  1504. * @return {Aigle} Returns an Aigle instance
  1505. * @example
  1506. * const order = [];
  1507. * const collection = [1, 4, 2];
  1508. * const iterator = num => {
  1509. * return Aigle.delay(num * 10)
  1510. * .then(() => {
  1511. * order.push(num);
  1512. * return num % 2 === 0;
  1513. * });
  1514. * };
  1515. * Aigle.resolve(collection)
  1516. * .findIndexSeries(iterator)
  1517. * .then(index => {
  1518. * console.log(index); // 2
  1519. * console.log(order); // [1, 4];
  1520. * });
  1521. *
  1522. * @example
  1523. * const order = [];
  1524. * const collection = [1, 4, 2];
  1525. * const iterator = num => {
  1526. * return Aigle.delay(num * 10)
  1527. * .then(() => {
  1528. * order.push(num);
  1529. * return false;
  1530. * });
  1531. * };
  1532. * Aigle.resolve(collection)
  1533. * .findIndexSeries(iterator)
  1534. * .then(index => {
  1535. * console.log(index); // -1
  1536. * console.log(order); // [1, 4, 2];
  1537. * });
  1538. */
  1539. findIndexSeries(iterator) {
  1540. return addProxy(this, FindIndexSeries, iterator);
  1541. }
  1542. /**
  1543. * `Aigle#findIndexLimit` is almost the same as [`Aigle#findIndex`](https://suguru03.github.io/aigle/docs/Aigle.html#findIndex)
  1544. * and [`Aigle#findIndexSeries`](https://suguru03.github.io/aigle/docs/Aigle.html#findIndexSeries)), but it will work with concurrency.
  1545. * @param {integer} [limit=8]
  1546. * @param {Function} iterator
  1547. * @return {Aigle} Returns an Aigle instance
  1548. * @example
  1549. * const order = [];
  1550. * const collection = [1, 5, 3, 4, 2];
  1551. * const iterator = (num, index) => {
  1552. * return Aigle.delay(num * 10)
  1553. * .then(() => {
  1554. * order.push(num);
  1555. * return num % 2 === 0;
  1556. * });
  1557. * };
  1558. * Aigle.resolve(collection)
  1559. * .findIndexLimit(2, iterator)
  1560. * .then(index => {
  1561. * console.log(index); // 4
  1562. * console.log(order); // [1, 3, 5, 2];
  1563. * });
  1564. *
  1565. * @example
  1566. * const order = [];
  1567. * const collection = [1, 5, 3, 4, 2];
  1568. * const iterator = num => {
  1569. * return Aigle.delay(num * 10)
  1570. * .then(() => {
  1571. * order.push(num);
  1572. * return num % 2 === 0;
  1573. * });
  1574. * };
  1575. * Aigle.resolve(collection)
  1576. * .findIndexLimit(2, iterator)
  1577. * .then(index => {
  1578. * console.log(index); // 4
  1579. * console.log(order); // [1, 2];
  1580. * });
  1581. */
  1582. findIndexLimit(limit, iterator) {
  1583. return addProxy(this, FindIndexLimit, limit, iterator);
  1584. }
  1585. /**
  1586. * @param {Function} iterator
  1587. * @return {Aigle} Returns an Aigle instance
  1588. */
  1589. findKey(iterator) {
  1590. return addProxy(this, FindKey, iterator);
  1591. }
  1592. /**
  1593. * @param {Function} iterator
  1594. * @return {Aigle} Returns an Aigle instance
  1595. */
  1596. findKeySeries(iterator) {
  1597. return addProxy(this, FindKeySeries, iterator);
  1598. }
  1599. /**
  1600. * @param {integer} [limit=8]
  1601. * @param {Function} iterator
  1602. * @return {Aigle} Returns an Aigle instance
  1603. */
  1604. findKeyLimit(limit, iterator) {
  1605. return addProxy(this, FindKeyLimit, limit, iterator);
  1606. }
  1607. /**
  1608. * @param {*} iterator
  1609. * @param {*} [args]
  1610. * @return {Aigle} Returns an Aigle instance
  1611. */
  1612. pick(iterator, ...args) {
  1613. return addProxy(this, Pick, iterator, args);
  1614. }
  1615. /**
  1616. * @alias pickBySeries
  1617. * @param {Function} iterator
  1618. */
  1619. pickSeries(iterator) {
  1620. return this.pickBySeries(iterator);
  1621. }
  1622. /**
  1623. * @alias pickByLimit
  1624. * @param {number} [limit=8]
  1625. * @param {Function} iterator
  1626. */
  1627. pickLimit(limit, iterator) {
  1628. return this.pickByLimit(limit, iterator);
  1629. }
  1630. /**
  1631. * `Aigle#pickBy` will execute [`Aigle.pickBy`](https://suguru03.github.io/aigle/docs/global.html#pickBy) using a previous promise value and a defined iterator.
  1632. * The value will be assigned as the first argument to [`Aigle.pickBy`](https://suguru03.github.io/aigle/docs/global.html#pickBy) and
  1633. * the iterator will be assigned as the second argument.
  1634. * @param {Function|string} iterator
  1635. * @return {Aigle} Returns an Aigle instance
  1636. * @example
  1637. * const order = [];
  1638. * const collection = [1, 4, 2];
  1639. * const iterator = num => {
  1640. * return Aigle.delay(num * 10)
  1641. * .then(() => {
  1642. * order.push(num);
  1643. * return num % 2;
  1644. * });
  1645. * };
  1646. * Aigle.resolve(collection)
  1647. * .pickBy(iterator)
  1648. * .then(object => {
  1649. * console.log(object); // { '0': 1 }
  1650. * console.log(order); // [1, 2, 4]
  1651. * });
  1652. *
  1653. * @example
  1654. * const order = [];
  1655. * const collection = { a: 1, b: 4, c: 2 };
  1656. * const iterator = num => {
  1657. * return Aigle.delay(num * 10)
  1658. * .then(() => {
  1659. * order.push(num);
  1660. * return num * 2;
  1661. * });
  1662. * };
  1663. * Aigle.resolve(collection)
  1664. * .pickBy(iterator)
  1665. * .then(object => {
  1666. * console.log(object); // { a: 1 }
  1667. * console.log(order); // [1, 2, 4]
  1668. * });
  1669. *
  1670. * @example
  1671. * const order = [];
  1672. * const collection = [{
  1673. * name: 'bargey', active: false
  1674. * }, {
  1675. * name: 'fread', active: true
  1676. * }];
  1677. * Aigle.resolve(collection)
  1678. * .pickBy('active')
  1679. * .then(object => {
  1680. * console.log(object); // { '1': { name: 'fread', active: true } }
  1681. * });
  1682. *
  1683. * @example
  1684. * const order = [];
  1685. * const collection = [{
  1686. * name: 'bargey', active: false
  1687. * }, {
  1688. * name: 'fread', active: true
  1689. * }];
  1690. * Aigle.resolve(collection)
  1691. * .pickBy(['name', 'fread'])
  1692. * .then(object => {
  1693. * console.log(object); // { '1': { name: 'fread', active: true } }
  1694. * });
  1695. *
  1696. * @example
  1697. * const order = [];
  1698. * const collection = [{
  1699. * name: 'bargey', active: false
  1700. * }, {
  1701. * name: 'fread', active: true
  1702. * }];
  1703. * Aigle.resolve(collection)
  1704. * .pickBy({ name: 'fread', active: true })
  1705. * .then(object => {
  1706. * console.log(object); // { '1': { name: 'fread', active: true } }
  1707. * });
  1708. */
  1709. pickBy(iterator) {
  1710. return addProxy(this, PickBy, iterator);
  1711. }
  1712. /**
  1713. * `Aigle#pickBySeries` is almost the same as [`Aigle#pickBy`](https://suguru03.github.io/aigle/docs/Aigle.html#pickBy), but it will work in series.
  1714. * @param {Function} iterator
  1715. * @return {Aigle} Returns an Aigle instance
  1716. * @example
  1717. * const order = [];
  1718. * const collection = [1, 4, 2];
  1719. * const iterator = num => {
  1720. * return Aigle.delay(num * 10)
  1721. * .then(() => {
  1722. * order.push(num);
  1723. * return num % 2;
  1724. * });
  1725. * };
  1726. * Aigle.resolve(collection)
  1727. * .pickBySeries(iterator)
  1728. * .then(object => {
  1729. * console.log(object); // { '0': 1 }
  1730. * console.log(order); // [1, 4, 2]
  1731. * });
  1732. *
  1733. * @example
  1734. * const order = [];
  1735. * const collection = { a: 1, b: 4, c: 2 };
  1736. * const iterator = num => {
  1737. * return Aigle.delay(num * 10)
  1738. * .then(() => {
  1739. * order.push(num);
  1740. * return num * 2;
  1741. * });
  1742. * };
  1743. * Aigle.resolve(collection)
  1744. * .pickBySeries(iterator)
  1745. * .then(object => {
  1746. * console.log(object); // { a: 1 }
  1747. * console.log(order); // [1, 4, 2]
  1748. * });
  1749. */
  1750. pickBySeries(iterator) {
  1751. return addProxy(this, PickBySeries, iterator);
  1752. }
  1753. /**
  1754. * `Aigle#pickByLimit` is almost the same as [`Aigle#pickBy`](https://suguru03.github.io/aigle/docs/Aigle.html#pickBy)
  1755. * and [`Aigle#pickBySeries`](https://suguru03.github.io/aigle/docs/Aigle.html#pickBySeries)), but it will work with concurrency.
  1756. * @param {number} [limit=8]
  1757. * @param {Function} iterator
  1758. * @return {Aigle} Returns an Aigle instance
  1759. * @example
  1760. * const order = [];
  1761. * const collection = [1, 5, 3, 4, 2];
  1762. * const iterator = (num, index) => {
  1763. * return Aigle.delay(num * 10)
  1764. * .then(() => {
  1765. * order.push(num);
  1766. * return num % 2;
  1767. * });
  1768. * };
  1769. * Aigle.resolve(collection)
  1770. * .pickByLimit(2, iterator)
  1771. * .then(object => {
  1772. * console.log(object); // { '0': 1, '1': 5, '2': 3 }
  1773. * console.log(order); // [1, 3, 5, 2, 4]
  1774. * });
  1775. *
  1776. * @example
  1777. * const order = [];
  1778. * const collection = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  1779. * const iterator = (num, key) => {
  1780. * return Aigle.delay(num * 10)
  1781. * .then(() => {
  1782. * order.push(num);
  1783. * return num % 2;
  1784. * });
  1785. * };
  1786. * Aigle.resolve(collection)
  1787. * .pickByLimit(2, iterator)
  1788. * .then(object => {
  1789. * console.log(object); // { a: 1, b: 5, c: 3 }
  1790. * console.log(order); // [1, 3, 5, 2, 4]
  1791. * });
  1792. *
  1793. * @example
  1794. * const order = [];
  1795. * const collection = [1, 5, 3, 4, 2];
  1796. * const iterator = num => {
  1797. * return Aigle.delay(num * 10)
  1798. * .then(() => {
  1799. * order.push(num);
  1800. * return num % 2;
  1801. * });
  1802. * };
  1803. * Aigle.resolve(collection)
  1804. * .pickByLimit(iterator)
  1805. * .then(object => {
  1806. * console.log(object); // { '0': 1, '1': 5, '2': 3 }
  1807. * console.log(order); // [1, 2, 3, 4, 5]
  1808. * });
  1809. */
  1810. pickByLimit(limit, iterator) {
  1811. return addProxy(this, PickByLimit, limit, iterator);
  1812. }
  1813. /**
  1814. * @param {*} iterator
  1815. * @param {*} [args]
  1816. * @return {Aigle} Returns an Aigle instance
  1817. */
  1818. omit(iterator, ...args) {
  1819. return addProxy(this, Omit, iterator, args);
  1820. }
  1821. /**
  1822. * @alias omitBySeries
  1823. * @param {Function} iterator
  1824. */
  1825. omitSeries(iterator) {
  1826. return this.omitBySeries(iterator);
  1827. }
  1828. /**
  1829. * @alias omitByLimit
  1830. * @param {number} [limit=8]
  1831. * @param {Function} iterator
  1832. */
  1833. omitLimit(limit, iterator) {
  1834. return this.omitByLimit(limit, iterator);
  1835. }
  1836. /**
  1837. * `Aigle#omitBy` will execute [`Aigle.omitBy`](https://suguru03.github.io/aigle/docs/global.html#omitBy) using a previous promise value and a defined iterator.
  1838. * The value will be assigned as the first argument to [`Aigle.omitBy`](https://suguru03.github.io/aigle/docs/global.html#omitBy) and
  1839. * the iterator will be assigned as the second argument.
  1840. * @param {Function|Array|Object|string} iterator
  1841. * @return {Aigle} Returns an Aigle instance
  1842. * @example
  1843. * const order = [];
  1844. * const collection = [1, 4, 2];
  1845. * const iterator = num => {
  1846. * return Aigle.delay(num * 10)
  1847. * .then(() => {
  1848. * order.push(num);
  1849. * return num % 2;
  1850. * });
  1851. * };
  1852. * Aigle.resolve(collection)
  1853. * .omitBy(iterator)
  1854. * .then(object => {
  1855. * console.log(object); // { '1': 4, '2': 4 }
  1856. * console.log(order); // [1, 2, 4]
  1857. * });
  1858. *
  1859. * @example
  1860. * const order = [];
  1861. * const collection = { a: 1, b: 4, c: 2 };
  1862. * const iterator = num => {
  1863. * return Aigle.delay(num * 10)
  1864. * .then(() => {
  1865. * order.push(num);
  1866. * return num % 2;
  1867. * });
  1868. * };
  1869. * Aigle.resolve(collection)
  1870. * .omitBy(iterator)
  1871. * .then(object => {
  1872. * console.log(object); // { b: 4, c: 2 }
  1873. * console.log(order); // [1, 2, 4]
  1874. * });
  1875. *
  1876. * @example
  1877. * const order = [];
  1878. * const collection = [{
  1879. * name: 'bargey', active: false
  1880. * }, {
  1881. * name: 'fread', active: true
  1882. * }];
  1883. * Aigle.resolve(collection)
  1884. * .omitBy('active')
  1885. * .then(object => {
  1886. * console.log(object); // { '0': { name: 'bargey', active: false } }
  1887. * });
  1888. *
  1889. * @example
  1890. * const order = [];
  1891. * const collection = [{
  1892. * name: 'bargey', active: false
  1893. * }, {
  1894. * name: 'fread', active: true
  1895. * }];
  1896. * Aigle.resolve(collection)
  1897. * .omitBy(['name', 'fread'])
  1898. * .then(object => {
  1899. * console.log(object); // { '0': { name: 'bargey', active: false } }
  1900. * });
  1901. *
  1902. * @example
  1903. * const order = [];
  1904. * const collection = [{
  1905. * name: 'bargey', active: false
  1906. * }, {
  1907. * name: 'fread', active: true
  1908. * }];
  1909. * Aigle.resolve(collection)
  1910. * .omitBy({ name: 'fread', active: true })
  1911. * .then(object => {
  1912. * console.log(object); // { '0': { name: 'bargey', active: false } }
  1913. * });
  1914. */
  1915. omitBy(iterator) {
  1916. return addProxy(this, OmitBy, iterator);
  1917. }
  1918. /**
  1919. * `Aigle#omitBySeries` is almost the same as [`Aigle#omitBy`](https://suguru03.github.io/aigle/docs/Aigle.html#omitBy), but it will work in series.
  1920. * @param {Function} iterator
  1921. * @return {Aigle} Returns an Aigle instance
  1922. * @example
  1923. * const order = [];
  1924. * const collection = [1, 4, 2];
  1925. * const iterator = num => {
  1926. * return Aigle.delay(num * 10)
  1927. * .then(() => {
  1928. * order.push(num);
  1929. * return num % 2;
  1930. * });
  1931. * };
  1932. * Aigle.resolve(collection)
  1933. * .omitBySeries(iterator)
  1934. * .then(object => {
  1935. * console.log(object); // { '1': 4, '2': 4 }
  1936. * console.log(order); // [1, 4, 2]
  1937. * });
  1938. *
  1939. * @example
  1940. * const order = [];
  1941. * const collection = { a: 1, b: 4, c: 2 };
  1942. * const iterator = num => {
  1943. * return Aigle.delay(num * 10)
  1944. * .then(() => {
  1945. * order.push(num);
  1946. * return num % 2;
  1947. * });
  1948. * };
  1949. * Aigle.resolve(collection)
  1950. * .omitBySeries(iterator)
  1951. * .then(object => {
  1952. * console.log(object); // { b: 4, c: 2 }
  1953. * console.log(order); // [1, 4, 2]
  1954. * });
  1955. */
  1956. omitBySeries(iterator) {
  1957. return addProxy(this, OmitBySeries, iterator);
  1958. }
  1959. /**
  1960. * `Aigle#omitByLimit` is almost the same as [`Aigle#omitBy`](https://suguru03.github.io/aigle/docs/Aigle.html#omitBy)
  1961. * and [`Aigle#omitBySeries`](https://suguru03.github.io/aigle/docs/Aigle.html#omitBySeries)), but it will work with concurrency.
  1962. * @param {number} [limit=8]
  1963. * @param {Function} iterator
  1964. * @return {Aigle} Returns an Aigle instance
  1965. * @example
  1966. * const order = [];
  1967. * const collection = [1, 5, 3, 4, 2];
  1968. * const iterator = (num, index) => {
  1969. * return Aigle.delay(num * 10)
  1970. * .then(() => {
  1971. * order.push(num);
  1972. * return num % 2;
  1973. * });
  1974. * };
  1975. * Aigle.resolve(collection)
  1976. * .omitByLimit(2, iterator)
  1977. * .then(object => {
  1978. * console.log(object); // { '3': 4, '4': 2 }
  1979. * console.log(order); // [1, 3, 5, 2, 4]
  1980. * });
  1981. *
  1982. * @example
  1983. * const order = [];
  1984. * const collection = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  1985. * const iterator = (num, key) => {
  1986. * return Aigle.delay(num * 10)
  1987. * .then(() => {
  1988. * order.push(num);
  1989. * return num % 2;
  1990. * });
  1991. * };
  1992. * Aigle.resolve(collection)
  1993. * .omitByLimit(2, iterator)
  1994. * .then(object => {
  1995. * console.log(object); // { d: 4, e: 2 }
  1996. * console.log(order); // [1, 3, 5, 2, 4]
  1997. * });
  1998. *
  1999. * @example
  2000. * const order = [];
  2001. * const collection = [1, 5, 3, 4, 2];
  2002. * const iterator = num => {
  2003. * return Aigle.delay(num * 10)
  2004. * .then(() => {
  2005. * order.push(num);
  2006. * return num % 2;
  2007. * });
  2008. * };
  2009. * Aigle.resolve(collection)
  2010. * .omitByLimit(iterator)
  2011. * .then(object => {
  2012. * console.log(object); // { '3': 4, '4': 2 }
  2013. * console.log(order); // [1, 2, 3, 4, 5]
  2014. * });
  2015. */
  2016. omitByLimit(limit, iterator) {
  2017. return addProxy(this, OmitByLimit, limit, iterator);
  2018. }
  2019. /**
  2020. * @param {Function} iterator
  2021. * @param {*} result
  2022. * @return {Aigle} Returns an Aigle instance
  2023. * @example
  2024. * const collection = [1, 4, 2];
  2025. * const iterator = (result, num, index) => {
  2026. * return Aigle.delay(num * 10)
  2027. * .then(() => result + num);
  2028. * };
  2029. * return Aigle.resolve(collection)
  2030. * .reduce(iterator, 1)
  2031. * .then(value => console.log(value)); // 8
  2032. *
  2033. * @example
  2034. * const collection = { a: 1, b: 4, c: 2 };
  2035. * const iterator = (result, num, key) => {
  2036. * return Aigle.delay(num * 10)
  2037. * .then(() => result + num);
  2038. * };
  2039. * return Aigle.resolve(collection)
  2040. * .reduce(iterator, '')
  2041. * .then(value => console.log(value)); // '142'
  2042. */
  2043. reduce(iterator, result) {
  2044. return addProxy(this, Reduce, iterator, result);
  2045. }
  2046. /**
  2047. * @param {Function} iterator
  2048. * @param {Array|Object} [accumulator]
  2049. * @return {Aigle} Returns an Aigle instance
  2050. * @example
  2051. * const order = [];
  2052. * const collection = [1, 4, 2];
  2053. * const iterator = (result, num, index) => {
  2054. * return Aigle.delay(num * 10)
  2055. * .then(() => {
  2056. * order.push(num);
  2057. * result[index] = num;
  2058. * });
  2059. * };
  2060. * Aigle.resolve(collection)
  2061. * .transform(iterator, {})
  2062. * .then(object => {
  2063. * console.log(object); // { '0': 1, '1': 4, '2': 2 }
  2064. * console.log(order); // [1, 2, 4]
  2065. * });
  2066. *
  2067. * @example
  2068. * const order = [];
  2069. * const collection = [1, 4, 2];
  2070. * const iterator = (result, num, index) => {
  2071. * return Aigle.delay(num * 10)
  2072. * .then(() => {
  2073. * order.push(num);
  2074. * result.push(num);
  2075. * });
  2076. * };
  2077. * Aigle.resolve(collection)
  2078. * .transform(iterator, {})
  2079. * .then(array => {
  2080. * console.log(array); // [1, 2, 4]
  2081. * console.log(order); // [1, 2, 4]
  2082. * });
  2083. *
  2084. * @example
  2085. * const order = [];
  2086. * const collection = { a: 1, b: 4, c: 2 };
  2087. * const iterator = (result, num, key) => {
  2088. * return Aigle.delay(num * 10)
  2089. * .then(() => {
  2090. * order.push(num);
  2091. * result.push(num);
  2092. * return num !== 2;
  2093. * });
  2094. * };
  2095. * Aigle.resolve(collection)
  2096. * .transform(iterator, [])
  2097. * .then(array => {
  2098. * console.log(array); // [1, 2]
  2099. * console.log(order); // [1, 2]
  2100. * });
  2101. */
  2102. transform(iterator, accumulator) {
  2103. return addProxy(this, Transform, iterator, accumulator);
  2104. }
  2105. /**
  2106. * @param {Function} iterator
  2107. * @param {Array|Object} [accumulator]
  2108. * @return {Aigle} Returns an Aigle instance
  2109. * @example
  2110. * const order = [];
  2111. * const collection = [1, 4, 2];
  2112. * const iterator = (result, num, index) => {
  2113. * return Aigle.delay(num * 10)
  2114. * .then(() => {
  2115. * order.push(num);
  2116. * result[index] = num;
  2117. * });
  2118. * };
  2119. * Aigle.resolve(collection)
  2120. * .transformSeries(iterator, {})
  2121. * .then(object => {
  2122. * console.log(object); // { '0': 1, '1': 4, '2': 2 }
  2123. * console.log(order); // [1, 4, 2]
  2124. * });
  2125. *
  2126. * @example
  2127. * const order = [];
  2128. * const collection = [1, 4, 2];
  2129. * const iterator = (result, num, index) => {
  2130. * return Aigle.delay(num * 10)
  2131. * .then(() => {
  2132. * order.push(num);
  2133. * result.push(num);
  2134. * });
  2135. * };
  2136. * Aigle.resolve(collection)
  2137. * .transformSeries(iterator, {})
  2138. * .then(array => {
  2139. * console.log(array); // [1, 4, 2]
  2140. * console.log(order); // [1, 4, 2]
  2141. * });
  2142. *
  2143. * @example
  2144. * const order = [];
  2145. * const collection = { a: 1, b: 4, c: 2 };
  2146. * const iterator = (result, num, key) => {
  2147. * return Aigle.delay(num * 10)
  2148. * .then(() => {
  2149. * order.push(num);
  2150. * result.push(num);
  2151. * return num !== 4;
  2152. * });
  2153. * };
  2154. * Aigle.resolve(collection)
  2155. * .transformSeries(iterator, [])
  2156. * .then(array => {
  2157. * console.log(array); // [1, 4]
  2158. * console.log(order); // [1, 4]
  2159. * });
  2160. */
  2161. transformSeries(iterator, accumulator) {
  2162. return addProxy(this, TransformSeries, iterator, accumulator);
  2163. }
  2164. /**
  2165. * @param {number} [limit=8]
  2166. * @param {Function} iterator
  2167. * @param {Array|Object} [accumulator]
  2168. * @example
  2169. * const order = [];
  2170. * const collection = [1, 5, 3, 4, 2];
  2171. * const iterator = (result, num, index) => {
  2172. * return Aigle.delay(num * 10)
  2173. * .then(() => {
  2174. * order.push(num);
  2175. * result[index] = num;
  2176. * });
  2177. * };
  2178. * Aigle.resolve(collection)
  2179. * .transformLimit(2, iterator, {})
  2180. * .then(object => {
  2181. * console.log(object); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }
  2182. * console.log(order); // [1, 5, 3, 4, 2]
  2183. * });
  2184. *
  2185. * @example
  2186. * const order = [];
  2187. * const collection = [1, 5, 3, 4, 2];
  2188. * const iterator = (result, num, index) => {
  2189. * return Aigle.delay(num * 10)
  2190. * .then(() => {
  2191. * order.push(num);
  2192. * result.push(num);
  2193. * });
  2194. * };
  2195. * Aigle.resolve(collection)
  2196. * .transformLimit(2, iterator, {})
  2197. * .then(array => {
  2198. * console.log(array); // [1, 5, 3, 4, 2]
  2199. * console.log(order); // [1, 5, 3, 4, 2]
  2200. * });
  2201. *
  2202. * @example
  2203. * const order = [];
  2204. * const collection = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  2205. * const iterator = (result, num, key) => {
  2206. * return Aigle.delay(num * 10)
  2207. * .then(() => {
  2208. * order.push(num);
  2209. * result.push(num);
  2210. * return num !== 4;
  2211. * });
  2212. * };
  2213. * Aigle.resolve(collection)
  2214. * .transformLimit(2, iterator, [])
  2215. * .then(array => {
  2216. * console.log(array); // [1, 5, 3, 4]
  2217. * console.log(order); // [1, 5, 3, 4]
  2218. * });
  2219. *
  2220. * @example
  2221. * const order = [];
  2222. * const collection = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  2223. * const iterator = (result, num, key) => {
  2224. * return Aigle.delay(num * 10)
  2225. * .then(() => {
  2226. * order.push(num);
  2227. * result.push(num);
  2228. * return num !== 4;
  2229. * });
  2230. * };
  2231. * Aigle.resolve(collection)
  2232. * .transformLimit(iterator, [])
  2233. * .then(array => {
  2234. * console.log(array); // [1, 2, 3, 4]
  2235. * console.log(order); // [1, 2, 3, 4]
  2236. * });
  2237. */
  2238. transformLimit(limit, iterator, accumulator) {
  2239. return addProxy(this, TransformLimit, limit, iterator, accumulator);
  2240. }
  2241. /**
  2242. * `Aigle#sortBy` will execute [`Aigle.sortBy`](https://suguru03.github.io/aigle/docs/global.html#sortBy) using a previous promise value and a defined iterator.
  2243. * The value will be assigned as the first argument to [`Aigle.sortBy`](https://suguru03.github.io/aigle/docs/global.html#sortBy) and
  2244. * the iterator will be assigned as the second argument.
  2245. * @param {Function|string} iterator
  2246. * @return {Aigle} Returns an Aigle instance
  2247. * @example
  2248. * const order = [];
  2249. * const collection = [1, 4, 2];
  2250. * const iterator = num => {
  2251. * return Aigle.delay(num * 10)
  2252. * .then(() => {
  2253. * order.push(num);
  2254. * return num;
  2255. * });
  2256. * };
  2257. * Aigle.resolve(collection)
  2258. * .sortBy(iterator)
  2259. * .then(array => {
  2260. * console.log(array); // [1, 2, 4]
  2261. * console.log(order); // [1, 2, 4]
  2262. * });
  2263. *
  2264. * @example
  2265. * const order = [];
  2266. * const collection = { a: 1, b: 4, c: 2 };
  2267. * const iterator = num => {
  2268. * return Aigle.delay(num * 10)
  2269. * .then(() => {
  2270. * order.push(num);
  2271. * return num;
  2272. * });
  2273. * };
  2274. * Aigle.resolve(collection)
  2275. * .sortBy(iterator)
  2276. * .then(array => {
  2277. * console.log(array); // [1, 2, 4]
  2278. * console.log(order); // [1, 2, 4]
  2279. * });
  2280. *
  2281. * @example
  2282. * const order = [];
  2283. * const collection = [{
  2284. * uid: 2, name: 'bargey', uid: 2
  2285. * }, {
  2286. * uid: 1, name: 'fread'
  2287. * }];
  2288. * Aigle.resolve(collection)
  2289. * .sortBy('uid')
  2290. * .then(array => {
  2291. * console.log(array); // [{ uid: 1, name: 'fread' }, { uid: 2, name: 'bargey' ]
  2292. * });
  2293. */
  2294. sortBy(iterator) {
  2295. return addProxy(this, SortBy, iterator);
  2296. }
  2297. /**
  2298. * `Aigle#sortBySeries` is almost the same as [`Aigle#sortBy`](https://suguru03.github.io/aigle/docs/Aigle.html#sortBy), but it will work in series.
  2299. * @param {Function} iterator
  2300. * @return {Aigle} Returns an Aigle instance
  2301. * @example
  2302. * const order = [];
  2303. * const collection = [1, 4, 2];
  2304. * const iterator = num => {
  2305. * return Aigle.delay(num * 10)
  2306. * .then(() => {
  2307. * order.push(num);
  2308. * return num;
  2309. * });
  2310. * };
  2311. * Aigle.resolve(collection)
  2312. * .sortBySeries(iterator)
  2313. * .then(array => {
  2314. * console.log(array); // [1, 2, 4]
  2315. * console.log(order); // [1, 4, 2]
  2316. * });
  2317. *
  2318. * @example
  2319. * const order = [];
  2320. * const collection = { a: 1, b: 4, c: 2 };
  2321. * const iterator = num => {
  2322. * return Aigle.delay(num * 10)
  2323. * .then(() => {
  2324. * order.push(num);
  2325. * return num;
  2326. * });
  2327. * };
  2328. * Aigle.resolve(collection)
  2329. * .sortBySeries(iterator)
  2330. * .then(array => {
  2331. * console.log(array); // [1, 2, 4]
  2332. * console.log(order); // [1, 4, 2]
  2333. * });
  2334. */
  2335. sortBySeries(iterator) {
  2336. return addProxy(this, SortBySeries, iterator);
  2337. }
  2338. /**
  2339. * `Aigle#sortByLimit` is almost the same as [`Aigle#sortBy`](https://suguru03.github.io/aigle/docs/Aigle.html#sortBy)
  2340. * and [`Aigle#sortBySeries`](https://suguru03.github.io/aigle/docs/Aigle.html#sortBySeries)), but it will work with concurrency.
  2341. * @param {number} [limit=8]
  2342. * @param {Function} iterator
  2343. * @return {Aigle} Returns an Aigle instance
  2344. * @example
  2345. * const order = [];
  2346. * const collection = [1, 5, 3, 4, 2];
  2347. * const iterator = (num, index) => {
  2348. * return Aigle.delay(num * 10)
  2349. * .then(() => {
  2350. * order.push(num);
  2351. * return num;
  2352. * });
  2353. * };
  2354. * Aigle.resolve(collection)
  2355. * .sortByLimit(2, iterator)
  2356. * .then(array => {
  2357. * console.log(array); // [1, 2, 3, 4, 5]
  2358. * console.log(order); // [1, 3, 5, 2, 4]
  2359. * });
  2360. *
  2361. * @example
  2362. * const order = [];
  2363. * const collection = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  2364. * const iterator = (num, key) => {
  2365. * return Aigle.delay(num * 10)
  2366. * .then(() => {
  2367. * order.push(num);
  2368. * return num;
  2369. * });
  2370. * };
  2371. * Aigle.resolve(collection)
  2372. * .sortByLimit(2, iterator)
  2373. * .then(array => {
  2374. * console.log(array); // [1, 2, 3, 4, 5]
  2375. * console.log(order); // [1, 3, 5, 2, 4]
  2376. * });
  2377. *
  2378. * @example
  2379. * const order = [];
  2380. * const collection = [1, 5, 3, 4, 2];
  2381. * const iterator = num => {
  2382. * return Aigle.delay(num * 10)
  2383. * .then(() => {
  2384. * order.push(num);
  2385. * return num;
  2386. * });
  2387. * };
  2388. * Aigle.resolve(collection)
  2389. * .sortByLimit(iterator)
  2390. * .then(array => {
  2391. * console.log(array); // [1, 2, 3, 4, 5]
  2392. * console.log(order); // [1, 2, 3, 4, 5]
  2393. * });
  2394. */
  2395. sortByLimit(limit, iterator) {
  2396. return addProxy(this, SortByLimit, limit, iterator);
  2397. }
  2398. /**
  2399. * `Aigle#some` will execute [`Aigle.some`](https://suguru03.github.io/aigle/docs/global.html#some) using a previous promise value and a defined iterator.
  2400. * The value will be assigned as the first argument to [`Aigle.some`](https://suguru03.github.io/aigle/docs/global.html#some) and
  2401. * the iterator will be assigned as the second argument.
  2402. * @param {Function|string} iterator
  2403. * @return {Aigle} Returns an Aigle instance
  2404. * @example
  2405. * const order = [];
  2406. * const collection = [1, 4, 2];
  2407. * const iterator = num => {
  2408. * return Aigle.delay(num * 10)
  2409. * .then(() => {
  2410. * order.push(num);
  2411. * return num % 2 === 0;
  2412. * });
  2413. * };
  2414. * Aigle.resolve(collection)
  2415. * .some(iterator)
  2416. * .then(bool => {
  2417. * console.log(bool); // true
  2418. * console.log(order); // [1, 2]
  2419. * });
  2420. *
  2421. * @example
  2422. * const order = [];
  2423. * const collection = { a: 1, b: 4, c: 2 };
  2424. * const iterator = num => {
  2425. * return Aigle.delay(num * 10)
  2426. * .then(() => {
  2427. * order.push(num);
  2428. * return num % 2 === 0;
  2429. * });
  2430. * };
  2431. * Aigle.resolve(collection)
  2432. * .some(iterator)
  2433. * .then(bool => {
  2434. * console.log(bool); // true
  2435. * console.log(order); // [1, 2]
  2436. * });
  2437. *
  2438. * @example
  2439. * const order = [];
  2440. * const collection = [1, 4, 2];
  2441. * const iterator = num => {
  2442. * return Aigle.delay(num * 10)
  2443. * .then(() => {
  2444. * order.push(num);
  2445. * return false;
  2446. * });
  2447. * };
  2448. * Aigle.resolve(collection)
  2449. * .some(iterator)
  2450. * .then(bool => {
  2451. * console.log(bool); // false
  2452. * console.log(order); // [1, 2, 4]
  2453. * });
  2454. *
  2455. * @example
  2456. * const collection = [{
  2457. * uid: 1, active: false
  2458. * }, {
  2459. * uid: 4, active: true
  2460. * }, {
  2461. * uid: 2, active: true
  2462. * }];
  2463. * Aigle.resolve(collection)
  2464. * .some('active')
  2465. * .then(value => console.log(value)); // true
  2466. *
  2467. * @example
  2468. * const collection = [{
  2469. * uid: 1, active: false
  2470. * }, {
  2471. * uid: 4, active: true
  2472. * }, {
  2473. * uid: 2, active: true
  2474. * }];
  2475. * Aigle.resolve(collection)
  2476. * .some(['uid', 4])
  2477. * .then(value => console.log(value)); // true
  2478. *
  2479. * @example
  2480. * const collection = [{
  2481. * uid: 1, active: false
  2482. * }, {
  2483. * uid: 4, active: true
  2484. * }, {
  2485. * uid: 2, active: true
  2486. * }];
  2487. * Aigle.resolve(collection)
  2488. * .some({ uid: 4 })
  2489. * .then(value => console.log(value)); // true
  2490. */
  2491. some(iterator) {
  2492. return addProxy(this, Some, iterator);
  2493. }
  2494. /**
  2495. * `Aigle#someSeries` is almost the same as [`Aigle#some`](https://suguru03.github.io/aigle/docs/Aigle.html#some), but it will work in series.
  2496. * @param {Function} iterator
  2497. * @return {Aigle} Returns an Aigle instance
  2498. * @example
  2499. * const order = [];
  2500. * const collection = [1, 4, 2];
  2501. * const iterator = num => {
  2502. * return Aigle.delay(num * 10)
  2503. * .then(() => {
  2504. * order.push(num);
  2505. * return num % 2 === 0;
  2506. * });
  2507. * };
  2508. * Aigle.resolve(collection)
  2509. * .someSeries(iterator)
  2510. * .then(bool => {
  2511. * console.log(bool); // true
  2512. * console.log(order); // [1, 4]
  2513. * });
  2514. *
  2515. * @example
  2516. * const order = [];
  2517. * const collection = { a: 1, b: 4, c: 2 };
  2518. * const iterator = num => {
  2519. * return Aigle.delay(num * 10)
  2520. * .then(() => {
  2521. * order.push(num);
  2522. * return num % 2 === 0;
  2523. * });
  2524. * };
  2525. * Aigle.resolve(collection)
  2526. * .someSeries(iterator)
  2527. * .then(bool => {
  2528. * console.log(bool); // true
  2529. * console.log(order); // [1, 4]
  2530. * });
  2531. *
  2532. * @example
  2533. * const order = [];
  2534. * const collection = [1, 4, 2];
  2535. * const iterator = num => {
  2536. * return Aigle.delay(num * 10)
  2537. * .then(() => {
  2538. * order.push(num);
  2539. * return false;
  2540. * });
  2541. * };
  2542. * Aigle.resolve(collection)
  2543. * .someSeries(iterator)
  2544. * .then(bool => {
  2545. * console.log(bool); // false
  2546. * console.log(order); // [1, 4, 2]
  2547. * });
  2548. */
  2549. someSeries(iterator) {
  2550. return addProxy(this, SomeSeries, iterator);
  2551. }
  2552. /**
  2553. * `Aigle#someLimit` is almost the same as [`Aigle#some`](https://suguru03.github.io/aigle/docs/Aigle.html#some)
  2554. * and [`Aigle#someSeries`](https://suguru03.github.io/aigle/docs/Aigle.html#someSeries)), but it will work with concurrency.
  2555. * @param {number} [limit=8]
  2556. * @param {Function} iterator
  2557. * @return {Aigle} Returns an Aigle instance
  2558. * @example
  2559. * const order = [];
  2560. * const collection = [1, 5, 3, 4, 2];
  2561. * const iterator = (num, index) => {
  2562. * return Aigle.delay(num * 10)
  2563. * .then(() => {
  2564. * order.push(num);
  2565. * return num % 2 === 0;
  2566. * });
  2567. * };
  2568. * Aigle.resolve(collection)
  2569. * .someLimit(2, iterator)
  2570. * .then(bool => {
  2571. * console.log(bool); // true
  2572. * console.log(order); // [1, 3, 5, 2]
  2573. * });
  2574. *
  2575. * @example
  2576. * const order = [];
  2577. * const collection = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  2578. * const iterator = (num, key) => {
  2579. * return Aigle.delay(num * 10)
  2580. * .then(() => {
  2581. * order.push(num);
  2582. * return num % 2 === 0;
  2583. * });
  2584. * };
  2585. * Aigle.resolve(collection)
  2586. * .someLimit(2, iterator)
  2587. * .then(bool => {
  2588. * console.log(bool); // true
  2589. * console.log(order); // [1, 3, 5, 2]
  2590. * });
  2591. *
  2592. * @example
  2593. * const order = [];
  2594. * const collection = [1, 5, 3, 4, 2];
  2595. * const iterator = num => {
  2596. * return Aigle.delay(num * 10)
  2597. * .then(() => {
  2598. * order.push(num);
  2599. * return num % 2 === 0;
  2600. * });
  2601. * };
  2602. * Aigle.resolve(collection)
  2603. * .someLimit(2, iterator)
  2604. * .then(bool => {
  2605. * console.log(bool); // true
  2606. * console.log(order); // [1, 2]
  2607. * });
  2608. */
  2609. someLimit(limit, iterator) {
  2610. return addProxy(this, SomeLimit, limit, iterator);
  2611. }
  2612. /**
  2613. * `Aigle#every` will execute [`Aigle.every`](https://suguru03.github.io/aigle/docs/global.html#every) using a previous promise value and a defined iterator.
  2614. * The value will be assigned as the first argument to [`Aigle.every`](https://suguru03.github.io/aigle/docs/global.html#every) and
  2615. * the iterator will be assigned as the second argument.
  2616. * @param {Function|Array|Object|string} iterator
  2617. * @return {Aigle} Returns an Aigle instance
  2618. * @example
  2619. * const order = [];
  2620. * const collection = [1, 4, 2];
  2621. * const iterator = (num, index, collection) => {
  2622. * return Aigle.delay(num * 10)
  2623. * .then(() => {
  2624. * order.push(num);
  2625. * return true;
  2626. * });
  2627. * };
  2628. * Aigle.resolve(collection)
  2629. * .every(iterator)
  2630. * .then(value => {
  2631. * console.log(value); // true
  2632. * console.log(order); // [1, 2, 4];
  2633. * });
  2634. *
  2635. * @example
  2636. * const order = [];
  2637. * const collection = { a: 1, b: 4, c: 2 };
  2638. * const iterator = (num, key, collection) => {
  2639. * return Aigle.delay(num * 10)
  2640. * .then(() => {
  2641. * order.push(num);
  2642. * return true;
  2643. * });
  2644. * };
  2645. * Aigle.resolve(collection)
  2646. * .every(iterator)
  2647. * .then(value => {
  2648. * console.log(value); // true
  2649. * console.log(order); // [1, 2, 4];
  2650. * });
  2651. *
  2652. * @example
  2653. * const order = [];
  2654. * const collection = [1, 4, 2];
  2655. * const iterator = num => {
  2656. * return Aigle.delay(num * 10)
  2657. * .then(() => {
  2658. * order.push(num);
  2659. * return n % 2;
  2660. * });
  2661. * };
  2662. * Aigle.resolve(collection)
  2663. * .every(iterator)
  2664. * .then(value => {
  2665. * console.log(value); // false
  2666. * console.log(order); // [1, 2];
  2667. * });
  2668. *
  2669. * @example
  2670. * const collection = [{
  2671. * uid: 1, active: false
  2672. * }, {
  2673. * uid: 4, active: true
  2674. * }, {
  2675. * uid: 2, active: true
  2676. * }];
  2677. * Aigle.resolve(collection)
  2678. * .every('active')
  2679. * .then(value => console.log(value)); // false
  2680. *
  2681. * @example
  2682. * const collection = [{
  2683. * uid: 1, active: false
  2684. * }, {
  2685. * uid: 4, active: true
  2686. * }, {
  2687. * uid: 2, active: true
  2688. * }];
  2689. * Aigle.resolve(collection)
  2690. * .every('active')
  2691. * .then(value => console.log(value)); // false
  2692. *
  2693. * @example
  2694. * const collection = [{
  2695. * uid: 1, active: false
  2696. * }, {
  2697. * uid: 4, active: true
  2698. * }, {
  2699. * uid: 2, active: true
  2700. * }];
  2701. * Aigle.resolve(collection)
  2702. * .every(['active', true])
  2703. * .then(value => console.log(value)); // false
  2704. *
  2705. * @example
  2706. * const collection = [{
  2707. * uid: 1, active: true
  2708. * }, {
  2709. * uid: 4, active: true
  2710. * }, {
  2711. * uid: 2, active: true
  2712. * }];
  2713. * Aigle.resolve(collection)
  2714. * .every({ active: true })
  2715. * .then(value => console.log(value)); // true
  2716. */
  2717. every(iterator) {
  2718. return addProxy(this, Every, iterator);
  2719. }
  2720. /**
  2721. * `Aigle#everySeries` is almost the same as [`Aigle#every`](https://suguru03.github.io/aigle/docs/Aigle.html#every), but it will work in series.
  2722. * @param {Function} iterator
  2723. * @return {Aigle} Returns an Aigle instance
  2724. * @example
  2725. * const order = [];
  2726. * const collection = [1, 4, 2];
  2727. * const iterator = (num, index, collection) => {
  2728. * return Aigle.delay(num * 10)
  2729. * .then(() => {
  2730. * order.push(num);
  2731. * return true;
  2732. * });
  2733. * };
  2734. * Aigle.resolve(collection)
  2735. * .everySeries(iterator)
  2736. * .then(value => {
  2737. * console.log(value); // true
  2738. * console.log(order); // [1, 4, 2];
  2739. * });
  2740. *
  2741. * @example
  2742. * const order = [];
  2743. * const collection = { a: 1, b: 4, c: 2 };
  2744. * const iterator = (num, key, collection) => {
  2745. * return Aigle.delay(num * 10)
  2746. * .then(() => {
  2747. * order.push(num);
  2748. * return true;
  2749. * });
  2750. * };
  2751. * Aigle.resolve(collection)
  2752. * .everySeries(iterator)
  2753. * .then(value => {
  2754. * console.log(value); // true
  2755. * console.log(order); // [1, 4, 2];
  2756. * });
  2757. *
  2758. * @example
  2759. * const order = [];
  2760. * const collection = [1, 4, 2];
  2761. * const iterator = num => {
  2762. * return Aigle.delay(num * 10)
  2763. * .then(() => {
  2764. * order.push(num);
  2765. * return n % 2;
  2766. * });
  2767. * };
  2768. * Aigle.resolve(collection)
  2769. * .everySeries(iterator)
  2770. * .then(value => {
  2771. * console.log(value); // false
  2772. * console.log(order); // [1, 4];
  2773. * });
  2774. */
  2775. everySeries(iterator) {
  2776. return addProxy(this, EverySeries, iterator);
  2777. }
  2778. /**
  2779. * `Aigle#everyLimit` is almost the same as [`Aigle#every`](https://suguru03.github.io/aigle/docs/Aigle.html#every) and
  2780. * [`Aigle#everySeries`](https://suguru03.github.io/aigle/docs/Aigle.html#everySeries), but it will work with concurrency.
  2781. * @param {number} [limit=8]
  2782. * @param {Function} iterator
  2783. * @return {Aigle} Returns an Aigle instance
  2784. * @example
  2785. * const order = [];
  2786. * const collection = [1, 5, 3, 4, 2];
  2787. * const iterator = (num, index, collection) => {
  2788. * return Aigle.delay(num * 10)
  2789. * .then(() => {
  2790. * order.push(num);
  2791. * return true;
  2792. * });
  2793. * };
  2794. * Aigle.resolve(collection)
  2795. * .everyLimit(2, iterator)
  2796. * .then(value => {
  2797. * console.log(value); // true
  2798. * console.log(order); // [1, 3, 5, 2, 4];
  2799. * });
  2800. *
  2801. * @example
  2802. * const order = [];
  2803. * const collection = {
  2804. * task1: 1,
  2805. * task2: 5,
  2806. * task3: 3,
  2807. * task4: 4,
  2808. * task5: 2
  2809. * };
  2810. * const iterator = (num, key, collection) => {
  2811. * return Aigle.delay(num * 10)
  2812. * .then(() => {
  2813. * order.push(num);
  2814. * return true;
  2815. * });
  2816. * };
  2817. * Aigle.resolve(collection)
  2818. * .everyLimit(2, iterator)
  2819. * .then(value => {
  2820. * console.log(value); // true
  2821. * console.log(order); // [1, 3, 5, 2, 4];
  2822. * });
  2823. *
  2824. * @example
  2825. * const order = [];
  2826. * const collection = [1, 5, 3, 4, 2];
  2827. * const iterator = num => {
  2828. * return Aigle.delay(num * 10)
  2829. * .then(() => {
  2830. * order.push(num);
  2831. * return num === 4;
  2832. * });
  2833. * };
  2834. * Aigle.resolve(collection)
  2835. * .everyLimit(iterator)
  2836. * .then(value => {
  2837. * console.log(value); // false
  2838. * console.log(order); // [1, 2, 3, 4];
  2839. * });
  2840. */
  2841. everyLimit(limit, iterator) {
  2842. return addProxy(this, EveryLimit, limit, iterator);
  2843. }
  2844. /**
  2845. * `Aigle#concat` will execute [`Aigle.concat`](https://suguru03.github.io/aigle/docs/global.html#concat) using a previous promise value and a defined iterator.
  2846. * The value will be assigned as the first argument to [`Aigle.concat`](https://suguru03.github.io/aigle/docs/global.html#concat) and
  2847. * the iterator will be assigned as the second argument.
  2848. * @param {Function} iterator
  2849. * @example
  2850. * const order = [];
  2851. * const collection = [1, 4, 2];
  2852. * const iterator = (num, index, collectioin) => {
  2853. * return Aigle.delay(num * 10)
  2854. * .then(() => {
  2855. * order.push(num);
  2856. * return num;
  2857. * });
  2858. * };
  2859. * Aigle.resolve(collection)
  2860. * .concat(iterator)
  2861. * .then(array => {
  2862. * console.log(array); // [1, 2, 4];
  2863. * console.log(order); // [1, 2, 4];
  2864. * });
  2865. *
  2866. * @example
  2867. * const order = [];
  2868. * const collection = { a: 1, b: 4, c: 2 };
  2869. * const iterator = (num, key, collection) => {
  2870. * return Aigle.delay(num * 10)
  2871. * .then(() => {
  2872. * order.push(num);
  2873. * return num;
  2874. * });
  2875. * };
  2876. * Aigle.resolve(collection)
  2877. * .concat(iterator)
  2878. * .then(array => {
  2879. * console.log(array); // [1, 2, 4];
  2880. * console.log(order); // [1, 2, 4];
  2881. * });
  2882. */
  2883. concat(iterator) {
  2884. return addProxy(this, Concat, iterator);
  2885. }
  2886. /**
  2887. * `Aigle#concatSeries` is almost the same as [`Aigle#concat`](https://suguru03.github.io/aigle/docs/Aigle.html#concat), but it will work in series.
  2888. * @param {Function} iterator
  2889. * @example
  2890. * const order = [];
  2891. * const collection = [1, 4, 2];
  2892. * const iterator = (num, index, collection) => {
  2893. * return Aigle.delay(num * 10)
  2894. * .then(() => {
  2895. * order.push(num);
  2896. * return num;
  2897. * });
  2898. * };
  2899. * Aigle.resolve(collection)
  2900. * .concatSeries(iterator)
  2901. * .then(array => {
  2902. * console.log(array); // [1, 4, 2];
  2903. * console.log(order); // [1, 4, 2];
  2904. * });
  2905. *
  2906. * @example
  2907. * const order = [];
  2908. * const collection = { a: 1, b: 4, c: 2 };
  2909. * const iterator = (num, key, collection) => {
  2910. * return Aigle.delay(num * 10)
  2911. * .then(() => {
  2912. * order.push(num);
  2913. * return num;
  2914. * });
  2915. * };
  2916. * Aigle.resolve(collection)
  2917. * .concatSeries(iterator)
  2918. * .then(array => {
  2919. * console.log(array); // [1, 4, 2];
  2920. * console.log(order); // [1, 4, 2];
  2921. * });
  2922. */
  2923. concatSeries(iterator) {
  2924. return addProxy(this, ConcatSeries, iterator);
  2925. }
  2926. /**
  2927. * `Aigle#concatLimit` is almost the same as [`Aigle#concat`](https://suguru03.github.io/aigle/docs/Aigle.html#concat)
  2928. * and [`Aigle#concatSeries`](https://suguru03.github.io/aigle/docs/Aigle.html#concatSeries)), but it will work with concurrency.
  2929. * @param {integer} [limit=8]
  2930. * @param {Function} iterator
  2931. * @example
  2932. * const order = [];
  2933. * const collection = [1, 5, 3, 4, 2];
  2934. * const iterator = (num, index, collection) => {
  2935. * return Aigle.delay(num * 10)
  2936. * .then(() => {
  2937. * order.push(num);
  2938. * return num;
  2939. * });
  2940. * };
  2941. * Aigle.resolve(collection)
  2942. * .concatLimit(2, iterator)
  2943. * .then(array => {
  2944. * console.log(array); // [1, 3, 5, 2, 4];
  2945. * console.log(order); // [1, 3, 5, 2, 4];
  2946. * });
  2947. *
  2948. * @example
  2949. * const order = [];
  2950. * const collection = {
  2951. * task1: 1,
  2952. * task2: 5,
  2953. * task3: 3,
  2954. * task4: 4,
  2955. * task5: 2
  2956. * };
  2957. * const iterator = (num, key, collection) => {
  2958. * return Aigle.delay(num * 10)
  2959. * .then(() => {
  2960. * order.push(num);
  2961. * return num;
  2962. * });
  2963. * };
  2964. * Aigle.resolve(collection)
  2965. * .concatLimit(2, iterator)
  2966. * .then(array => {
  2967. * console.log(array); // [1, 3, 5, 2, 4];
  2968. * console.log(order); // [1, 3, 5, 2, 4];
  2969. * });
  2970. *
  2971. * @example
  2972. * const order = [];
  2973. * const collection = [1, 5, 3, 4, 2];
  2974. * const iterator = num => {
  2975. * return Aigle.delay(num * 10)
  2976. * .then(() => {
  2977. * order.push(num);
  2978. * return num;
  2979. * });
  2980. * };
  2981. * Aigle.resolve(collection)
  2982. * .concatLimit(iterator)
  2983. * .then(array => {
  2984. * console.log(array); // [1, 3, 5, 2, 4];
  2985. * console.log(order); // [1, 3, 5, 2, 4];
  2986. * });
  2987. */
  2988. concatLimit(limit, iterator) {
  2989. return addProxy(this, ConcatLimit, limit, iterator);
  2990. }
  2991. /**
  2992. * @param {Function|string} iterator
  2993. * @return {Aigle} Returns an Aigle instance
  2994. * @example
  2995. * const order = [];
  2996. * const collection = [1, 4, 2];
  2997. * const iterator = (num, index) => {
  2998. * return Aigle.delay(num * 10)
  2999. * .then(() => {
  3000. * order.push(num);
  3001. * return num % 2;
  3002. * });
  3003. * };
  3004. * Aigle.resolve(collection)
  3005. * .groupBy(iterator)
  3006. * .then(object => {
  3007. * console.log(object); // { '0': [2, 4], '1': [1] };
  3008. * console.log(order); // [1, 2, 4];
  3009. * });
  3010. *
  3011. * @example
  3012. * const order = [];
  3013. * const collection = { a: 1, b: 4, c: 2 };
  3014. * const iterator = (num, key) => {
  3015. * return Aigle.delay(num * 10)
  3016. * .then(() => {
  3017. * order.push(num);
  3018. * return num % 2;
  3019. * });
  3020. * };
  3021. * Aigle.resolve(collection)
  3022. * .groupBy(iterator)
  3023. * .then(object => {
  3024. * console.log(object); // { '0': [2, 4], '1': [1] };
  3025. * console.log(order); // [1, 2, 4];
  3026. * });
  3027. *
  3028. * @example
  3029. * const order = [];
  3030. * const collection = ['one', 'two', 'three'];
  3031. * Aigle.resolve(collection)
  3032. * .groupBy('length')
  3033. * .then(object => {
  3034. * console.log(object); // { '3': ['one', 'two'], '5': ['three'] };
  3035. * });
  3036. */
  3037. groupBy(iterator) {
  3038. return addProxy(this, GroupBy, iterator);
  3039. }
  3040. /**
  3041. * @param {Function} iterator
  3042. * @return {Aigle} Returns an Aigle instance
  3043. * @example
  3044. * const order = [];
  3045. * const collection = [1, 4, 2];
  3046. * const iterator = (num, index) => {
  3047. * return Aigle.delay(num * 10)
  3048. * .then(() => {
  3049. * order.push(num);
  3050. * return num % 2;
  3051. * });
  3052. * };
  3053. * Aigle.resolve(collection)
  3054. * .groupBySeries(iterator)
  3055. * .then(object => {
  3056. * console.log(object); // { '0': [4, 2], '1': [1] };
  3057. * console.log(order); // [1, 4, 2];
  3058. * });
  3059. *
  3060. * @example
  3061. * const order = [];
  3062. * const collection = { a: 1, b: 4, c: 2 };
  3063. * const iterator = (num, key) => {
  3064. * return Aigle.delay(num * 10)
  3065. * .then(() => {
  3066. * order.push(num);
  3067. * return num % 2;
  3068. * });
  3069. * };
  3070. * Aigle.resolve(collection)
  3071. * .groupBySeries(iterator)
  3072. * .then(object => {
  3073. * console.log(object); // { '0': [4, 2], '1': [1] };
  3074. * console.log(order); // [1, 4, 2];
  3075. * });
  3076. */
  3077. groupBySeries(iterator) {
  3078. return addProxy(this, GroupBySeries, iterator);
  3079. }
  3080. /**
  3081. * @param {number} [limit=8]
  3082. * @param {Function} iterator
  3083. * @return {Aigle} Returns an Aigle instance
  3084. * @example
  3085. * const order = [];
  3086. * const collection = [1, 5, 3, 4, 2];
  3087. * const iterator = (num, index) => {
  3088. * return Aigle.delay(num * 10)
  3089. * .then(() => {
  3090. * order.push(num);
  3091. * return num % 2;
  3092. * });
  3093. * };
  3094. * Aigle.resolve(collection)
  3095. * .groupByLimit(2, iterator)
  3096. * .then(object => {
  3097. * console.log(object); // { '0': [2, 4], '1': [1, 3, 5] };
  3098. * console.log(order); // [1, 3, 5, 2, 4];
  3099. * });
  3100. *
  3101. * @example
  3102. * const order = [];
  3103. * const collection = {
  3104. * task1: 1,
  3105. * task2: 5,
  3106. * task3: 3,
  3107. * task4: 4,
  3108. * task5: 2
  3109. * };
  3110. * const iterator = (num, key) => {
  3111. * return Aigle.delay(num * 10)
  3112. * .then(() => {
  3113. * order.push(num);
  3114. * return num % 2;
  3115. * });
  3116. * };
  3117. * Aigle.resolve(collection)
  3118. * .groupByLimit(2, iterator)
  3119. * .then(object => {
  3120. * console.log(object); // { '0': [2, 4], '1': [1, 3, 5] };
  3121. * console.log(order); // [1, 3, 5, 2, 4];
  3122. * });
  3123. *
  3124. * @example
  3125. * const order = [];
  3126. * const collection = [1, 5, 3, 4, 2];
  3127. * const iterator = num => {
  3128. * return Aigle.delay(num * 10)
  3129. * .then(() => {
  3130. * order.push(num);
  3131. * return num % 2;
  3132. * });
  3133. * };
  3134. * Aigle.resolve(collection)
  3135. * .groupByLimit(iterator)
  3136. * .then(object => {
  3137. * console.log(object); // { '0': [2, 4], '1': [1, 3, 5] };
  3138. * console.log(order); // [1, 2, 3, 4, 5];
  3139. * });
  3140. */
  3141. groupByLimit(limit, iterator) {
  3142. return addProxy(this, GroupByLimit, limit, iterator);
  3143. }
  3144. /**
  3145. * After a previous promise is resolved, the timer will be started with `ms`.
  3146. * After `ms`, the delay's promise will be resolved with the previous promise value.
  3147. * @param {number} ms
  3148. * @example
  3149. * Aigle.resolve()
  3150. * .delay(10)
  3151. * .then(value => console.log(value); // undefined
  3152. *
  3153. * @example
  3154. * Aigle.resolve('test')
  3155. * .delay(10)
  3156. * .then(value => console.log(value); // 'test'
  3157. */
  3158. delay(ms) {
  3159. return addAigle(this, new Delay(ms));
  3160. }
  3161. /**
  3162. * @param {number} ms
  3163. * @param {*} [message]
  3164. * @example
  3165. * const { TimeoutError } = Aigle;
  3166. * Aigle.delay(100)
  3167. * .timeout(10)
  3168. * .catch(TimeoutError, error => {
  3169. * console.log(error); // operation timed out
  3170. * });
  3171. */
  3172. timeout(ms, message) {
  3173. return addReceiver(this, new Timeout(ms, message));
  3174. }
  3175. /**
  3176. * @param {Function} tester
  3177. * @param {Function} iterator
  3178. */
  3179. whilst(tester, iterator) {
  3180. return this.then((value) => whilst(value, tester, iterator));
  3181. }
  3182. /**
  3183. * @param {Function} iterator
  3184. * @param {Function} tester
  3185. * @example
  3186. * const order = [];
  3187. * const tester = num => {
  3188. * order.push(`t:${num}`);
  3189. * return Aigle.delay(10)
  3190. * .then(() => num !== 4);
  3191. * };
  3192. * const iterator = count => {
  3193. * const num = ++count;
  3194. * order.push(`i:${num}`);
  3195. * return Aigle.delay(10)
  3196. * .then(() => num);
  3197. * };
  3198. * Aigle.resolve(0)
  3199. * .doWhilst(iterator, tester)
  3200. * .then(value => {
  3201. * console.log(value); // 4
  3202. * console.log(order); // [ 'i:1', 't:1', 'i:2', 't:2', 'i:3', 't:3', 'i:4', 't:4' ]
  3203. * });
  3204. */
  3205. doWhilst(iterator, tester) {
  3206. return this.then((value) => doWhilst(value, iterator, tester));
  3207. }
  3208. /**
  3209. * @param {Function} tester
  3210. * @param {Function} iterator
  3211. */
  3212. until(tester, iterator) {
  3213. return this.then((value) => until(value, tester, iterator));
  3214. }
  3215. /**
  3216. * @param {Function} iterator
  3217. * @param {Function} tester
  3218. * @example
  3219. * const order = [];
  3220. * const tester = num => {
  3221. * order.push(`t:${num}`);
  3222. * return Aigle.delay(10)
  3223. * .then(() => num === 4);
  3224. * };
  3225. * const iterator = count => {
  3226. * const num = ++count;
  3227. * order.push(`i:${num}`);
  3228. * return Aigle.delay(10)
  3229. * .then(() => num);
  3230. * };
  3231. * Aigle.resolve(0)
  3232. * .doUntil(iterator, tester)
  3233. * .then(value => {
  3234. * console.log(value); // 4
  3235. * console.log(order); // [ 'i:1', 't:1', 'i:2', 't:2', 'i:3', 't:3', 'i:4', 't:4' ]
  3236. * });
  3237. */
  3238. doUntil(iterator, tester) {
  3239. return this.then((value) => doUntil(value, iterator, tester));
  3240. }
  3241. /**
  3242. * @param {Function} onFulfilled
  3243. * @return {Aigle} Returns an Aigle instance
  3244. * @example
  3245. * Aigle.resolve(3)
  3246. * .thru(value => ++value)
  3247. * .then(value => {
  3248. * console.log(value); // 4;
  3249. * });
  3250. */
  3251. thru(onFulfilled) {
  3252. return this.then((value) => thru(value, onFulfilled));
  3253. }
  3254. /**
  3255. * @param {Function} onFulfilled
  3256. * @return {Aigle} Returns an Aigle instance
  3257. * @example
  3258. * Aigle.resolve([1, 4, 2])
  3259. * .tap(array => array.pop())
  3260. * .then(array => {
  3261. * console.log(array); // [1, 4]
  3262. * });
  3263. */
  3264. tap(onFulfilled) {
  3265. return this.then((value) => tap(value, onFulfilled));
  3266. }
  3267. /**
  3268. * @param {Function} iterator
  3269. * @return {Aigle} Returns an Aigle instance
  3270. * @example
  3271. * const order = [];
  3272. * const timer = [30, 20, 10];
  3273. * const iterator = n => {
  3274. * return Aigle.delay(timer[n])
  3275. * .then(() => {
  3276. * order.push(n);
  3277. * return n;
  3278. * });
  3279. * };
  3280. * Aigle.resolve(3)
  3281. * .times(iterator)
  3282. * .then(array => {
  3283. * console.log(array); // [0, 1, 2]
  3284. * console.log(order); // [2, 1, 0]
  3285. * });
  3286. */
  3287. times(iterator) {
  3288. return addProxy(this, Times, iterator);
  3289. }
  3290. /**
  3291. * @param {Function} iterator
  3292. * @return {Aigle} Returns an Aigle instance
  3293. * @example
  3294. * const order = [];
  3295. * const timer = [30, 20, 10];
  3296. * const iterator = n => {
  3297. * return Aigle.delay(timer[n])
  3298. * .then(() => {
  3299. * order.push(n);
  3300. * return n;
  3301. * });
  3302. * };
  3303. * Aigle.resolve(3)
  3304. * .timesSeries(iterator)
  3305. * .then(array => {
  3306. * console.log(array); // [0, 1, 2]
  3307. * console.log(order); // [0, 1, 2]
  3308. * });
  3309. */
  3310. timesSeries(iterator) {
  3311. return addProxy(this, TimesSeries, iterator);
  3312. }
  3313. /**
  3314. * @param {number} [limit=8]
  3315. * @param {Function} iterator
  3316. * @return {Aigle} Returns an Aigle instance
  3317. * @example
  3318. * const order = [];
  3319. * const timer = [30, 20, 10];
  3320. * const iterator = n => {
  3321. * return Aigle.delay(timer[n])
  3322. * .then(() => {
  3323. * order.push(n);
  3324. * return n;
  3325. * });
  3326. * };
  3327. * Aigle.resolve(3)
  3328. * .timesLimit(2, iterator)
  3329. * .then(array => {
  3330. * console.log(array); // [0, 1, 2]
  3331. * console.log(order); // [1, 0, 2]
  3332. * });
  3333. *
  3334. * @example
  3335. * const order = [];
  3336. * const timer = [30, 20, 10];
  3337. * const iterator = n => {
  3338. * return Aigle.delay(timer[n])
  3339. * .then(() => {
  3340. * order.push(n);
  3341. * return n;
  3342. * });
  3343. * };
  3344. * Aigle.resolve(3)
  3345. * .timesLimit(iterator)
  3346. * .then(array => {
  3347. * console.log(array); // [0, 1, 2]
  3348. * console.log(order); // [2, 1, 0]
  3349. * });
  3350. */
  3351. timesLimit(limit, iterator) {
  3352. return addProxy(this, TimesLimit, limit, iterator);
  3353. }
  3354. /**
  3355. * @param {Function} handler
  3356. */
  3357. disposer(handler) {
  3358. return new Disposer(this, handler);
  3359. }
  3360. /* internal functions */
  3361. _resolve(value) {
  3362. if (this._resolved !== 0) {
  3363. return;
  3364. }
  3365. this._resolved = 1;
  3366. this._value = value;
  3367. if (this._receiver === undefined) {
  3368. return;
  3369. }
  3370. this._callResolve();
  3371. }
  3372. _callResolve() {
  3373. const { _receiver } = this;
  3374. this._receiver = undefined;
  3375. if (_receiver instanceof AigleProxy) {
  3376. _receiver._callResolve(this._value, this._key);
  3377. } else if (this._key === INTERNAL) {
  3378. _receiver._resolve(this._value);
  3379. } else {
  3380. callResolve(_receiver, this._onFulfilled, this._value);
  3381. }
  3382. if (!this._receivers) {
  3383. return;
  3384. }
  3385. const { _value, _key, _receivers } = this;
  3386. this._receivers = undefined;
  3387. let i = -1;
  3388. const { array } = _receivers;
  3389. while (++i < _receivers.length) {
  3390. const { receiver, onFulfilled } = array[i];
  3391. if (receiver instanceof AigleProxy) {
  3392. receiver._callResolve(_value, _key);
  3393. } else {
  3394. callResolve(receiver, onFulfilled, _value);
  3395. }
  3396. }
  3397. }
  3398. _reject(reason) {
  3399. if (this._resolved !== 0) {
  3400. return;
  3401. }
  3402. this._resolved = 2;
  3403. this._value = reason;
  3404. if (this._receiver === undefined) {
  3405. this._receiver = UNHANDLED;
  3406. invokeAsync(this);
  3407. return;
  3408. }
  3409. stackTraces && reconstructStack(this);
  3410. this._callReject();
  3411. }
  3412. _callReject() {
  3413. const { _receiver } = this;
  3414. this._receiver = undefined;
  3415. if (_receiver === undefined || _receiver === UNHANDLED) {
  3416. printWarning(this._value);
  3417. process.emit('unhandledRejection', this._value);
  3418. return;
  3419. }
  3420. if (_receiver === INTERNAL) {
  3421. return;
  3422. }
  3423. if (_receiver instanceof AigleProxy) {
  3424. _receiver._callReject(this._value);
  3425. } else if (this._key === INTERNAL) {
  3426. _receiver._reject(this._value);
  3427. } else {
  3428. callReject(_receiver, this._onRejected, this._value);
  3429. }
  3430. if (!this._receivers) {
  3431. return;
  3432. }
  3433. const { _value, _receivers } = this;
  3434. this._receivers = undefined;
  3435. let i = -1;
  3436. const { array } = _receivers;
  3437. while (++i < _receivers.length) {
  3438. const { receiver, onRejected } = array[i];
  3439. if (receiver instanceof AigleProxy) {
  3440. receiver._callReject(_value);
  3441. } else {
  3442. callReject(receiver, onRejected, _value);
  3443. }
  3444. }
  3445. }
  3446. _addReceiver(receiver, key) {
  3447. this._key = key;
  3448. this._receiver = receiver;
  3449. }
  3450. }
  3451. Aigle.prototype._execute = execute;
  3452. module.exports = Aigle;
  3453. module.exports.default = Aigle;
  3454. /* functions, classes */
  3455. const { all, All } = require('./all');
  3456. const { allSettled, AllSettled } = require('./allSettled');
  3457. const attempt = require('./attempt');
  3458. const { race, Race } = require('./race');
  3459. const { props, Props } = require('./props');
  3460. const { parallel, Parallel } = require('./parallel');
  3461. const { series, Series } = require('./series');
  3462. const { parallelLimit, ParallelLimit } = require('./parallelLimit');
  3463. const { each, Each } = require('./each');
  3464. const { eachSeries, EachSeries } = require('./eachSeries');
  3465. const { eachLimit, EachLimit } = require('./eachLimit');
  3466. const { map, Map } = require('./map');
  3467. const { mapSeries, MapSeries } = require('./mapSeries');
  3468. const { mapLimit, MapLimit } = require('./mapLimit');
  3469. const { mapValues, MapValues } = require('./mapValues');
  3470. const { mapValuesSeries, MapValuesSeries } = require('./mapValuesSeries');
  3471. const { mapValuesLimit, MapValuesLimit } = require('./mapValuesLimit');
  3472. const { filter, Filter } = require('./filter');
  3473. const { filterSeries, FilterSeries } = require('./filterSeries');
  3474. const { filterLimit, FilterLimit } = require('./filterLimit');
  3475. const { reject, Reject } = require('./reject');
  3476. const { rejectSeries, RejectSeries } = require('./rejectSeries');
  3477. const { rejectLimit, RejectLimit } = require('./rejectLimit');
  3478. const { find, Find } = require('./find');
  3479. const { findSeries, FindSeries } = require('./findSeries');
  3480. const { findLimit, FindLimit } = require('./findLimit');
  3481. const { findIndex, FindIndex } = require('./findIndex');
  3482. const { findIndexSeries, FindIndexSeries } = require('./findIndexSeries');
  3483. const { findIndexLimit, FindIndexLimit } = require('./findIndexLimit');
  3484. const { findKey, FindKey } = require('./findKey');
  3485. const { findKeySeries, FindKeySeries } = require('./findKeySeries');
  3486. const { findKeyLimit, FindKeyLimit } = require('./findKeyLimit');
  3487. const { pick, Pick } = require('./pick');
  3488. const { pickBy, PickBy } = require('./pickBy');
  3489. const { pickBySeries, PickBySeries } = require('./pickBySeries');
  3490. const { pickByLimit, PickByLimit } = require('./pickByLimit');
  3491. const { omit, Omit } = require('./omit');
  3492. const { omitBy, OmitBy } = require('./omitBy');
  3493. const { omitBySeries, OmitBySeries } = require('./omitBySeries');
  3494. const { omitByLimit, OmitByLimit } = require('./omitByLimit');
  3495. const { reduce, Reduce } = require('./reduce');
  3496. const { transform, Transform } = require('./transform');
  3497. const { transformSeries, TransformSeries } = require('./transformSeries');
  3498. const { transformLimit, TransformLimit } = require('./transformLimit');
  3499. const { sortBy, SortBy } = require('./sortBy');
  3500. const { sortBySeries, SortBySeries } = require('./sortBySeries');
  3501. const { sortByLimit, SortByLimit } = require('./sortByLimit');
  3502. const { some, Some } = require('./some');
  3503. const { someSeries, SomeSeries } = require('./someSeries');
  3504. const { someLimit, SomeLimit } = require('./someLimit');
  3505. const { every, Every } = require('./every');
  3506. const { everySeries, EverySeries } = require('./everySeries');
  3507. const { everyLimit, EveryLimit } = require('./everyLimit');
  3508. const { concat, Concat } = require('./concat');
  3509. const { concatSeries, ConcatSeries } = require('./concatSeries');
  3510. const { concatLimit, ConcatLimit } = require('./concatLimit');
  3511. const { groupBy, GroupBy } = require('./groupBy');
  3512. const { groupBySeries, GroupBySeries } = require('./groupBySeries');
  3513. const { groupByLimit, GroupByLimit } = require('./groupByLimit');
  3514. const { join, Spread } = require('./join');
  3515. const promisify = require('./promisify');
  3516. const promisifyAll = require('./promisifyAll');
  3517. const { delay, Delay } = require('./delay');
  3518. const Timeout = require('./timeout');
  3519. const { whilst } = require('./whilst');
  3520. const { doWhilst } = require('./doWhilst');
  3521. const { until } = require('./until');
  3522. const doUntil = require('./doUntil');
  3523. const retry = require('./retry');
  3524. const thru = require('./thru');
  3525. const tap = require('./tap');
  3526. const flow = require('./flow');
  3527. const { times, Times } = require('./times');
  3528. const { timesSeries, TimesSeries } = require('./timesSeries');
  3529. const { timesLimit, TimesLimit } = require('./timesLimit');
  3530. const { using, Disposer } = require('./using');
  3531. const { resolveStack, reconstructStack } = require('./debug');
  3532. const { createProxy } = require('./internal/mixin');
  3533. Aigle.VERSION = VERSION;
  3534. Aigle.Aigle = Aigle;
  3535. /* core functions */
  3536. Aigle.resolve = _resolve;
  3537. Aigle.reject = _reject;
  3538. /* collections */
  3539. Aigle.all = all;
  3540. Aigle.allSettled = allSettled;
  3541. Aigle.race = race;
  3542. Aigle.props = props;
  3543. Aigle.series = series;
  3544. Aigle.parallel = parallel;
  3545. Aigle.parallelLimit = parallelLimit;
  3546. Aigle.each = each;
  3547. Aigle.eachSeries = eachSeries;
  3548. Aigle.eachLimit = eachLimit;
  3549. Aigle.forEach = each;
  3550. Aigle.forEachSeries = eachSeries;
  3551. Aigle.forEachLimit = eachLimit;
  3552. Aigle.map = map;
  3553. Aigle.mapSeries = mapSeries;
  3554. Aigle.mapLimit = mapLimit;
  3555. Aigle.mapValues = mapValues;
  3556. Aigle.mapValuesSeries = mapValuesSeries;
  3557. Aigle.mapValuesLimit = mapValuesLimit;
  3558. Aigle.filter = filter;
  3559. Aigle.filterSeries = filterSeries;
  3560. Aigle.filterLimit = filterLimit;
  3561. Aigle.rejectSeries = rejectSeries;
  3562. Aigle.rejectLimit = rejectLimit;
  3563. Aigle.find = find;
  3564. Aigle.findSeries = findSeries;
  3565. Aigle.findLimit = findLimit;
  3566. Aigle.findIndex = findIndex;
  3567. Aigle.findIndexSeries = findIndexSeries;
  3568. Aigle.findIndexLimit = findIndexLimit;
  3569. Aigle.findKey = findKey;
  3570. Aigle.findKeySeries = findKeySeries;
  3571. Aigle.findKeyLimit = findKeyLimit;
  3572. Aigle.detect = find;
  3573. Aigle.detectSeries = findSeries;
  3574. Aigle.detectLimit = findLimit;
  3575. Aigle.pick = pick;
  3576. Aigle.pickSeries = pickBySeries;
  3577. Aigle.pickLimit = pickByLimit;
  3578. Aigle.pickBy = pickBy;
  3579. Aigle.pickBySeries = pickBySeries;
  3580. Aigle.pickByLimit = pickByLimit;
  3581. Aigle.omit = omit;
  3582. Aigle.omitSeries = omitBySeries;
  3583. Aigle.omitLimit = omitByLimit;
  3584. Aigle.omitBy = omitBy;
  3585. Aigle.omitBySeries = omitBySeries;
  3586. Aigle.omitByLimit = omitByLimit;
  3587. Aigle.reduce = reduce;
  3588. Aigle.transform = transform;
  3589. Aigle.transformSeries = transformSeries;
  3590. Aigle.transformLimit = transformLimit;
  3591. Aigle.sortBy = sortBy;
  3592. Aigle.sortBySeries = sortBySeries;
  3593. Aigle.sortByLimit = sortByLimit;
  3594. Aigle.some = some;
  3595. Aigle.someSeries = someSeries;
  3596. Aigle.someLimit = someLimit;
  3597. Aigle.every = every;
  3598. Aigle.everySeries = everySeries;
  3599. Aigle.everyLimit = everyLimit;
  3600. Aigle.concat = concat;
  3601. Aigle.concatSeries = concatSeries;
  3602. Aigle.concatLimit = concatLimit;
  3603. Aigle.groupBy = groupBy;
  3604. Aigle.groupBySeries = groupBySeries;
  3605. Aigle.groupByLimit = groupByLimit;
  3606. Aigle.attempt = attempt;
  3607. Aigle.try = attempt;
  3608. Aigle.join = join;
  3609. Aigle.promisify = promisify;
  3610. Aigle.promisifyAll = promisifyAll;
  3611. Aigle.delay = delay;
  3612. Aigle.whilst = whilst;
  3613. Aigle.doWhilst = doWhilst;
  3614. Aigle.until = until;
  3615. Aigle.doUntil = doUntil;
  3616. Aigle.retry = retry;
  3617. Aigle.thru = thru;
  3618. Aigle.tap = tap;
  3619. Aigle.flow = flow;
  3620. Aigle.times = times;
  3621. Aigle.timesSeries = timesSeries;
  3622. Aigle.timesLimit = timesLimit;
  3623. Aigle.using = using;
  3624. Aigle.mixin = mixin;
  3625. /* debug */
  3626. Aigle.config = config;
  3627. Aigle.longStackTraces = longStackTraces;
  3628. /* errors */
  3629. const { CancellationError, TimeoutError } = require('./error');
  3630. Aigle.CancellationError = CancellationError;
  3631. Aigle.TimeoutError = TimeoutError;
  3632. function _resolve(value) {
  3633. if (value instanceof AigleCore) {
  3634. return value;
  3635. }
  3636. const promise = new Aigle(INTERNAL);
  3637. callReceiver(promise, value);
  3638. return promise;
  3639. }
  3640. function _reject(reason, iterator) {
  3641. if (arguments.length === 2 && typeof iterator === 'function') {
  3642. return reject(reason, iterator);
  3643. }
  3644. const promise = new Aigle(INTERNAL);
  3645. promise._reject(reason);
  3646. return promise;
  3647. }
  3648. function execute(executor) {
  3649. stackTraces && resolveStack(this);
  3650. try {
  3651. executor(
  3652. (value) => {
  3653. if (executor === undefined) {
  3654. return;
  3655. }
  3656. executor = undefined;
  3657. callReceiver(this, value);
  3658. },
  3659. (reason) => {
  3660. if (executor === undefined) {
  3661. return;
  3662. }
  3663. executor = undefined;
  3664. this._reject(reason);
  3665. }
  3666. );
  3667. } catch (e) {
  3668. if (executor === undefined) {
  3669. return;
  3670. }
  3671. executor = undefined;
  3672. this._reject(e);
  3673. }
  3674. }
  3675. function executeWithCancel(executor) {
  3676. stackTraces && resolveStack(this);
  3677. try {
  3678. executor(
  3679. (value) => {
  3680. if (executor === undefined) {
  3681. return;
  3682. }
  3683. if (value instanceof Aigle && value._resolved === 0) {
  3684. this._parent = value;
  3685. }
  3686. executor = undefined;
  3687. callReceiver(this, value);
  3688. },
  3689. (reason) => {
  3690. if (executor === undefined) {
  3691. return;
  3692. }
  3693. executor = undefined;
  3694. this._reject(reason);
  3695. },
  3696. (handler) => {
  3697. if (typeof handler !== 'function') {
  3698. throw new TypeError('onCancel must be function');
  3699. }
  3700. if (this._resolved !== 0) {
  3701. return;
  3702. }
  3703. if (this._onCancelQueue === undefined) {
  3704. this._onCancelQueue = new Queue();
  3705. }
  3706. this._onCancelQueue.push(handler);
  3707. }
  3708. );
  3709. } catch (e) {
  3710. if (executor === undefined) {
  3711. return;
  3712. }
  3713. executor = undefined;
  3714. this._reject(e);
  3715. }
  3716. }
  3717. function createOnRejected(errorTypes, onRejected) {
  3718. return (reason) => {
  3719. let l = errorTypes.length;
  3720. while (l--) {
  3721. const errorType = errorTypes[l];
  3722. if (errorType === Error || errorType.prototype instanceof Error) {
  3723. if (reason instanceof errorType) {
  3724. return onRejected(reason);
  3725. }
  3726. } else if (errorType(reason)) {
  3727. return onRejected(reason);
  3728. }
  3729. }
  3730. errorObj.e = reason;
  3731. return errorObj;
  3732. };
  3733. }
  3734. function createFinallyHandler(promise, handler) {
  3735. return () => {
  3736. const { _resolved, _value } = promise;
  3737. const p = call0(handler);
  3738. if (p === errorObj) {
  3739. return p;
  3740. }
  3741. if (p instanceof AigleCore) {
  3742. switch (p._resolved) {
  3743. case 1:
  3744. p._value = _value;
  3745. return p;
  3746. case 2:
  3747. return p;
  3748. }
  3749. }
  3750. const receiver = new Aigle(INTERNAL);
  3751. if (!p || !p.then) {
  3752. receiver._resolved = _resolved;
  3753. receiver._value = _value;
  3754. } else if (_resolved === 1) {
  3755. p.then(
  3756. () => receiver._resolve(_value),
  3757. (reason) => receiver._reject(reason)
  3758. );
  3759. } else {
  3760. p.then(
  3761. () => receiver._reject(_value),
  3762. (reason) => receiver._reject(reason)
  3763. );
  3764. }
  3765. return receiver;
  3766. };
  3767. }
  3768. function addAigle(promise, receiver, onFulfilled, onRejected) {
  3769. stackTraces && resolveStack(receiver);
  3770. if (promise._receiver === undefined || promise._receiver === INTERNAL) {
  3771. promise._resolved !== 0 && invokeAsync(promise);
  3772. promise._receiver = receiver;
  3773. promise._onFulfilled = onFulfilled;
  3774. promise._onRejected = onRejected;
  3775. } else if (promise._receiver === UNHANDLED) {
  3776. promise._receiver = receiver;
  3777. promise._onFulfilled = onFulfilled;
  3778. promise._onRejected = onRejected;
  3779. } else {
  3780. if (!promise._receivers) {
  3781. promise._receivers = new Queue();
  3782. }
  3783. promise._receivers.push({ receiver, onFulfilled, onRejected });
  3784. }
  3785. return receiver;
  3786. }
  3787. function addReceiver(promise, receiver) {
  3788. stackTraces && resolveStack(receiver);
  3789. promise._resolved !== 0 && invokeAsync(promise);
  3790. promise._receiver = receiver;
  3791. return receiver._promise;
  3792. }
  3793. function addProxy(promise, Proxy, arg1, arg2, arg3) {
  3794. if (stackTraces) {
  3795. stackTraces = false;
  3796. const receiver = addProxy(promise, Proxy, arg1, arg2, arg3);
  3797. stackTraces = true;
  3798. resolveStack(receiver);
  3799. return receiver;
  3800. }
  3801. switch (promise._resolved) {
  3802. case 0:
  3803. const receiver = new Proxy(PENDING, arg1, arg2, arg3);
  3804. if (promise._receiver === undefined) {
  3805. promise._receiver = receiver;
  3806. } else {
  3807. if (!promise._receivers) {
  3808. promise._receivers = new Queue();
  3809. }
  3810. promise._receivers.push({ receiver });
  3811. }
  3812. return receiver._promise;
  3813. case 1:
  3814. return new Proxy(promise._value, arg1, arg2, arg3)._execute();
  3815. case 2:
  3816. return Aigle.reject(promise._value);
  3817. }
  3818. }
  3819. /**
  3820. * @param {Object} opts
  3821. * @param {boolean} [opts.longStackTraces]
  3822. * @param {boolean} [opts.cancellation]
  3823. */
  3824. function config(opts) {
  3825. opts = opts || {};
  3826. if (opts.longStackTraces !== undefined) {
  3827. stackTraces = !!opts.longStackTraces;
  3828. }
  3829. if (opts.cancellation !== undefined) {
  3830. Aigle.prototype._execute = opts.cancellation ? executeWithCancel : execute;
  3831. }
  3832. }
  3833. function longStackTraces() {
  3834. stackTraces = true;
  3835. }
  3836. /**
  3837. * Add functions which sources has to the Aigle class functions and static functions.
  3838. * The functions will be converted asynchronous functions.
  3839. * If an extended function returns a promise instance, the function will wait until the promise is resolved.
  3840. *
  3841. * @param {Object} sources
  3842. * @param {Object} [opts]
  3843. * @param {boolean} [opts.promisify=true]
  3844. * @param {boolean} [opts.override=false]
  3845. * @example
  3846. * Aigle.mixin(require('lodash'));
  3847. * const array = [1, 2, 3];
  3848. * return Aigle.map(array, n => Aigle.delay(10, n * 2))
  3849. * .sum()
  3850. * .then(value => {
  3851. * console.log(value; // 12
  3852. * });
  3853. *
  3854. * @example
  3855. * Aigle.mixin(require('lodash'));
  3856. * const array = [1.1, 1.4, 2.2];
  3857. * return Aigle.map(array, n => Aigle.delay(10, n * 2))
  3858. * .uniqBy(n => Aigle.delay(10, Math.floor(n))
  3859. * .then(array => {
  3860. * console.log(array; // [2.2, 4.4]
  3861. * });
  3862. */
  3863. function mixin(sources, opts = {}) {
  3864. const { override, promisify = true } = opts;
  3865. Object.getOwnPropertyNames(sources).forEach((key) => {
  3866. const func = sources[key];
  3867. if (typeof func !== 'function' || (Aigle[key] && !override)) {
  3868. return;
  3869. }
  3870. // check lodash chain
  3871. if (key === 'chain') {
  3872. const obj = func();
  3873. if (obj && obj.__chain__) {
  3874. Aigle.chain = _resolve;
  3875. Aigle.prototype.value = function () {
  3876. return this;
  3877. };
  3878. return;
  3879. }
  3880. }
  3881. const Proxy = createProxy(func, promisify);
  3882. Aigle[key] = function (value, arg1, arg2, arg3) {
  3883. return new Proxy(value, arg1, arg2, arg3)._execute();
  3884. };
  3885. Aigle.prototype[key] = function (arg1, arg2, arg3) {
  3886. return addProxy(this, Proxy, arg1, arg2, arg3);
  3887. };
  3888. });
  3889. return Aigle;
  3890. }