Skip to content

Commit

Permalink
fix: code review notes
Browse files Browse the repository at this point in the history
  • Loading branch information
yamcodes committed Sep 13, 2024
1 parent 64e0b6b commit d0fafa8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 49 deletions.
8 changes: 2 additions & 6 deletions benchmarks/array/cartesianProduct.bench.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import * as _ from 'radashi'

describe('cartesianProduct', () => {
bench('with no arguments', () => {
_.cartesianProduct()
})

bench('with single empty array', () => {
bench('with an empty array (n=1)', () => {
_.cartesianProduct([])
})

bench('with one non-empty array (n=1)', () => {
bench('with a non-empty array (n=1)', () => {
_.cartesianProduct(['a', 'b', 'c'])
})

Expand Down
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export * from './array/unique.ts'
export * from './array/unzip.ts'
export * from './array/zip.ts'
export * from './array/zipToObject.ts'

export * from './async/AggregateError.ts'
export * from './async/all.ts'
export * from './async/defer.ts'
Expand Down
44 changes: 44 additions & 0 deletions tests/array/cartesianProduct.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as _ from 'radashi'

describe('cartesianProduct return type', () => {
test('with an empty array', () => {
const result = _.cartesianProduct([])
expectTypeOf(result).toEqualTypeOf<[never][]>()
})
test('with two arrays of the same type', () => {
const result = _.cartesianProduct(['red', 'blue'], ['fast', 'slow'])
const [[v1, v2]] = result
expectTypeOf(result).toEqualTypeOf<[string, string][]>()
expectTypeOf(v1).toEqualTypeOf<string>()
expectTypeOf(v2).toEqualTypeOf<string>()
})
test('with two arrays of different types', () => {
const result = _.cartesianProduct(['red', 'blue'], [1, 2])
const [[v1, v2]] = result
expectTypeOf(result).toEqualTypeOf<[string, number][]>()
expectTypeOf(v1).toEqualTypeOf<string>()
expectTypeOf(v2).toEqualTypeOf<number>()
})
test('with three arrays of different types', () => {
const result = _.cartesianProduct(['red', 'blue'], [1, 2], [true, false])
const [[v1, v2, v3]] = result
expectTypeOf(result).toEqualTypeOf<[string, number, boolean][]>()
expectTypeOf(v1).toEqualTypeOf<string>()
expectTypeOf(v2).toEqualTypeOf<number>()
expectTypeOf(v3).toEqualTypeOf<boolean>()
})
test('with constant arrays of different types', () => {
const result = _.cartesianProduct(['red', 'blue'] as const, [1, 2] as const)
const [[v1, v2]] = result
expectTypeOf(result).toEqualTypeOf<['red' | 'blue', 1 | 2][]>()
expectTypeOf(v1).toEqualTypeOf<'red' | 'blue'>()
expectTypeOf(v2).toEqualTypeOf<1 | 2>()
})
test('with a mix of constant and non-constant types', () => {
const result = _.cartesianProduct(['red', 'blue'], [1, 2] as const)
const [[v1, v2]] = result
expectTypeOf(result).toEqualTypeOf<[string, 1 | 2][]>()
expectTypeOf(v1).toEqualTypeOf<string>()
expectTypeOf(v2).toEqualTypeOf<1 | 2>()
})
})
43 changes: 0 additions & 43 deletions tests/array/cartesianProduct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,46 +45,3 @@ describe('cartesianProduct', () => {
])
})
})

describe('cartesianProduct types', () => {
test('with an empty array', () => {
const result = _.cartesianProduct([])
expectTypeOf(result).toEqualTypeOf<[never][]>()
})
test('with two arrays of the same type', () => {
const result = _.cartesianProduct(['red', 'blue'], ['fast', 'slow'])
const [[v1, v2]] = result
expectTypeOf(result).toEqualTypeOf<[string, string][]>()
expectTypeOf(v1).toEqualTypeOf<string>()
expectTypeOf(v2).toEqualTypeOf<string>()
})
test('with two arrays of different types', () => {
const result = _.cartesianProduct(['red', 'blue'], [1, 2])
const [[v1, v2]] = result
expectTypeOf(result).toEqualTypeOf<[string, number][]>()
expectTypeOf(v1).toEqualTypeOf<string>()
expectTypeOf(v2).toEqualTypeOf<number>()
})
test('with three arrays of different types', () => {
const result = _.cartesianProduct(['red', 'blue'], [1, 2], [true, false])
const [[v1, v2, v3]] = result
expectTypeOf(result).toEqualTypeOf<[string, number, boolean][]>()
expectTypeOf(v1).toEqualTypeOf<string>()
expectTypeOf(v2).toEqualTypeOf<number>()
expectTypeOf(v3).toEqualTypeOf<boolean>()
})
test('with constant arrays of different types', () => {
const result = _.cartesianProduct(['red', 'blue'] as const, [1, 2] as const)
const [[v1, v2]] = result
expectTypeOf(result).toEqualTypeOf<['red' | 'blue', 1 | 2][]>()
expectTypeOf(v1).toEqualTypeOf<'red' | 'blue'>()
expectTypeOf(v2).toEqualTypeOf<1 | 2>()
})
test('with a mix of constant and non-constant types', () => {
const result = _.cartesianProduct(['red', 'blue'], [1, 2] as const)
const [[v1, v2]] = result
expectTypeOf(result).toEqualTypeOf<[string, 1 | 2][]>()
expectTypeOf(v1).toEqualTypeOf<string>()
expectTypeOf(v2).toEqualTypeOf<1 | 2>()
})
})

0 comments on commit d0fafa8

Please sign in to comment.