@jeniex/utils 1.0.1 → 1.0.3

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/browser/index.cjs CHANGED
@@ -1,5 +1,3 @@
1
- // @ts-nocheck
2
-
3
1
  'use strict';
4
2
 
5
3
  function isString(object) {
@@ -40,6 +38,27 @@ async function sleep(milliSeconds) {
40
38
  });
41
39
  }
42
40
 
41
+ const scriptInfo = typeof GM === 'undefined' ? GM_info : GM.info;
42
+ const SCRIPT_NAME = scriptInfo.script.name;
43
+ /** The identifier of the script to be used in logging */
44
+ const LOG_ID = `[${SCRIPT_NAME}]:`;
45
+ /** The initial tab URL on the script run */
46
+ const TAB_URL = window.location.href;
47
+
48
+ function alert(message) {
49
+ if (message === undefined) {
50
+ window.alert(`[ ${SCRIPT_NAME} ]`);
51
+ return;
52
+ }
53
+ window.alert(`[ ${SCRIPT_NAME} ]\n\n${message}`);
54
+ }
55
+ function confirm(message) {
56
+ return window.confirm(`[ ${SCRIPT_NAME} ]\n\n${message}`);
57
+ }
58
+ function prompt(message, _default) {
59
+ return window.prompt(`[ ${SCRIPT_NAME} ]\n\n${message}`, _default);
60
+ }
61
+
43
62
  function $(selectors, parent) {
44
63
  const element = (parent ?? document).querySelector(ensureJoin(selectors));
45
64
  if (element === null) {
@@ -55,14 +74,95 @@ function $$(selectors, parent) {
55
74
  return elements;
56
75
  }
57
76
 
77
+ function createFlagMessage(flag) {
78
+ const { message: messageHeader, values, type } = flag;
79
+ let message = messageHeader;
80
+ if (values !== undefined) {
81
+ message += `\n\n${values.join(' | ')}`;
82
+ }
83
+ if (type === 'number' && flag.range !== undefined) {
84
+ const [min, max] = flag.range;
85
+ if (min !== undefined) {
86
+ message += `\nMinimum: ${min}`;
87
+ }
88
+ if (max !== undefined) {
89
+ message += `\nMaximum: ${max}`;
90
+ }
91
+ }
92
+ return message;
93
+ }
94
+
95
+ function validateFlagValue(flag, promptValue) {
96
+ const { type, name, values } = flag;
97
+ if (promptValue === null || promptValue === '') {
98
+ throw new Error(`You did not provide a value for "${name}" option.`);
99
+ }
100
+ switch (type) {
101
+ case 'boolean': {
102
+ if (promptValue !== 'yes' && promptValue !== 'no') {
103
+ throw new Error(`Invalid value for "${name}" option!`);
104
+ }
105
+ return promptValue === 'yes';
106
+ }
107
+ case 'string': {
108
+ if (values !== undefined && !values.includes(promptValue)) {
109
+ throw new Error(`Provided value for "${name}" option is not in the list.`);
110
+ }
111
+ return promptValue;
112
+ }
113
+ case 'number': {
114
+ const promptValueAsNumber = Number(promptValue);
115
+ if (Number.isNaN(promptValueAsNumber)) {
116
+ throw new TypeError(`You need to provide a number for "${name}" option!`);
117
+ }
118
+ if (values !== undefined && !values.includes(promptValueAsNumber)) {
119
+ throw new Error(`Provided value for "${name}" option is not in the list.`);
120
+ }
121
+ const [min, max] = flag.range ?? [];
122
+ if (min !== undefined && promptValueAsNumber < min) {
123
+ throw new Error(`Provided value for "${name}" option is less than the minimum.`);
124
+ }
125
+ if (max !== undefined && promptValueAsNumber > max) {
126
+ throw new Error(`Provided value for "${name}" option is greater than the maximum.`);
127
+ }
128
+ return promptValueAsNumber;
129
+ }
130
+ default: {
131
+ throw new Error(`Unsupported flag type for "${name}" option.`);
132
+ }
133
+ }
134
+ }
135
+
136
+ function processFlag(flag) {
137
+ const message = createFlagMessage(flag);
138
+ const promptValue = prompt(message, flag.default?.toString());
139
+ return [flag.name, validateFlagValue(flag, promptValue)];
140
+ }
141
+ async function getOptions(flags) {
142
+ const options = {};
143
+ for (const flag of flags) {
144
+ const [name, value] = processFlag(flag);
145
+ options[name] = value;
146
+ await sleep(300);
147
+ }
148
+ return options;
149
+ }
150
+
58
151
  exports.$ = $;
59
152
  exports.$$ = $$;
153
+ exports.LOG_ID = LOG_ID;
154
+ exports.SCRIPT_NAME = SCRIPT_NAME;
155
+ exports.TAB_URL = TAB_URL;
156
+ exports.alert = alert;
60
157
  exports.asserted = asserted;
158
+ exports.confirm = confirm;
61
159
  exports.ensureJoin = ensureJoin;
160
+ exports.getOptions = getOptions;
62
161
  exports.isFalsy = isFalsy;
63
162
  exports.isNotNullish = isNotNullish;
64
163
  exports.isNullish = isNullish;
65
164
  exports.isString = isString;
66
165
  exports.isTruthy = isTruthy;
67
166
  exports.noop = noop;
167
+ exports.prompt = prompt;
68
168
  exports.sleep = sleep;
@@ -15,9 +15,45 @@ declare function sleep(milliSeconds: number): Promise<void>;
15
15
  type Nullable<T> = T | null | undefined;
16
16
  type Fn<T = any, U = void> = (param: T) => U;
17
17
 
18
+ declare const SCRIPT_NAME: string;
19
+ /** The identifier of the script to be used in logging */
20
+ declare const LOG_ID: string;
21
+ /** The initial tab URL on the script run */
22
+ declare const TAB_URL: string;
23
+
24
+ declare function alert(message?: string): void;
25
+ declare function confirm(message: string): boolean;
26
+ declare function prompt(message: string, _default?: string): string | null;
27
+
18
28
  type Parent = Document | HTMLElement;
19
29
  type Selectors = string[] | string;
20
30
  declare function $<T = HTMLElement>(selectors: Selectors, parent?: Parent): T;
21
31
  declare function $$<T extends Element = HTMLElement>(selectors: Selectors, parent?: Parent): NodeListOf<T>;
22
32
 
23
- export { $, $$, Fn, Nullable, asserted, ensureJoin, isFalsy, isNotNullish, isNullish, isString, isTruthy, noop, sleep };
33
+ interface FlagBase {
34
+ name: string;
35
+ message: string;
36
+ }
37
+ interface FlagBoolean extends FlagBase {
38
+ type: 'boolean';
39
+ values?: ['no', 'yes'] | ['yes', 'no'];
40
+ default: 'no' | 'yes';
41
+ }
42
+ interface FlagString extends FlagBase {
43
+ type: 'string';
44
+ values?: string[];
45
+ default: string;
46
+ }
47
+ interface FlagNumber extends FlagBase {
48
+ type: 'number';
49
+ values?: number[];
50
+ range?: [number, number] | [number, undefined] | [undefined, number];
51
+ default: number;
52
+ }
53
+ type Flag = FlagBoolean | FlagNumber | FlagString;
54
+ type FlagValue = boolean | number | string;
55
+ type Options = Record<string, FlagValue>;
56
+
57
+ declare function getOptions(flags: Flag[]): Promise<Options>;
58
+
59
+ export { $, $$, Flag, Fn, LOG_ID, Nullable, SCRIPT_NAME, TAB_URL, alert, asserted, confirm, ensureJoin, getOptions, isFalsy, isNotNullish, isNullish, isString, isTruthy, noop, prompt, sleep };
package/browser/index.mjs CHANGED
@@ -1,5 +1,3 @@
1
- // @ts-nocheck
2
-
3
1
  function isString(object) {
4
2
  return typeof object === 'string';
5
3
  }
@@ -38,6 +36,27 @@ async function sleep(milliSeconds) {
38
36
  });
39
37
  }
