@zoodogood/utils 1.0.2-3 → 1.0.4-change.334

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,37 @@
1
+ export function CreateMessage({ content, title, url, author, thumbnail, description, color, fields, image, video, footer, timestamp, ephemeral, fetchReply, components, files, reference }: {
2
+ content: any;
3
+ title: any;
4
+ url: any;
5
+ author: any;
6
+ thumbnail: any;
7
+ description: any;
8
+ color: any;
9
+ fields: any;
10
+ image: any;
11
+ video: any;
12
+ footer: any;
13
+ timestamp: any;
14
+ ephemeral: any;
15
+ fetchReply: any;
16
+ components: any;
17
+ files: any;
18
+ reference: any;
19
+ }): {
20
+ embeds: EmbedBuilder[];
21
+ files: any;
22
+ reply: {
23
+ messageReference: any;
24
+ };
25
+ components: any;
26
+ content: any;
27
+ ephemeral: any;
28
+ fetchReply: any;
29
+ };
30
+ export function CreateModal({ title, customId, components }: {
31
+ title: any;
32
+ customId: any;
33
+ components: any;
34
+ }): import("discord.js").APIModalInteractionResponseCallbackData;
35
+ export function SimplifyComponents(data: any): any;
36
+ export function isEmptyEmbed(embed: any): boolean;
37
+ import { EmbedBuilder } from 'discord.js';
@@ -0,0 +1,79 @@
1
+ import { EmbedBuilder, ModalBuilder, resolveColor, ActionRow } from 'discord.js';
2
+ import { ComponentType } from 'discord-api-types/v10';
3
+ function isEmptyEmbed(embed) {
4
+ const DEFAULT_PROPERTIES = {
5
+ title: undefined,
6
+ author: undefined,
7
+ thumbnail: undefined,
8
+ description: undefined,
9
+ fields: undefined,
10
+ image: undefined,
11
+ footer: undefined,
12
+ timestamp: undefined,
13
+ video: undefined
14
+ };
15
+ // in isEmptyEmbed context is a good compare
16
+ const isEqualDefault = (key) => String(embed[key]) === String(DEFAULT_PROPERTIES[key]);
17
+ const isEveryPropertyDefault = Object.keys(DEFAULT_PROPERTIES)
18
+ .every(isEqualDefault);
19
+ return isEveryPropertyDefault;
20
+ }
21
+ function CreateMessage({ content, title, url, author, thumbnail, description, color, fields, image, video, footer, timestamp, ephemeral, fetchReply, components, files, reference }) {
22
+ const message = {};
23
+ thumbnail && (thumbnail = { url: thumbnail });
24
+ image && (image = { url: image });
25
+ video && (video = { url: video });
26
+ color = resolveColor(color !== null && color !== void 0 ? color : "Random");
27
+ const embed = new EmbedBuilder({
28
+ title, url, author, thumbnail,
29
+ description, color, fields,
30
+ image, video, footer, timestamp
31
+ });
32
+ if (!isEmptyEmbed(embed.data)) {
33
+ message.embeds = [embed];
34
+ }
35
+ if (files) {
36
+ message.files = files;
37
+ }
38
+ if (reference)
39
+ message.reply = {
40
+ messageReference: reference
41
+ };
42
+ message.components = components ? SimplifyComponents(components) : null;
43
+ message.content = content;
44
+ message.ephemeral = ephemeral;
45
+ message.fetchReply = fetchReply;
46
+ return message;
47
+ }
48
+ function CreateModal({ title, customId, components }) {
49
+ components = SimplifyComponents(components);
50
+ return new ModalBuilder({ title, customId, components }).toJSON();
51
+ }
52
+ function SimplifyComponents(data) {
53
+ if (data instanceof Array && data.length === 0)
54
+ return [];
55
+ const isComponent = (component) => "type" in component;
56
+ const isActionRow = (component) => component instanceof Array && isComponent(component.at(0)) || component instanceof ActionRow;
57
+ const isComponents = (component) => component.length && isActionRow(component.at(0));
58
+ const argumentType = [
59
+ isComponent(data),
60
+ isActionRow(data),
61
+ isComponents(data)
62
+ ].findIndex(Boolean);
63
+ if (argumentType === -1)
64
+ throw new TypeError("expected component");
65
+ const inArray = (component) => [component];
66
+ const arrayToActionRow = (componentsArray) => {
67
+ if (componentsArray.type === ComponentType.ActionRow)
68
+ return componentsArray;
69
+ return { type: ComponentType.ActionRow, components: componentsArray };
70
+ };
71
+ const actionRowInArray = (actionRow) => [actionRow];
72
+ if (argumentType <= 0)
73
+ data = inArray(data);
74
+ if (argumentType <= 1)
75
+ data = actionRowInArray(data);
76
+ data = data.map(arrayToActionRow);
77
+ return data;
78
+ }
79
+ 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 {
@@ -7,7 +6,7 @@ interface IContext {
7
6
  resolve: any;
8
7
  reject: any;
9
8
  };
10
- whenEnd: Promise<any>;
9
+ whenEnd: Promise<unknown>;
11
10
  child: ChildProcessWithoutNullStreams;
12
11
  emitter: EventsEmitter;
13
12
  outString: string;
@@ -18,7 +17,7 @@ interface IParams {
18
17
  }
19
18
  declare const _default: ({ root, logger }: IParams) => {
20
19
  run: (command: string, params: string[]) => IContext;
21
- info: (string: string) => void;
20
+ info: (log: string) => void;
22
21
  _npm: string;
23
22
  };
24
23
  export default _default;
@@ -56,7 +56,7 @@ export default ({ root, logger = false }) => {
56
56
  },
57
57
  },
