Skip to content

Commit

Permalink
refactor(embed): remove array support in favor of rest params (#7498)
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Feb 20, 2022
1 parent ffecf08 commit b3fa2ec
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 36 deletions.
10 changes: 5 additions & 5 deletions packages/builders/__tests__/messages/embed.test.ts
Expand Up @@ -311,10 +311,10 @@ describe('Embed', () => {
describe('Embed Fields', () => {
test('GIVEN an embed with a pre-defined field THEN returns valid toJSON data', () => {
const embed = new Embed({
fields: [{ name: 'foo', value: 'bar', inline: undefined }],
fields: [{ name: 'foo', value: 'bar' }],
});
expect(embed.toJSON()).toStrictEqual({
fields: [{ name: 'foo', value: 'bar', inline: undefined }],
fields: [{ name: 'foo', value: 'bar' }],
});
});

Expand All @@ -323,7 +323,7 @@ describe('Embed', () => {
embed.addField({ name: 'foo', value: 'bar' });

expect(embed.toJSON()).toStrictEqual({
fields: [{ name: 'foo', value: 'bar', inline: undefined }],
fields: [{ name: 'foo', value: 'bar' }],
});
});

Expand All @@ -332,7 +332,7 @@ describe('Embed', () => {
embed.addFields({ name: 'foo', value: 'bar' });

expect(embed.toJSON()).toStrictEqual({
fields: [{ name: 'foo', value: 'bar', inline: undefined }],
fields: [{ name: 'foo', value: 'bar' }],
});
});

Expand All @@ -341,7 +341,7 @@ describe('Embed', () => {
embed.addFields({ name: 'foo', value: 'bar' }, { name: 'foo', value: 'baz' });

expect(embed.spliceFields(0, 1).toJSON()).toStrictEqual({
fields: [{ name: 'foo', value: 'baz', inline: undefined }],
fields: [{ name: 'foo', value: 'baz' }],
});
});

Expand Down
18 changes: 0 additions & 18 deletions packages/builders/src/messages/embed/Embed.ts
Expand Up @@ -4,9 +4,6 @@ import {
colorPredicate,
descriptionPredicate,
embedFieldsArrayPredicate,
fieldInlinePredicate,
fieldNamePredicate,
fieldValuePredicate,
footerTextPredicate,
timestampPredicate,
titlePredicate,
Expand Down Expand Up @@ -94,19 +91,4 @@ export class Embed extends UnsafeEmbed {
// Data assertions
return super.setURL(urlPredicate.parse(url)!);
}

/**
* Normalizes field input and resolves strings
*
* @param fields Fields to normalize
*/
public static override normalizeFields(...fields: APIEmbedField[]): APIEmbedField[] {
return fields.flat(Infinity).map((field) => {
fieldNamePredicate.parse(field.name);
fieldValuePredicate.parse(field.value);
fieldInlinePredicate.parse(field.inline);

return { name: field.name, value: field.value, inline: field.inline ?? undefined };
});
}
}
13 changes: 0 additions & 13 deletions packages/builders/src/messages/embed/UnsafeEmbed.ts
Expand Up @@ -190,7 +190,6 @@ export class UnsafeEmbed implements Equatable<APIEmbed | UnsafeEmbed> {
* @param fields The fields to add
*/
public addFields(...fields: APIEmbedField[]): this {
fields = UnsafeEmbed.normalizeFields(...fields);
if (this.data.fields) this.data.fields.push(...fields);
else this.data.fields = fields;
return this;
Expand All @@ -204,7 +203,6 @@ export class UnsafeEmbed implements Equatable<APIEmbed | UnsafeEmbed> {
* @param fields The replacing field objects
*/
public spliceFields(index: number, deleteCount: number, ...fields: APIEmbedField[]): this {
fields = UnsafeEmbed.normalizeFields(...fields);
if (this.data.fields) this.data.fields.splice(index, deleteCount, ...fields);
else this.data.fields = fields;
return this;
Expand Down Expand Up @@ -337,15 +335,4 @@ export class UnsafeEmbed implements Equatable<APIEmbed | UnsafeEmbed> {
const { image, thumbnail, ...otherData } = data;
return isEqual(otherData, thisData) && image?.url === thisImage?.url && thumbnail?.url === thisThumbnail?.url;
}

/**
* Normalizes field input and resolves strings
*
* @param fields Fields to normalize
*/
public static normalizeFields(...fields: APIEmbedField[]): APIEmbedField[] {
return fields
.flat(Infinity)
.map((field) => ({ name: field.name, value: field.value, inline: field.inline ?? undefined }));
}
}

0 comments on commit b3fa2ec

Please sign in to comment.