'use strict'; const { Each } = require('./each'); const { setShorthand } = require('./internal/collection'); class OmitBy extends Each { constructor(collection, iterator) { super(collection, iterator, set); this._result = {}; } } module.exports = { omitBy, OmitBy }; function set(collection) { setShorthand.call(this, collection); this._callResolve = this._keys === undefined ? callResolveArray : callResolveObject; return this; } function callResolveArray(value, index) { if (!value) { this._result[index] = this._coll[index]; } if (--this._rest === 0) { this._promise._resolve(this._result); } } function callResolveObject(value, index) { if (!value) { const key = this._keys[index]; this._result[key] = this._coll[key]; } if (--this._rest === 0) { this._promise._resolve(this._result); } } /** * `Aigle.omitBy` has almost the same functionality as [`Aigle.reject`](https://suguru03.github.io/aigle/docs/global.html#reject). * It will return an object as a result. * @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; * }); * }; * Aigle.omitBy(collection, iterator) * .then(object => { * console.log(object); // { '1': 4, '2': 4 } * console.log(order); // [1, 2, 4] * }); * * @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; * }); * }; * Aigle.omitBy(collection, iterator) * .then(object => { * console.log(object); // { b: 4, c: 2 } * console.log(order); // [1, 2, 4] * }); * * @example * const order = []; * const collection = [{ * name: 'bargey', active: false * }, { * name: 'fread', active: true * }]; * Aigle.omitBy(collection, 'active') * .then(object => { * console.log(object); // { '0': { name: 'bargey', active: false } } * }); * * @example * const order = []; * const collection = [{ * name: 'bargey', active: false * }, { * name: 'fread', active: true * }]; * Aigle.omitBy(collection, ['name', 'fread']) * .then(object => { * console.log(object); // { '0': { name: 'bargey', active: false } } * }); * * @example * const order = []; * const collection = [{ * name: 'bargey', active: false * }, { * name: 'fread', active: true * }]; * Aigle.omitBy(collection, { name: 'fread', active: true }) * .then(object => { * console.log(object); // { '0': { name: 'bargey', active: false } } * }); */ function omitBy(collection, iterator) { return new OmitBy(collection, iterator)._execute(); }