Skip to content

boil

Extract unique values from an array, similar to deduplication operations.

Syntax

typescript
boil<T>(
  array: T[],
  key?: (item: T) => any
): T[]

Parameters

  • array (T[]): The array to process
  • key ((item: T) => any, optional): Key extraction function for comparison

Return Value

  • T[]: Deduplicated array

Examples

Basic Usage

typescript
import { boil } from 'radash'

const numbers = [1, 2, 2, 3, 3, 4, 5, 5]
const uniqueNumbers = boil(numbers)
// [1, 2, 3, 4, 5]

Using Object Arrays

typescript
import { boil } from 'radash'

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 1, name: 'Alice' }, // duplicate
  { id: 3, name: 'Charlie' }
]

const uniqueUsers = boil(users, user => user.id)
// [
//   { id: 1, name: 'Alice' },
//   { id: 2, name: 'Bob' },
//   { id: 3, name: 'Charlie' }
// ]

Using Multiple Keys

typescript
import { boil } from 'radash'

const products = [
  { id: 1, name: 'Laptop', category: 'Electronics' },
  { id: 2, name: 'Phone', category: 'Electronics' },
  { id: 1, name: 'Laptop', category: 'Electronics' }, // duplicate
  { id: 3, name: 'Book', category: 'Education' }
]

const uniqueProducts = boil(products, product => `${product.id}-${product.category}`)
// [
//   { id: 1, name: 'Laptop', category: 'Electronics' },
//   { id: 2, name: 'Phone', category: 'Electronics' },
//   { id: 3, name: 'Book', category: 'Education' }
// ]

Handling String Arrays

typescript
import { boil } from 'radash'

const tags = ['javascript', 'typescript', 'javascript', 'react', 'typescript']
const uniqueTags = boil(tags)
// ['javascript', 'typescript', 'react']

Handling Nested Objects

typescript
import { boil } from 'radash'

const orders = [
  { id: 1, customer: { id: 101, name: 'Alice' } },
  { id: 2, customer: { id: 102, name: 'Bob' } },
  { id: 3, customer: { id: 101, name: 'Alice' } } // duplicate customer
]

const uniqueByCustomer = boil(orders, order => order.customer.id)
// [
//   { id: 1, customer: { id: 101, name: 'Alice' } },
//   { id: 2, customer: { id: 102, name: 'Bob' } }
// ]

Handling Date Objects

typescript
import { boil } from 'radash'

const events = [
  { id: 1, date: new Date('2023-01-01'), title: 'Event 1' },
  { id: 2, date: new Date('2023-01-02'), title: 'Event 2' },
  { id: 3, date: new Date('2023-01-01'), title: 'Event 3' } // duplicate date
]

const uniqueByDate = boil(events, event => event.date.toISOString().split('T')[0])
// [
//   { id: 1, date: new Date('2023-01-01'), title: 'Event 1' },
//   { id: 2, date: new Date('2023-01-02'), title: 'Event 2' }
// ]

Notes

  1. Preserve Order: Keeps the first occurrence of elements
  2. Key Function: If no key function is provided, directly compares element values
  3. Object Comparison: For objects, a key function needs to be provided for effective deduplication
  4. Performance: Time complexity is O(n²), suitable for small to medium-sized arrays

Differences from Other Functions

  • boil: Deduplication operation, preserves the first occurrence of elements
  • unique: Similar functionality, but may have different implementations
  • distinct: Similar functionality in other libraries

Performance

  • Time Complexity: O(n²)
  • Space Complexity: O(n)
  • Use Cases: Deduplication operations for small to medium-sized arrays

Released under the MIT License.