New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@miyauci/upsert

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@miyauci/upsert

Maps for emplace, TC39 proposal-upsert implementation

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2K
decreased by-14.6%
Maintainers
1
Weekly downloads
 
Created
Source

upsert

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM

Maps for emplace, TC39 proposal-upsert implementation.

Entrypoint

This project provides ponyfill and polyfill.

Polyfill has a side effect, so its endpoints are isolated.

The entrypoint of each are as follows:

TypeEntrypoint
Ponyfillmod.ts
Polyfillpolyfill.ts

Emplace

Add a value to a map like if it does not already have something at key, and will also update an existing value at key.

import { emplace } from "https://deno.land/x/upsert@$VERSION/mod.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

declare const map: Map<string, number>;
declare const key: string;

const result = emplace(map, key, {
  insert: () => 0,
  update: (existing) => existing + 1,
});
assert(map.has(key));

Just insert if missing

You might omit an update if you're handling data that doesn't change, but can still be appended.

import { emplace } from "https://deno.land/x/upsert@$VERSION/mod.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";
import {
  assertType,
  type IsExact,
} from "https://deno.land/std/testing/types.ts";

declare const map: Map<string, number>;
declare const key: string;
declare const value: number;

const result = emplace(map, key, { insert: () => value });

assert(map.has(key));
assertType<IsExact<typeof result, number>>(true);

Just update if present

You might want to omit an insert if you want to perform a function on all existing values.

import { emplace } from "https://deno.land/x/upsert@$VERSION/mod.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";
import {
  assertType,
  type IsExact,
} from "https://deno.land/std/testing/types.ts";

declare const map: Map<string, number>;
declare const key: string;

const result = emplace(map, key, { update: (existing) => existing + 1 });

assertType<IsExact<typeof result, number | undefined>>(true);

Emplaceable

Mixin for emplace.

import { Emplaceable } from "https://deno.land/x/upsert@$VERSION/mod.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

class MyMap extends Emplaceable(Map) {}

assert(new MyMap().emplace);

EmplaceableMap

Map with Emplaceable implementation.

import { EmplaceableMap } from "https://deno.land/x/upsert@$VERSION/mod.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

const map = new EmplaceableMap<string, number>();
declare const key: string;

map.emplace(key, {
  insert: () => 0,
  update: (existing) => existing + 1,
});

assert(map.has(key));

EmplaceableWeakMap

WeakMap with Emplaceable implementation.

import { EmplaceableWeakMap } from "https://deno.land/x/upsert@$VERSION/mod.ts";

const weakMap = new EmplaceableWeakMap();

Polyfill

Polyfill affects the global object. You must be very careful when using it.

import "https://deno.land/x/upsert@$VERSION/polyfill.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

assert(Map.prototype.emplace);
assert(WeakMap.prototype.emplace);

API

See deno doc for all APIs.

License

Copyright © 2023-present Tomoki Miyauchi.

Released under the MIT license

Keywords

FAQs

Package last updated on 14 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc