This file contains hidden or 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
import { expect, it } from "vitest"; | |
import { LRUCache } from "./lruCache"; | |
it("should move entry to end of map on access", () => { | |
const cache = new LRUCache(); | |
cache.set("a", 1).set("b", 2).set("c", 3); | |
expect([...cache.entries()]).toEqual([ |
This file contains hidden or 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
import { expect, it } from "vitest"; | |
import { partition } from "./partition"; | |
it("should divide array by provided filters", () => { | |
expect( | |
partition( | |
[1, "2", undefined, "test", {}], | |
(item) => typeof item === "number", | |
(item) => typeof item === "string" |
This file contains hidden or 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
import { expect, it } from "vitest"; | |
import { sort } from "./sort"; | |
it("should sort based on multiple comparators", () => { | |
const users = [ | |
{ name: "Alice", age: 39, active: true }, | |
{ name: "Caroline", age: 25, active: false }, | |
{ name: "Alice", age: 38, active: false }, | |
{ name: "Bob", age: 39, active: true }, |
This file contains hidden or 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
/** | |
* Takes array of items and filter and\ | |
* mapping functions to transform the\ | |
* array in a single traversal. | |
* @param items array of items | |
* @param filter narrowing function | |
* @param mapper mapping function | |
* @returns function to filter and map items | |
* @example | |
* const isDefined = (n: number | undefined) => n !== undefined; |
This file contains hidden or 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
/** | |
* An optimized version of Promise.all + map. | |
* When needing to map results of Promise.all | |
* we need to wait for all promised to resolve | |
* and then mapping from start to end can | |
* begin. There is no need to wait for all | |
* promises to resolve. Instead we can map each | |
* promise's result as soon as it has resolved. | |
* @param promises array of promises to map | |
* @param mapper mapping to apply to each resolved promise |
This file contains hidden or 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
type Options = { | |
timeout?: number; | |
interval?: number; | |
signal?: AbortSignal; | |
}; | |
type PromiseExecutor<T> = ( | |
resolve: (value: T) => void, | |
reject: (reason?: any) => void | |
) => void; |
This file contains hidden or 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
/** | |
* Initializes a mutation observer instance on | |
* document.body (by default) that executes the | |
* provided callback as soon as the target selector | |
* is detected. | |
* @param {String} selector target CSS selector | |
* @param {Function} callback function to execute | |
* @param {Object} options | |
* @param {HTMLElement} options.observeTarget parent node to observe | |
* @param {Boolean} options.passAllElements whether callback gets first or all elements |
This file contains hidden or 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
// fn: T -> K | |
type MapFn<T,K> = { | |
(param: T): K; | |
} | |
// fn: T -> T | |
type IsoFn<T> = MapFn<T,T>; | |
// Example | |
interface User { |
This file contains hidden or 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
// @example: | |
// interface ParseFn { | |
// (s: string): number; | |
// } | |
// | |
// const parseServerResponse: Async<ParseFn> = ... | |
type Async<T extends (...args: any) => any> = | |
(...args: Parameters<T>) => Promise<ReturnType<T>>; | |
This file contains hidden or 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
type IsoFn<T> = { | |
(param: T): T; | |
} |
NewerOlder