Modularized implementation of lodash's takeWhile function for creating array slices until a predicate returns falsey
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
The modularized implementation of lodash's takeWhile function, creating array slices with elements taken from the beginning until a predicate returns falsey. This package provides a lightweight, focused utility for array manipulation with support for multiple callback styles.
npm install lodash.takewhileCommonJS (primary supported format):
var takeWhile = require('lodash.takewhile');Note: This is a legacy package that predates ES modules and primarily supports CommonJS. For modern projects, consider using the full lodash library with ES module imports.
var takeWhile = require('lodash.takewhile');
// Basic function predicate
var result = takeWhile([1, 2, 3, 4, 5], function(n) {
return n < 4;
});
// => [1, 2, 3]
// With array of objects
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
// Property shorthand - take while 'active' is falsey
var inactiveUsers = takeWhile(users, 'active');
// => [] (stops immediately since first user has falsey 'active')Creates a slice of array with elements taken from the beginning until predicate returns falsey.
/**
* Creates a slice of array with elements taken from the beginning.
* Elements are taken until predicate returns falsey.
*
* @param {Array} array - The array to query
* @param {Function|Object|string} [predicate=_.identity] - The function invoked per iteration
* @param {*} [thisArg] - The this binding of predicate
* @returns {Array} Returns the slice of array
*/
function takeWhile(array, predicate, thisArg);Predicate Callback Styles:
(value, index, array)Usage Examples:
var takeWhile = require('lodash.takewhile');
// Function predicate
takeWhile([1, 2, 3, 4, 5], function(n) {
return n < 3;
});
// => [1, 2]
// Function predicate with index
takeWhile(['a', 'b', 'c', 'd'], function(value, index) {
return index < 2;
});
// => ['a', 'b']
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
// Matches object shorthand - take elements matching the object
takeWhile(users, { 'active': false });
// => [{ 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }]
// MatchesProperty shorthand - property name and value
takeWhile(users, 'active', false);
// => [{ 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }]
// Property shorthand - check truthiness of property
takeWhile(users, 'active');
// => [] (first element has falsey 'active', so empty result)
// Default predicate behavior (no predicate provided)
takeWhile([1, 2, 3]);
// => [1, 2, 3] (defaults to _.identity predicate, all values are truthy)
takeWhile([1, 0, 3]);
// => [1] (stops at falsey value 0)
// Empty or invalid input handling
takeWhile(null);
// => []
takeWhile([]);
// => []ThisArg Binding:
var context = { threshold: 3 };
takeWhile([1, 2, 3, 4, 5], function(n) {
return n < this.threshold;
}, context);
// => [1, 2]Since this is a JavaScript package, types are described using JSDoc conventions:
/**
* @typedef {Function} PredicateFunction
* @param {*} value - The current element being processed
* @param {number} index - The index of the current element
* @param {Array} array - The array being processed
* @returns {boolean} True to continue taking elements, false to stop
*/
/**
* @typedef {Object} MatchesObject
* @description Object with properties to match against array elements
*/
/**
* @typedef {string} PropertyPath
* @description Property name or path to check on array elements
*/
/**
* @typedef {PredicateFunction|MatchesObject|PropertyPath} Predicate
* @description Any of the supported predicate formats
*/