Skip to content

refactor pipe usage & impl, no more 'any func's #958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions src/v2/commands/npm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
MessageEmbed,
Collection,
MessageActionRow,
MessageButton,
MessageSelectMenu,
} from 'discord.js';
import type { MessageComponentTypes } from 'discord.js/typings/enums';
Expand Down Expand Up @@ -42,26 +41,28 @@ const list = new (Intl as any).ListFormat();
const fetch: typeof getData = getData;
const formatDateFromNow: typeof formatDistanceToNow = formatDistanceToNow;

const getFirstTenResults = pipe<Iterable<NPMResponse>, NPMEmbed[]>([
take<NPMResponse>(10),
map(({ name, date, description, links, publisher, maintainers }) => ({
author: {
name: publisher.username,
// icon_url: publisher.avatars.small,
url: `https://www.npmjs.com/~${publisher.username}`,
},
description,
externalUrls: {
homepage: links.homepage,
repository: links.repository,
},
lastUpdate: `${formatDateFromNow(new Date(date))} ago`,
maintainers: maintainers.length,
name,
url: links.npm,
})),
const getFirstTenResults = pipe(
take(10),
map<NPMResponse, NPMEmbed>(
({ name, date, description, links, publisher, maintainers }) => ({
author: {
name: publisher.username,
// icon_url: publisher.avatars.small,
url: `https://www.npmjs.com/~${publisher.username}`,
},
description,
externalUrls: {
homepage: links.homepage,
repository: links.repository,
},
lastUpdate: `${formatDateFromNow(new Date(date))} ago`,
maintainers: maintainers.length,
name,
url: links.npm,
})
),
collect,
]);
);

// msg: Message, searchTerm: string
const handleNpmCommand = async (
Expand Down
8 changes: 4 additions & 4 deletions src/v2/commands/post/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ const sendAlert = (
}
};

const generateFields = pipe<Answers, Iterable<OutputField>>([
filter(
const generateFields = pipe(
filter<[string, string]>(
([key, val]: [string, string]) =>
!['guidelines'].includes(key) &&
!(key === 'remote' && val.toLowerCase() === 'onsite')
),
map(([key, val]: [string, string]): OutputField => {
map(([key, val]): OutputField => {
let value = val;
switch (key) {
case 'compensation':
Expand All @@ -186,7 +186,7 @@ const generateFields = pipe<Answers, Iterable<OutputField>>([
value: createMarkdownCodeBlock(value.replace(/```/gu, '')),
};
}),
]);
);

const createUserTag = (username: string, discriminator: string) =>
`${username}#${discriminator}`;
Expand Down
6 changes: 1 addition & 5 deletions src/v2/commands/resource/handlers/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ ${BACKTICKS}
`.trim()
);

const transform = pipe<Iterable<ResourceDescription>, string>([
mapTransform,
collect,
(arr: string[]) => arr.join('\n'),
]);
const transform = pipe(mapTransform, collect, arr => arr.join('\n'));

const resources = [
{
Expand Down
8 changes: 4 additions & 4 deletions src/v2/modules/mod/commands/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const generateButtons = (roles: typeof ROLES | typeof NOTIFY_ROLES) =>
.setStyle('SECONDARY')
.setEmoji(item.emoji)
);
const chunkAndRowify = pipe<
Iterable<MessageButton>,
Iterable<MessageActionRow>
>([chunk(5), map(x => new MessageActionRow().addComponents(...x))]);
const chunkAndRowify = pipe(
chunk<MessageButton>(5),
map(x => new MessageActionRow().addComponents(...x))
);

export async function setupRoles(
interaction: CommandInteraction
Expand Down
33 changes: 24 additions & 9 deletions src/v2/utils/pipe.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
type PipeFunctions<Input, Output> = [
(input: Input) => unknown,
...Function[],
(input: unknown) => Output
];
type Fn<A = unknown, B = unknown> = (input: A) => B;

export function pipe<Input, Output>(
fns: PipeFunctions<Input, Output>
): (input: Input) => Output {
return (item: Input) => fns.reduce((input, fn) => fn(input), item) as Output;
export function pipe<A, B>(ab: Fn<A, B>): Fn<A, B>;
export function pipe<A, B, C>(ab: Fn<A, B>, bc: Fn<B, C>): Fn<A, C>;
export function pipe<A, B, C, D>(
ab: Fn<A, B>,
bc: Fn<B, C>,
cd: Fn<C, D>
): Fn<A, D>;
export function pipe<A, B, C, D, E>(
ab: Fn<A, B>,
bc: Fn<B, C>,
cd: Fn<C, D>,
de: Fn<D, E>
): Fn<A, E>;
export function pipe<A, B, C, D, E, F>(
ab: Fn<A, B>,
bc: Fn<B, C>,
cd: Fn<C, D>,
de: Fn<D, E>,
ef: Fn<E, F>
): Fn<A, F>;

export function pipe(...fns: Fn[]): Fn {
return (input: unknown) => fns.reduce((acc, fn) => fn(acc), input);
}