@zoodogood/utils 1.1.2-change.1051 → 1.1.3-change.1055
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/helpers.d.ts +18 -0
- package/lib/discordjs/helpers.js +48 -0
- package/lib/discordjs/mod.d.ts +2 -0
- package/lib/discordjs/mod.js +2 -0
- package/lib/discordjs/simplify.d.ts +54 -0
- package/lib/discordjs/simplify.js +116 -0
- package/lib/index.d.ts +2 -0
- package/lib/nodejs/getChildProcessUtils.d.ts +23 -0
- package/lib/nodejs/getChildProcessUtils.js +98 -0
- package/lib/nodejs/mod.d.ts +1 -0
- package/lib/nodejs/mod.js +1 -0
- package/lib/objectives/CustomCollector.d.ts +25 -0
- package/lib/objectives/CustomCollector.js +59 -0
- package/lib/objectives/DotNotatedInterface.d.ts +23 -0
- package/lib/objectives/DotNotatedInterface.js +60 -0
- package/lib/objectives/ExtendedEnum/BooleanWithMessage.d.ts +13 -0
- package/lib/objectives/ExtendedEnum/BooleanWithMessage.js +11 -0
- package/lib/objectives/ExtendedEnum/Option.d.ts +13 -0
- package/lib/objectives/ExtendedEnum/Option.js +11 -0
- package/lib/objectives/ExtendedEnum/Result.d.ts +18 -0
- package/lib/objectives/ExtendedEnum/Result.js +16 -0
- package/lib/objectives/ExtendedEnum/index.d.ts +1 -0
- package/lib/objectives/ExtendedEnum/index.js +1 -0
- package/lib/objectives/ExtendedEnum/main.d.ts +17 -0
- package/lib/objectives/ExtendedEnum/main.js +59 -0
- package/lib/objectives/ExtendedEnum/mod.d.ts +2 -0
- package/lib/objectives/ExtendedEnum/mod.js +2 -0
- package/lib/objectives/GlitchText.d.ts +16 -0
- package/lib/objectives/GlitchText.js +50 -0
- package/lib/objectives/equal.d.ts +5 -0
- package/lib/objectives/equal.js +25 -0
- 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/getRandomValue.d.ts +7 -0
- package/lib/objectives/getRandomValue.js +8 -0
- package/lib/objectives/mod.d.ts +11 -0
- package/lib/objectives/mod.js +12 -0
- package/lib/objectives/rangeToArray.d.ts +1 -0
- package/lib/objectives/rangeToArray.js +11 -0
- package/lib/primitives/TextTableBuilder.d.ts +71 -0
- package/lib/primitives/TextTableBuilder.js +319 -0
- package/lib/primitives/mod.d.ts +6 -0
- package/lib/primitives/mod.js +12 -0
- package/package.json +3 -3
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type APIBaseButton = import("discord-api-types/v10").APIButtonComponentBase<any>;
|
|
2
|
+
/**
|
|
3
|
+
* @typedef {import("discord-api-types/v10").APIButtonComponentBase} APIBaseButton
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param {Partial<APIBaseButton>[]} resolable
|
|
8
|
+
*/
|
|
9
|
+
export function justButtonComponents(resolable: Partial<import("discord-api-types/v10").APIButtonComponentBase<any>>[]): {
|
|
10
|
+
label?: string | undefined;
|
|
11
|
+
style: any;
|
|
12
|
+
emoji?: import("discord-api-types/v10").APIMessageComponentEmoji | undefined;
|
|
13
|
+
disabled?: boolean | undefined;
|
|
14
|
+
type: import("discord-api-types/v10").ComponentType.Button | ComponentType;
|
|
15
|
+
customId: string;
|
|
16
|
+
}[];
|
|
17
|
+
export function justSendMessage(target: any, options: any): Promise<any>;
|
|
18
|
+
import { ComponentType as ComponentType_1 } from "discord.js";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { BaseInteraction, ButtonStyle, ComponentType } from "discord.js";
|
|
11
|
+
import { CreateMessage } from "./simplify.js";
|
|
12
|
+
const DEFAULTS_FOR_BUTTON = {
|
|
13
|
+
type: ComponentType.Button,
|
|
14
|
+
style: ButtonStyle.Secondary,
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {import("discord-api-types/v10").APIButtonComponentBase} APIBaseButton
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param {Partial<APIBaseButton>[]} resolable
|
|
22
|
+
*/
|
|
23
|
+
function justButtonComponents(resolable) {
|
|
24
|
+
const buttons = resolable.map((data, i) => (Object.assign(Object.assign(Object.assign({}, DEFAULTS_FOR_BUTTON), { customId: `button.${i + 1}` }), data)));
|
|
25
|
+
return buttons;
|
|
26
|
+
}
|
|
27
|
+
function justSendMessage(target, options) {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
const messagePayload = CreateMessage(options);
|
|
30
|
+
const message = target instanceof BaseInteraction
|
|
31
|
+
? yield (options.edit
|
|
32
|
+
? target.replied
|
|
33
|
+
? target.editReply(messagePayload)
|
|
34
|
+
: target.update(messagePayload)
|
|
35
|
+
: target.reply(messagePayload))
|
|
36
|
+
: yield (options.edit
|
|
37
|
+
? target.edit(messagePayload)
|
|
38
|
+
: target.send(messagePayload));
|
|
39
|
+
if (options.delete) {
|
|
40
|
+
setTimeout(() => message.delete(), options.delete);
|
|
41
|
+
}
|
|
42
|
+
if (options.reactions) {
|
|
43
|
+
options.reactions.filter(Boolean).forEach((react) => message.react(react));
|
|
44
|
+
}
|
|
45
|
+
return message;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
export { justButtonComponents, justSendMessage };
|
|
@@ -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 };
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import type { ChildProcessWithoutNullStreams } from "child_process";
|
|
3
|
+
import EventsEmitter from "events";
|
|
4
|
+
interface IContext {
|
|
5
|
+
exitter: {
|
|
6
|
+
resolve: any;
|
|
7
|
+
reject: any;
|
|
8
|
+
};
|
|
9
|
+
whenEnd: Promise<unknown>;
|
|
10
|
+
child: ChildProcessWithoutNullStreams;
|
|
11
|
+
emitter: EventsEmitter;
|
|
12
|
+
outString: string;
|
|
13
|
+
}
|
|
14
|
+
interface IParams {
|
|
15
|
+
root: string;
|
|
16
|
+
logger?: boolean;
|
|
17
|
+
}
|
|
18
|
+
declare const _default: ({ root, logger }: IParams) => {
|
|
19
|
+
run: (command: string, params: string[]) => IContext;
|
|
20
|
+
info: (log: string) => void;
|
|
21
|
+
_npm: string;
|
|
22
|
+
};
|
|
23
|
+
export default _default;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { spawn } from "child_process";
|
|
2
|
+
import EventsEmitter from "events";
|
|
3
|
+
export default ({ root, logger = false }) => {
|
|
4
|
+
// Solve problem: https://stackoverflow.com/questions/43230346/error-spawn-npm-enoent
|
|
5
|
+
const _npm = process.platform === "win32" ? "npm.cmd" : "npm";
|
|
6
|
+
const EventsCallbacks = [
|
|
7
|
+
{
|
|
8
|
+
key: "stdoutError",
|
|
9
|
+
callback({ exitter }, error) {
|
|
10
|
+
logger && info(`Error: ${error.message}`);
|
|
11
|
+
exitter.reject(error);
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
key: "stderrError",
|
|
16
|
+
callback({ exitter }, error) {
|
|
17
|
+
logger && info(`Error: ${error.message}`);
|
|
18
|
+
exitter.reject(error);
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
key: "stdoutData",
|
|
23
|
+
callback(context, data) {
|
|
24
|
+
const content = String(data);
|
|
25
|
+
context.emitter.emit("data", content);
|
|
26
|
+
context.outString = context.outString.concat(content);
|
|
27
|
+
logger && console.info(content);
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
key: "stderrData",
|
|
32
|
+
callback(context, data) {
|
|
33
|
+
const content = String(data);
|
|
34
|
+
context.emitter.emit("data", content);
|
|
35
|
+
context.outString = context.outString.concat(content);
|
|
36
|
+
logger && console.info(content);
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
key: "message",
|
|
41
|
+
callback(_context, data) {
|
|
42
|
+
logger && console.info(data.toString());
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
key: "exit",
|
|
47
|
+
callback({ exitter, outString }) {
|
|
48
|
+
exitter.resolve(outString);
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
key: "error",
|
|
53
|
+
callback({ exitter }, error) {
|
|
54
|
+
logger && info(`Error: ${error.message}`);
|
|
55
|
+
exitter.reject(error);
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
];
|
|
59
|
+
const info = (log) => console.info(`\x1b[1m${log}\x1b[22m`);
|
|
60
|
+
const run = (command, params) => {
|
|
61
|
+
const child = spawn(command, params, { cwd: root });
|
|
62
|
+
const exitter = { resolve: null, reject: null };
|
|
63
|
+
const promise = new Promise((resolve, reject) => Object.assign(exitter, { resolve, reject }));
|
|
64
|
+
const emitter = new EventsEmitter();
|
|
65
|
+
const outString = "";
|
|
66
|
+
const context = {
|
|
67
|
+
exitter,
|
|
68
|
+
whenEnd: promise,
|
|
69
|
+
child,
|
|
70
|
+
emitter,
|
|
71
|
+
outString,
|
|
72
|
+
};
|
|
73
|
+
const events = Object.fromEntries(EventsCallbacks.map(({ callback, key }) => [
|
|
74
|
+
key,
|
|
75
|
+
callback.bind(null, context),
|
|
76
|
+
]));
|
|
77
|
+
(() => {
|
|
78
|
+
child.stdout.on("error", events.stdoutError);
|
|
79
|
+
child.stderr.on("error", events.stderrError);
|
|
80
|
+
child.stdout.on("data", events.stdoutData);
|
|
81
|
+
child.stderr.on("data", events.stderrData);
|
|
82
|
+
child.on("error", events.error);
|
|
83
|
+
child.on("message", events.message);
|
|
84
|
+
child.on("exit", events.exit);
|
|
85
|
+
})();
|
|
86
|
+
promise.finally(() => {
|
|
87
|
+
child.stdout.removeListener("error", events.stdoutError);
|
|
88
|
+
child.stderr.removeListener("error", events.stderrError);
|
|
89
|
+
child.stdout.removeListener("data", events.stdoutData);
|
|
90
|
+
child.stderr.removeListener("data", events.stderrData);
|
|
91
|
+
child.removeListener("error", events.error);
|
|
92
|
+
child.removeListener("message", events.message);
|
|
93
|
+
child.removeListener("exit", events.exit);
|
|
94
|
+
});
|
|
95
|
+
return context;
|
|
96
|
+
};
|
|
97
|
+
return { run, info, _npm };
|
|
98
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as getChildProcessUtils } from './getChildProcessUtils.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as getChildProcessUtils } from './getChildProcessUtils.js';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
interface ICustomCollectorOptions {
|
|
3
|
+
target: Required<{
|
|
4
|
+
removeListener: CallableFunction;
|
|
5
|
+
on: CallableFunction;
|
|
6
|
+
}>;
|
|
7
|
+
event: string;
|
|
8
|
+
filter?: Function;
|
|
9
|
+
time: number;
|
|
10
|
+
}
|
|
11
|
+
declare class CustomCollector {
|
|
12
|
+
#private;
|
|
13
|
+
timeoutId?: NodeJS.Timeout;
|
|
14
|
+
target: ICustomCollectorOptions["target"];
|
|
15
|
+
event: ICustomCollectorOptions["event"];
|
|
16
|
+
filter: ICustomCollectorOptions["filter"];
|
|
17
|
+
time: ICustomCollectorOptions["time"];
|
|
18
|
+
constructor({ target, event, filter, time }: ICustomCollectorOptions);
|
|
19
|
+
setCallback(callback: CallableFunction): void;
|
|
20
|
+
handle(handler: CallableFunction): void;
|
|
21
|
+
end(): void;
|
|
22
|
+
removeTimeout(): void;
|
|
23
|
+
setTimeout(ms: number): void;
|
|
24
|
+
}
|
|
25
|
+
export { CustomCollector };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _CustomCollector_callback;
|
|
13
|
+
class CustomCollector {
|
|
14
|
+
constructor({ target, event, filter, time = 0 }) {
|
|
15
|
+
_CustomCollector_callback.set(this, void 0);
|
|
16
|
+
if ("on" in target === false) {
|
|
17
|
+
throw new Error("Target must be similar to EventEmitter.prototype — target haven't method 'on'");
|
|
18
|
+
}
|
|
19
|
+
this.target = target;
|
|
20
|
+
this.event = event;
|
|
21
|
+
this.filter = filter;
|
|
22
|
+
this.time = time;
|
|
23
|
+
}
|
|
24
|
+
setCallback(callback) {
|
|
25
|
+
const handler = (...params) => {
|
|
26
|
+
const passed = !this.filter || this.filter(params);
|
|
27
|
+
if (!!passed === true) {
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
callback.apply(this, params);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
this.handle(handler);
|
|
33
|
+
}
|
|
34
|
+
handle(handler) {
|
|
35
|
+
this.end();
|
|
36
|
+
__classPrivateFieldSet(this, _CustomCollector_callback, handler, "f");
|
|
37
|
+
this.target.on(this.event, __classPrivateFieldGet(this, _CustomCollector_callback, "f"));
|
|
38
|
+
if (this.time > 0) {
|
|
39
|
+
this.setTimeout(this.time);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
end() {
|
|
43
|
+
if (this.timeoutId) {
|
|
44
|
+
this.removeTimeout();
|
|
45
|
+
}
|
|
46
|
+
if (__classPrivateFieldGet(this, _CustomCollector_callback, "f")) {
|
|
47
|
+
this.target.removeListener(this.event, __classPrivateFieldGet(this, _CustomCollector_callback, "f"));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
removeTimeout() {
|
|
51
|
+
clearTimeout(this.timeoutId);
|
|
52
|
+
}
|
|
53
|
+
setTimeout(ms) {
|
|
54
|
+
const callback = this.end.bind(this);
|
|
55
|
+
this.timeoutId = setTimeout(callback, ms);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
_CustomCollector_callback = new WeakMap();
|
|
59
|
+
export { CustomCollector };
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BaseExtendedEnum, BaseExtendedEnumPartial } from "./main.js";
|
|
2
|
+
declare class True<T> extends BaseExtendedEnumPartial<T> {
|
|
3
|
+
static value: number;
|
|
4
|
+
}
|
|
5
|
+
declare class False<T> extends BaseExtendedEnumPartial<T> {
|
|
6
|
+
static value: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class BooleanWithMessage extends BaseExtendedEnum {
|
|
9
|
+
static True: typeof True;
|
|
10
|
+
static False: typeof False;
|
|
11
|
+
}
|
|
12
|
+
export type TBooleanWithMessage<T = unknown, E = unknown> = True<T> | False<E>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BaseExtendedEnum, BaseExtendedEnumPartial } from "./main.js";
|
|
2
|
+
class True extends BaseExtendedEnumPartial {
|
|
3
|
+
}
|
|
4
|
+
True.value = 1;
|
|
5
|
+
class False extends BaseExtendedEnumPartial {
|
|
6
|
+
}
|
|
7
|
+
False.value = 0;
|
|
8
|
+
export class BooleanWithMessage extends BaseExtendedEnum {
|
|
9
|
+
}
|
|
10
|
+
BooleanWithMessage.True = True;
|
|
11
|
+
BooleanWithMessage.False = False;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BaseExtendedEnum, BaseExtendedEnumPartial } from "./main.js";
|
|
2
|
+
declare class Some<T> extends BaseExtendedEnumPartial<T> {
|
|
3
|
+
static value: number;
|
|
4
|
+
}
|
|
5
|
+
declare class None<T> extends BaseExtendedEnumPartial<T> {
|
|
6
|
+
static value: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class Option extends BaseExtendedEnum {
|
|
9
|
+
static Some: typeof Some;
|
|
10
|
+
static None: typeof None;
|
|
11
|
+
}
|
|
12
|
+
export type TOption<T = unknown, E = unknown> = Some<T> | None<E>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BaseExtendedEnum, BaseExtendedEnumPartial } from "./main.js";
|
|
2
|
+
class Some extends BaseExtendedEnumPartial {
|
|
3
|
+
}
|
|
4
|
+
Some.value = 1;
|
|
5
|
+
class None extends BaseExtendedEnumPartial {
|
|
6
|
+
}
|
|
7
|
+
None.value = 0;
|
|
8
|
+
export class Option extends BaseExtendedEnum {
|
|
9
|
+
}
|
|
10
|
+
Option.Some = Some;
|
|
11
|
+
Option.None = None;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BaseExtendedEnum, BaseExtendedEnumPartial } from "./main.js";
|
|
2
|
+
declare class Ok<T> extends BaseExtendedEnumPartial<T> {
|
|
3
|
+
static value: number;
|
|
4
|
+
}
|
|
5
|
+
declare class Err<T> extends BaseExtendedEnumPartial<T> {
|
|
6
|
+
static value: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class Result extends BaseExtendedEnum {
|
|
9
|
+
static Ok: typeof Ok;
|
|
10
|
+
static Err: typeof Err;
|
|
11
|
+
static StatusProcessing: number;
|
|
12
|
+
static StatusSuccess: number;
|
|
13
|
+
static StatusNotFound: number;
|
|
14
|
+
static StatusImTeapot: number;
|
|
15
|
+
static StatusInternalError: number;
|
|
16
|
+
}
|
|
17
|
+
export type TResult<T = unknown, E = unknown> = Ok<T> | Err<E>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BaseExtendedEnum, BaseExtendedEnumPartial } from "./main.js";
|
|
2
|
+
class Ok extends BaseExtendedEnumPartial {
|
|
3
|
+
}
|
|
4
|
+
Ok.value = 1;
|
|
5
|
+
class Err extends BaseExtendedEnumPartial {
|
|
6
|
+
}
|
|
7
|
+
Err.value = 0;
|
|
8
|
+
export class Result extends BaseExtendedEnum {
|
|
9
|
+
}
|
|
10
|
+
Result.Ok = Ok;
|
|
11
|
+
Result.Err = Err;
|
|
12
|
+
Result.StatusProcessing = 102;
|
|
13
|
+
Result.StatusSuccess = 200;
|
|
14
|
+
Result.StatusNotFound = 404;
|
|
15
|
+
Result.StatusImTeapot = 418;
|
|
16
|
+
Result.StatusInternalError = 500;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./mod.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./mod.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare class BaseExtendedEnumPartial<T = undefined> {
|
|
2
|
+
#private;
|
|
3
|
+
static value: number;
|
|
4
|
+
static from(data: object): any;
|
|
5
|
+
get props(): T | undefined;
|
|
6
|
+
set props(props: T);
|
|
7
|
+
constructor(props: T);
|
|
8
|
+
includes(props: T): boolean;
|
|
9
|
+
some(props: T): boolean;
|
|
10
|
+
is(constructor: typeof BaseExtendedEnumPartial): boolean;
|
|
11
|
+
toBoolean(): boolean;
|
|
12
|
+
unwrap(): T | undefined;
|
|
13
|
+
expect(message?: string): T | undefined;
|
|
14
|
+
}
|
|
15
|
+
export declare class BaseExtendedEnum {
|
|
16
|
+
static is(instance: BaseExtendedEnumPartial<unknown>, constructor: typeof BaseExtendedEnumPartial): boolean;
|
|
17
|
+
}
|