Skip to main content
Home

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
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
a year ago (0.0.5)
function getOrCreate
getOrCreate<
K,
V,
>
(
map: MapLike<K, V>,
key: K,
createValue: (key: K) => V,
): V

Gets the value associated with the specified key from the map. If the key does not exist, creates a new value using the provided createValue function, associates it with the key in the map, and returns the new value.

Type Parameters

The type of the keys in the map.

The type of the values in the map.

Parameters

map: MapLike<K, V>

The map to get or create a value from.

key: K

The key to get or create a value for.

createValue: (key: K) => V

The function to create a new value if the key does not exist.

Return Type

The value associated with the key, or the newly created value.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@thai/get-or-create

Import symbol

import { getOrCreate } from "@thai/get-or-create";
or

Import directly with a jsr specifier

import { getOrCreate } from "jsr:@thai/get-or-create";

Add Package

pnpm i jsr:@thai/get-or-create
or (using pnpm 10.8 or older)
pnpm dlx jsr add @thai/get-or-create

Import symbol

import { getOrCreate } from "@thai/get-or-create";

Add Package

yarn add jsr:@thai/get-or-create
or (using Yarn 4.8 or older)
yarn dlx jsr add @thai/get-or-create

Import symbol

import { getOrCreate } from "@thai/get-or-create";

Add Package

vlt install jsr:@thai/get-or-create

Import symbol

import { getOrCreate } from "@thai/get-or-create";

Add Package

npx jsr add @thai/get-or-create

Import symbol

import { getOrCreate } from "@thai/get-or-create";

Add Package

bunx jsr add @thai/get-or-create

Import symbol

import { getOrCreate } from "@thai/get-or-create";