@zoodogood/utils 1.0.4 → 1.0.5-change.421

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.
@@ -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 };
@@ -1,5 +1,4 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
- /// <reference types="node" resolution-mode="require"/>
3
2
  import type { ChildProcessWithoutNullStreams } from "child_process";
4
3
  import EventsEmitter from "events";
5
4
  interface IContext {
@@ -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 { getRandomValue } from "./getRandomValue.js";
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(getRandomValue({ min: MIN, max: MAX })));
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
+ }
@@ -0,0 +1,7 @@
1
+ interface IGetRandomValueOptions {
2
+ min?: number;
3
+ max: number;
4
+ needRound?: boolean;
5
+ }
6
+ declare function getRandomNumberInRange({ min, max, needRound }: IGetRandomValueOptions): number;
7
+ export { getRandomNumberInRange };
@@ -0,0 +1,8 @@
1
+ function getRandomNumberInRange({ min = 0, max, needRound = true }) {
2
+ let value = Math.random() * (max - min + Number(needRound)) + min;
3
+ if (needRound) {
4
+ value = Math.floor(value);
5
+ }
6
+ return value;
7
+ }
8
+ export { getRandomNumberInRange };
@@ -1,8 +1,8 @@
1
1
  export * from './CustomCollector.js';
2
2
  export * from './GlitchText.js';
3
- export * from './getRandomValue.js';
3
+ export * from './getRandomNumberInRange.js';
4
4
  export * from './rangeToArray.js';
5
- declare function omit(object: object, filter: CallableFunction): {
5
+ declare function omit(object: Object, filter: CallableFunction): {
6
6
  [k: string]: any;
7
7
  };
8
8
  export { omit };
@@ -1,6 +1,6 @@
1
1
  export * from './CustomCollector.js';
2
2
  export * from './GlitchText.js';
3
- export * from './getRandomValue.js';
3
+ export * from './getRandomNumberInRange.js';
4
4
  export * from './rangeToArray.js';
5
5
  function omit(object, filter) {
6
6
  const entries = Object.entries(object)
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "@zoodogood/utils",
3
3
  "type": "module",
4
- "version": "1.0.4",
4
+ "version": "1.0.5-change.421",
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": "node index",
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.