This release is 2 versions behind 0.0.5 — the latest version of @thai/get-or-create. Jump to latest
@thai/get-or-create@0.0.3Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
Convenience function to get or create a value in a map
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers




JSR Score
100%
Published
2 years ago (0.0.3)
The @thai/get-or-create package provides the getOrCreate function that can be used to get or create a value in a map.
Usage
import { getOrCreate } from "@thai/get-or-create"; // Let's calculate a tally for each word in this array. const words = ["one", "two", "three", "one"]; // We'll use a map to store the tally. const tally = new Map<string, { count: number }>(); // We can use the `getOrCreate` function to get or create // the counter object for each word. for (const word of words) { getOrCreate(tally, word, () => ({ count: 0 })).count++; } // The tally map now contains the count for each word: // // Map(3) { // 'one' => { count: 2 }, // 'two' => { count: 1 }, // 'three' => { count: 1 } // }
Built and signed on
GitHub Actions
Add Package
deno add jsr:@thai/get-or-create
Import symbol
import * as get_or_create from "@thai/get-or-create";
Import directly with a jsr specifier
import * as get_or_create from "jsr:@thai/get-or-create";
Add Package
pnpm i jsr:@thai/get-or-create
pnpm dlx jsr add @thai/get-or-create
Import symbol
import * as get_or_create from "@thai/get-or-create";
Add Package
yarn add jsr:@thai/get-or-create
yarn dlx jsr add @thai/get-or-create
Import symbol
import * as get_or_create from "@thai/get-or-create";
Add Package
vlt install jsr:@thai/get-or-create
Import symbol
import * as get_or_create from "@thai/get-or-create";
Add Package
npx jsr add @thai/get-or-create
Import symbol
import * as get_or_create from "@thai/get-or-create";
Add Package
bunx jsr add @thai/get-or-create
Import symbol
import * as get_or_create from "@thai/get-or-create";