diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-03-13 21:24:18 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-03-13 21:24:18 -0400 |
commit | 45d1ba227e11aa4c401b45df9db7402d239fae5b (patch) | |
tree | 7d201358df28984e81b1377fa7bdd507ef3221db | |
parent | cc18d21bceee8685b4735de983b549141d720019 (diff) | |
download | tanzanite-45d1ba227e11aa4c401b45df9db7402d239fae5b.tar.gz tanzanite-45d1ba227e11aa4c401b45df9db7402d239fae5b.tar.bz2 tanzanite-45d1ba227e11aa4c401b45df9db7402d239fae5b.zip |
feat: improve inspect wrapper
-rw-r--r-- | src/lib/common/typings/BushInspectOptions.ts | 55 | ||||
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 47 |
2 files changed, 54 insertions, 48 deletions
diff --git a/src/lib/common/typings/BushInspectOptions.ts b/src/lib/common/typings/BushInspectOptions.ts index 38c70b1..9b91c0e 100644 --- a/src/lib/common/typings/BushInspectOptions.ts +++ b/src/lib/common/typings/BushInspectOptions.ts @@ -6,10 +6,11 @@ import { type InspectOptions } from 'util'; export interface BushInspectOptions extends InspectOptions { /** * If `true`, object's non-enumerable symbols and properties are included in the - * formatted result. [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) and [`WeakSet`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet) entries are also included as well as - * user defined prototype properties (excluding method properties). + * formatted result. [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) + * and [`WeakSet`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet) entries + * are also included as well as user defined prototype properties (excluding method properties). * - * **Default**: `false`. + * @default false */ showHidden?: boolean | undefined; @@ -18,37 +19,42 @@ export interface BushInspectOptions extends InspectOptions { * for inspecting large objects. To recurse up to the maximum call stack size pass * `Infinity` or `null`. * - * **Default**: `2`. + * @default 2 */ depth?: number | null | undefined; /** - * If `true`, the output is styled with ANSI color codes. Colors are customizable. See [Customizing util.inspect colors](https://nodejs.org/api/util.html#util_customizing_util_inspect_colors). + * If `true`, the output is styled with ANSI color codes. Colors are customizable. See + * [Customizing util.inspect colors](https://nodejs.org/api/util.html#util_customizing_util_inspect_colors). * - * **Default**: `false`. + * @default false */ colors?: boolean | undefined; /** * If `false`, `[util.inspect.custom](depth, opts)` functions are not invoked. * - * **Default**: `true`. + * @default true */ customInspect?: boolean | undefined; /** - * If `true`, `Proxy` inspection includes the [`target` and `handler`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#Terminology) objects. + * If `true`, `Proxy` inspection includes the + * [`target` and `handler`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#Terminology) + * objects. * - * **Default**: `false`. + * @default false */ showProxy?: boolean | undefined; /** - * Specifies the maximum number of `Array`, [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) and - * [`WeakSet`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet) elements to include when formatting. Set to `null` or `Infinity` to - * show all elements. Set to `0` or negative to show no elements. + * Specifies the maximum number of `Array`, [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), + * [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) and + * [`WeakSet`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet) elements to + * include when formatting. Set to `null` or `Infinity` to show all elements. + * Set to `0` or negative to show no elements. * - * **Default**: `100`. + * @default 100 */ maxArrayLength?: number | null | undefined; @@ -57,7 +63,7 @@ export interface BushInspectOptions extends InspectOptions { * `null` or `Infinity` to show all elements. Set to `0` or negative to show no * characters. * - * **Default**: `10000`. + * @default 10000 */ maxStringLength?: number | null | undefined; @@ -66,7 +72,7 @@ export interface BushInspectOptions extends InspectOptions { * `Infinity` to format the input as a single line (in combination with compact set * to `true` or any number >= `1`). * - * **Default**: `80`. + * @default 80 */ breakLength?: number | undefined; @@ -76,16 +82,18 @@ export interface BushInspectOptions extends InspectOptions { * number, the most `n` inner elements are united on a single line as long as all * properties fit into `breakLength`. Short array elements are also grouped together. * - * **Default**: `3` + * @default 3 */ compact?: boolean | number | undefined; /** * If set to `true` or a function, all properties of an object, and `Set` and `Map` - * entries are sorted in the resulting string. If set to `true` the [default sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) is used. - * If set to a function, it is used as a [compare function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters). + * entries are sorted in the resulting string. If set to `true` the + * [default sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) is used. + * If set to a function, it is used as a + * [compare function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters). * - * **Default**: `false`. + * @default false */ sorted?: boolean | ((a: string, b: string) => number) | undefined; @@ -95,7 +103,14 @@ export interface BushInspectOptions extends InspectOptions { * corresponding setter are inspected. This might cause side effects depending on * the getter function. * - * **Default**: `false`. + * @default false */ getters?: 'get' | 'set' | boolean | undefined; + + /** + * Whether or not to inspect strings. + * + * @default false + */ + inspectStrings?: boolean; } diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index 04014ef..9b63736 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -26,12 +26,10 @@ import { APIEmbed, APIMessage, OAuth2Scopes } from 'discord-api-types/v9'; import { Constants as DiscordConstants, Embed, - GuildMember, Message, PermissionFlagsBits, PermissionsBitField, PermissionsString, - ThreadMember, User, Util as DiscordUtil, type CommandInteraction, @@ -252,17 +250,6 @@ export class BushClientUtil extends ClientUtil { } /** - * Uses {@link inspect} with custom defaults. - * @param object - The object you would like to inspect. - * @param options - The options you would like to use to inspect the object. - * @returns The inspected object. - */ - public inspect(object: any, options?: BushInspectOptions): string { - const optionsWithDefaults = this.#getDefaultInspectOptions(options); - return inspect(object, optionsWithDefaults); - } - - /** * Generate defaults for {@link inspect}. * @param options The options to create defaults with. * @returns The default options combined with the specified options. @@ -322,6 +309,20 @@ export class BushClientUtil extends ClientUtil { } /** + * Uses {@link inspect} with custom defaults. + * @param object - The object you would like to inspect. + * @param options - The options you would like to use to inspect the object. + * @returns The inspected object. + */ + public inspect(object: any, options?: BushInspectOptions): string { + const optionsWithDefaults = this.#getDefaultInspectOptions(options); + + if (!optionsWithDefaults.inspectStrings && typeof object === 'string') return object; + + return inspect(object, optionsWithDefaults); + } + + /** * Takes an any value, inspects it, redacts credentials, and puts it in a codeblock * (and uploads to hast if the content is too long). * @param input The object to be inspect, redacted, and put into a codeblock. @@ -333,11 +334,10 @@ export class BushClientUtil extends ClientUtil { public async inspectCleanRedactCodeblock( input: any, language?: CodeBlockLang | '', - inspectOptions?: BushInspectOptions & { inspectStrings?: boolean }, + inspectOptions?: BushInspectOptions, length = 1024 ) { - input = - !inspectOptions?.inspectStrings && typeof input === 'string' ? input : this.inspect(input, inspectOptions ?? undefined); + input = this.inspect(input, inspectOptions ?? undefined); if (inspectOptions) inspectOptions.inspectStrings = undefined; input = this.discord.cleanCodeBlockContent(input); input = this.redact(input); @@ -351,7 +351,7 @@ export class BushClientUtil extends ClientUtil { * @returns The {@link HasteResults}. */ public async inspectCleanRedactHaste(input: any, inspectOptions?: BushInspectOptions): Promise<HasteResults> { - input = typeof input !== 'string' ? this.inspect(input, inspectOptions ?? undefined) : input; + input = this.inspect(input, inspectOptions ?? undefined); input = this.redact(input); return this.haste(input, true); } @@ -363,7 +363,7 @@ export class BushClientUtil extends ClientUtil { * @returns The redacted and inspected object. */ public inspectAndRedact(input: any, inspectOptions?: BushInspectOptions): string { - input = typeof input !== 'string' ? this.inspect(input, inspectOptions ?? undefined) : input; + input = this.inspect(input, inspectOptions ?? undefined); return this.redact(input); } @@ -694,16 +694,7 @@ export class BushClientUtil extends ClientUtil { */ public async resolveNonCachedUser(user: UserResolvable | undefined | null): Promise<BushUser | undefined> { if (user == null) return undefined; - const resolvedUser = - user instanceof User - ? <BushUser>user - : user instanceof GuildMember - ? <BushUser>user.user - : user instanceof ThreadMember - ? <BushUser>user.user - : user instanceof Message - ? <BushUser>user.author - : undefined; + const resolvedUser = client.users.resolve(user); return resolvedUser ?? (await client.users.fetch(user as Snowflake).catch(() => undefined)); } |