or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash-takewhile

Modularized implementation of lodash's takeWhile function for creating array slices until a predicate returns falsey

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.takewhile@3.1.x

To install, run

npx @tessl/cli install tessl/npm-lodash-takewhile@3.1.0

index.mddocs/

lodash.takewhile

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.

Package Information

  • Package Name: lodash.takewhile
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.takewhile

Core Imports

CommonJS (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.

Basic Usage

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')

Capabilities

Array Slicing with Predicate

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:

  1. Function Predicate: Custom function receiving (value, index, array)
  2. Property Shorthand: String property name to check truthiness
  3. MatchesProperty Shorthand: Property name with specific value to match
  4. Matches Object Shorthand: Object with properties to match against

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]

Types

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
 */