Skip to content

fork

Split an array into two arrays, one containing elements that meet the condition, and the other containing elements that do not.

Basic Usage

typescript
import { fork } from 'radash'

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

const [evens, odds] = fork(numbers, x => x % 2 === 0)
// evens: [2, 4, 6, 8, 10]
// odds: [1, 3, 5, 7, 9]

Syntax

typescript
function fork<T>(
  array: readonly T[],
  condition: (item: T, index: number, array: readonly T[]) => boolean
): [T[], T[]]

Parameters

  • array (readonly T[]): The array to bifurcate
  • condition (function): Function to determine if an element meets the condition
    • item (T): Current element
    • index (number): Index of the current element
    • array (readonly T[]): The original array

Return Value

Returns a tuple containing two arrays:

  • The first array: elements that meet the condition
  • The second array: elements that do not meet the condition

Examples

Separate Users

typescript
import { fork } from 'radash'

const users = [
  { id: 1, name: 'Alice', age: 25, active: true },
  { id: 2, name: 'Bob', age: 30, active: false },
  { id: 3, name: 'Charlie', age: 35, active: true },
  { id: 4, name: 'Diana', age: 28, active: false }
]

const [activeUsers, inactiveUsers] = fork(users, user => user.active)

console.log(activeUsers)
// [
//   { id: 1, name: 'Alice', age: 25, active: true },
//   { id: 3, name: 'Charlie', age: 35, active: true }
// ]

console.log(inactiveUsers)
// [
//   { id: 2, name: 'Bob', age: 30, active: false },
//   { id: 4, name: 'Diana', age: 28, active: false }
// ]

Separate Numbers

typescript
import { fork } from 'radash'

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

// Separate positive and negative numbers
const [positives, negatives] = fork(numbers, x => x > 0)
// positives: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// negatives: []

// Separate numbers greater than 5 and less than or equal to 5
const [large, small] = fork(numbers, x => x > 5)
// large: [6, 7, 8, 9, 10]
// small: [1, 2, 3, 4, 5]

Separate Strings

typescript
import { fork } from 'radash'

const words = ['apple', 'banana', 'cherry', 'date', 'elderberry']

const [longWords, shortWords] = fork(words, word => word.length > 5)
// longWords: ['banana', 'cherry', 'elderberry']
// shortWords: ['apple', 'date']

Using Index

typescript
import { fork } from 'radash'

const items = ['a', 'b', 'c', 'd', 'e']

const [firstHalf, secondHalf] = fork(items, (_, index) => index < 2)
// firstHalf: ['a', 'b']
// secondHalf: ['c', 'd', 'e']

Notes

  1. Keep the original array unchanged: fork does not modify the original array, but returns new arrays
  2. Condition function: The condition function should return a boolean value to determine which array the element belongs to
  3. Empty array handling: If the original array is empty, returns two empty arrays
  4. Performance: Time complexity is O(n), where n is the array length

Differences from Other Methods

  • filter(): Only returns elements that meet the condition
  • fork(): Returns two arrays, one with elements that meet the condition and one with those that do not
  • partition(): Similar functionality, but fork is more concise

Practical Application Scenarios

  1. User management: Separate active and inactive users
  2. Data processing: Separate valid and invalid data
  3. File processing: Separate successful and failed file operations
  4. Form validation: Separate fields that pass and fail validation

Released under the MIT License.