From 782e08a86d21ab9b20d34d49f4b38fb1f1d29db5 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:39:52 -0400 Subject: [PATCH 01/10] wip --- src/array/toArray.ts | 7 +++++++ src/array/toArrayIfExists.ts | 11 +++++++++++ 2 files changed, 18 insertions(+) create mode 100644 src/array/toArray.ts create mode 100644 src/array/toArrayIfExists.ts diff --git a/src/array/toArray.ts b/src/array/toArray.ts new file mode 100644 index 00000000..79fbb872 --- /dev/null +++ b/src/array/toArray.ts @@ -0,0 +1,7 @@ +export function toArray( + value: T, +): T extends readonly (infer U)[] ? U[] : [T] + +export function toArray(value: T | readonly T[]): any { + return Array.isArray(value) ? [...value] : [value] +} diff --git a/src/array/toArrayIfExists.ts b/src/array/toArrayIfExists.ts new file mode 100644 index 00000000..1a677339 --- /dev/null +++ b/src/array/toArrayIfExists.ts @@ -0,0 +1,11 @@ +export function toArrayIfExists( + value: T | readonly T[], +): T extends any[] + ? T[number][] + : T extends readonly any[] + ? readonly T[number][] + : Exclude[] | undefined + +export function toArrayIfExists(value: T | readonly T[]): any { + return Array.isArray(value) ? [...value] : value != null ? [value] : undefined +} From 5df63bdde6d8ca1fd483841a4c22e74764c30164 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Fri, 12 Jul 2024 19:59:05 -0400 Subject: [PATCH 02/10] rename to castArray --- src/array/{toArray.ts => castArray.ts} | 4 ++-- src/array/{toArrayIfExists.ts => castArrayIfExists.ts} | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename src/array/{toArray.ts => castArray.ts} (52%) rename src/array/{toArrayIfExists.ts => castArrayIfExists.ts} (70%) diff --git a/src/array/toArray.ts b/src/array/castArray.ts similarity index 52% rename from src/array/toArray.ts rename to src/array/castArray.ts index 79fbb872..33071b51 100644 --- a/src/array/toArray.ts +++ b/src/array/castArray.ts @@ -1,7 +1,7 @@ -export function toArray( +export function castArray( value: T, ): T extends readonly (infer U)[] ? U[] : [T] -export function toArray(value: T | readonly T[]): any { +export function castArray(value: T | readonly T[]): any { return Array.isArray(value) ? [...value] : [value] } diff --git a/src/array/toArrayIfExists.ts b/src/array/castArrayIfExists.ts similarity index 70% rename from src/array/toArrayIfExists.ts rename to src/array/castArrayIfExists.ts index 1a677339..4ee0ad18 100644 --- a/src/array/toArrayIfExists.ts +++ b/src/array/castArrayIfExists.ts @@ -1,4 +1,4 @@ -export function toArrayIfExists( +export function castArrayIfExists( value: T | readonly T[], ): T extends any[] ? T[number][] @@ -6,6 +6,6 @@ export function toArrayIfExists( ? readonly T[number][] : Exclude[] | undefined -export function toArrayIfExists(value: T | readonly T[]): any { +export function castArrayIfExists(value: T | readonly T[]): any { return Array.isArray(value) ? [...value] : value != null ? [value] : undefined } From 15c33b67e1de5cc72e773d38b94847b734626426 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sat, 13 Jul 2024 16:20:18 -0400 Subject: [PATCH 03/10] move functions to src/casted --- src/{array => casted}/castArray.ts | 0 src/{array => casted}/castArrayIfExists.ts | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/{array => casted}/castArray.ts (100%) rename src/{array => casted}/castArrayIfExists.ts (100%) diff --git a/src/array/castArray.ts b/src/casted/castArray.ts similarity index 100% rename from src/array/castArray.ts rename to src/casted/castArray.ts diff --git a/src/array/castArrayIfExists.ts b/src/casted/castArrayIfExists.ts similarity index 100% rename from src/array/castArrayIfExists.ts rename to src/casted/castArrayIfExists.ts From 11c231ec516878dbcc0e2a453e910343ddc48944 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sat, 13 Jul 2024 16:21:55 -0400 Subject: [PATCH 04/10] fix: use slice to preserve sparseness --- src/casted/castArray.ts | 2 +- src/casted/castArrayIfExists.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/casted/castArray.ts b/src/casted/castArray.ts index 33071b51..64cbe689 100644 --- a/src/casted/castArray.ts +++ b/src/casted/castArray.ts @@ -3,5 +3,5 @@ export function castArray( ): T extends readonly (infer U)[] ? U[] : [T] export function castArray(value: T | readonly T[]): any { - return Array.isArray(value) ? [...value] : [value] + return Array.isArray(value) ? value.slice() : [value] } diff --git a/src/casted/castArrayIfExists.ts b/src/casted/castArrayIfExists.ts index 4ee0ad18..080cf60f 100644 --- a/src/casted/castArrayIfExists.ts +++ b/src/casted/castArrayIfExists.ts @@ -7,5 +7,9 @@ export function castArrayIfExists( : Exclude[] | undefined export function castArrayIfExists(value: T | readonly T[]): any { - return Array.isArray(value) ? [...value] : value != null ? [value] : undefined + return Array.isArray(value) + ? value.slice() + : value != null + ? [value] + : undefined } From 637a1e1683472fab1f57b3434317f25e4149911b Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sat, 13 Jul 2024 16:22:22 -0400 Subject: [PATCH 05/10] update mod.ts --- src/mod.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mod.ts b/src/mod.ts index abe2092e..c4e5a588 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -38,7 +38,9 @@ export * from './async/retry.ts' export * from './async/sleep.ts' export * from './async/tryit.ts' -export * from './casted/castMapping' +export * from './casted/castArray.ts' +export * from './casted/castArrayIfExists.ts' +export * from './casted/castMapping.ts' export * from './curry/callable.ts' export * from './curry/chain.ts' From 23f62a1e0f82ae5bec5109119be3f7eaf9a3b84c Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sat, 13 Jul 2024 16:42:30 -0400 Subject: [PATCH 06/10] add castArray tests --- tests/casted/castArray.test-d.ts | 33 ++++++++++++++++++++++++++++++++ tests/casted/castArray.test.ts | 31 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tests/casted/castArray.test-d.ts create mode 100644 tests/casted/castArray.test.ts diff --git a/tests/casted/castArray.test-d.ts b/tests/casted/castArray.test-d.ts new file mode 100644 index 00000000..01950f0d --- /dev/null +++ b/tests/casted/castArray.test-d.ts @@ -0,0 +1,33 @@ +import * as _ from 'radashi' + +describe('castArray', () => { + test('mutable arrays', () => { + const input: number[] = [1, 2, 3] + expectTypeOf(_.castArray(input)).toEqualTypeOf() + }) + test('readonly arrays', () => { + const input: readonly number[] = [1, 2, 3] + + // castArray clones the input array, so "readonly" is removed. + expectTypeOf(_.castArray(input)).toEqualTypeOf() + }) + test('union type with array type and primitive type', () => { + const input = 1 as number[] | number + const output = _.castArray(input) + + expectTypeOf(output).toEqualTypeOf() + }) + test('union type with two array types', () => { + const input = [1, 2, 3] as number[] | readonly string[] + const output = _.castArray(input) + + expectTypeOf(output).toEqualTypeOf() + }) + test('primitive types', () => { + expectTypeOf(_.castArray(1)).toEqualTypeOf<[number]>() + expectTypeOf(_.castArray('a')).toEqualTypeOf<[string]>() + expectTypeOf(_.castArray(true)).toEqualTypeOf<[true] | [false]>() + expectTypeOf(_.castArray(null)).toEqualTypeOf<[null]>() + expectTypeOf(_.castArray(undefined)).toEqualTypeOf<[undefined]>() + }) +}) diff --git a/tests/casted/castArray.test.ts b/tests/casted/castArray.test.ts new file mode 100644 index 00000000..73c10298 --- /dev/null +++ b/tests/casted/castArray.test.ts @@ -0,0 +1,31 @@ +import * as _ from 'radashi' + +describe('castArray', () => { + test('return the same array if input is an array', () => { + const input = [1, 2, 3] + expect(_.castArray(input)).toEqual(input) + }) + test('return a new array with the same elements if input is an array', () => { + const input = [1, 2, 3] + const result = _.castArray(input) + expect(result).toEqual(input) + expect(result).not.toBe(input) // Ensure it's a copy + }) + test('return an array with the input if input is not an array', () => { + const input = 1 + expect(_.castArray(input)).toEqual([input]) + }) + test('handle primitives', () => { + expect(_.castArray(1)).toEqual([1]) + expect(_.castArray('a')).toEqual(['a']) + expect(_.castArray(true)).toEqual([true]) + expect(_.castArray(null)).toEqual([null]) + expect(_.castArray(undefined)).toEqual([undefined]) + }) + test('handle objects and functions', () => { + const obj = { a: 1 } + const func = () => {} + expect(_.castArray(obj)).toEqual([obj]) + expect(_.castArray(func)).toEqual([func]) + }) +}) From 27381f092392a5c17218392b2f31aa4ddb49a2d2 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sat, 13 Jul 2024 16:46:45 -0400 Subject: [PATCH 07/10] add docs --- docs/casted/castArray.mdx | 16 ++++++++++++++++ docs/casted/castArrayIfExists.mdx | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 docs/casted/castArray.mdx create mode 100644 docs/casted/castArrayIfExists.mdx diff --git a/docs/casted/castArray.mdx b/docs/casted/castArray.mdx new file mode 100644 index 00000000..e330644f --- /dev/null +++ b/docs/casted/castArray.mdx @@ -0,0 +1,16 @@ +--- +title: castArray +description: Cast a value into an array +--- + +### Usage + +The `castArray` function ensures that the input value is always returned as an array. If the input is already an array, it returns a shallow copy of the array. If the input is not an array, it wraps the input in a new array. + +```ts +import * as _ from 'radashi' + +_.castArray(1) // => [1] +_.castArray([1, 2, 3]) // => [1, 2, 3] +_.castArray('hello') // => ['hello'] +``` diff --git a/docs/casted/castArrayIfExists.mdx b/docs/casted/castArrayIfExists.mdx new file mode 100644 index 00000000..af0075ab --- /dev/null +++ b/docs/casted/castArrayIfExists.mdx @@ -0,0 +1,18 @@ +--- +title: castArrayIfExists +description: Cast a non-nullish value into an array +--- + +### Usage + +The `castArrayIfExists` function ensures that a non-nullish input value is always returned as an array. If the input is already an array, it returns a shallow copy of the array. If the input is not an array, it wraps the input in a new array. Nullish values (null or undefined) are passed through as is. + +```ts +import * as _ from 'radashi' + +_.castArrayIfExists(1) // => [1] +_.castArrayIfExists([1, 2, 3]) // => [1, 2, 3] +_.castArrayIfExists('hello') // => ['hello'] +_.castArrayIfExists(null) // => null +_.castArrayIfExists(undefined) // => undefined +``` From 9afb01287384fdac558f1d0f0ea79165e580550e Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sat, 13 Jul 2024 17:27:53 -0400 Subject: [PATCH 08/10] add tests for castArrayIfExists and fix it --- src/casted/castArrayIfExists.ts | 51 +++++++++++++----- tests/casted/castArrayIfExists.test-d.ts | 66 ++++++++++++++++++++++++ tests/casted/castArrayIfExists.test.ts | 34 ++++++++++++ 3 files changed, 137 insertions(+), 14 deletions(-) create mode 100644 tests/casted/castArrayIfExists.test-d.ts create mode 100644 tests/casted/castArrayIfExists.test.ts diff --git a/src/casted/castArrayIfExists.ts b/src/casted/castArrayIfExists.ts index 080cf60f..8e2efd98 100644 --- a/src/casted/castArrayIfExists.ts +++ b/src/casted/castArrayIfExists.ts @@ -1,15 +1,38 @@ -export function castArrayIfExists( - value: T | readonly T[], -): T extends any[] - ? T[number][] - : T extends readonly any[] - ? readonly T[number][] - : Exclude[] | undefined - -export function castArrayIfExists(value: T | readonly T[]): any { - return Array.isArray(value) - ? value.slice() - : value != null - ? [value] - : undefined +/** + * Casts the given value to an array if it's not equal to `null` or + * `undefined`. If the value is an array, it returns a shallow copy of + * the array. Otherwise, it returns a new array containing the value. + * + * @see https://radashi-org.github.io/reference/casted/castArrayIfExists + * @example + * ```ts + * castArrayIfExists(1) // => [1] + * castArrayIfExists(null) // => null + * castArrayIfExists(undefined) // => undefined + * castArrayIfExists([1, 2, 3]) // => [1, 2, 3] + * ``` + */ +export function castArrayIfExists(value: T): CastArrayIfExists +export function castArrayIfExists(value: unknown): unknown { + return Array.isArray(value) ? value.slice() : value != null ? [value] : value } + +/** + * The return type of the {@link castArrayIfExists} function. + * + * @see https://radashi-org.github.io/reference/casted/castArrayIfExists + */ +export type CastArrayIfExists = [T] extends [never] + ? never[] + : [unknown] extends [T] + ? unknown[] | null | undefined + : + | (T extends any + ? T extends readonly (infer U)[] + ? U[] + : never + : never) + | (Exclude extends never + ? never + : Exclude[]) + | Extract diff --git a/tests/casted/castArrayIfExists.test-d.ts b/tests/casted/castArrayIfExists.test-d.ts new file mode 100644 index 00000000..c6941093 --- /dev/null +++ b/tests/casted/castArrayIfExists.test-d.ts @@ -0,0 +1,66 @@ +import * as _ from 'radashi' + +describe('castArrayIfExists', () => { + test('mutable arrays', () => { + const input: number[] = [1, 2, 3] + const output = _.castArrayIfExists(input) + + expectTypeOf(output).toEqualTypeOf() + }) + test('readonly arrays', () => { + const input: readonly number[] = [1, 2, 3] + const output = _.castArrayIfExists(input) + + // castArrayIfExists clones the input array, so "readonly" is removed. + expectTypeOf(output).toEqualTypeOf() + }) + test('added properties are lost', () => { + const input = [] as number[] & { foo?: boolean } + const output = _.castArrayIfExists(input) + + expectTypeOf(output).toEqualTypeOf() + }) + test('union type with array type and primitive type', () => { + const input = 1 as number[] | number + const output = _.castArrayIfExists(input) + + expectTypeOf(output).toEqualTypeOf() + }) + test('union type with two primitive types', () => { + const input = 1 as number | string + const output = _.castArrayIfExists(input) + + expectTypeOf(output).toEqualTypeOf<(number | string)[]>() + }) + test('union type with two array types', () => { + const input = [1, 2, 3] as number[] | readonly string[] + const output = _.castArrayIfExists(input) + + expectTypeOf(output).toEqualTypeOf() + }) + test('primitive types', () => { + expectTypeOf(_.castArrayIfExists(1)).toEqualTypeOf() + expectTypeOf(_.castArrayIfExists('a')).toEqualTypeOf() + expectTypeOf(_.castArrayIfExists(true)).toEqualTypeOf() + expectTypeOf(_.castArrayIfExists(null)).toEqualTypeOf() + expectTypeOf(_.castArrayIfExists(undefined)).toEqualTypeOf() + }) + test('never type', () => { + const input = 1 as never + const output = _.castArrayIfExists(input) + + expectTypeOf(output).toEqualTypeOf() + }) + test('any type', () => { + const input = 1 as any + const output = _.castArrayIfExists(input) + + expectTypeOf(output).toEqualTypeOf() + }) + test('unknown type', () => { + const input = 1 as unknown + const output = _.castArrayIfExists(input) + + expectTypeOf(output).toEqualTypeOf() + }) +}) diff --git a/tests/casted/castArrayIfExists.test.ts b/tests/casted/castArrayIfExists.test.ts new file mode 100644 index 00000000..f93c9169 --- /dev/null +++ b/tests/casted/castArrayIfExists.test.ts @@ -0,0 +1,34 @@ +import * as _ from 'radashi' + +describe('castArrayIfExists', () => { + test('return a shallow copy if input is an array', () => { + const input = [1, 2, 3] + expect(_.castArrayIfExists(input)).toEqual(input) + expect(_.castArrayIfExists(input)).not.toBe(input) + }) + test('return a new array with the same elements if input is an array', () => { + const input = [1, 2, 3] + const result = _.castArrayIfExists(input) + expect(result).toEqual(input) + expect(result).not.toBe(input) // Ensure it's a copy + }) + test('return an array with the input if input is not an array', () => { + const input = 1 + expect(_.castArrayIfExists(input)).toEqual([input]) + }) + test('handle nullish values', () => { + expect(_.castArrayIfExists(null)).toEqual(null) + expect(_.castArrayIfExists(undefined)).toEqual(undefined) + }) + test('handle primitives', () => { + expect(_.castArrayIfExists(1)).toEqual([1]) + expect(_.castArrayIfExists('a')).toEqual(['a']) + expect(_.castArrayIfExists(true)).toEqual([true]) + }) + test('handle objects and functions', () => { + const obj = { a: 1 } + const func = () => {} + expect(_.castArrayIfExists(obj)).toEqual([obj]) + expect(_.castArrayIfExists(func)).toEqual([func]) + }) +}) From 0bb4f54c113debaf3629f1bf0ac93fd667f1b62c Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sat, 13 Jul 2024 17:29:26 -0400 Subject: [PATCH 09/10] more type tests for castArray and fix it --- src/casted/castArray.ts | 40 ++++++++++++++++++++++++---- tests/casted/castArray.test-d.ts | 45 +++++++++++++++++++++++++++----- tests/casted/castArray.test.ts | 3 ++- 3 files changed, 75 insertions(+), 13 deletions(-) diff --git a/src/casted/castArray.ts b/src/casted/castArray.ts index 64cbe689..3edbdbf9 100644 --- a/src/casted/castArray.ts +++ b/src/casted/castArray.ts @@ -1,7 +1,37 @@ -export function castArray( - value: T, -): T extends readonly (infer U)[] ? U[] : [T] - -export function castArray(value: T | readonly T[]): any { +/** + * Casts the given value to an array. If the value is already an + * array, a shallow copy is returned. Otherwise, a new array + * containing the value is returned. + * + * @see https://radashi-org.github.io/reference/casted/castArray + * @example + * ```ts + * castArray(1) // => [1] + * castArray([1, 2]) // => [1, 2] + * castArray(null) // => [null] + * castArray(undefined) // => [undefined] + * ``` + */ +export function castArray(value: T): CastArray +export function castArray(value: unknown): unknown { return Array.isArray(value) ? value.slice() : [value] } + +/** + * The return type of the {@link castArray} function. + * + * @see https://radashi-org.github.io/reference/casted/castArray + */ +export type CastArray = [T] extends [never] + ? never[] + : [unknown] extends [T] + ? unknown[] + : + | (T extends any + ? T extends readonly (infer U)[] + ? U[] + : never + : never) + | (Exclude extends never + ? never + : Exclude[]) diff --git a/tests/casted/castArray.test-d.ts b/tests/casted/castArray.test-d.ts index 01950f0d..04e8f1f0 100644 --- a/tests/casted/castArray.test-d.ts +++ b/tests/casted/castArray.test-d.ts @@ -7,15 +7,28 @@ describe('castArray', () => { }) test('readonly arrays', () => { const input: readonly number[] = [1, 2, 3] + const output = _.castArray(input) // castArray clones the input array, so "readonly" is removed. - expectTypeOf(_.castArray(input)).toEqualTypeOf() + expectTypeOf(output).toEqualTypeOf() + }) + test('added properties are lost', () => { + const input = [] as number[] & { foo?: boolean } + const output = _.castArray(input) + + expectTypeOf(output).toEqualTypeOf() }) test('union type with array type and primitive type', () => { const input = 1 as number[] | number const output = _.castArray(input) - expectTypeOf(output).toEqualTypeOf() + expectTypeOf(output).toEqualTypeOf() + }) + test('union type with two primitive types', () => { + const input = 1 as number | string + const output = _.castArray(input) + + expectTypeOf(output).toEqualTypeOf<(number | string)[]>() }) test('union type with two array types', () => { const input = [1, 2, 3] as number[] | readonly string[] @@ -24,10 +37,28 @@ describe('castArray', () => { expectTypeOf(output).toEqualTypeOf() }) test('primitive types', () => { - expectTypeOf(_.castArray(1)).toEqualTypeOf<[number]>() - expectTypeOf(_.castArray('a')).toEqualTypeOf<[string]>() - expectTypeOf(_.castArray(true)).toEqualTypeOf<[true] | [false]>() - expectTypeOf(_.castArray(null)).toEqualTypeOf<[null]>() - expectTypeOf(_.castArray(undefined)).toEqualTypeOf<[undefined]>() + expectTypeOf(_.castArray(1)).toEqualTypeOf() + expectTypeOf(_.castArray('a')).toEqualTypeOf() + expectTypeOf(_.castArray(true)).toEqualTypeOf() + expectTypeOf(_.castArray(null)).toEqualTypeOf() + expectTypeOf(_.castArray(undefined)).toEqualTypeOf() + }) + test('never type', () => { + const input = 1 as never + const output = _.castArray(input) + + expectTypeOf(output).toEqualTypeOf() + }) + test('any type', () => { + const input = 1 as any + const output = _.castArray(input) + + expectTypeOf(output).toEqualTypeOf() + }) + test('unknown type', () => { + const input = 1 as unknown + const output = _.castArray(input) + + expectTypeOf(output).toEqualTypeOf() }) }) diff --git a/tests/casted/castArray.test.ts b/tests/casted/castArray.test.ts index 73c10298..8e8699a5 100644 --- a/tests/casted/castArray.test.ts +++ b/tests/casted/castArray.test.ts @@ -1,9 +1,10 @@ import * as _ from 'radashi' describe('castArray', () => { - test('return the same array if input is an array', () => { + test('return a shallow copy if input is an array', () => { const input = [1, 2, 3] expect(_.castArray(input)).toEqual(input) + expect(_.castArray(input)).not.toBe(input) }) test('return a new array with the same elements if input is an array', () => { const input = [1, 2, 3] From 9f9c804b1dd0ef079a1ecef8cd2c53b38da2665f Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sat, 13 Jul 2024 17:30:40 -0400 Subject: [PATCH 10/10] add benchmarks --- benchmarks/casted/castArray.bench.ts | 14 ++++++++++++++ benchmarks/casted/castArrayIfExists.bench.ts | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 benchmarks/casted/castArray.bench.ts create mode 100644 benchmarks/casted/castArrayIfExists.bench.ts diff --git a/benchmarks/casted/castArray.bench.ts b/benchmarks/casted/castArray.bench.ts new file mode 100644 index 00000000..744c32d5 --- /dev/null +++ b/benchmarks/casted/castArray.bench.ts @@ -0,0 +1,14 @@ +import * as _ from 'radashi' +import { bench } from 'vitest' + +describe('castArray', () => { + bench('with an array', () => { + _.castArray(new Array(100)) + }) + bench('with number', () => { + _.castArray(1) + }) + bench('with null', () => { + _.castArray(null) + }) +}) diff --git a/benchmarks/casted/castArrayIfExists.bench.ts b/benchmarks/casted/castArrayIfExists.bench.ts new file mode 100644 index 00000000..7da35fb3 --- /dev/null +++ b/benchmarks/casted/castArrayIfExists.bench.ts @@ -0,0 +1,14 @@ +import * as _ from 'radashi' +import { bench } from 'vitest' + +describe('castArrayIfExists', () => { + bench('with an array', () => { + _.castArrayIfExists(new Array(100)) + }) + bench('with number', () => { + _.castArrayIfExists(1) + }) + bench('with null', () => { + _.castArrayIfExists(null) + }) +})