Split filter out.

This commit is contained in:
John-David Dalton
2017-04-16 08:49:26 -05:00
parent 8b74809f1c
commit 64a9975488
4 changed files with 47 additions and 56 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

29
filterObject.js Normal file
View File

@@ -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