mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 19:43:29 +01:00
feat: add @discordjs/util (#8591)
* feat: add @discordjs/util * fix: builders test * refactor: make rest use lazy for ESM import * chore: make requested changes * Apply suggestions from code review Co-authored-by: Parbez <imranbarbhuiya.fsd@gmail.com> Co-authored-by: A. Román <kyradiscord@gmail.com> * chore: make requested changes and add tests * chore: regen lockfile * test: add type tests * chore: push missing files * chore: make requested changes * chore: update CI stuff * chore: fix lockfile * chore: make requested changes Co-authored-by: Parbez <imranbarbhuiya.fsd@gmail.com> Co-authored-by: A. Román <kyradiscord@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,33 +1,5 @@
|
||||
import { describe, test, expect } from 'vitest';
|
||||
import {
|
||||
isJSONEncodable,
|
||||
isEquatable,
|
||||
ActionRowBuilder,
|
||||
enableValidators,
|
||||
disableValidators,
|
||||
isValidationEnabled,
|
||||
} from '../src/index.js';
|
||||
|
||||
describe('isEquatable', () => {
|
||||
test('returns true if the object is equatable', () => {
|
||||
expect(isEquatable({ equals: () => true })).toBeTruthy();
|
||||
});
|
||||
|
||||
test('returns false if the object is not equatable', () => {
|
||||
expect(isEquatable({})).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('isJSONEncodable', () => {
|
||||
test('returns true if the object is JSON encodable', () => {
|
||||
expect(isJSONEncodable({ toJSON: () => ({}) })).toBeTruthy();
|
||||
expect(isJSONEncodable(new ActionRowBuilder())).toBeTruthy();
|
||||
});
|
||||
|
||||
test('returns false if the object is not JSON encodable', () => {
|
||||
expect(isJSONEncodable({})).toBeFalsy();
|
||||
});
|
||||
});
|
||||
import { enableValidators, disableValidators, isValidationEnabled } from '../src/index.js';
|
||||
|
||||
describe('validation', () => {
|
||||
test('enables validation', () => {
|
||||
|
||||
@@ -54,6 +54,7 @@
|
||||
},
|
||||
"homepage": "https://discord.js.org",
|
||||
"dependencies": {
|
||||
"@discordjs/util": "workspace:^",
|
||||
"@sapphire/shapeshift": "^3.6.0",
|
||||
"discord-api-types": "^0.37.11",
|
||||
"fast-deep-equal": "^3.1.3",
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import type { JSONEncodable } from '@discordjs/util';
|
||||
import type {
|
||||
APIActionRowComponent,
|
||||
APIActionRowComponentTypes,
|
||||
APIBaseComponent,
|
||||
ComponentType,
|
||||
} from 'discord-api-types/v10';
|
||||
import type { JSONEncodable } from '../util/jsonEncodable';
|
||||
|
||||
export type AnyAPIActionRowComponent = APIActionRowComponent<APIActionRowComponentTypes> | APIActionRowComponentTypes;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { JSONEncodable } from '@discordjs/util';
|
||||
import type { APIMessageComponentEmoji, APISelectMenuOption } from 'discord-api-types/v10';
|
||||
import type { JSONEncodable } from '../../util/jsonEncodable.js';
|
||||
import {
|
||||
defaultValidator,
|
||||
emojiValidator,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { isJSONEncodable, type Equatable, type JSONEncodable } from '@discordjs/util';
|
||||
import { ComponentType, type TextInputStyle, type APITextInputComponent } from 'discord-api-types/v10';
|
||||
import isEqual from 'fast-deep-equal';
|
||||
import type { Equatable } from '../../util/equatable';
|
||||
import { isJSONEncodable, type JSONEncodable } from '../../util/jsonEncodable.js';
|
||||
import { customIdValidator } from '../Assertions.js';
|
||||
import { ComponentBuilder } from '../Component.js';
|
||||
import {
|
||||
|
||||
@@ -36,8 +36,7 @@ export * from './interactions/slashCommands/mixins/SharedSlashCommandOptions.js'
|
||||
export * as ContextMenuCommandAssertions from './interactions/contextMenuCommands/Assertions.js';
|
||||
export * from './interactions/contextMenuCommands/ContextMenuCommandBuilder.js';
|
||||
|
||||
export * from './util/jsonEncodable.js';
|
||||
export * from './util/equatable.js';
|
||||
export * from './util/componentUtil.js';
|
||||
export * from './util/normalizeArray.js';
|
||||
export * from './util/validation.js';
|
||||
export * from '@discordjs/util';
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { JSONEncodable } from '@discordjs/util';
|
||||
import type {
|
||||
APIActionRowComponent,
|
||||
APIModalActionRowComponent,
|
||||
@@ -6,7 +7,6 @@ import type {
|
||||
import { ActionRowBuilder, type ModalActionRowComponentBuilder } from '../../components/ActionRow.js';
|
||||
import { customIdValidator } from '../../components/Assertions.js';
|
||||
import { createComponentBuilder } from '../../components/Components.js';
|
||||
import type { JSONEncodable } from '../../util/jsonEncodable';
|
||||
import { normalizeArray, type RestOrArray } from '../../util/normalizeArray.js';
|
||||
import { titleValidator, validateRequiredParameters } from './Assertions.js';
|
||||
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
/**
|
||||
* Represents a structure that can be checked against another
|
||||
* given structure for equality
|
||||
*
|
||||
* @typeParam T - The type of object to compare the current object to
|
||||
*/
|
||||
export interface Equatable<T> {
|
||||
/**
|
||||
* Whether or not this is equal to another structure
|
||||
*/
|
||||
equals(other: T): boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if an object is equatable or not.
|
||||
*
|
||||
* @param maybeEquatable - The object to check against
|
||||
*/
|
||||
export function isEquatable(maybeEquatable: unknown): maybeEquatable is Equatable<unknown> {
|
||||
return maybeEquatable !== null && typeof maybeEquatable === 'object' && 'equals' in maybeEquatable;
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
/**
|
||||
* Represents an object capable of representing itself as a JSON object
|
||||
*
|
||||
* @typeParam T - The JSON type corresponding to {@link JSONEncodable.toJSON} outputs.
|
||||
*/
|
||||
export interface JSONEncodable<T> {
|
||||
/**
|
||||
* Transforms this object to its JSON format
|
||||
*/
|
||||
toJSON(): T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if an object is encodable or not.
|
||||
*
|
||||
* @param maybeEncodable - The object to check against
|
||||
*/
|
||||
export function isJSONEncodable(maybeEncodable: unknown): maybeEncodable is JSONEncodable<unknown> {
|
||||
return maybeEncodable !== null && typeof maybeEncodable === 'object' && 'toJSON' in maybeEncodable;
|
||||
}
|
||||
Reference in New Issue
Block a user