From 797aa63608d5dbae1e18a4a8ad0b67e72bf27bc2 Mon Sep 17 00:00:00 2001 From: Pedro Gallardo Date: Sun, 6 Aug 2023 18:24:17 -0300 Subject: [PATCH] feat: agrega getId y elimina resolve --- src/Emulator.ts | 31 ++----------------------------- src/lib/methods/getId.spec.ts | 13 +++++++++++++ src/lib/methods/getId.ts | 12 ++++++++++++ src/lib/methods/index.ts | 2 ++ src/types/Exotic.ts | 29 +++++++++++------------------ 5 files changed, 40 insertions(+), 47 deletions(-) create mode 100644 src/lib/methods/getId.spec.ts create mode 100644 src/lib/methods/getId.ts diff --git a/src/Emulator.ts b/src/Emulator.ts index 2092fce..d99e577 100755 --- a/src/Emulator.ts +++ b/src/Emulator.ts @@ -1,6 +1,5 @@ import lib from "./lib"; import Exotic from "./types/Exotic"; -import { findProxy, map } from "./utils"; import { EventEmitter } from "events"; export default class Emulator extends EventEmitter implements Exotic.Emulator { @@ -69,33 +68,7 @@ export default class Emulator extends EventEmitter implements Exotic.Emulator { return lib.methods.entriesAfter(this, value); } - // faltando - - useId() {} - - encode(value: unknown): unknown { - if (findProxy(value)) { - const { id } = map.proxies.get(this.use(value)); - return { id, encoded: true }; // TODO: usar Symbol para saber si es encoded o no - } - - if (typeof value === "object" && value) { - const copy = Array.isArray(value) ? [] : {}; - - for (const key in value) { - copy[key] = this.encode(value[key]); - } - - value = copy; - } - - return value; - } - - resolve(value: any): Exotic.proxy.public { - const proxy = findProxy(value); - if (!proxy) return value; - const { id, target } = map.proxies.get(proxy); - return { id, target }; + getId(value: Exotic.traceable): number { + return lib.methods.getId(this, value); } } diff --git a/src/lib/methods/getId.spec.ts b/src/lib/methods/getId.spec.ts new file mode 100644 index 0000000..b6dc2b4 --- /dev/null +++ b/src/lib/methods/getId.spec.ts @@ -0,0 +1,13 @@ +import Emulator from "../../Emulator"; +const $ = new Emulator(); + +describe("(method) getId", () => { + it("Returns the proxy's id ", () => { + const totalProxies = $.length; + const proxy = $.use(); + const another = $.use(); + + expect($.getId(proxy)).toBe(totalProxies + 1); + expect($.getId(another)).toBe(totalProxies + 2); + }); +}); diff --git a/src/lib/methods/getId.ts b/src/lib/methods/getId.ts new file mode 100644 index 0000000..ca0a317 --- /dev/null +++ b/src/lib/methods/getId.ts @@ -0,0 +1,12 @@ +import Exotic from "../../types/Exotic"; +import { findProxy, map } from "../../utils"; + +export default function getId( + scope: Exotic.Emulator, + value: Exotic.traceable, +): number { + const proxy = findProxy(value); + if (!proxy) return NaN; + const { id } = map.proxies.get(proxy); + return id; +} diff --git a/src/lib/methods/index.ts b/src/lib/methods/index.ts index 35538de..c61960c 100644 --- a/src/lib/methods/index.ts +++ b/src/lib/methods/index.ts @@ -9,6 +9,7 @@ import isRevoked from "./isRevoked"; import entries from "./entries"; import entriesBefore from "./entriesBefore"; import entriesAfter from "./entriesAfter"; +import getId from "./getId"; export default { useRef, @@ -22,4 +23,5 @@ export default { entries, entriesBefore, entriesAfter, + getId, }; diff --git a/src/types/Exotic.ts b/src/types/Exotic.ts index efff8e8..0f02492 100644 --- a/src/types/Exotic.ts +++ b/src/types/Exotic.ts @@ -11,12 +11,13 @@ declare namespace Exotic { target(value?: any): any; parent(value?: traceable): undefined | Proxy; children(value?: traceable): Proxy[]; - ownKeys(value?: Exotic.traceable): Exotic.key[]; - revoke(value: Exotic.traceable): boolean; - isRevoked(value: Exotic.traceable): boolean; - entries(): Iterable; - entriesBefore(value: Exotic.traceable): Iterable; - entriesAfter(value: Exotic.traceable): Iterable; + ownKeys(value?: traceable): key[]; + revoke(value: traceable): boolean; + isRevoked(value: traceable): boolean; + entries(): Iterable; + entriesBefore(value: traceable): Iterable; + entriesAfter(value: traceable): Iterable; + getId(value: traceable): number; refs: key[]; active: number; revoked: number; @@ -49,16 +50,13 @@ declare namespace Exotic { args?: any[]; } - interface public { + interface data { id: number; target?: any; - } - - interface data extends public { revoke(): void; revoked: boolean; mock: Mock; - origin?: proxy.origin | undefined; + origin?: origin | undefined; scope: Emulator; sandbox: sandbox; refKey: key; @@ -69,13 +67,8 @@ declare namespace Exotic { // eslint-disable-next-line @typescript-eslint/no-namespace namespace emulator { - interface options { - [x: string]: any; - } - - interface refs { - [x: key]: Proxy; - } + type options = Record; + type refs = Record; interface data { options: options;