40
38
 
39
+ const scriptInfo = typeof GM === 'undefined' ? GM_info : GM.info;
40
+ const SCRIPT_NAME = scriptInfo.script.name;
41
+ /** The identifier of the script to be used in logging */
42
+ const LOG_ID = `[${SCRIPT_NAME}]:`;
43
+ /** The initial tab URL on the script run */
44
+ const TAB_URL = window.location.href;
45
+
46
+ function alert(message) {
47
+ if (message === undefined) {
48
+ window.alert(`[ ${SCRIPT_NAME} ]`);
49
+ return;
50
+ }
51
+ window.alert(`[ ${SCRIPT_NAME} ]\n\n${message}`);
52
+ }
53
+ function confirm(message) {
54
+ return window.confirm(`[ ${SCRIPT_NAME} ]\n\n${message}`);
55
+ }
56
+ function prompt(message, _default) {
57
+ return window.prompt(`[ ${SCRIPT_NAME} ]\n\n${message}`, _default);
58
+ }
59
+
41
60
  function $(selectors, parent) {
42
61
  const element = (parent ?? document).querySelector(ensureJoin(selectors));
43
62
  if (element === null) {
@@ -53,4 +72,78 @@ function $$(selectors, parent) {
53
72
  return elements;
54
73
  }
55
74
 
56
- export { $, $$, asserted, ensureJoin, isFalsy, isNotNullish, isNullish, isString, isTruthy, noop, sleep };
75
+ function createFlagMessage(flag) {
76
+ const { message: messageHeader, values, type } = flag;
77
+ let message = messageHeader;
78
+ if (values !== undefined) {
79
+ message += `\n\n${values.join(' | ')}`;
80
+ }
81
+ if (type === 'number' && flag.range !== undefined) {
82
+ const [min, max] = flag.range;
83
+ if (min !== undefined) {
84
+ message += `\nMinimum: ${min}`;
85
+ }
86
+ if (max !== undefined) {
87
+ message += `\nMaximum: ${max}`;
88
+ }
89
+ }
90
+ return message;
91
+ }
92
+
93
+ function validateFlagValue(flag, promptValue) {
94
+ const { type, name, values } = flag;
95
+ if (promptValue === null || promptValue === '') {
96
+ throw new Error(`You did not provide a value for "${name}" option.`);
97
+ }
98
+ switch (type) {
99
+ case 'boolean': {
100
+ if (promptValue !== 'yes' && promptValue !== 'no') {
101
+ throw new Error(`Invalid value for "${name}" option!`);
102
+ }
103
+ return promptValue === 'yes';
104
+ }
105
+ case 'string': {
106
+ if (values !== undefined && !values.includes(promptValue)) {
107
+ throw new Error(`Provided value for "${name}" option is not in the list.`);
108
+ }
109
+ return promptValue;
110
+ }
111
+ case 'number': {
112
+ const promptValueAsNumber = Number(promptValue);
113
+ if (Number.isNaN(promptValueAsNumber)) {
114
+ throw new TypeError(`You need to provide a number for "${name}" option!`);
115
+ }
116
+ if (values !== undefined && !values.includes(promptValueAsNumber)) {
117
+ throw new Error(`Provided value for "${name}" option is not in the list.`);
118
+ }
119
+ const [min, max] = flag.range ?? [];
120
+ if (min !== undefined && promptValueAsNumber < min) {
121
+ throw new Error(`Provided value for "${name}" option is less than the minimum.`);
122
+ }
123
+ if (max !== undefined && promptValueAsNumber > max) {
124
+ throw new Error(`Provided value for "${name}" option is greater than the maximum.`);
125
+ }
126
+ return promptValueAsNumber;
127
+ }
128
+ default: {
129
+ throw new Error(`Unsupported flag type for "${name}" option.`);
130
+ }
131
+ }
132
+ }
133
+
134
+ function processFlag(flag) {
135
+ const message = createFlagMessage(flag);
136
+ const promptValue = prompt(message, flag.default?.toString());
137
+ return [flag.name, validateFlagValue(flag, promptValue)];
138
+ }
139
+ async function getOptions(flags) {
140
+ const options = {};
141
+ for (const flag of flags) {
142
+ const [name, value] = processFlag(flag);
143
+ options[name] = value;
144
+ await sleep(300);
145
+ }
146
+ return options;
147
+ }
148
+
149
+ export { $, $$, LOG_ID, SCRIPT_NAME, TAB_URL, alert, asserted, confirm, ensureJoin, getOptions, isFalsy, isNotNullish, isNullish, isString, isTruthy, noop, prompt, sleep };
package/node/index.cjs CHANGED
@@ -1,5 +1,3 @@
1
- // @ts-nocheck
2
-
3
1
  'use strict';
4
2
 
5
3
  var fs = require('node:fs');
package/node/index.mjs CHANGED
@@ -1,5 +1,3 @@
1
- // @ts-nocheck
2
-
3
1
  import fs from 'node:fs';
4
2
  import fsp from 'node:fs/promises';
5
3
  import path from 'node:path';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jeniex/utils",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "author": "JenieX",
5
5
  "homepage": "https://github.com/JenieX/utils",
6
6
  "type": "module",
@@ -31,6 +31,7 @@
31
31
  "@jeniex/eslint-config": "^0.0.3",
32
32
  "@rollup/plugin-typescript": "^11.1.3",
33
33
  "@types/node": "^20.5.7",
34
+ "@violentmonkey/types": "^0.1.5",
34
35
  "rollup": "^3.28.0",
35
36
  "rollup-plugin-dts": "^5.3.1",
36
37
  "tslib": "^2.6.2"