'use strict'; const { Each } = require('./each'); const { setShorthand } = require('./internal/collection'); class Some extends Each { constructor(collection, iterator) { super(collection, iterator, setShorthand); this._result = false; } _callResolve(value) { if (value) { this._promise._resolve(true); } else if (--this._rest === 0) { this._promise._resolve(false); } } } module.exports = { some, Some }; /** * `Aigle.some` has almost the same functionality as `Array#some`. * It iterates all elements of `collection` and executes `iterator` using each element on parallel. * The `iterator` needs to return a promise or something.. * If a promise is returned, the function will wait until the promise is fulfilled. * If the result is truthy, the function will return true otherwise false. * @param {Array|Object} collection * @param {Function|Array|Object|string} iterator * @return {Aigle} Returns an Aigle instance * @example * const order = []; * const collection = [1, 4, 2]; * const iterator = num => { * return Aigle.delay(num * 10) * .then(() => { * order.push(num); * return num % 2 === 0; * }); * }; * Aigle.some(collection, iterator) * .then(bool => { * console.log(bool); // true * console.log(order); // [1, 2] * }); * * @example * const order = []; * const collection = { a: 1, b: 4, c: 2 }; * const iterator = num => { * return Aigle.delay(num * 10) * .then(() => { * order.push(num); * return num % 2 === 0; * }); * }; * Aigle.some(collection, iterator) * .then(bool => { * console.log(bool); // true * console.log(order); // [1, 2] * }); * * @example * const order = []; * const collection = [1, 4, 2]; * const iterator = num => { * return Aigle.delay(num * 10) * .then(() => { * order.push(num); * return false; * }); * }; * Aigle.some(collection, iterator) * .then(bool => { * console.log(bool); // false * console.log(order); // [1, 2, 4] * }); * * @example * const collection = [{ * uid: 1, active: false * }, { * uid: 4, active: true * }, { * uid: 2, active: true * }]; * Aigle.some(collection, 'active') * .then(value => console.log(value)); // true * * @example * const collection = [{ * uid: 1, active: false * }, { * uid: 4, active: true * }, { * uid: 2, active: true * }]; * Aigle.some(collection, ['uid', 4]) * .then(value => console.log(value)); // true * * @example * const collection = [{ * uid: 1, active: false * }, { * uid: 4, active: true * }, { * uid: 2, active: true * }]; * Aigle.some(collection, { uid: 4 }) * .then(value => console.log(value)); // true */ function some(collection, iterator) { return new Some(collection, iterator)._execute(); }