v12.4.0
New Functions
Add remove function → PR #344
The remove function removes elements from an array based on the specified predicate function.
- Removes elements that satisfy the predicate function.
- Returns a new array with the removed elements.
- Does not mutate the original array.
import * as _ from 'radashi'
const numbers = [1, 2, 3, 4, 5]
const removed = _.remove(numbers, value => value % 2 === 0)
console.log(removed) // Output: [1, 3, 5]Thanks to nusohiro for their work on this feature!
Add toResult function → PR #375
The toResult function converts a PromiseLike to a Promise<Result>.
- Converts a resolved promise to
[undefined, value]. - Converts a rejected promise to
[Error, undefined]. - Rethrows non-Error rejections.
import { toResult, Result } from 'radashi'
const good = async (): Promise<number> => 1
const bad = async (): Promise<number> => {
throw new Error('bad')
}
const goodResult = await toResult(good())
// => [undefined, 1]
const badResult = await toResult(bad())
// => [Error('bad'), undefined]Thanks to Alec Larson for their work on this feature!
Add memoLastCall function → PR #353
The memoLastCall function creates a memoized version of a function that caches only its most recent call. This is useful for optimizing expensive calculations when only the latest result needs to be cached, making it more memory-efficient than traditional memoization.
- Caches the last result of a function call.
- Returns the cached result if the function is called with the same arguments as the previous call.
- Optimizes expensive calculations by avoiding recalculation when the arguments are the same.
import * as _ from 'radashi'
const expensiveCalculation = (x: number, y: number): number => {
console.log('Calculating...')
return x + y
}
const memoizedCalc = _.memoLastCall(expensiveCalculation)
console.log(memoizedCalc(2, 3)) // Outputs: \"Calculating...\" then 5
console.log(memoizedCalc(2, 3)) // Outputs: 5 (uses cached result)
console.log(memoizedCalc(3, 4)) // Outputs: \"Calculating...\" then 7
console.log(memoizedCalc(2, 3)) // Outputs: \"Calculating...\" then 5 (previous cache was overwritten)Thanks to Alec Larson for their work on this feature!
Add isAsyncIterable function → PR #366
The isAsyncIterable function checks if a value is an async iterable.
- Returns
truefor async iterables created by an async generator function. - Returns
truefor objects with a[Symbol.asyncIterator]method. - Returns
falsefor everything else.
import * as _ from 'radashi'
_.isAsyncIterable(
(async function* () {
yield 1
})(),
)
// => true
_.isAsyncIterable([1, 2, 3])
// => falseThanks to Alec Larson for their work on this feature!
Add isBigInt function → PR #369
The isBigInt function returns true if the given value is a BigInt.
- Returns
truewhentypeofreturns'bigint'. - Returns
falsefor everything else.
import * as _ from 'radashi'
_.isBigInt(0n) // => true
_.isBigInt(BigInt(0)) // => true
_.isBigInt(12) // => false
_.isBigInt('0n') // => falseThanks to Shan Shaji for their work on this feature!
New Features
BigInt support in isEmpty → PR #374
The isEmpty function now supports BigInt values.
- Returns
truefor0norBigInt(0).
import * as _ from 'radashi'
_.isEmpty(0n) // => true
_.isEmpty(BigInt(0)) // => true
_.isEmpty(1n) // => falseThanks to Alec Larson for their work on this feature!