-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update utility functions and add tests
- Loading branch information
Showing
13 changed files
with
239 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Returns an array containing the elements from `arr1` that are not present in `arr2`. | ||
* | ||
* @template T - The type of elements in the arrays. | ||
* @param {T[]} arr1 - The first array. | ||
* @param {T[]} arr2 - The second array. | ||
* @returns {T[]} - The difference between `arr1` and `arr2`. | ||
*/ | ||
export function diff<T>(arr1: T[], arr2: T[]): T[] { | ||
return arr1.filter((value) => !arr2.includes(value)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Returns a new array that contains unique elements from both input arrays. | ||
* | ||
* @template T - The type of elements in the arrays. | ||
* @param arr1 - The first input array. | ||
* @param arr2 - The second input array. | ||
* @returns A new array that contains unique elements from both input arrays. | ||
*/ | ||
export function union<T>(arr1: T[], arr2: T[]): T[] { | ||
return [...new Set([...arr1, ...arr2])]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Zips two arrays together, creating an array of tuples where each tuple contains | ||
* corresponding elements from both input arrays. | ||
* | ||
* @template T - The type of elements in the arrays. | ||
* @param {T[]} arr1 - The first input array. | ||
* @param {T[]} arr2 - The second input array. | ||
* @returns {[T, T][]} - An array of tuples containing corresponding elements from both input arrays. | ||
*/ | ||
export function zip<T>(arr1: T[], arr2: T[]): [T, T][] { | ||
const minLength = Math.min(arr1.length, arr2.length); | ||
return arr1.slice(0, minLength).map((value, index) => [value, arr2[index]]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Retries a given function a specified number of times if it fails. | ||
* @param fn - The function to be retried. | ||
* @param retries - The number of times to retry the function. | ||
* @returns A promise that resolves with the result of the function if it succeeds, or rejects with the last error if it fails after all retries. | ||
*/ | ||
export function retry<T>(fn: () => Promise<T>, retries: number): Promise<T> { | ||
return fn().catch((error) => { | ||
if (retries > 0) { | ||
return retry(fn, retries - 1); | ||
} | ||
throw error; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { expect, describe, it } from "bun:test"; | ||
import { diff } from "../../src/array/diff"; | ||
|
||
describe("diff", () => { | ||
it("should return the difference between two arrays", () => { | ||
const arr1 = [1, 2, 3, 4, 5]; | ||
const arr2 = [3, 4, 5, 6, 7]; | ||
const result = diff(arr1, arr2); | ||
expect(result).toEqual([1, 2]); | ||
}); | ||
|
||
it("should return an empty array if both arrays are identical", () => { | ||
const arr1 = [1, 2, 3]; | ||
const arr2 = [1, 2, 3]; | ||
const result = diff(arr1, arr2); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("should return the first array if the second array is empty", () => { | ||
const arr1 = [1, 2, 3]; | ||
const arr2: number[] = []; | ||
const result = diff(arr1, arr2); | ||
expect(result).toEqual(arr1); | ||
}); | ||
|
||
it("should return an empty array if the first array is empty", () => { | ||
const arr1: number[] = []; | ||
const arr2 = [1, 2, 3]; | ||
const result = diff(arr1, arr2); | ||
expect(result).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { expect, describe, it } from "bun:test"; | ||
import { union } from "../../src/array/union"; | ||
|
||
describe("union", () => { | ||
it("should return the union of two arrays", () => { | ||
const arr1 = [1, 2, 3, 4, 5]; | ||
const arr2 = [3, 4, 5, 6, 7]; | ||
const result = union(arr1, arr2); | ||
expect(result).toEqual([1, 2, 3, 4, 5, 6, 7]); | ||
}); | ||
|
||
it("should return the first array if the second array is empty", () => { | ||
const arr1 = [1, 2, 3]; | ||
const arr2: number[] = []; | ||
const result = union(arr1, arr2); | ||
expect(result).toEqual(arr1); | ||
}); | ||
|
||
it("should return the second array if the first array is empty", () => { | ||
const arr1: number[] = []; | ||
const arr2 = [1, 2, 3]; | ||
const result = union(arr1, arr2); | ||
expect(result).toEqual(arr2); | ||
}); | ||
|
||
it("should return an empty array if both arrays are empty", () => { | ||
const arr1: number[] = []; | ||
const arr2: number[] = []; | ||
const result = union(arr1, arr2); | ||
expect(result).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { expect, describe, it } from "bun:test"; | ||
import { zip } from "../../src/array/zip"; | ||
|
||
describe("zip", () => { | ||
it("should return an array of tuples with corresponding elements from two arrays", () => { | ||
const arr1: number[] = [1, 2, 3]; | ||
const arr2: number[] = [4, 5, 6]; | ||
const result = zip(arr1, arr2); | ||
expect(result).toEqual([ | ||
[1, 4], | ||
[2, 5], | ||
[3, 6], | ||
]); | ||
}); | ||
|
||
it("should return an empty array if either of the input arrays is empty", () => { | ||
const arr1: number[] = []; | ||
const arr2: number[] = [4, 5, 6]; | ||
const result = zip(arr1, arr2); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("should return an empty array if both input arrays are empty", () => { | ||
const arr1: number[] = []; | ||
const arr2: number[] = []; | ||
const result = zip(arr1, arr2); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("should truncate the longer array if the input arrays have different lengths", () => { | ||
const arr1: number[] = [1, 2, 3]; | ||
const arr2: number[] = [4, 5]; | ||
const result = zip(arr1, arr2); | ||
expect(result).toEqual([ | ||
[1, 4], | ||
[2, 5], | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { expect, describe, it, jest } from "bun:test"; | ||
import { retry } from "../../src/function/retry"; | ||
|
||
describe("retry", () => { | ||
it("should resolve with the result of the function if it succeeds on the first try", async () => { | ||
const fn = jest.fn().mockResolvedValue("success"); | ||
const result = await retry(fn, 3); | ||
expect(result).toBe("success"); | ||
expect(fn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should resolve with the result of the function if it succeeds after multiple retries", async () => { | ||
const fn = jest.fn().mockRejectedValueOnce(new Error("error 1")).mockRejectedValueOnce(new Error("error 2")).mockResolvedValue("success"); | ||
const result = await retry(fn, 3); | ||
expect(result).toBe("success"); | ||
expect(fn).toHaveBeenCalledTimes(3); | ||
}); | ||
}); |