58
58
  ];
59
- const info = (string) => console.info(`\x1b[1m${string}\x1b[22m`);
59
+ const info = (log) => console.info(`\x1b[1m${log}\x1b[22m`);
60
60
  const run = (command, params) => {
61
61
  const child = spawn(command, params, { cwd: root });
62
62
  const exitter = { resolve: null, reject: null };
@@ -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,7 +1,8 @@
1
1
  export * from './CustomCollector.js';
2
2
  export * from './GlitchText.js';
3
- export * from './getRandomValue.js';
4
- declare function omit(object: object, filter: CallableFunction): {
3
+ export * from './getRandomNumberInRange.js';
4
+ export * from './rangeToArray.js';
5
+ declare function omit(object: Object, filter: CallableFunction): {
5
6
  [k: string]: any;
6
7
  };
7
8
  export { omit };
@@ -1,6 +1,7 @@
1
1
  export * from './CustomCollector.js';
2
2
  export * from './GlitchText.js';
3
- export * from './getRandomValue.js';
3
+ export * from './getRandomNumberInRange.js';
4
+ export * from './rangeToArray.js';
4
5
  function omit(object, filter) {
5
6
  const entries = Object.entries(object)
6
7
  .filter(([key, value], i) => filter(key, value, i));
@@ -0,0 +1 @@
1
+ export declare function rangeToArray([min, max]: [number, number]): number | number[];
@@ -0,0 +1,6 @@
1
+ export function rangeToArray([min, max]) {
2
+ if (isNaN(min) || isNaN(max)) {
3
+ return NaN;
4
+ }
5
+ return [...new Array(max - min + 1)].map((_, index) => index + min);
6
+ }
package/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "@zoodogood/utils",
3
3
  "type": "module",
4
- "version": "1.0.2-3",
4
+ "version": "1.0.4-change.334",
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",
11
- "docs-build": "cd ./docs & retype build --output ./public",
12
- "prepack": "tsc"
10
+ "test": "vitest",
11
+ "docs-build": "cd ./docs && retype build --output ./public",
12
+ "docs-watch": "cd ./docs && retype watch",
13
+ "prepack": "tsc",
14
+ "build": "tsc"
13
15
  },
14
16
  "typings": "lib/index",
15
17
  "exports": {
@@ -29,8 +31,9 @@
29
31
  "devDependencies": {
30
32
  "@types/node": "^20.5.9",
31
33
  "discord.js": "^14.13.0",
32
- "retype": "^0.2.0",
33
- "typescript": "^5.2.2"
34
+ "retypeapp-linux-x64": "^3.3.0",
35
+ "typescript": "^5.2.2",
36
+ "vitest": "^0.34.5"
34
37
  },
35
38
  "peerDependencies": {
36
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.