@jeniex/utils 1.0.2 → 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) {
@@ -76,15 +74,14 @@ function $$(selectors, parent) {
76
74
  return elements;
77
75
  }
78
76
 
79
- function handleFlag(flag) {
80
- const { type, name, message: header, values, default: defaultValue } = flag;
81
- let message = header;
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
- message += '\n';
87
- const [min, max] = flag.range ?? [];
84
+ const [min, max] = flag.range;
88
85
  if (min !== undefined) {
89
86
  message += `\nMinimum: ${min}`;
90
87
  }
@@ -92,47 +89,59 @@ function handleFlag(flag) {
92
89
  message += `\nMaximum: ${max}`;
93
90
  }
94
91
  }
95
- const flagValue = prompt(message, defaultValue.toString());
96
- if (flagValue === null || flagValue === '') {
92
+ return message;
93
+ }
94
+
95
+ function validateFlagValue(flag, promptValue) {
96
+ const { type, name, values } = flag;
97
+ if (promptValue === null || promptValue === '') {
97
98
  throw new Error(`You did not provide a value for "${name}" option.`);
98
99
  }
99
100
  switch (type) {
100
101
  case 'boolean': {
101
- if (flagValue !== 'yes' && flagValue !== 'no') {
102
+ if (promptValue !== 'yes' && promptValue !== 'no') {
102
103
  throw new Error(`Invalid value for "${name}" option!`);
103
104
  }
104
- return [name, flagValue === 'yes'];
105
+ return promptValue === 'yes';
105
106
  }
106
107
  case 'string': {
107
- if (values !== undefined && !values.includes(flagValue)) {
108
+ if (values !== undefined && !values.includes(promptValue)) {
108
109
  throw new Error(`Provided value for "${name}" option is not in the list.`);
109
110
  }
110
- return [name, flagValue];
111
+ return promptValue;
111
112
  }
112
113
  case 'number': {
113
- const [min, max] = flag.range ?? [];
114
- const flagValueAsNumber = Number(flagValue);
115
- if (Number.isNaN(flagValueAsNumber)) {
114
+ const promptValueAsNumber = Number(promptValue);
115
+ if (Number.isNaN(promptValueAsNumber)) {
116
116
  throw new TypeError(`You need to provide a number for "${name}" option!`);
117
117
  }
118
- if (values !== undefined && !values.includes(flagValueAsNumber)) {
118
+ if (values !== undefined && !values.includes(promptValueAsNumber)) {
119
119
  throw new Error(`Provided value for "${name}" option is not in the list.`);
120
120
  }
121
- if (min !== undefined && flagValueAsNumber < min) {
121
+ const [min, max] = flag.range ?? [];
122
+ if (min !== undefined && promptValueAsNumber < min) {
122
123
  throw new Error(`Provided value for "${name}" option is less than the minimum.`);
123
124
  }
124
- if (max !== undefined && flagValueAsNumber > max) {
125
+ if (max !== undefined && promptValueAsNumber > max) {
125
126
  throw new Error(`Provided value for "${name}" option is greater than the maximum.`);
126
127
  }
127
- return [name, flagValueAsNumber];
128
+ return promptValueAsNumber;
129
+ }
130
+ default: {
131
+ throw new Error(`Unsupported flag type for "${name}" option.`);
128
132
  }
129
133
  }
130
- throw new Error('Something went wrong while handling a flag');
131
134
  }
132
- async function createOptions(flags) {
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) {
133
142
  const options = {};
134
143
  for (const flag of flags) {
135
- const [name, value] = handleFlag(flag);
144
+ const [name, value] = processFlag(flag);
136
145
  options[name] = value;
137
146
  await sleep(300);
138
147
  }
@@ -147,8 +156,8 @@ exports.TAB_URL = TAB_URL;
147
156
  exports.alert = alert;
148
157
  exports.asserted = asserted;
149
158
  exports.confirm = confirm;
150
- exports.createOptions = createOptions;
151
159
  exports.ensureJoin = ensureJoin;
160
+ exports.getOptions = getOptions;
152
161
  exports.isFalsy = isFalsy;
153
162
  exports.isNotNullish = isNotNullish;
154
163
  exports.isNullish = isNullish;
@@ -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
- export { $, $$, Flag, Fn, LOG_ID, Nullable, SCRIPT_NAME, TAB_URL, alert, asserted, confirm, createOptions, ensureJoin, isFalsy, isNotNullish, isNullish, isString, isTruthy, noop, prompt, sleep };
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,14 @@ function $$(selectors, parent) {
74
72
  return elements;
75
73
  }
76
74
 
77
- function handleFlag(flag) {
78
- const { type, name, message: header, values, default: defaultValue } = flag;
79
- let message = header;
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
- message += '\n';
85
- const [min, max] = flag.range ?? [];
82
+ const [min, max] = flag.range;
86
83
  if (min !== undefined) {
87
84
  message += `\nMinimum: ${min}`;
88
85
  }
@@ -90,51 +87,63 @@ function handleFlag(flag) {
90
87
  message += `\nMaximum: ${max}`;
91
88
  }
92
89
  }
93
- const flagValue = prompt(message, defaultValue.toString());
94
- if (flagValue === null || flagValue === '') {
90
+ return message;
91
+ }
92
+
93
+ function validateFlagValue(flag, promptValue) {
94
+ const { type, name, values } = flag;
95
+ if (promptValue === null || promptValue === '') {
95
96
  throw new Error(`You did not provide a value for "${name}" option.`);
96
97
  }
97
98
  switch (type) {
98
99
  case 'boolean': {
99
- if (flagValue !== 'yes' && flagValue !== 'no') {
100
+ if (promptValue !== 'yes' && promptValue !== 'no') {
100
101
  throw new Error(`Invalid value for "${name}" option!`);
101
102
  }
102
- return [name, flagValue === 'yes'];
103
+ return promptValue === 'yes';
103
104
  }
104
105
  case 'string': {
105
- if (values !== undefined && !values.includes(flagValue)) {
106
+ if (values !== undefined && !values.includes(promptValue)) {
106
107
  throw new Error(`Provided value for "${name}" option is not in the list.`);
107
108
  }
108
- return [name, flagValue];
109
+ return promptValue;
109
110
  }
110
111
  case 'number': {
111
- const [min, max] = flag.range ?? [];
112
- const flagValueAsNumber = Number(flagValue);
113
- if (Number.isNaN(flagValueAsNumber)) {
112
+ const promptValueAsNumber = Number(promptValue);
113
+ if (Number.isNaN(promptValueAsNumber)) {
114
114
  throw new TypeError(`You need to provide a number for "${name}" option!`);
115
115
  }
116
- if (values !== undefined && !values.includes(flagValueAsNumber)) {
116
+ if (values !== undefined && !values.includes(promptValueAsNumber)) {
117
117
  throw new Error(`Provided value for "${name}" option is not in the list.`);
118
118
  }
119
- if (min !== undefined && flagValueAsNumber < min) {
119
+ const [min, max] = flag.range ?? [];
120
+ if (min !== undefined && promptValueAsNumber < min) {
120
121
  throw new Error(`Provided value for "${name}" option is less than the minimum.`);
121
122
  }
122
- if (max !== undefined && flagValueAsNumber > max) {
123
+ if (max !== undefined && promptValueAsNumber > max) {
123
124
  throw new Error(`Provided value for "${name}" option is greater than the maximum.`);
124
125
  }
125
- return [name, flagValueAsNumber];
126
+ return promptValueAsNumber;
127
+ }
128
+ default: {
129
+ throw new Error(`Unsupported flag type for "${name}" option.`);
126
130
  }
127
131
  }
128
- throw new Error('Something went wrong while handling a flag');
129
132
  }
130
- async function createOptions(flags) {
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) {
131
140
  const options = {};
132
141
  for (const flag of flags) {
133
- const [name, value] = handleFlag(flag);
142
+ const [name, value] = processFlag(flag);
134
143
  options[name] = value;
135
144
  await sleep(300);
136
145
  }
137
146
  return options;
138
147
  }
139
148
 
140
- export { $, $$, LOG_ID, SCRIPT_NAME, TAB_URL, alert, asserted, confirm, createOptions, ensureJoin, isFalsy, isNotNullish, isNullish, isString, isTruthy, noop, prompt, sleep };
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.2",
3
+ "version": "1.0.3",
4
4
  "author": "JenieX",
5
5
  "homepage": "https://github.com/JenieX/utils",
6
6
  "type": "module",