@zoodogood/utils 1.0.4 → 1.0.6-change.694
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/discordjs/mod.d.ts +54 -0
- package/lib/discordjs/mod.js +116 -0
- package/lib/nodejs/getChildProcessUtils.d.ts +0 -1
- package/lib/objectives/DotNotatedInterface.d.ts +23 -0
- package/lib/objectives/DotNotatedInterface.js +60 -0
- package/lib/objectives/GlitchText.js +2 -2
- package/lib/objectives/getRandomElementFromArray.d.ts +7 -0
- package/lib/objectives/getRandomElementFromArray.js +24 -0
- package/lib/objectives/getRandomNumberInRange.d.ts +7 -0
- package/lib/objectives/getRandomNumberInRange.js +8 -0
- package/lib/objectives/mod.d.ts +2 -2
- package/lib/objectives/mod.js +1 -1
- package/lib/primitives/TextTableBuilder.d.ts +66 -0
- package/lib/primitives/TextTableBuilder.js +269 -0
- package/package.json +6 -4
- package/readme.md +4 -1
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a message object for Discord.
|
|
3
|
+
*
|
|
4
|
+
* @param {Object} params = Parameters for creating the message.
|
|
5
|
+
* @param {string} [params.content] - The message content.
|
|
6
|
+
* @param {string} [params.title] - The title of the embed.
|
|
7
|
+
* @param {string} [params.url] - The URL of the embed.
|
|
8
|
+
* @param {Object} [params.author] - The author of the embed.
|
|
9
|
+
* @param {string} [params.thumbnail] - The thumbnail URL of the embed.
|
|
10
|
+
* @param {string} [params.description] - The description of the embed.
|
|
11
|
+
* @param {string} [params.color] - The color of the embed.
|
|
12
|
+
* @param {Object[]} [params.fields] - The fields of the embed.
|
|
13
|
+
* @param {string} [params.image] - The image URL of the embed.
|
|
14
|
+
* @param {string} [params.video] - The video URL of the embed.
|
|
15
|
+
* @param {Object} [params.footer] - The footer of the embed.
|
|
16
|
+
* @param {string} [params.timestamp] - The timestamp of the embed.
|
|
17
|
+
* @param {boolean} [params.ephemeral] - Whether the message should be ephemeral.
|
|
18
|
+
* @param {boolean} [params.fetchReply] - Whether to fetch the message reply.
|
|
19
|
+
* @param {Object[] | Object} [params.components] - The message components.
|
|
20
|
+
* @param {Object[] | Object[]} [params.files] - The message files.
|
|
21
|
+
* @param {string} [params.reference] - The message reference.
|
|
22
|
+
* @returns {Object} - The message object.
|
|
23
|
+
*/
|
|
24
|
+
export function CreateMessage({ content, title, url, author, thumbnail, description, color, fields, image, video, footer, timestamp, ephemeral, fetchReply, components, files, reference, }: {
|
|
25
|
+
content?: string | undefined;
|
|
26
|
+
title?: string | undefined;
|
|
27
|
+
url?: string | undefined;
|
|
28
|
+
author?: Object | undefined;
|
|
29
|
+
thumbnail?: string | undefined;
|
|
30
|
+
description?: string | undefined;
|
|
31
|
+
color?: string | undefined;
|
|
32
|
+
fields?: Object[] | undefined;
|
|
33
|
+
image?: string | undefined;
|
|
34
|
+
video?: string | undefined;
|
|
35
|
+
footer?: Object | undefined;
|
|
36
|
+
timestamp?: string | undefined;
|
|
37
|
+
ephemeral?: boolean | undefined;
|
|
38
|
+
fetchReply?: boolean | undefined;
|
|
39
|
+
components?: Object | Object[] | undefined;
|
|
40
|
+
files?: Object[] | undefined;
|
|
41
|
+
reference?: string | undefined;
|
|
42
|
+
}): Object;
|
|
43
|
+
export function CreateModal({ title, customId, components }: {
|
|
44
|
+
title: any;
|
|
45
|
+
customId: any;
|
|
46
|
+
components: any;
|
|
47
|
+
}): import("discord.js").APIModalInteractionResponseCallbackData;
|
|
48
|
+
export function SimplifyComponents(data: any): any;
|
|
49
|
+
/**
|
|
50
|
+
*
|
|
51
|
+
* @param {*} embed
|
|
52
|
+
* @returns {boolean}
|
|
53
|
+
*/
|
|
54
|
+
export function isEmptyEmbed(embed: any): boolean;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { EmbedBuilder, ModalBuilder, resolveColor, ActionRow, } from "discord.js";
|
|
2
|
+
import { ComponentType } from "discord-api-types/v10";
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param {*} embed
|
|
6
|
+
* @returns {boolean}
|
|
7
|
+
*/
|
|
8
|
+
function isEmptyEmbed(embed) {
|
|
9
|
+
const DEFAULT_PROPERTIES = {
|
|
10
|
+
title: undefined,
|
|
11
|
+
author: undefined,
|
|
12
|
+
thumbnail: undefined,
|
|
13
|
+
description: undefined,
|
|
14
|
+
fields: undefined,
|
|
15
|
+
image: undefined,
|
|
16
|
+
footer: undefined,
|
|
17
|
+
timestamp: undefined,
|
|
18
|
+
video: undefined,
|
|
19
|
+
};
|
|
20
|
+
// in isEmptyEmbed context is a good compare
|
|
21
|
+
const isEqualDefault = (key) => String(embed[key]) === String(DEFAULT_PROPERTIES[key]);
|
|
22
|
+
const isEveryPropertyDefault = Object.keys(DEFAULT_PROPERTIES).every(isEqualDefault);
|
|
23
|
+
return isEveryPropertyDefault;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create a message object for Discord.
|
|
27
|
+
*
|
|
28
|
+
* @param {Object} params = Parameters for creating the message.
|
|
29
|
+
* @param {string} [params.content] - The message content.
|
|
30
|
+
* @param {string} [params.title] - The title of the embed.
|
|
31
|
+
* @param {string} [params.url] - The URL of the embed.
|
|
32
|
+
* @param {Object} [params.author] - The author of the embed.
|
|
33
|
+
* @param {string} [params.thumbnail] - The thumbnail URL of the embed.
|
|
34
|
+
* @param {string} [params.description] - The description of the embed.
|
|
35
|
+
* @param {string} [params.color] - The color of the embed.
|
|
36
|
+
* @param {Object[]} [params.fields] - The fields of the embed.
|
|
37
|
+
* @param {string} [params.image] - The image URL of the embed.
|
|
38
|
+
* @param {string} [params.video] - The video URL of the embed.
|
|
39
|
+
* @param {Object} [params.footer] - The footer of the embed.
|
|
40
|
+
* @param {string} [params.timestamp] - The timestamp of the embed.
|
|
41
|
+
* @param {boolean} [params.ephemeral] - Whether the message should be ephemeral.
|
|
42
|
+
* @param {boolean} [params.fetchReply] - Whether to fetch the message reply.
|
|
43
|
+
* @param {Object[] | Object} [params.components] - The message components.
|
|
44
|
+
* @param {Object[] | Object[]} [params.files] - The message files.
|
|
45
|
+
* @param {string} [params.reference] - The message reference.
|
|
46
|
+
* @returns {Object} - The message object.
|
|
47
|
+
*/
|
|
48
|
+
function CreateMessage({ content, title, url, author, thumbnail, description, color, fields, image, video, footer, timestamp, ephemeral, fetchReply, components, files, reference, }) {
|
|
49
|
+
const message = {};
|
|
50
|
+
thumbnail && (thumbnail = { url: thumbnail });
|
|
51
|
+
image && (image = { url: image });
|
|
52
|
+
video && (video = { url: video });
|
|
53
|
+
color = resolveColor(color !== null && color !== void 0 ? color : "Random");
|
|
54
|
+
const embed = new EmbedBuilder({
|
|
55
|
+
title,
|
|
56
|
+
url,
|
|
57
|
+
author,
|
|
58
|
+
thumbnail,
|
|
59
|
+
description,
|
|
60
|
+
color,
|
|
61
|
+
fields,
|
|
62
|
+
image,
|
|
63
|
+
video,
|
|
64
|
+
footer,
|
|
65
|
+
timestamp,
|
|
66
|
+
});
|
|
67
|
+
if (!isEmptyEmbed(embed.data)) {
|
|
68
|
+
message.embeds = [embed];
|
|
69
|
+
}
|
|
70
|
+
if (files) {
|
|
71
|
+
message.files = files;
|
|
72
|
+
}
|
|
73
|
+
if (reference)
|
|
74
|
+
message.reply = {
|
|
75
|
+
messageReference: reference,
|
|
76
|
+
};
|
|
77
|
+
message.components = components ? SimplifyComponents(components) : null;
|
|
78
|
+
message.content = content;
|
|
79
|
+
message.ephemeral = ephemeral;
|
|
80
|
+
message.fetchReply = fetchReply;
|
|
81
|
+
return message;
|
|
82
|
+
}
|
|
83
|
+
function CreateModal({ title, customId, components }) {
|
|
84
|
+
components = SimplifyComponents(components);
|
|
85
|
+
return new ModalBuilder({ title, customId, components }).toJSON();
|
|
86
|
+
}
|
|
87
|
+
function SimplifyComponents(data) {
|
|
88
|
+
if (data instanceof Array && data.length === 0)
|
|
89
|
+
return [];
|
|
90
|
+
const isComponent = (component) => "type" in component && component.type !== ComponentType.ActionRow;
|
|
91
|
+
const isActionRow = (component) => (component instanceof Array && isComponent(component.at(0))) ||
|
|
92
|
+
component instanceof ActionRow;
|
|
93
|
+
const isComponents = (component) => component instanceof Array && isActionRow(component.at(0));
|
|
94
|
+
const argumentType = [
|
|
95
|
+
isComponent(data),
|
|
96
|
+
isActionRow(data),
|
|
97
|
+
isComponents(data),
|
|
98
|
+
].findIndex(Boolean);
|
|
99
|
+
if (argumentType === -1)
|
|
100
|
+
throw new TypeError("expected component");
|
|
101
|
+
const wrapToArray = (component) => [component];
|
|
102
|
+
const arrayToActionRow = (componentsArray) => {
|
|
103
|
+
const isActionRow = "type" in componentsArray &&
|
|
104
|
+
componentsArray.type === ComponentType.ActionRow;
|
|
105
|
+
if (isActionRow)
|
|
106
|
+
return componentsArray;
|
|
107
|
+
return { type: ComponentType.ActionRow, components: componentsArray };
|
|
108
|
+
};
|
|
109
|
+
if (argumentType <= 0)
|
|
110
|
+
data = wrapToArray(data);
|
|
111
|
+
if (argumentType <= 1)
|
|
112
|
+
data = wrapToArray(data);
|
|
113
|
+
data = data.map(arrayToActionRow);
|
|
114
|
+
return data;
|
|
115
|
+
}
|
|
116
|
+
export { CreateMessage, CreateModal, SimplifyComponents, isEmptyEmbed };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
interface IMethodsOptions {
|
|
2
|
+
defaultValue?: any;
|
|
3
|
+
allowSetFunctions?: boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare class DotNotatedInterface {
|
|
6
|
+
target: Record<string, unknown>;
|
|
7
|
+
constructor(target: DotNotatedInterface["target"]);
|
|
8
|
+
getItem<T>(key: string, options?: IMethodsOptions): T | null;
|
|
9
|
+
setItem<T>(key: string, value: CallableFunction, options?: IMethodsOptions): T;
|
|
10
|
+
hasItem(key: string, options?: IMethodsOptions): boolean;
|
|
11
|
+
removeItem(key: string, options?: IMethodsOptions): boolean;
|
|
12
|
+
getParentAndlastSegmentByNotatedKey(key: string, { needFillIfParentNotExists }: {
|
|
13
|
+
needFillIfParentNotExists?: boolean | undefined;
|
|
14
|
+
}): {
|
|
15
|
+
parent: object | null;
|
|
16
|
+
lastSegment: string;
|
|
17
|
+
};
|
|
18
|
+
fillIsNotExists({ parent, segment, }: {
|
|
19
|
+
parent: {};
|
|
20
|
+
segment: string;
|
|
21
|
+
}): string;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export class DotNotatedInterface {
|
|
2
|
+
constructor(target) {
|
|
3
|
+
this.target = target;
|
|
4
|
+
}
|
|
5
|
+
getItem(key, options = {}) {
|
|
6
|
+
var _a;
|
|
7
|
+
const { parent, lastSegment } = this.getParentAndlastSegmentByNotatedKey(key, { needFillIfParentNotExists: false });
|
|
8
|
+
if (!parent || lastSegment in parent === false) {
|
|
9
|
+
return (_a = options.defaultValue) !== null && _a !== void 0 ? _a : null;
|
|
10
|
+
}
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
return parent[lastSegment];
|
|
13
|
+
}
|
|
14
|
+
setItem(key, value, options = {}) {
|
|
15
|
+
if (typeof value === "function" && !options.allowSetFunctions) {
|
|
16
|
+
value = value(this.getItem(key, options));
|
|
17
|
+
}
|
|
18
|
+
const { parent, lastSegment } = this.getParentAndlastSegmentByNotatedKey(key, { needFillIfParentNotExists: true });
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
return (parent[lastSegment] = value);
|
|
21
|
+
}
|
|
22
|
+
hasItem(key, options = {}) {
|
|
23
|
+
const { parent, lastSegment } = this.getParentAndlastSegmentByNotatedKey(key, { needFillIfParentNotExists: false });
|
|
24
|
+
if (parent === null) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return lastSegment in parent;
|
|
28
|
+
}
|
|
29
|
+
removeItem(key, options = {}) {
|
|
30
|
+
const { parent, lastSegment } = this.getParentAndlastSegmentByNotatedKey(key, { needFillIfParentNotExists: false });
|
|
31
|
+
if (!parent) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
return delete parent[lastSegment];
|
|
36
|
+
}
|
|
37
|
+
getParentAndlastSegmentByNotatedKey(key, { needFillIfParentNotExists = false }) {
|
|
38
|
+
const pathSegments = key.split(".");
|
|
39
|
+
if (!pathSegments.length) {
|
|
40
|
+
throw new Error("Empty path to property");
|
|
41
|
+
}
|
|
42
|
+
let parent = this.target;
|
|
43
|
+
for (let segment of pathSegments.slice(0, -1)) {
|
|
44
|
+
needFillIfParentNotExists &&
|
|
45
|
+
(segment = this.fillIsNotExists({ parent, segment }));
|
|
46
|
+
if (parent[segment] === undefined) {
|
|
47
|
+
return { parent: null, lastSegment: segment };
|
|
48
|
+
}
|
|
49
|
+
parent = parent[segment];
|
|
50
|
+
}
|
|
51
|
+
return { parent, lastSegment: pathSegments.at(-1) };
|
|
52
|
+
}
|
|
53
|
+
fillIsNotExists({ parent, segment, }) {
|
|
54
|
+
if (segment in parent === false) {
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
parent[segment] = {};
|
|
57
|
+
}
|
|
58
|
+
return segment;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getRandomNumberInRange } from "./getRandomNumberInRange.js";
|
|
2
2
|
class GlitchText {
|
|
3
3
|
constructor(from = "", to = "hello, world", { step = 15, random = false, maximum = 100 } = {}) {
|
|
4
4
|
this.from = from;
|
|
@@ -18,7 +18,7 @@ class GlitchText {
|
|
|
18
18
|
word.pop();
|
|
19
19
|
if (word.length < target.length)
|
|
20
20
|
word.push(String.fromCharCode(~~(Math.random() * 50)));
|
|
21
|
-
word.forEach((_, index, array) => array[index] = String.fromCharCode(
|
|
21
|
+
word.forEach((_, index, array) => array[index] = String.fromCharCode(getRandomNumberInRange({ min: MIN, max: MAX })));
|
|
22
22
|
yield word.join("");
|
|
23
23
|
}
|
|
24
24
|
if (this.maximum)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
interface IParams {
|
|
2
|
+
needPop?: boolean;
|
|
3
|
+
associatedWeights?: number[];
|
|
4
|
+
}
|
|
5
|
+
export declare function getRandomElementIndexInWeights(weights: NonNullable<IParams["associatedWeights"]>): number | never;
|
|
6
|
+
export declare function getRandomElementFromArray<T>(array: T[], { needPop, associatedWeights }?: IParams): T;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { getRandomNumberInRange } from "./getRandomNumberInRange.js";
|
|
2
|
+
export function getRandomElementIndexInWeights(weights) {
|
|
3
|
+
if (weights.length < 1) {
|
|
4
|
+
new Error("Invalid array length");
|
|
5
|
+
}
|
|
6
|
+
let previousLimit = 0;
|
|
7
|
+
const thresholds = weights.map((weight) => (previousLimit += weight));
|
|
8
|
+
const lotterySecretNumber = Math.random() * thresholds.at(-1);
|
|
9
|
+
return thresholds.findIndex((threshold) => threshold >= lotterySecretNumber);
|
|
10
|
+
}
|
|
11
|
+
export function getRandomElementFromArray(array, { needPop, associatedWeights } = {}) {
|
|
12
|
+
if (associatedWeights && associatedWeights.length !== array.length) {
|
|
13
|
+
throw new Error("Incorrectly passed argument associatedWeights: The length of the associatedWeights must exactly match the length of the weights array");
|
|
14
|
+
}
|
|
15
|
+
const index = associatedWeights
|
|
16
|
+
? getRandomElementIndexInWeights(associatedWeights)
|
|
17
|
+
: getRandomNumberInRange({ max: array.length });
|
|
18
|
+
const input = array[index];
|
|
19
|
+
if (needPop) {
|
|
20
|
+
array.splice(index, 1);
|
|
21
|
+
associatedWeights === null || associatedWeights === void 0 ? void 0 : associatedWeights.splice(index, 1);
|
|
22
|
+
}
|
|
23
|
+
return input;
|
|
24
|
+
}
|
package/lib/objectives/mod.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export * from './CustomCollector.js';
|
|
2
2
|
export * from './GlitchText.js';
|
|
3
|
-
export * from './
|
|
3
|
+
export * from './getRandomNumberInRange.js';
|
|
4
4
|
export * from './rangeToArray.js';
|
|
5
|
-
declare function omit(object:
|
|
5
|
+
declare function omit(object: Object, filter: CallableFunction): {
|
|
6
6
|
[k: string]: any;
|
|
7
7
|
};
|
|
8
8
|
export { omit };
|
package/lib/objectives/mod.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from './CustomCollector.js';
|
|
2
2
|
export * from './GlitchText.js';
|
|
3
|
-
export * from './
|
|
3
|
+
export * from './getRandomNumberInRange.js';
|
|
4
4
|
export * from './rangeToArray.js';
|
|
5
5
|
function omit(object, filter) {
|
|
6
6
|
const entries = Object.entries(object)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
interface ITextTableGeneratorContext {
|
|
2
|
+
content: string;
|
|
3
|
+
metadata: IContextMetadata;
|
|
4
|
+
currentRow: number;
|
|
5
|
+
}
|
|
6
|
+
interface IContextMetadata {
|
|
7
|
+
columns?: {
|
|
8
|
+
largestLength: number;
|
|
9
|
+
}[];
|
|
10
|
+
tableWidth?: number;
|
|
11
|
+
separatorsIndexesInRow?: number[];
|
|
12
|
+
}
|
|
13
|
+
declare enum CellAlignEnum {
|
|
14
|
+
Left = 0,
|
|
15
|
+
Right = 1,
|
|
16
|
+
Center = 2
|
|
17
|
+
}
|
|
18
|
+
interface ICellOptions {
|
|
19
|
+
gapLeft: number;
|
|
20
|
+
gapRight: number;
|
|
21
|
+
align: CellAlignEnum;
|
|
22
|
+
removeNextSeparator?: boolean;
|
|
23
|
+
}
|
|
24
|
+
interface ITableCell {
|
|
25
|
+
value: string;
|
|
26
|
+
options: ICellOptions;
|
|
27
|
+
}
|
|
28
|
+
type TCellSetSymbolCallback = (context: ITextTableGeneratorContext, index: number) => string;
|
|
29
|
+
interface ITableOptions {
|
|
30
|
+
borderLeft: null | TCellSetSymbolCallback;
|
|
31
|
+
borderRight: null | TCellSetSymbolCallback;
|
|
32
|
+
borderTop: null | TCellSetSymbolCallback;
|
|
33
|
+
borderBottom: null | TCellSetSymbolCallback;
|
|
34
|
+
separator: string;
|
|
35
|
+
}
|
|
36
|
+
declare enum BorderDirectionEnum {
|
|
37
|
+
BorderLeft = 0,
|
|
38
|
+
BorderRight = 1,
|
|
39
|
+
BorderTop = 2,
|
|
40
|
+
BorderBottom = 3
|
|
41
|
+
}
|
|
42
|
+
declare enum SpecialRowTypeEnum {
|
|
43
|
+
Default = 0,
|
|
44
|
+
Display = 1
|
|
45
|
+
}
|
|
46
|
+
interface ITableSpecialDisplayRow {
|
|
47
|
+
type: SpecialRowTypeEnum.Display;
|
|
48
|
+
display: TCellSetSymbolCallback;
|
|
49
|
+
}
|
|
50
|
+
type TTableRow = ITableCell[] | ITableSpecialDisplayRow;
|
|
51
|
+
declare class TextTableBuilder {
|
|
52
|
+
rows: TTableRow[];
|
|
53
|
+
protected options: ITableOptions;
|
|
54
|
+
setBorderOptions(callback?: TCellSetSymbolCallback, directions?: BorderDirectionEnum[]): this;
|
|
55
|
+
addRowWithElements(elements: ITableCell["value"][], optionsForEveryElement?: Partial<ICellOptions>): this;
|
|
56
|
+
addMultilineRowWithElements(elements: ITableCell["value"][], optionsForEveryElement?: Partial<ICellOptions>): this;
|
|
57
|
+
addEmptyRow(): this;
|
|
58
|
+
addRowSeparator(setSymbol?: TCellSetSymbolCallback): this;
|
|
59
|
+
addCellAtRow(rowIndex: number, cellValue: ITableCell["value"], cellOptions: ICellOptions): this;
|
|
60
|
+
pushCellToArray<T = ITableCell>(array: (T | ITableCell)[], cellValue: ITableCell["value"], cellOptions?: Partial<ICellOptions>): this;
|
|
61
|
+
pushRowToTable(row: TTableRow): this;
|
|
62
|
+
generateTextContent(): string;
|
|
63
|
+
}
|
|
64
|
+
export { TextTableBuilder };
|
|
65
|
+
export { SpecialRowTypeEnum, CellAlignEnum, BorderDirectionEnum };
|
|
66
|
+
export type { ICellOptions, IContextMetadata, ITableCell, ITableOptions, ITextTableGeneratorContext, TTableRow, };
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
function getRowType(row) {
|
|
2
|
+
if ("type" in row === false) {
|
|
3
|
+
return SpecialRowTypeEnum.Default;
|
|
4
|
+
}
|
|
5
|
+
return row.type;
|
|
6
|
+
}
|
|
7
|
+
function isCell(target) {
|
|
8
|
+
return "value" in target && "options" in target;
|
|
9
|
+
}
|
|
10
|
+
class TextTableGenerator {
|
|
11
|
+
constructor(rows, options) {
|
|
12
|
+
this.data = [];
|
|
13
|
+
this.context = {
|
|
14
|
+
content: "",
|
|
15
|
+
metadata: {},
|
|
16
|
+
currentRow: 0,
|
|
17
|
+
};
|
|
18
|
+
this.data = rows;
|
|
19
|
+
this.options = options;
|
|
20
|
+
this.parseMetadata();
|
|
21
|
+
}
|
|
22
|
+
generateTextContent() {
|
|
23
|
+
const context = this.context;
|
|
24
|
+
if (this.options.borderTop !== null) {
|
|
25
|
+
context.content += this.drawBlockBorder(BorderDirectionEnum.BorderTop);
|
|
26
|
+
context.content += "\n";
|
|
27
|
+
}
|
|
28
|
+
for (const row of this.data) {
|
|
29
|
+
context.currentRow = this.data.indexOf(row);
|
|
30
|
+
context.content += this.drawRow(row);
|
|
31
|
+
context.content += "\n";
|
|
32
|
+
}
|
|
33
|
+
if (this.options.borderBottom !== null) {
|
|
34
|
+
context.content += this.drawBlockBorder(BorderDirectionEnum.BorderBottom);
|
|
35
|
+
}
|
|
36
|
+
return context.content;
|
|
37
|
+
}
|
|
38
|
+
getColumns() {
|
|
39
|
+
const columns = [];
|
|
40
|
+
const rows = this.data;
|
|
41
|
+
const largestCellsCount = Math.max(...rows
|
|
42
|
+
.filter((row) => getRowType(row) === SpecialRowTypeEnum.Default)
|
|
43
|
+
.map((row) => row.length));
|
|
44
|
+
for (let index = 0; index < largestCellsCount; index++) {
|
|
45
|
+
const column = rows.map((row) => getRowType(row) === SpecialRowTypeEnum.Default
|
|
46
|
+
? row.at(index)
|
|
47
|
+
: { row });
|
|
48
|
+
columns.push(column);
|
|
49
|
+
}
|
|
50
|
+
return columns;
|
|
51
|
+
}
|
|
52
|
+
parseMetadata() {
|
|
53
|
+
const metadata = this.context.metadata;
|
|
54
|
+
const columns = this.getColumns();
|
|
55
|
+
const columnsMetadata = columns.map((column) => ({
|
|
56
|
+
largestLength: Math.max(...column.map((element) => isCell(element)
|
|
57
|
+
? this.calculateCellMinWidth(element)
|
|
58
|
+
: 0)),
|
|
59
|
+
}));
|
|
60
|
+
metadata.columns = columnsMetadata;
|
|
61
|
+
metadata.tableWidth = this.calculateTableWidth();
|
|
62
|
+
metadata.separatorsIndexesInRow = (() => {
|
|
63
|
+
const indexes = [];
|
|
64
|
+
let current = 0;
|
|
65
|
+
if (!!this.options.borderLeft) {
|
|
66
|
+
indexes.push(current);
|
|
67
|
+
}
|
|
68
|
+
for (const { largestLength } of columnsMetadata) {
|
|
69
|
+
current += largestLength + 1;
|
|
70
|
+
indexes.push(current);
|
|
71
|
+
}
|
|
72
|
+
if (!this.options.borderRight) {
|
|
73
|
+
indexes.pop();
|
|
74
|
+
}
|
|
75
|
+
return indexes;
|
|
76
|
+
})();
|
|
77
|
+
}
|
|
78
|
+
drawRow(row) {
|
|
79
|
+
var _a, _b, _c, _d, _e, _f;
|
|
80
|
+
let content = "";
|
|
81
|
+
const context = this.context;
|
|
82
|
+
const metadata = context.metadata;
|
|
83
|
+
if (getRowType(row) === SpecialRowTypeEnum.Default) {
|
|
84
|
+
row = row;
|
|
85
|
+
content += (_c = (_b = (_a = this.options).borderLeft) === null || _b === void 0 ? void 0 : _b.call(_a, context, context.currentRow)) !== null && _c !== void 0 ? _c : "";
|
|
86
|
+
for (const cellIndex in row) {
|
|
87
|
+
const cell = row.at(+cellIndex);
|
|
88
|
+
const expectedWidth = metadata.columns.at(+cellIndex).largestLength;
|
|
89
|
+
content += this.drawCell(cell, expectedWidth);
|
|
90
|
+
if (+cellIndex !== row.length - 1) {
|
|
91
|
+
content += !cell.options.removeNextSeparator
|
|
92
|
+
? this.options.separator
|
|
93
|
+
: " ";
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
content += (_f = (_e = (_d = this.options).borderRight) === null || _e === void 0 ? void 0 : _e.call(_d, context, context.currentRow)) !== null && _f !== void 0 ? _f : "";
|
|
97
|
+
return content;
|
|
98
|
+
}
|
|
99
|
+
if (getRowType(row) === SpecialRowTypeEnum.Display) {
|
|
100
|
+
row = row;
|
|
101
|
+
content += this.drawLine(row.display);
|
|
102
|
+
}
|
|
103
|
+
return content;
|
|
104
|
+
}
|
|
105
|
+
drawCell(cell, expectedWidth) {
|
|
106
|
+
const { gapLeft, gapRight, align } = cell.options;
|
|
107
|
+
const { value } = cell;
|
|
108
|
+
const minContentLength = this.calculateCellMinWidth(cell);
|
|
109
|
+
const addidableGapLeft = align === CellAlignEnum.Right
|
|
110
|
+
? expectedWidth - minContentLength
|
|
111
|
+
: align === CellAlignEnum.Center
|
|
112
|
+
? Math.floor((expectedWidth - minContentLength) / 2)
|
|
113
|
+
: 0;
|
|
114
|
+
const addidableGapRight = align === CellAlignEnum.Left
|
|
115
|
+
? expectedWidth - minContentLength
|
|
116
|
+
: align === CellAlignEnum.Center
|
|
117
|
+
? Math.ceil((expectedWidth - minContentLength) / 2)
|
|
118
|
+
: 0;
|
|
119
|
+
return `${" ".repeat(gapLeft + addidableGapLeft)}${value}${" ".repeat(gapRight + addidableGapRight)}`;
|
|
120
|
+
}
|
|
121
|
+
drawLine(setSymbol) {
|
|
122
|
+
let content = "";
|
|
123
|
+
const context = this.context;
|
|
124
|
+
const length = this.calculateTableWidth();
|
|
125
|
+
for (const index in [...new Array(length)]) {
|
|
126
|
+
const symbol = setSymbol(context, +index);
|
|
127
|
+
content += symbol;
|
|
128
|
+
}
|
|
129
|
+
return content;
|
|
130
|
+
}
|
|
131
|
+
drawBlockBorder(borderDirection) {
|
|
132
|
+
const callback = borderDirection === BorderDirectionEnum.BorderTop
|
|
133
|
+
? this.options.borderTop
|
|
134
|
+
: this.options.borderBottom;
|
|
135
|
+
return this.drawLine(callback);
|
|
136
|
+
}
|
|
137
|
+
calculateTableWidth() {
|
|
138
|
+
const { columns } = this.context.metadata;
|
|
139
|
+
if (!columns) {
|
|
140
|
+
throw new Error("No columns field; Tip: Use <this>.parseMetadata() previous");
|
|
141
|
+
}
|
|
142
|
+
const cells = columns.reduce((acc, current) => current.largestLength + acc, 0);
|
|
143
|
+
const separators = columns.length - 1;
|
|
144
|
+
const borders = +!!this.options.borderLeft + +!!this.options.borderRight;
|
|
145
|
+
return cells + separators + borders;
|
|
146
|
+
}
|
|
147
|
+
calculateCellMinWidth(cell) {
|
|
148
|
+
return cell.value.length + cell.options.gapLeft + cell.options.gapRight;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
var CellAlignEnum;
|
|
152
|
+
(function (CellAlignEnum) {
|
|
153
|
+
CellAlignEnum[CellAlignEnum["Left"] = 0] = "Left";
|
|
154
|
+
CellAlignEnum[CellAlignEnum["Right"] = 1] = "Right";
|
|
155
|
+
CellAlignEnum[CellAlignEnum["Center"] = 2] = "Center";
|
|
156
|
+
})(CellAlignEnum || (CellAlignEnum = {}));
|
|
157
|
+
const DEFAULT_CELL_OPTIONS = {
|
|
158
|
+
gapLeft: 2,
|
|
159
|
+
gapRight: 2,
|
|
160
|
+
align: CellAlignEnum.Left,
|
|
161
|
+
};
|
|
162
|
+
var BorderDirectionEnum;
|
|
163
|
+
(function (BorderDirectionEnum) {
|
|
164
|
+
BorderDirectionEnum[BorderDirectionEnum["BorderLeft"] = 0] = "BorderLeft";
|
|
165
|
+
BorderDirectionEnum[BorderDirectionEnum["BorderRight"] = 1] = "BorderRight";
|
|
166
|
+
BorderDirectionEnum[BorderDirectionEnum["BorderTop"] = 2] = "BorderTop";
|
|
167
|
+
BorderDirectionEnum[BorderDirectionEnum["BorderBottom"] = 3] = "BorderBottom";
|
|
168
|
+
})(BorderDirectionEnum || (BorderDirectionEnum = {}));
|
|
169
|
+
const DEFAULT_TABLE_OPTIONS = {
|
|
170
|
+
borderLeft: null,
|
|
171
|
+
borderRight: null,
|
|
172
|
+
borderTop: null,
|
|
173
|
+
borderBottom: null,
|
|
174
|
+
separator: "|",
|
|
175
|
+
};
|
|
176
|
+
var SpecialRowTypeEnum;
|
|
177
|
+
(function (SpecialRowTypeEnum) {
|
|
178
|
+
SpecialRowTypeEnum[SpecialRowTypeEnum["Default"] = 0] = "Default";
|
|
179
|
+
SpecialRowTypeEnum[SpecialRowTypeEnum["Display"] = 1] = "Display";
|
|
180
|
+
})(SpecialRowTypeEnum || (SpecialRowTypeEnum = {}));
|
|
181
|
+
class TextTableBuilder {
|
|
182
|
+
constructor() {
|
|
183
|
+
this.rows = [];
|
|
184
|
+
this.options = DEFAULT_TABLE_OPTIONS;
|
|
185
|
+
}
|
|
186
|
+
setBorderOptions(callback = () => "|", directions = [
|
|
187
|
+
BorderDirectionEnum.BorderLeft,
|
|
188
|
+
BorderDirectionEnum.BorderRight,
|
|
189
|
+
]) {
|
|
190
|
+
for (const direction of directions) {
|
|
191
|
+
switch (direction) {
|
|
192
|
+
case BorderDirectionEnum.BorderLeft:
|
|
193
|
+
this.options.borderLeft = callback;
|
|
194
|
+
break;
|
|
195
|
+
case BorderDirectionEnum.BorderRight:
|
|
196
|
+
this.options.borderRight = callback;
|
|
197
|
+
break;
|
|
198
|
+
case BorderDirectionEnum.BorderTop:
|
|
199
|
+
this.options.borderTop = callback;
|
|
200
|
+
break;
|
|
201
|
+
case BorderDirectionEnum.BorderBottom:
|
|
202
|
+
this.options.borderBottom = callback;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return this;
|
|
207
|
+
}
|
|
208
|
+
addRowWithElements(elements, optionsForEveryElement) {
|
|
209
|
+
const row = [];
|
|
210
|
+
for (const value of elements) {
|
|
211
|
+
this.pushCellToArray(row, value, optionsForEveryElement);
|
|
212
|
+
}
|
|
213
|
+
this.pushRowToTable(row);
|
|
214
|
+
return this;
|
|
215
|
+
}
|
|
216
|
+
addMultilineRowWithElements(elements, optionsForEveryElement) {
|
|
217
|
+
const separatedElements = elements.map((value) => value.split("\n"));
|
|
218
|
+
const largestHeight = Math.max(...separatedElements.map((sub) => sub.length));
|
|
219
|
+
for (let index = 0; index < largestHeight; index++) {
|
|
220
|
+
this.addRowWithElements(separatedElements.map((sub) => { var _a; return (_a = sub.at(index)) !== null && _a !== void 0 ? _a : ""; }), optionsForEveryElement);
|
|
221
|
+
}
|
|
222
|
+
return this;
|
|
223
|
+
}
|
|
224
|
+
addEmptyRow() {
|
|
225
|
+
this.pushRowToTable([]);
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
addRowSeparator(setSymbol = () => "-") {
|
|
229
|
+
const row = {
|
|
230
|
+
display: setSymbol,
|
|
231
|
+
type: SpecialRowTypeEnum.Display,
|
|
232
|
+
};
|
|
233
|
+
this.pushRowToTable(row);
|
|
234
|
+
return this;
|
|
235
|
+
}
|
|
236
|
+
addCellAtRow(rowIndex, cellValue, cellOptions) {
|
|
237
|
+
const row = this.rows.at(rowIndex);
|
|
238
|
+
if (!row) {
|
|
239
|
+
throw new RangeError("");
|
|
240
|
+
}
|
|
241
|
+
if (getRowType(row) !== SpecialRowTypeEnum.Default) {
|
|
242
|
+
throw new Error("Is not default row: without cells");
|
|
243
|
+
}
|
|
244
|
+
this.pushCellToArray(row, cellValue, cellOptions);
|
|
245
|
+
return this;
|
|
246
|
+
}
|
|
247
|
+
pushCellToArray(array, cellValue, cellOptions = {}) {
|
|
248
|
+
const options = Object.assign({}, DEFAULT_CELL_OPTIONS, cellOptions);
|
|
249
|
+
array.push({ value: cellValue, options });
|
|
250
|
+
return this;
|
|
251
|
+
}
|
|
252
|
+
pushRowToTable(row) {
|
|
253
|
+
this.rows.push(row);
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
generateTextContent() {
|
|
257
|
+
return new TextTableGenerator(this.rows, this.options).generateTextContent();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
export { TextTableBuilder };
|
|
261
|
+
export { SpecialRowTypeEnum, CellAlignEnum, BorderDirectionEnum };
|
|
262
|
+
const builder = new TextTableBuilder()
|
|
263
|
+
.setBorderOptions()
|
|
264
|
+
.addRowWithElements(["1", "212", "3"], { align: CellAlignEnum.Left })
|
|
265
|
+
.addRowWithElements(["1", "2", "3"], { align: CellAlignEnum.Right })
|
|
266
|
+
.addRowSeparator()
|
|
267
|
+
.addRowWithElements(["777", "555", "1999"], { align: CellAlignEnum.Center });
|
|
268
|
+
// the example has been simplified
|
|
269
|
+
console.log(builder.generateTextContent());
|
package/package.json
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zoodogood/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.6-change.694",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/index.d.ts",
|
|
8
8
|
"homepage": "https://zoodogood.github.io/utils",
|
|
9
9
|
"scripts": {
|
|
10
|
-
"test": "
|
|
10
|
+
"test": "vitest",
|
|
11
11
|
"docs-build": "cd ./docs && retype build --output ./public",
|
|
12
12
|
"docs-watch": "cd ./docs && retype watch",
|
|
13
|
-
"prepack": "tsc"
|
|
13
|
+
"prepack": "tsc",
|
|
14
|
+
"build": "tsc"
|
|
14
15
|
},
|
|
15
16
|
"typings": "lib/index",
|
|
16
17
|
"exports": {
|
|
@@ -31,7 +32,8 @@
|
|
|
31
32
|
"@types/node": "^20.5.9",
|
|
32
33
|
"discord.js": "^14.13.0",
|
|
33
34
|
"retypeapp-linux-x64": "^3.3.0",
|
|
34
|
-
"typescript": "^5.2.2"
|
|
35
|
+
"typescript": "^5.2.2",
|
|
36
|
+
"vitest": "^0.34.5"
|
|
35
37
|
},
|
|
36
38
|
"peerDependencies": {
|
|
37
39
|
"discord.js": "=>14.x.x"
|
package/readme.md
CHANGED
|
@@ -7,4 +7,7 @@ https://zoodogood.github.io/utils/public
|
|
|
7
7
|
export { ending } from '@zoodogood/utils/primitives';
|
|
8
8
|
```
|
|
9
9
|
|
|
10
|
-
[NPM](https://www.npmjs.com/package/@zoodogood/utils)
|
|
10
|
+
[NPM](https://www.npmjs.com/package/@zoodogood/utils)
|
|
11
|
+
|
|
12
|
+
#### From developer:
|
|
13
|
+
(Wa) Use only documented functions that are not marked "Unstable". Thanks.
|