Skip to content

Commit

Permalink
feat: improve method overload signature
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jun 14, 2023
1 parent bf0b7e2 commit db0d137
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions _dev_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export {
assert,
assertEquals,
assertFalse,
} from "https://deno.land/std@0.190.0/testing/asserts.ts";
export { describe, it } from "https://deno.land/std@0.190.0/testing/bdd.ts";
export {
Expand Down
14 changes: 7 additions & 7 deletions emplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@ export function emplace<K, V, M>(
export function emplace<K, V, M>(
map: Readonly<MapLike<K, V>> & M,
key: K,
handler: Readonly<{ insert: (key: K, map: M) => V }>,
handler: Readonly<Insertable<K, V, M>>,
): V;
export function emplace<K, V, M>(
map: Readonly<MapLike<K, V>> & M,
key: K,
handler: Readonly<{ update: (existing: V, key: K, map: M) => V }>,
handler: Readonly<Updatable<K, V, M>>,
): V | undefined;
export function emplace<K, V, M>(
map: Readonly<MapLike<K, V>> & M,
key: K,
{ update, insert }: Partial<Readonly<EmplaceHandler<K, V, M>>>,
handler: Readonly<Insertable<K, V, M> | Updatable<K, V, M>>,
): V | undefined {
if (map.has(key)) {
const value = map.get(key)!;

if (update) {
const updated = update(value, key, map);
if ("update" in handler) {
const updated = handler.update(value, key, map);

map.set(key, updated);

Expand All @@ -122,8 +122,8 @@ export function emplace<K, V, M>(
}
}

if (insert) {
const inserted = insert(key, map);
if ("insert" in handler) {
const inserted = handler.insert(key, map);

map.set(key, inserted);

Expand Down
19 changes: 17 additions & 2 deletions mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,23 @@ export function emplace<K, V, M>(
this: Readonly<MapLike<K, V>> & M,
key: K,
handler: Readonly<EmplaceHandler<K, V, M>>,
): V {
return _emplace(this, key, handler);
): V;
export function emplace<K, V, M>(
this: Readonly<MapLike<K, V>> & M,
key: K,
handler: Readonly<Insertable<K, V, M>>,
): V;
export function emplace<K, V, M>(
this: Readonly<MapLike<K, V>> & M,
key: K,
handler: Readonly<Updatable<K, V, M>>,
): V | undefined;
export function emplace<K, V, M>(
this: Readonly<MapLike<K, V>> & M,
key: K,
handler: Readonly<Insertable<K, V, M> | Updatable<K, V, M>>,
): V | undefined {
return _emplace(this, key, handler as EmplaceHandler<K, V, M>);
}

/** Mixin for {@link emplace}.
Expand Down
14 changes: 10 additions & 4 deletions mixin_test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.

import { emplace, Emplaceable } from "./mixin.ts";
import { assert, assertEquals, describe, it } from "./_dev_deps.ts";
import {
assert,
assertEquals,
assertFalse,
describe,
it,
} from "./_dev_deps.ts";

describe("emplace", () => {
it("should bind map and do emplace", () => {
const map = new Map();

assertEquals(
emplace.bind(map)("a", { insert: () => "a", update: () => "b" }),
"a",
emplace.bind(map)("a", { update: () => "b" }),
undefined,
);

assert(map.has("a"));
assertFalse(map.has("a"));
});
});

Expand Down

0 comments on commit db0d137

Please sign in to comment.