@jeniex/utils 1.0.2 → 1.0.4
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 +33 -23
- package/browser/index.d.ts +3 -3
- package/browser/index.mjs +33 -23
- package/node/index.cjs +0 -2
- package/node/index.mjs +0 -2
- package/package.json +1 -1
package/browser/index.cjs
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
|
|
3
1
|
'use strict';
|
|
4
2
|
|
|
5
3
|
function isString(object) {
|
|
@@ -76,15 +74,15 @@ function $$(selectors, parent) {
|
|
|
76
74
|
return elements;
|
|
77
75
|
}
|
|
78
76
|
|
|
79
|
-
function
|
|
80
|
-
const {
|
|
81
|
-
let message =
|
|
77
|
+
function createFlagMessage(flag) {
|
|
78
|
+
const { message: messageHeader, values, type } = flag;
|
|
79
|
+
let message = messageHeader;
|
|
82
80
|
if (values !== undefined) {
|
|
83
81
|
message += `\n\n${values.join(' | ')}`;
|
|
84
82
|
}
|
|
85
83
|
if (type === 'number' && flag.range !== undefined) {
|
|
86
84
|
message += '\n';
|
|
87
|
-
const [min, max] = flag.range
|
|
85
|
+
const [min, max] = flag.range;
|
|
88
86
|
if (min !== undefined) {
|
|
89
87
|
message += `\nMinimum: ${min}`;
|
|
90
88
|
}
|
|
@@ -92,47 +90,59 @@ function handleFlag(flag) {
|
|
|
92
90
|
message += `\nMaximum: ${max}`;
|
|
93
91
|
}
|
|
94
92
|
}
|
|
95
|
-
|
|
96
|
-
|
|
93
|
+
return message;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function validateFlagValue(flag, promptValue) {
|
|
97
|
+
const { type, name, values } = flag;
|
|
98
|
+
if (promptValue === null || promptValue === '') {
|
|
97
99
|
throw new Error(`You did not provide a value for "${name}" option.`);
|
|
98
100
|
}
|
|
99
101
|
switch (type) {
|
|
100
102
|
case 'boolean': {
|
|
101
|
-
if (
|
|
103
|
+
if (promptValue !== 'yes' && promptValue !== 'no') {
|
|
102
104
|
throw new Error(`Invalid value for "${name}" option!`);
|
|
103
105
|
}
|
|
104
|
-
return
|
|
106
|
+
return promptValue === 'yes';
|
|
105
107
|
}
|
|
106
108
|
case 'string': {
|
|
107
|
-
if (values !== undefined && !values.includes(
|
|
109
|
+
if (values !== undefined && !values.includes(promptValue)) {
|
|
108
110
|
throw new Error(`Provided value for "${name}" option is not in the list.`);
|
|
109
111
|
}
|
|
110
|
-
return
|
|
112
|
+
return promptValue;
|
|
111
113
|
}
|
|
112
114
|
case 'number': {
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
if (Number.isNaN(flagValueAsNumber)) {
|
|
115
|
+
const promptValueAsNumber = Number(promptValue);
|
|
116
|
+
if (Number.isNaN(promptValueAsNumber)) {
|
|
116
117
|
throw new TypeError(`You need to provide a number for "${name}" option!`);
|
|
117
118
|
}
|
|
118
|
-
if (values !== undefined && !values.includes(
|
|
119
|
+
if (values !== undefined && !values.includes(promptValueAsNumber)) {
|
|
119
120
|
throw new Error(`Provided value for "${name}" option is not in the list.`);
|
|
120
121
|
}
|
|
121
|
-
|
|
122
|
+
const [min, max] = flag.range ?? [];
|
|
123
|
+
if (min !== undefined && promptValueAsNumber < min) {
|
|
122
124
|
throw new Error(`Provided value for "${name}" option is less than the minimum.`);
|
|
123
125
|
}
|
|
124
|
-
if (max !== undefined &&
|
|
126
|
+
if (max !== undefined && promptValueAsNumber > max) {
|
|
125
127
|
throw new Error(`Provided value for "${name}" option is greater than the maximum.`);
|
|
126
128
|
}
|
|
127
|
-
return
|
|
129
|
+
return promptValueAsNumber;
|
|
130
|
+
}
|
|
131
|
+
default: {
|
|
132
|
+
throw new Error(`Unsupported flag type for "${name}" option.`);
|
|
128
133
|
}
|
|
129
134
|
}
|
|
130
|
-
throw new Error('Something went wrong while handling a flag');
|
|
131
135
|
}
|
|
132
|
-
|
|
136
|
+
|
|
137
|
+
function processFlag(flag) {
|
|
138
|
+
const message = createFlagMessage(flag);
|
|
139
|
+
const promptValue = prompt(message, flag.default?.toString());
|
|
140
|
+
return [flag.name, validateFlagValue(flag, promptValue)];
|
|
141
|
+
}
|
|
142
|
+
async function getOptions(flags) {
|
|
133
143
|
const options = {};
|
|
134
144
|
for (const flag of flags) {
|
|
135
|
-
const [name, value] =
|
|
145
|
+
const [name, value] = processFlag(flag);
|
|
136
146
|
options[name] = value;
|
|
137
147
|
await sleep(300);
|
|
138
148
|
}
|
|
@@ -147,8 +157,8 @@ exports.TAB_URL = TAB_URL;
|
|
|
147
157
|
exports.alert = alert;
|
|
148
158
|
exports.asserted = asserted;
|
|
149
159
|
exports.confirm = confirm;
|
|
150
|
-
exports.createOptions = createOptions;
|
|
151
160
|
exports.ensureJoin = ensureJoin;
|
|
161
|
+
exports.getOptions = getOptions;
|
|
152
162
|
exports.isFalsy = isFalsy;
|
|
153
163
|
exports.isNotNullish = isNotNullish;
|
|
154
164
|
exports.isNullish = isNullish;
|
package/browser/index.d.ts
CHANGED
|
@@ -51,9 +51,9 @@ interface FlagNumber extends FlagBase {
|
|
|
51
51
|
default: number;
|
|
52
52
|
}
|
|
53
53
|
type Flag = FlagBoolean | FlagNumber | FlagString;
|
|
54
|
-
|
|
55
54
|
type FlagValue = boolean | number | string;
|
|
56
55
|
type Options = Record<string, FlagValue>;
|
|
57
|
-
declare function createOptions(flags: Flag[]): Promise<Options>;
|
|
58
56
|
|
|
59
|
-
|
|
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
|
}
|
|
@@ -74,15 +72,15 @@ function $$(selectors, parent) {
|
|
|
74
72
|
return elements;
|
|
75
73
|
}
|
|
76
74
|
|
|
77
|
-
function
|
|
78
|
-
const {
|
|
79
|
-
let message =
|
|
75
|
+
function createFlagMessage(flag) {
|
|
76
|
+
const { message: messageHeader, values, type } = flag;
|
|
77
|
+
let message = messageHeader;
|
|
80
78
|
if (values !== undefined) {
|
|
81
79
|
message += `\n\n${values.join(' | ')}`;
|
|
82
80
|
}
|
|
83
81
|
if (type === 'number' && flag.range !== undefined) {
|
|
84
82
|
message += '\n';
|
|
85
|
-
const [min, max] = flag.range
|
|
83
|
+
const [min, max] = flag.range;
|
|
86
84
|
if (min !== undefined) {
|
|
87
85
|
message += `\nMinimum: ${min}`;
|
|
88
86
|
}
|
|
@@ -90,51 +88,63 @@ function handleFlag(flag) {
|
|
|
90
88
|
message += `\nMaximum: ${max}`;
|
|
91
89
|
}
|
|
92
90
|
}
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
return message;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function validateFlagValue(flag, promptValue) {
|
|
95
|
+
const { type, name, values } = flag;
|
|
96
|
+
if (promptValue === null || promptValue === '') {
|
|
95
97
|
throw new Error(`You did not provide a value for "${name}" option.`);
|
|
96
98
|
}
|
|
97
99
|
switch (type) {
|
|
98
100
|
case 'boolean': {
|
|
99
|
-
if (
|
|
101
|
+
if (promptValue !== 'yes' && promptValue !== 'no') {
|
|
100
102
|
throw new Error(`Invalid value for "${name}" option!`);
|
|
101
103
|
}
|
|
102
|
-
return
|
|
104
|
+
return promptValue === 'yes';
|
|
103
105
|
}
|
|
104
106
|
case 'string': {
|
|
105
|
-
if (values !== undefined && !values.includes(
|
|
107
|
+
if (values !== undefined && !values.includes(promptValue)) {
|
|
106
108
|
throw new Error(`Provided value for "${name}" option is not in the list.`);
|
|
107
109
|
}
|
|
108
|
-
return
|
|
110
|
+
return promptValue;
|
|
109
111
|
}
|
|
110
112
|
case 'number': {
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
if (Number.isNaN(flagValueAsNumber)) {
|
|
113
|
+
const promptValueAsNumber = Number(promptValue);
|
|
114
|
+
if (Number.isNaN(promptValueAsNumber)) {
|
|
114
115
|
throw new TypeError(`You need to provide a number for "${name}" option!`);
|
|
115
116
|
}
|
|
116
|
-
if (values !== undefined && !values.includes(
|
|
117
|
+
if (values !== undefined && !values.includes(promptValueAsNumber)) {
|
|
117
118
|
throw new Error(`Provided value for "${name}" option is not in the list.`);
|
|
118
119
|
}
|
|
119
|
-
|
|
120
|
+
const [min, max] = flag.range ?? [];
|
|
121
|
+
if (min !== undefined && promptValueAsNumber < min) {
|
|
120
122
|
throw new Error(`Provided value for "${name}" option is less than the minimum.`);
|
|
121
123
|
}
|
|
122
|
-
if (max !== undefined &&
|
|
124
|
+
if (max !== undefined && promptValueAsNumber > max) {
|
|
123
125
|
throw new Error(`Provided value for "${name}" option is greater than the maximum.`);
|
|
124
126
|
}
|
|
125
|
-
return
|
|
127
|
+
return promptValueAsNumber;
|
|
128
|
+
}
|
|
129
|
+
default: {
|
|
130
|
+
throw new Error(`Unsupported flag type for "${name}" option.`);
|
|
126
131
|
}
|
|
127
132
|
}
|
|
128
|
-
throw new Error('Something went wrong while handling a flag');
|
|
129
133
|
}
|
|
130
|
-
|
|
134
|
+
|
|
135
|
+
function processFlag(flag) {
|
|
136
|
+
const message = createFlagMessage(flag);
|
|
137
|
+
const promptValue = prompt(message, flag.default?.toString());
|
|
138
|
+
return [flag.name, validateFlagValue(flag, promptValue)];
|
|
139
|
+
}
|
|
140
|
+
async function getOptions(flags) {
|
|
131
141
|
const options = {};
|
|
132
142
|
for (const flag of flags) {
|
|
133
|
-
const [name, value] =
|
|
143
|
+
const [name, value] = processFlag(flag);
|
|
134
144
|
options[name] = value;
|
|
135
145
|
await sleep(300);
|
|
136
146
|
}
|
|
137
147
|
return options;
|
|
138
148
|
}
|
|
139
149
|
|
|
140
|
-
export { $, $$, LOG_ID, SCRIPT_NAME, TAB_URL, alert, asserted, confirm,
|
|
150
|
+
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
package/node/index.mjs
CHANGED