deeply prune defined undefined values in test adapter
This commit is contained in:
7
packages/reacord/helpers/is-object.ts
Normal file
7
packages/reacord/helpers/is-object.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export function isObject<T>(
|
||||||
|
value: T,
|
||||||
|
): value is Exclude<T, Primitive | AnyFunction> {
|
||||||
|
return typeof value === "object" && value !== null
|
||||||
|
}
|
||||||
|
type Primitive = string | number | boolean | undefined | null
|
||||||
|
type AnyFunction = (...args: any[]) => any
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
export function pruneNullishValues<T extends object>(
|
|
||||||
object: T,
|
|
||||||
): PruneNullishValues<T> {
|
|
||||||
const result: any = {}
|
|
||||||
for (const [key, value] of Object.entries(object)) {
|
|
||||||
if (value != undefined) {
|
|
||||||
result[key] = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
type PruneNullishValues<T> = {
|
|
||||||
[Key in keyof T]: NonNullable<T[Key]>
|
|
||||||
}
|
|
||||||
27
packages/reacord/helpers/prune-nullish-values.ts
Normal file
27
packages/reacord/helpers/prune-nullish-values.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { isObject } from "./is-object"
|
||||||
|
|
||||||
|
export function pruneNullishValues<T>(input: T): PruneNullishValues<T> {
|
||||||
|
if (Array.isArray(input)) {
|
||||||
|
return input.filter(Boolean).map((item) => pruneNullishValues(item)) as any
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isObject(input)) {
|
||||||
|
return input as any
|
||||||
|
}
|
||||||
|
|
||||||
|
const result: any = {}
|
||||||
|
for (const [key, value] of Object.entries(input)) {
|
||||||
|
if (value != undefined) {
|
||||||
|
result[key] = isObject(value) ? pruneNullishValues(value) : value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
type PruneNullishValues<Input> = Input extends ReadonlyArray<infer Value>
|
||||||
|
? ReadonlyArray<NonNullable<Value>>
|
||||||
|
: Input extends object
|
||||||
|
? {
|
||||||
|
[Key in keyof Input]: NonNullable<Input[Key]>
|
||||||
|
}
|
||||||
|
: Input
|
||||||
@@ -3,7 +3,7 @@ import * as Discord from "discord.js"
|
|||||||
import type { ReactNode } from "react"
|
import type { ReactNode } from "react"
|
||||||
import type { Except } from "type-fest"
|
import type { Except } from "type-fest"
|
||||||
import { pick } from "../../helpers/pick"
|
import { pick } from "../../helpers/pick"
|
||||||
import { pruneNullishValues } from "../../helpers/prune-null-values"
|
import { pruneNullishValues } from "../../helpers/prune-nullish-values"
|
||||||
import { raise } from "../../helpers/raise"
|
import { raise } from "../../helpers/raise"
|
||||||
import { toUpper } from "../../helpers/to-upper"
|
import { toUpper } from "../../helpers/to-upper"
|
||||||
import type { ComponentInteraction } from "../internal/interaction"
|
import type { ComponentInteraction } from "../internal/interaction"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import type { ReactNode } from "react"
|
|||||||
import { expect } from "vitest"
|
import { expect } from "vitest"
|
||||||
import { logPretty } from "../helpers/log-pretty"
|
import { logPretty } from "../helpers/log-pretty"
|
||||||
import { omit } from "../helpers/omit"
|
import { omit } from "../helpers/omit"
|
||||||
|
import { pruneNullishValues } from "../helpers/prune-nullish-values"
|
||||||
import { raise } from "../helpers/raise"
|
import { raise } from "../helpers/raise"
|
||||||
import type {
|
import type {
|
||||||
ChannelInfo,
|
ChannelInfo,
|
||||||
@@ -88,14 +89,21 @@ export class ReacordTester extends Reacord {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sampleMessages() {
|
sampleMessages() {
|
||||||
return this.messages.map((message) => ({
|
return pruneNullishValues(
|
||||||
...message.options,
|
this.messages.map((message) => ({
|
||||||
actionRows: message.options.actionRows.map((row) =>
|
...message.options,
|
||||||
row.map((component) =>
|
actionRows: message.options.actionRows.map((row) =>
|
||||||
omit(component, ["customId", "onClick", "onSelect", "onSelectValue"]),
|
row.map((component) =>
|
||||||
|
omit(component, [
|
||||||
|
"customId",
|
||||||
|
"onClick",
|
||||||
|
"onSelect",
|
||||||
|
"onSelectValue",
|
||||||
|
]),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
})),
|
||||||
}))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
findButtonByLabel(label: string) {
|
findButtonByLabel(label: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user