diff --git a/.internal/arrayFilter.js b/.internal/arrayFilter.js deleted file mode 100644 index b2bfab18a..000000000 --- a/.internal/arrayFilter.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * A specialized version of `filter` for arrays. - * - * @private - * @param {Array} [array] The array to iterate over. - * @param {Function} predicate The function invoked per iteration. - * @returns {Array} Returns the new filtered array. - */ -function arrayFilter(array, predicate) { - let index = -1 - let resIndex = 0 - const length = array == null ? 0 : array.length - const result = [] - - while (++index < length) { - const value = array[index] - if (predicate(value, index, array)) { - result[resIndex++] = value - } - } - return result -} - -export default arrayFilter diff --git a/.internal/baseFilter.js b/.internal/baseFilter.js deleted file mode 100644 index 68b204708..000000000 --- a/.internal/baseFilter.js +++ /dev/null @@ -1,21 +0,0 @@ -import baseEach from './baseEach.js' - -/** - * The base implementation of `filter`. - * - * @private - * @param {Array|Object} collection The collection to iterate over. - * @param {Function} predicate The function invoked per iteration. - * @returns {Array} Returns the new filtered array. - */ -function baseFilter(collection, predicate) { - const result = [] - baseEach(collection, (value, index, collection) => { - if (predicate(value, index, collection)) { - result.push(value) - } - }) - return result -} - -export default baseFilter diff --git a/filter.js b/filter.js index 4ce334f5f..98606e531 100644 --- a/filter.js +++ b/filter.js @@ -1,16 +1,13 @@ -import arrayFilter from './.internal/arrayFilter.js' -import baseFilter from './.internal/baseFilter.js' - /** - * Iterates over elements of `collection`, returning an array of all elements + * Iterates over elements of `array`, returning an array of all elements * `predicate` returns truthy for. The predicate is invoked with three - * arguments: (value, index|key, collection). + * arguments: (value, index, array). * * **Note:** Unlike `remove`, this method returns a new array. * - * @since 0.1.0 - * @category Collection - * @param {Array|Object} collection The collection to iterate over. + * @since 5.0.0 + * @category Array + * @param {Array} array The array to iterate over. * @param {Function} predicate The function invoked per iteration. * @returns {Array} Returns the new filtered array. * @see pull, pullAll, pullAllBy, pullAllWith, pullAt, remove, reject @@ -24,9 +21,19 @@ import baseFilter from './.internal/baseFilter.js' * filter(users, ({ active }) => active) * // => objects for ['barney'] */ -function filter(collection, predicate) { - const func = Array.isArray(collection) ? arrayFilter : baseFilter - return func(collection, predicate) +function filter(array, predicate) { + let index = -1 + let resIndex = 0 + const length = array == null ? 0 : array.length + const result = [] + + while (++index < length) { + const value = array[index] + if (predicate(value, index, array)) { + result[resIndex++] = value + } + } + return result } export default filter diff --git a/filterObject.js b/filterObject.js new file mode 100644 index 000000000..3bcd0ef74 --- /dev/null +++ b/filterObject.js @@ -0,0 +1,29 @@ +/** + * Iterates over properties of `object`, returning an array of all elements + * `predicate` returns truthy for. The predicate is invoked with three + * arguments: (value, key, object). + * + * @since 5.0.0 + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} predicate The function invoked per iteration. + * @returns {Array} Returns the new filtered array. + * @see pull, pullAll, pullAllBy, pullAllWith, pullAt, remove, reject + * @example + * + * const object = { 'a': 5, 'b': 8, 'c': 10 } + * + * filterObject(object, (n) => !(n % 5)) + * // => [5, 10] + */ +function filterObject(object, predicate) { + const result = [] + Object.keys(Object(object)).forEach((key) => { + if (predicate(object[key], key, object)) { + result.push(value) + } + }) + return result +} + +export default filterObject