Categories

Take While

Take items while a predicate matches using lodash _.takeWhile

Take items from the start of an array while a predicate stays true.

Highlights:

  • Uses lodash _.takeWhile
  • Supports numeric and string predicates
  • Returns the matching prefix as JSON

Example:

  • Array: [1, 2, 3, 1]
  • Predicate: less-than
  • Compare: 3
  • Result: [1, 2]

Enter a JSON array

Select the condition for taking items

Value to compare against (required for most predicates)

Key Facts

Category
Data Processing
Input Types
textarea, select, text
Output Type
text
Sample Coverage
4
API Ready
Yes

Overview

The Take While tool allows you to extract a prefix from an array by selecting elements from the beginning until a specified condition is no longer met.

When to Use

  • When you need to truncate a list of data as soon as a specific value threshold is reached.
  • When filtering log entries or time-series data to capture only the initial sequence that satisfies a condition.
  • When cleaning datasets by removing trailing elements that fall outside of your required criteria.

How It Works

  • Input your data as a standard JSON array.
  • Select a predicate type from the list, such as 'less than' or 'string starts with'.
  • Provide a comparison value to define the boundary for the extraction.
  • The tool processes the array using lodash _.takeWhile and returns the matching prefix.

Use Cases

Extracting a sequence of successful transactions from a list until the first failure occurs.
Parsing a list of configuration settings until a specific delimiter or invalid entry is found.
Isolating the initial period of a sensor data stream that remains below a critical temperature threshold.

Examples

1. Extracting Low-Value Transactions

Background
A financial analyst has a list of transaction amounts and needs to identify the initial streak of small purchases before a large transaction occurs.
Problem
Manually identifying the cutoff point in a long list of numbers is prone to error.
How to Use
Input the transaction array, select 'Less than', and set the compare value to 100.
Example Config
Array: [20, 45, 80, 150, 30], Predicate: Less than, Compare Value: 100
Outcome
Returns [20, 45, 80], stopping at 150.

2. Filtering Log Prefixes

Background
A developer needs to extract all log messages that start with a specific status code prefix before the system encountered an error.
Problem
Need to isolate the healthy startup sequence from a log array.
How to Use
Input the log array, select 'String starts with', and set the compare value to 'INFO'.
Example Config
Array: ['INFO: start', 'INFO: init', 'ERROR: fail', 'INFO: retry'], Predicate: String starts with, Compare Value: INFO
Outcome
Returns ['INFO: start', 'INFO: init'], stopping at the first 'ERROR' entry.

Try with Samples

json

Related Hubs

FAQ

What happens if the first element does not match the predicate?

The tool will return an empty array because the condition is not met at the very start of the sequence.

Does this tool modify the original array?

No, the tool processes the input and returns a new JSON array containing only the matching prefix.

Can I use this for string arrays?

Yes, you can use predicates like 'string starts with' or 'string contains' to filter string-based arrays.

What is the difference between this and a standard filter?

A filter checks every element in the array, whereas Take While stops processing immediately once it encounters an element that fails the condition.

Is the comparison value mandatory?

It is required for most predicates, though some options like 'is truthy' or 'is nullish' do not require an additional comparison value.

API Documentation

Request Endpoint

POST /en/api/tools/take-while

Request Parameters

Parameter Name Type Required Description
array textarea Yes Enter a JSON array
predicateType select Yes Select the condition for taking items
compareValue text No Value to compare against (required for most predicates)

Response Format

{
  "result": "Processed text content",
  "error": "Error message (optional)",
  "message": "Notification message (optional)",
  "metadata": {
    "key": "value"
  }
}
Text: Text

AI MCP Documentation

Add this tool to your MCP server configuration:

{
  "mcpServers": {
    "elysiatools-take-while": {
      "name": "take-while",
      "description": "Take items while a predicate matches using lodash _.takeWhile",
      "baseUrl": "https://elysiatools.com/mcp/sse?toolId=take-while",
      "command": "",
      "args": [],
      "env": {},
      "isActive": true,
      "type": "sse"
    }
  }
}

You can chain multiple tools, e.g.: `https://elysiatools.com/mcp/sse?toolId=png-to-webp,jpg-to-webp,gif-to-webp`, max 20 tools.

If you encounter any issues, please contact us at [email protected]