mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 03:17:49 +00:00
Split filter out.
This commit is contained in:
@@ -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
|
|
||||||
@@ -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
|
|
||||||
29
filter.js
29
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
|
* `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.
|
* **Note:** Unlike `remove`, this method returns a new array.
|
||||||
*
|
*
|
||||||
* @since 0.1.0
|
* @since 5.0.0
|
||||||
* @category Collection
|
* @category Array
|
||||||
* @param {Array|Object} collection The collection to iterate over.
|
* @param {Array} array The array to iterate over.
|
||||||
* @param {Function} predicate The function invoked per iteration.
|
* @param {Function} predicate The function invoked per iteration.
|
||||||
* @returns {Array} Returns the new filtered array.
|
* @returns {Array} Returns the new filtered array.
|
||||||
* @see pull, pullAll, pullAllBy, pullAllWith, pullAt, remove, reject
|
* @see pull, pullAll, pullAllBy, pullAllWith, pullAt, remove, reject
|
||||||
@@ -24,9 +21,19 @@ import baseFilter from './.internal/baseFilter.js'
|
|||||||
* filter(users, ({ active }) => active)
|
* filter(users, ({ active }) => active)
|
||||||
* // => objects for ['barney']
|
* // => objects for ['barney']
|
||||||
*/
|
*/
|
||||||
function filter(collection, predicate) {
|
function filter(array, predicate) {
|
||||||
const func = Array.isArray(collection) ? arrayFilter : baseFilter
|
let index = -1
|
||||||
return func(collection, predicate)
|
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
|
export default filter
|
||||||
|
|||||||
29
filterObject.js
Normal file
29
filterObject.js
Normal 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
|
||||||
Reference in New Issue
Block a user