Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions _internal-tools/check-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ async function getTypescriptFiles(directory: string): Promise<string[]> {
if (!entry.name.endsWith('.ts')) continue
if (entry.name === BARREL_FILE_NAME) continue
if (entry.name.endsWith('.test.ts')) continue
if (entry.name.endsWith('.fixture.ts')) continue
if (entry.name.endsWith('.bench.ts')) continue
expected.push(entry.name)
}
expected.sort()
Expand Down
6 changes: 6 additions & 0 deletions array/collection-key-by.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { collectionKeyBy } from './collection-key-by.ts'
import { collectionKeyByFixture } from './collection-key-by.fixture.ts'

Deno.bench('collectionKeyBy', () => {
collectionKeyBy(collectionKeyByFixture, 'id')
})
5 changes: 5 additions & 0 deletions array/collection-key-by.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const collectionKeyByFixture = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' },
]
7 changes: 2 additions & 5 deletions array/collection-key-by.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertEquals } from '@std/assert'
import { collectionKeyBy } from './collection-key-by.ts'
import { collectionKeyByFixture } from './collection-key-by.fixture.ts'

const userCollection = {
'1': { id: 1, name: 'Alice' },
Expand All @@ -11,12 +12,8 @@ Deno.test('collectionKeyBy', async (t) => {
await t.step(
'should transform a collection of objects into an object keyed by a specified key',
() => {
const collection = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, {
id: 3,
name: 'Charlie',
}]
const key = 'id'
assertEquals(collectionKeyBy(collection, key), userCollection)
assertEquals(collectionKeyBy(collectionKeyByFixture, key), userCollection)
},
)

Expand Down
9 changes: 9 additions & 0 deletions array/common-elements.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { commonElements } from './common-elements.ts'
import {
commonElementsFixtureA,
commonElementsFixtureB,
} from './common-elements.fixture.ts'

Deno.bench('commonElements', () => {
commonElements(commonElementsFixtureA, commonElementsFixtureB)

Check warning on line 8 in array/common-elements.bench.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

array/common-elements.bench.ts#L8

'commonElements' is deprecated. Deprecated and scheduled for removal in v6, native Set operations should be preferred. Migration (native): ```typescript import { assertEquals } from '@std/assert' const leftArray = [1, 2, 3] const rightArray = [2, 3, 4] const result = [...new Set(leftArray).intersection(new Set(rightArray))] assertEquals(result, [2, 3]) ``` API availability: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/intersection#browser_compatibility
})
2 changes: 2 additions & 0 deletions array/common-elements.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const commonElementsFixtureA = [1, 2, 3] as const
export const commonElementsFixtureB = [2, 3, 4] as const
8 changes: 6 additions & 2 deletions array/common-elements.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { assertEquals } from '@std/assert'
import { commonElements } from './common-elements.ts'
import {
commonElementsFixtureA,
commonElementsFixtureB,
} from './common-elements.fixture.ts'

const array1 = [1, 2, 3] as const
const array2 = [2, 3, 4] as const
const array1 = commonElementsFixtureA
const array2 = commonElementsFixtureB
const emptyArray: number[] = [] as const
const singleElement1 = [1] as const
const singleElement2 = [2] as const
Expand Down
6 changes: 6 additions & 0 deletions array/count-by.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { countBy } from './count-by.ts'
import { countByFixture } from './count-by.fixture.ts'

Deno.bench('countBy', () => {
countBy(countByFixture, (n: number) => n % 2 === 0)
})
1 change: 1 addition & 0 deletions array/count-by.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const countByFixture = [1, 2, 3, 4, 5]
4 changes: 2 additions & 2 deletions array/count-by.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { assertEquals } from '@std/assert'
import { countBy } from './count-by.ts'
import { countByFixture } from './count-by.fixture.ts'

Deno.test('countBy', async (t) => {
const isEven = (number_: number): boolean => number_ % 2 === 0

await t.step(
'should count the number of elements that satisfy a condition',
() => {
const array = [1, 2, 3, 4, 5]
assertEquals(countBy(array, isEven), 2)
assertEquals(countBy(countByFixture, isEven), 2)
},
)

Expand Down
10 changes: 10 additions & 0 deletions array/filter-by-date.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { filterByDate } from './filter-by-date.ts'
import { filterByDateFixture } from './filter-by-date.fixture.ts'

Deno.bench('filterByDate', () => {
filterByDate({
array: filterByDateFixture,
keyOrFunction: 'date',
options: { year: 2021 },
})
})
9 changes: 9 additions & 0 deletions array/filter-by-date.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const filterByDate1 = { date: new Date(2020, 6, 1) }
export const filterByDate2 = { date: new Date(2021, 6, 1) }
export const filterByDate3 = { date: new Date(2022, 6, 1) }

export const filterByDateFixture = [
filterByDate1,
filterByDate2,
filterByDate3,
]
14 changes: 10 additions & 4 deletions array/filter-by-date.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { assert, assertEquals } from '@std/assert'
import { filterByDate } from './filter-by-date.ts'
import {
filterByDate1,
filterByDate2,
filterByDate3,
filterByDateFixture,
} from './filter-by-date.fixture.ts'

Deno.test(
'filterByDate',
async (t) => {
const date1 = { date: new Date(2020, 6, 1) } // July 1, 2020
const date2 = { date: new Date(2021, 6, 1) } // July 1, 2021
const date3 = { date: new Date(2022, 6, 1) } // July 1, 2022
const dates = [date1, date2, date3]
const date1 = filterByDate1
const date2 = filterByDate2

Check warning on line 14 in array/filter-by-date.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

array/filter-by-date.test.ts#L14

'date2' is assigned a value but never used.
const date3 = filterByDate3

Check warning on line 15 in array/filter-by-date.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

array/filter-by-date.test.ts#L15

'date3' is assigned a value but never used.
const dates = filterByDateFixture

await t.step('should filter an array of objects by a year', () => {
const { data: filtered, error } = filterByDate(
Expand Down
6 changes: 6 additions & 0 deletions array/filter-by.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { filterBy } from './filter-by.ts'
import { filterByFixture } from './filter-by.fixture.ts'

Deno.bench('filterBy', () => {
filterBy({ array: filterByFixture, keyOrFunction: 'category', value: 'a' })
})
5 changes: 5 additions & 0 deletions array/filter-by.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const filterByFixture = [
{ id: 1, category: 'a', name: 'First' },
{ id: 2, category: 'b', name: 'Second' },
{ id: 3, category: 'a', name: 'Third' },
]
9 changes: 7 additions & 2 deletions array/filter-by.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertEquals } from '@std/assert'
import { filterBy } from './filter-by.ts'
import { filterByFixture } from './filter-by.fixture.ts'

Deno.test('filterBy', async (t) => {
const array = [
Expand All @@ -11,7 +12,11 @@ Deno.test('filterBy', async (t) => {
]

await t.step('filters by key/value returning all matches', () => {
const result = filterBy({ array, keyOrFunction: 'category', value: 'a' })
const result = filterBy({
array: filterByFixture,
keyOrFunction: 'category',
value: 'a',
})
assertEquals(result, [
{ id: 1, category: 'a', name: 'First' },
{ id: 3, category: 'a', name: 'Third' },
Expand All @@ -22,7 +27,7 @@ Deno.test('filterBy', async (t) => {
'returns empty array when no objects match the key/value',
() => {
const result = filterBy({
array,
array: filterByFixture,
keyOrFunction: 'category',
value: 'z',
})
Expand Down
11 changes: 11 additions & 0 deletions array/find-and-update.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { findAndUpdate } from './find-and-update.ts'
import { findAndUpdateFixture } from './find-and-update.fixture.ts'

Deno.bench('findAndUpdate', () => {
findAndUpdate({
array: findAndUpdateFixture,
key: 'id',
value: 1,
updates: { name: 'Updated' },
})
})
4 changes: 4 additions & 0 deletions array/find-and-update.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const findAndUpdateFixture = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
]
9 changes: 4 additions & 5 deletions array/find-and-update.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { assertEquals } from '@std/assert'
import { findAndUpdate } from './find-and-update.ts'
import { findAndUpdateFixture } from './find-and-update.fixture.ts'

Deno.test('findAndUpdate', async (t) => {
await t.step('should update an object in an array', () => {
const array = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
const result = findAndUpdate({
array,
array: findAndUpdateFixture,
key: 'id',
value: 1,
updates: { name: 'Updated' },
Expand All @@ -14,14 +14,13 @@ Deno.test('findAndUpdate', async (t) => {
})

await t.step('should not update an object if the key does not match', () => {
const array = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
const result = findAndUpdate({
array,
array: findAndUpdateFixture,
key: 'id',
value: 3,
updates: { name: 'Updated' },
})
assertEquals(result, array)
assertEquals(result, findAndUpdateFixture)
})

await t.step('should handle an empty array', () => {
Expand Down
6 changes: 6 additions & 0 deletions array/find-by.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { findBy } from './find-by.ts'
import { findByFixture } from './find-by.fixture.ts'

Deno.bench('findBy', () => {
findBy({ array: findByFixture, keyOrFunction: 'id', value: 1 })
})
5 changes: 5 additions & 0 deletions array/find-by.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const findByFixture = [
{ id: 1, name: 'First' },
{ id: 2, name: 'Second' },
{ id: 3, name: 'Third' },
]
7 changes: 2 additions & 5 deletions array/find-by.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { assertEquals } from '@std/assert'
import { findBy } from './find-by.ts'
import { findByFixture } from './find-by.fixture.ts'

Deno.test('findBy', async (t) => {
const array = [
{ id: 1, name: 'First' },
{ id: 2, name: 'Second' },
{ id: 3, name: 'Third' },
]
const array = findByFixture

await t.step('find by key/value (existing)', () => {
const result = findBy({ array, keyOrFunction: 'id', value: 1 })
Expand Down
6 changes: 6 additions & 0 deletions array/frequency.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { frequency } from './frequency.ts'
import { frequencyFixture } from './frequency.fixture.ts'

Deno.bench('frequency', () => {
frequency(frequencyFixture)
})
1 change: 1 addition & 0 deletions array/frequency.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const frequencyFixture = ['apple', 'banana', 'apple', 'cherry']
8 changes: 6 additions & 2 deletions array/frequency.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { assertEquals } from '@std/assert'
import { frequency } from './frequency.ts'
import { frequencyFixture } from './frequency.fixture.ts'

Deno.test('frequency', async (t) => {
await t.step(
'should calculate the frequency of each unique element in an array',
() => {
const array = ['apple', 'banana', 'apple', 'cherry']
assertEquals(frequency(array), { 'apple': 2, 'banana': 1, 'cherry': 1 })
assertEquals(frequency(frequencyFixture), {
'apple': 2,
'banana': 1,
'cherry': 1,
})
},
)

Expand Down
6 changes: 6 additions & 0 deletions array/group-by.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { groupBy } from './group-by.ts'
import { groupByFixture } from './group-by.fixture.ts'

Deno.bench('groupBy', () => {
groupBy(groupByFixture, 'id')

Check warning on line 5 in array/group-by.bench.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

array/group-by.bench.ts#L5

'groupBy' is deprecated. Deprecated and scheduled for removal in v6, native grouping should be preferred. Migration (native): ```typescript import { assertEquals } from '@std/assert' const objects = [{ id: 1, name: 'Object 1' }, { id: 2, name: 'Object 2' }, { id: 1, name: 'Object 3' }] const grouped = Object.groupBy(objects, (object_) => String(object_.id)) assertEquals(grouped, { '1': [{ id: 1, name: 'Object 1' }, { id: 1, name: 'Object 3' }], '2': [{ id: 2, name: 'Object 2' }] }) ``` API availability: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy#browser_compatibility
})
5 changes: 5 additions & 0 deletions array/group-by.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const groupByFixture = [
{ id: 1, name: 'Object 1' },
{ id: 2, name: 'Object 2' },
{ id: 1, name: 'Object 3' },
]
6 changes: 2 additions & 4 deletions array/group-by.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { assertEquals } from '@std/assert'
import { groupBy } from './group-by.ts'
import { groupByFixture } from './group-by.fixture.ts'

Deno.test('groupBy', async (t) => {
const objects = [{ id: 1, name: 'Object 1' }, { id: 2, name: 'Object 2' }, {
id: 1,
name: 'Object 3',
}]
const objects = groupByFixture

await t.step('should group objects by a specific key', () => {
const result = groupBy(objects, 'id')
Expand Down
6 changes: 6 additions & 0 deletions array/key-by.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { keyBy } from './key-by.ts'
import { keyByFixture } from './key-by.fixture.ts'

Deno.bench('keyBy', () => {
keyBy(keyByFixture, 'id')
})
5 changes: 5 additions & 0 deletions array/key-by.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const keyByFixture = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' },
]
7 changes: 2 additions & 5 deletions array/key-by.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertEquals } from '@std/assert'
import { keyBy } from './key-by.ts'
import { keyByFixture } from './key-by.fixture.ts'

const userCollection = {
'1': { id: 1, name: 'Alice' },
Expand All @@ -11,12 +12,8 @@ Deno.test('keyBy', async (t) => {
await t.step(
'should transform an array of objects into an object keyed by a specified key',
() => {
const array = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, {
id: 3,
name: 'Charlie',
}]
const key = 'id'
assertEquals(keyBy(array, key), userCollection)
assertEquals(keyBy(keyByFixture, key), userCollection)
},
)

Expand Down
6 changes: 6 additions & 0 deletions array/max-by.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { maxBy } from './max-by.ts'
import { maxByFixture } from './max-by.fixture.ts'

Deno.bench('maxBy', () => {
maxBy(maxByFixture, 'value')
})
5 changes: 5 additions & 0 deletions array/max-by.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const maxByFixture = [
{ id: 1, value: 10 },
{ id: 2, value: 5 },
{ id: 3, value: 20 },
]
6 changes: 2 additions & 4 deletions array/max-by.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { assertEquals } from '@std/assert'
import { maxBy } from './max-by.ts'
import { maxByFixture } from './max-by.fixture.ts'

Deno.test('maxBy', async (t) => {
const objects = [{ id: 1, value: 10 }, { id: 2, value: 5 }, {
id: 3,
value: 20,
}]
const objects = maxByFixture

await t.step(
'should return the object with the maximum value for a specific key',
Expand Down
9 changes: 9 additions & 0 deletions array/merge-unique.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { mergeUnique } from './merge-unique.ts'
import {
mergeUniqueFixtureA,
mergeUniqueFixtureB,
} from './merge-unique.fixture.ts'

Deno.bench('mergeUnique', () => {
mergeUnique(mergeUniqueFixtureA, mergeUniqueFixtureB)

Check warning on line 8 in array/merge-unique.bench.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

array/merge-unique.bench.ts#L8

'mergeUnique' is deprecated. Deprecated and scheduled for removal in v6, native Set operations should be preferred. Migration (native): ```typescript import { assertEquals } from '@std/assert' const array1 = [1, 2, 3] const array2 = [2, 3, 4] const result = [...new Set(array1).union(new Set(array2))] assertEquals(result, [1, 2, 3, 4]) ``` API availability: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/union#browser_compatibility
})
2 changes: 2 additions & 0 deletions array/merge-unique.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const mergeUniqueFixtureA = [1, 2, 3] as const
export const mergeUniqueFixtureB = [2, 3, 4] as const
Loading
Loading