Skip to content

Commit

Permalink
Add keys, values, and entries symbols for Switch.inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Apr 11, 2024
1 parent ce10f32 commit 84f2c09
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-cherries-fly.md
@@ -0,0 +1,5 @@
---
"nxjs-runtime": patch
---

Add `keys`, `values`, and `entries` symbols for `Switch.inspect`
35 changes: 28 additions & 7 deletions packages/runtime/src/inspect.ts
Expand Up @@ -135,24 +135,45 @@ 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'
? `${ctor.name} `
: '';
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';
Expand Down
39 changes: 22 additions & 17 deletions packages/runtime/src/polyfills/url.ts
Expand Up @@ -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<URL, URLSearchParams>();

/**
Expand Down Expand Up @@ -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',
],
});
6 changes: 6 additions & 0 deletions packages/runtime/src/switch/profile.ts
@@ -1,4 +1,5 @@
import { $ } from '../$';
import { inspect } from '../inspect';
import { proto } from '../utils';

let init = false;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 84f2c09

Please sign in to comment.