From 84f2c090162eac084b0c489b4cce750c4bb515cf Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 10 Apr 2024 18:26:43 -0700 Subject: [PATCH] Add `keys`, `values`, and `entries` symbols for `Switch.inspect` --- .changeset/short-cherries-fly.md | 5 ++++ packages/runtime/src/inspect.ts | 35 ++++++++++++++++++----- packages/runtime/src/polyfills/url.ts | 39 +++++++++++++++----------- packages/runtime/src/switch/profile.ts | 6 ++++ 4 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 .changeset/short-cherries-fly.md diff --git a/.changeset/short-cherries-fly.md b/.changeset/short-cherries-fly.md new file mode 100644 index 00000000..28e94d53 --- /dev/null +++ b/.changeset/short-cherries-fly.md @@ -0,0 +1,5 @@ +--- +"nxjs-runtime": patch +--- + +Add `keys`, `values`, and `entries` symbols for `Switch.inspect` diff --git a/packages/runtime/src/inspect.ts b/packages/runtime/src/inspect.ts index e5ce5bb3..a97b63a0 100644 --- a/packages/runtime/src/inspect.ts +++ b/packages/runtime/src/inspect.ts @@ -135,10 +135,13 @@ export const inspect = (v: unknown, opts: InspectOptions = {}): string => { }; inspect.custom = Symbol('Switch.inspect.custom'); +inspect.keys = Symbol('Switch.inspect.keys'); +inspect.values = Symbol('Switch.inspect.values'); +inspect.entries = Symbol('Switch.inspect.entries'); function printObject(v: any, opts: InspectOptions) { const { depth = 1 } = opts; - const keys = Object.keys(v); + const keys = new Set([...(v[inspect.keys]?.() || []), ...Object.keys(v)]); const ctor = v.constructor; const className = ctor && ctor !== Object && typeof ctor.name === 'string' @@ -146,13 +149,31 @@ function printObject(v: any, opts: InspectOptions) { : ''; let contents = ''; let endSpace = ''; - if (keys.length > 0) { - let len = 0; - const parts = keys.map((k) => { - const l = `${k}: ${inspect(v[k], { ...opts, depth: depth + 1 })}`; + let len = 0; + const parts: string[] = []; + if (typeof v[inspect.values] === 'function') { + for (const val of v[inspect.values]()) { + const l = inspect(val, { ...opts, depth: depth + 1 }); len += l.length; - return l; - }); + parts.push(l); + } + } + if (typeof v[inspect.entries] === 'function') { + for (const [k, val] of v[inspect.entries]()) { + const l = `${inspect(k, { + ...opts, + depth: depth + 1, + })} => ${inspect(val, { ...opts, depth: depth + 1 })}`; + len += l.length; + parts.push(l); + } + } + for (const k of keys) { + const l = `${k}: ${inspect(v[k], { ...opts, depth: depth + 1 })}`; + len += l.length; + parts.push(l); + } + if (parts.length > 0) { if (len > 60) { const space = ' '.repeat(depth); contents = parts.map((p) => `\n${space}${p},`).join('') + '\n'; diff --git a/packages/runtime/src/polyfills/url.ts b/packages/runtime/src/polyfills/url.ts index 65345c1e..145918c5 100644 --- a/packages/runtime/src/polyfills/url.ts +++ b/packages/runtime/src/polyfills/url.ts @@ -169,6 +169,13 @@ export class URLSearchParams implements globalThis.URLSearchParams { $.urlSearchInit(URLSearchParams); def(URLSearchParams); +Object.defineProperty(URLSearchParams.prototype, inspect.entries, { + enumerable: false, + value(this: URLSearchParams) { + return this.entries(); + }, +}); + const searchParams = new WeakMap(); /** @@ -271,22 +278,20 @@ export class URL implements globalThis.URL { $.urlInit(URL); def(URL); -Object.defineProperty(URL.prototype, inspect.custom, { +Object.defineProperty(URL.prototype, inspect.keys, { enumerable: false, - value(this: URL) { - return `URL ${inspect({ - hash: this.hash, - host: this.host, - hostname: this.hostname, - href: this.href, - origin: this.origin, - password: this.password, - pathname: this.pathname, - port: this.port, - protocol: this.protocol, - search: this.search, - searchParams: this.searchParams, - username: this.username, - })}`; - }, + value: () => [ + 'hash', + 'host', + 'hostname', + 'href', + 'origin', + 'password', + 'pathname', + 'port', + 'protocol', + 'search', + 'searchParams', + 'username', + ], }); diff --git a/packages/runtime/src/switch/profile.ts b/packages/runtime/src/switch/profile.ts index deb740ed..5ef01491 100644 --- a/packages/runtime/src/switch/profile.ts +++ b/packages/runtime/src/switch/profile.ts @@ -1,4 +1,5 @@ import { $ } from '../$'; +import { inspect } from '../inspect'; import { proto } from '../utils'; let init = false; @@ -50,6 +51,11 @@ export class Profile { } $.accountProfileInit(Profile); +Object.defineProperty(Profile.prototype, inspect.keys, { + enumerable: false, + value: () => ['uid', 'nickname', 'image'], +}); + let p: Profile | null = null; export interface CurrentProfileOptions {