generated from didinele/typescript-docker-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move /utilities into this repo
- Loading branch information
Showing
63 changed files
with
4,609 additions
and
2,341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
yarn commitlint --edit $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
yarn build && yarn lint | ||
yarn build && yarn lint && yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# `@chatsift/discord-utils` | ||
|
||
[![GitHub](https://img.shields.io/badge/License-GNU%20AGPLv3-yellow.svg)](https://github.com/chatsift/automoderator/blob/main/LICENSE) | ||
[![npm](https://img.shields.io/npm/v/@chatsift/discord-utils?color=crimson&logo=npm)](https://www.npmjs.com/package/@chatsift/discord-utils) | ||
[![TypeScript](https://github.com/chatsift/automoderator/actions/workflows/test.yml/badge.svg)](https://github.com/chatsift/automoderator/actions/workflows/test.yml) | ||
|
||
Niche utilities for working with Discord's API | ||
|
||
## Installation | ||
|
||
- `npm install @chatsift/discord-utils` | ||
- `pnpm install @chatsift/discord-utils` | ||
- `yarn add @chatsift/discord-utils` | ||
|
||
## Contributing | ||
|
||
Please see the main [README.md](https://github.com/chatsift/automoderator) for info on how to contribute to this package or the other `@chatsift` packages. | ||
|
||
## LICENSE | ||
|
||
This project is licensed under the GNU AGPLv3 license. | ||
|
||
It should, however, be noted that some packages are forks of other open source projects, and are therefore, sub-licensed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "@chatsift/discord-utils", | ||
"description": "Niche utilities for working with Discord's raw API", | ||
"version": "0.5.0", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
}, | ||
"directories": { | ||
"lib": "src" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "tsup && tsc", | ||
"test": "vitest run", | ||
"lint": "eslint src", | ||
"prepack": "yarn build" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/chatsift/automoderator.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/chatsift/automoderator/issues" | ||
}, | ||
"homepage": "https://github.com/chatsift/automoderator", | ||
"devDependencies": { | ||
"@types/node": "^20.14.12", | ||
"tsup": "^8.2.3", | ||
"typescript": "^5.5.4", | ||
"vitest": "^2.0.4" | ||
}, | ||
"dependencies": { | ||
"discord-api-types": "^0.37.93", | ||
"tslib": "^2.6.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { APIEmbed, APIEmbedField } from 'discord-api-types/v10'; | ||
import { describe, test, expect } from 'vitest'; | ||
import { addFields, ellipsis, MESSAGE_LIMITS, truncateEmbed } from '../embed.js'; | ||
|
||
describe('addFields', () => { | ||
test('no existing fields', () => { | ||
const embed: APIEmbed = {}; | ||
|
||
const field: APIEmbedField = { name: 'foo', value: 'bar' }; | ||
expect(addFields(embed, field)).toStrictEqual({ ...embed, fields: [field] }); | ||
}); | ||
|
||
test('existing fields', () => { | ||
const field: APIEmbedField = { name: 'foo', value: 'bar' }; | ||
const embed: APIEmbed = { fields: [field] }; | ||
|
||
expect(addFields(embed, field)).toStrictEqual({ ...embed, fields: [field, field] }); | ||
}); | ||
}); | ||
|
||
describe('ellipsis', () => { | ||
test('no ellipsis', () => { | ||
expect(ellipsis('foo', 5)).toBe('foo'); | ||
}); | ||
|
||
test('ellipsis', () => { | ||
expect(ellipsis('foobar', 4)).toBe('f...'); | ||
}); | ||
|
||
test('too long for ellipsis', () => { | ||
expect(ellipsis('foo', 2)).toBe('fo'); | ||
}); | ||
}); | ||
|
||
describe('truncateEmbed', () => { | ||
test('basic embed properties', () => { | ||
const embed: APIEmbed = { | ||
title: 'foo'.repeat(256), | ||
description: 'bar'.repeat(4_096), | ||
author: { name: 'baz'.repeat(256) }, | ||
footer: { text: 'qux'.repeat(2_048) }, | ||
}; | ||
|
||
const truncated = truncateEmbed(embed); | ||
|
||
expect(truncated.title).toBe(ellipsis(embed.title!, MESSAGE_LIMITS.EMBEDS.TITLE)); | ||
expect(truncated.description).toBe(ellipsis(embed.description!, MESSAGE_LIMITS.EMBEDS.DESCRIPTION)); | ||
expect(truncated.author?.name).toBe(ellipsis(embed.author!.name, MESSAGE_LIMITS.EMBEDS.AUTHOR)); | ||
expect(truncated.footer?.text).toBe(ellipsis(embed.footer!.text, MESSAGE_LIMITS.EMBEDS.FOOTER)); | ||
}); | ||
|
||
test('fields', () => { | ||
const embed: APIEmbed = { | ||
fields: Array.from<APIEmbedField>({ length: 30 }).fill({ name: 'foo', value: 'bar' }), | ||
}; | ||
|
||
expect(truncateEmbed(embed).fields).toHaveLength(MESSAGE_LIMITS.EMBEDS.FIELD_COUNT); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { APIChannel } from 'discord-api-types/v10'; | ||
import { ChannelType } from 'discord-api-types/v10'; | ||
import { test, expect } from 'vitest'; | ||
import { sortChannels } from '../sortChannels.js'; | ||
|
||
test('sorting a list of channels', () => { | ||
// Higher position than the category, but should come out on top | ||
const first = { | ||
id: '1', | ||
position: 1, | ||
type: ChannelType.GuildText, | ||
} as unknown as APIChannel; | ||
|
||
const second = { | ||
id: '0', | ||
position: 0, | ||
type: ChannelType.GuildCategory, | ||
} as unknown as APIChannel; | ||
|
||
const third = { | ||
id: '2', | ||
position: 0, | ||
type: ChannelType.GuildText, | ||
parent_id: '0', | ||
} as unknown as APIChannel; | ||
|
||
const fourth = { | ||
id: '3', | ||
position: 1, | ||
type: ChannelType.GuildText, | ||
parent_id: '0', | ||
} as unknown as APIChannel; | ||
|
||
expect(sortChannels([first, second, third, fourth])).toStrictEqual([first, second, third, fourth]); | ||
}); |
Oops, something went wrong.