@jeniex/utils 1.0.19 → 1.0.21

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
@@ -78,6 +78,53 @@ async function sleep(milliSeconds) {
78
78
  });
79
79
  }
80
80
 
81
+ /**
82
+ * Adds leading zeros to a number.
83
+ *
84
+ * @category String
85
+ */
86
+ function padZeros(number, length) {
87
+ if (Math.abs(number).toString().length > length) {
88
+ throw new Error('Provided length can not be less than the digits of the number.');
89
+ }
90
+ if (number < 0) {
91
+ throw new Error('Provided number must be greater than zero.');
92
+ }
93
+ const padded = '0'.repeat(length - 1) + number;
94
+ if (number < 10) {
95
+ return padded;
96
+ }
97
+ return padded.slice(-length);
98
+ }
99
+
100
+ const lowercaseWords = new Set([
101
+ 'a', 'an', 'the',
102
+ 'and', 'but', 'for', 'nor', 'or', 'so', 'yet',
103
+ 'about', 'across', 'after', 'along', 'among', 'around', 'as', 'at', 'before', 'between', 'by', 'during', 'in', 'into', 'of', 'on', 'over', 'since', 'through', 'throughout', 'to', 'under', 'with', 'within',
104
+ 'away', 'back', 'down', 'far', 'off', 'on', 'out', 'up',
105
+ 'he', 'her', 'him', 'his', 'it', 'its', 'me', 'my', 'our', 'she', 'their', 'them', 'they', 'us', 'we', 'you', 'your',
106
+ ]);
107
+ function capitalizeWord(word) {
108
+ return word[0].toUpperCase() + word.slice(1).toLowerCase();
109
+ }
110
+ /**
111
+ * capitalizes a word or a sentence.
112
+ *
113
+ * @category String
114
+ */
115
+ function capitalize(sentence) {
116
+ if (sentence.includes(' ')) {
117
+ return sentence.split(' ').map((word, index) => {
118
+ const lowercaseWord = word.toLowerCase();
119
+ if (index > 0 && lowercaseWords.has(lowercaseWord)) {
120
+ return lowercaseWord;
121
+ }
122
+ return capitalizeWord(word);
123
+ }).join(' ');
124
+ }
125
+ return capitalizeWord(sentence);
126
+ }
127
+
81
128
  let infoObject;
82
129
  if (typeof GM !== 'undefined') {
83
130
  infoObject = GM.info;
@@ -111,7 +158,7 @@ function prompt(message, _default) {
111
158
 
112
159
  async function fishResponse(url, options) {
113
160
  const response = await fetch(url, options);
114
- if (!response.ok) {
161
+ if (!response.ok && !response.url.startsWith('file:///')) {
115
162
  throw new Error(`Request to ${response.url} ended with ${response.status} status.`);
116
163
  }
117
164
  return response;
@@ -131,14 +178,15 @@ async function fishXResponse(url, fishOptions) {
131
178
  timeout: timeOut,
132
179
  onprogress: onProgress,
133
180
  onload({ response, statusText, status, finalUrl }) {
181
+ const isFileURL = finalUrl.startsWith('file:///');
134
182
  const ok = status >= 200 && status < 300;
135
- if (!ok) {
183
+ if (!ok && !isFileURL) {
136
184
  reject(new Error(`Request to ${url} ended with ${status} status.`));
137
185
  return;
138
186
  }
139
187
  const properResponse = new Response(response, {
140
188
  statusText,
141
- status,
189
+ status: isFileURL ? 200 : status,
142
190
  });
143
191
  Object.defineProperty(properResponse, 'url', { value: finalUrl });
144
192
  resolve(properResponse);
@@ -196,14 +244,14 @@ const fishX = {
196
244
  function $(selectors, parent) {
197
245
  const element = (parent ?? document).querySelector(join(selectors));
198
246
  if (element === null) {
199
- throw new Error(`Couldn't find the element with the selector ${selectors}`);
247
+ throw new Error(`Could not find the element with the selector ${selectors}`);
200
248
  }
201
249
  return element;
202
250
  }
203
251
  function $$(selectors, parent) {
204
252
  const elements = (parent ?? document).querySelectorAll(join(selectors));
205
253
  if (elements.length === 0) {
206
- throw new Error(`Couldn't find any element with the selector ${selectors}`);
254
+ throw new Error(`Could not find any element with the selector ${selectors}`);
207
255
  }
208
256
  return elements;
209
257
  }
@@ -373,6 +421,7 @@ exports.$$ = $$;
373
421
  exports.addStyle = addStyle;
374
422
  exports.alert = alert;
375
423
  exports.asserted = asserted;
424
+ exports.capitalize = capitalize;
376
425
  exports.confirm = confirm;
377
426
  exports.fish = fish;
378
427
  exports.fishResponse = fishResponse;
@@ -393,6 +442,7 @@ exports.isTruthy = isTruthy;
393
442
  exports.join = join;
394
443
  exports.logId = logId;
395
444
  exports.noop = noop;
445
+ exports.padZeros = padZeros;
396
446
  exports.pageLoad = pageLoad;
397
447
  exports.prompt = prompt;
398
448
  exports.remove = remove;
@@ -35,6 +35,20 @@ declare function isFunction<T extends Function>(object: unknown): object is T;
35
35
  */
36
36
  declare function sleep(milliSeconds: number): Promise<void>;
37
37
 
38
+ /**
39
+ * Adds leading zeros to a number.
40
+ *
41
+ * @category String
42
+ */
43
+ declare function padZeros(number: number, length: number): string;
44
+
45
+ /**
46
+ * capitalizes a word or a sentence.
47
+ *
48
+ * @category String
49
+ */
50
+ declare function capitalize(sentence: string): string;
51
+
38
52
  type Nullable<T> = T | null | undefined;
39
53
  type Fn<T = void, U = any> = (param?: U) => T;
40
54
  type Prettify<T> = {
@@ -132,4 +146,4 @@ type Options = Record<string, FlagValue>;
132
146
 
133
147
  declare function getOptions(flags: Flag[]): Promise<Options>;
134
148
 
135
- export { $, $$, Flag, Fn, Nullable, Prettify, addStyle, alert, asserted, confirm, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, logId, noop, pageLoad, prompt, remove, scriptName, setSearchParam, sleep, tabURL, toString };
149
+ export { $, $$, Flag, Fn, Nullable, Prettify, addStyle, alert, asserted, capitalize, confirm, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, logId, noop, padZeros, pageLoad, prompt, remove, scriptName, setSearchParam, sleep, tabURL, toString };
package/browser/index.js CHANGED
@@ -76,6 +76,53 @@ async function sleep(milliSeconds) {
76
76
  });
77
77
  }
78
78
 
79
+ /**
80
+ * Adds leading zeros to a number.
81
+ *
82
+ * @category String
83
+ */
84
+ function padZeros(number, length) {
85
+ if (Math.abs(number).toString().length > length) {
86
+ throw new Error('Provided length can not be less than the digits of the number.');
87
+ }
88
+ if (number < 0) {
89
+ throw new Error('Provided number must be greater than zero.');
90
+ }
91
+ const padded = '0'.repeat(length - 1) + number;
92
+ if (number < 10) {
93
+ return padded;
94
+ }
95
+ return padded.slice(-length);
96
+ }
97
+
98
+ const lowercaseWords = new Set([
99
+ 'a', 'an', 'the',
100
+ 'and', 'but', 'for', 'nor', 'or', 'so', 'yet',
101
+ 'about', 'across', 'after', 'along', 'among', 'around', 'as', 'at', 'before', 'between', 'by', 'during', 'in', 'into', 'of', 'on', 'over', 'since', 'through', 'throughout', 'to', 'under', 'with', 'within',
102
+ 'away', 'back', 'down', 'far', 'off', 'on', 'out', 'up',
103
+ 'he', 'her', 'him', 'his', 'it', 'its', 'me', 'my', 'our', 'she', 'their', 'them', 'they', 'us', 'we', 'you', 'your',
104
+ ]);
105
+ function capitalizeWord(word) {
106
+ return word[0].toUpperCase() + word.slice(1).toLowerCase();
107
+ }
108
+ /**
109
+ * capitalizes a word or a sentence.
110
+ *
111
+ * @category String
112
+ */
113
+ function capitalize(sentence) {
114
+ if (sentence.includes(' ')) {
115
+ return sentence.split(' ').map((word, index) => {
116
+ const lowercaseWord = word.toLowerCase();
117
+ if (index > 0 && lowercaseWords.has(lowercaseWord)) {
118
+ return lowercaseWord;
119
+ }
120
+ return capitalizeWord(word);
121
+ }).join(' ');
122
+ }
123
+ return capitalizeWord(sentence);
124
+ }
125
+
79
126
  let infoObject;
80
127
  if (typeof GM !== 'undefined') {
81
128
  infoObject = GM.info;
@@ -109,7 +156,7 @@ function prompt(message, _default) {
109
156
 
110
157
  async function fishResponse(url, options) {
111
158
  const response = await fetch(url, options);
112
- if (!response.ok) {
159
+ if (!response.ok && !response.url.startsWith('file:///')) {
113
160
  throw new Error(`Request to ${response.url} ended with ${response.status} status.`);
114
161
  }
115
162
  return response;
@@ -129,14 +176,15 @@ async function fishXResponse(url, fishOptions) {
129
176
  timeout: timeOut,
130
177
  onprogress: onProgress,
131
178
  onload({ response, statusText, status, finalUrl }) {
179
+ const isFileURL = finalUrl.startsWith('file:///');
132
180
  const ok = status >= 200 && status < 300;
133
- if (!ok) {
181
+ if (!ok && !isFileURL) {
134
182
  reject(new Error(`Request to ${url} ended with ${status} status.`));
135
183
  return;
136
184
  }
137
185
  const properResponse = new Response(response, {
138
186
  statusText,
139
- status,
187
+ status: isFileURL ? 200 : status,
140
188
  });
141
189
  Object.defineProperty(properResponse, 'url', { value: finalUrl });
142
190
  resolve(properResponse);
@@ -194,14 +242,14 @@ const fishX = {
194
242
  function $(selectors, parent) {
195
243
  const element = (parent ?? document).querySelector(join(selectors));
196
244
  if (element === null) {
197
- throw new Error(`Couldn't find the element with the selector ${selectors}`);
245
+ throw new Error(`Could not find the element with the selector ${selectors}`);
198
246
  }
199
247
  return element;
200
248
  }
201
249
  function $$(selectors, parent) {
202
250
  const elements = (parent ?? document).querySelectorAll(join(selectors));
203
251
  if (elements.length === 0) {
204
- throw new Error(`Couldn't find any element with the selector ${selectors}`);
252
+ throw new Error(`Could not find any element with the selector ${selectors}`);
205
253
  }
206
254
  return elements;
207
255
  }
@@ -366,4 +414,4 @@ async function getOptions(flags) {
366
414
  return options;
367
415
  }
368
416
 
369
- export { $, $$, addStyle, alert, asserted, confirm, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, logId, noop, pageLoad, prompt, remove, scriptName, setSearchParam, sleep, tabURL, toString };
417
+ export { $, $$, addStyle, alert, asserted, capitalize, confirm, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, logId, noop, padZeros, pageLoad, prompt, remove, scriptName, setSearchParam, sleep, tabURL, toString };
package/node/index.cjs CHANGED
@@ -82,6 +82,53 @@ async function sleep(milliSeconds) {
82
82
  });
83
83
  }
84
84
 
85
+ /**
86
+ * Adds leading zeros to a number.
87
+ *
88
+ * @category String
89
+ */
90
+ function padZeros(number, length) {
91
+ if (Math.abs(number).toString().length > length) {
92
+ throw new Error('Provided length can not be less than the digits of the number.');
93
+ }
94
+ if (number < 0) {
95
+ throw new Error('Provided number must be greater than zero.');
96
+ }
97
+ const padded = '0'.repeat(length - 1) + number;
98
+ if (number < 10) {
99
+ return padded;
100
+ }
101
+ return padded.slice(-length);
102
+ }
103
+
104
+ const lowercaseWords = new Set([
105
+ 'a', 'an', 'the',
106
+ 'and', 'but', 'for', 'nor', 'or', 'so', 'yet',
107
+ 'about', 'across', 'after', 'along', 'among', 'around', 'as', 'at', 'before', 'between', 'by', 'during', 'in', 'into', 'of', 'on', 'over', 'since', 'through', 'throughout', 'to', 'under', 'with', 'within',
108
+ 'away', 'back', 'down', 'far', 'off', 'on', 'out', 'up',
109
+ 'he', 'her', 'him', 'his', 'it', 'its', 'me', 'my', 'our', 'she', 'their', 'them', 'they', 'us', 'we', 'you', 'your',
110
+ ]);
111
+ function capitalizeWord(word) {
112
+ return word[0].toUpperCase() + word.slice(1).toLowerCase();
113
+ }
114
+ /**
115
+ * capitalizes a word or a sentence.
116
+ *
117
+ * @category String
118
+ */
119
+ function capitalize(sentence) {
120
+ if (sentence.includes(' ')) {
121
+ return sentence.split(' ').map((word, index) => {
122
+ const lowercaseWord = word.toLowerCase();
123
+ if (index > 0 && lowercaseWords.has(lowercaseWord)) {
124
+ return lowercaseWord;
125
+ }
126
+ return capitalizeWord(word);
127
+ }).join(' ');
128
+ }
129
+ return capitalizeWord(sentence);
130
+ }
131
+
85
132
  /**
86
133
  * Lists the files that are present in the provided folder path.
87
134
  * @throws an error if the provided folder path does not exist.
@@ -193,6 +240,7 @@ async function readJSON(filePath, withComments = false) {
193
240
  }
194
241
 
195
242
  exports.asserted = asserted;
243
+ exports.capitalize = capitalize;
196
244
  exports.isBoolean = isBoolean;
197
245
  exports.isFalsy = isFalsy;
198
246
  exports.isFunction = isFunction;
@@ -207,6 +255,7 @@ exports.listFiles = listFiles;
207
255
  exports.listFolders = listFolders;
208
256
  exports.listFoldersSync = listFoldersSync;
209
257
  exports.noop = noop;
258
+ exports.padZeros = padZeros;
210
259
  exports.readJSON = readJSON;
211
260
  exports.remove = remove;
212
261
  exports.removeFiles = removeFiles;
package/node/index.d.ts CHANGED
@@ -35,6 +35,20 @@ declare function isFunction<T extends Function>(object: unknown): object is T;
35
35
  */
36
36
  declare function sleep(milliSeconds: number): Promise<void>;
37
37
 
38
+ /**
39
+ * Adds leading zeros to a number.
40
+ *
41
+ * @category String
42
+ */
43
+ declare function padZeros(number: number, length: number): string;
44
+
45
+ /**
46
+ * capitalizes a word or a sentence.
47
+ *
48
+ * @category String
49
+ */
50
+ declare function capitalize(sentence: string): string;
51
+
38
52
  type Nullable<T> = T | null | undefined;
39
53
  type Fn<T = void, U = any> = (param?: U) => T;
40
54
  type Prettify<T> = {
@@ -95,4 +109,4 @@ declare function removeFiles({ folderPath, filter }: RemoveFilesOpt): Promise<vo
95
109
 
96
110
  declare function readJSON<T = object>(filePath: string, withComments?: boolean): Promise<T>;
97
111
 
98
- export { Fn, Nullable, Prettify, asserted, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, readJSON, remove, removeFiles, sleep, toString };
112
+ export { Fn, Nullable, Prettify, asserted, capitalize, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, sleep, toString };
package/node/index.js CHANGED
@@ -80,6 +80,53 @@ async function sleep(milliSeconds) {
80
80
  });
81
81
  }
82
82
 
83
+ /**
84
+ * Adds leading zeros to a number.
85
+ *
86
+ * @category String
87
+ */
88
+ function padZeros(number, length) {
89
+ if (Math.abs(number).toString().length > length) {
90
+ throw new Error('Provided length can not be less than the digits of the number.');
91
+ }
92
+ if (number < 0) {
93
+ throw new Error('Provided number must be greater than zero.');
94
+ }
95
+ const padded = '0'.repeat(length - 1) + number;
96
+ if (number < 10) {
97
+ return padded;
98
+ }
99
+ return padded.slice(-length);
100
+ }
101
+
102
+ const lowercaseWords = new Set([
103
+ 'a', 'an', 'the',
104
+ 'and', 'but', 'for', 'nor', 'or', 'so', 'yet',
105
+ 'about', 'across', 'after', 'along', 'among', 'around', 'as', 'at', 'before', 'between', 'by', 'during', 'in', 'into', 'of', 'on', 'over', 'since', 'through', 'throughout', 'to', 'under', 'with', 'within',
106
+ 'away', 'back', 'down', 'far', 'off', 'on', 'out', 'up',
107
+ 'he', 'her', 'him', 'his', 'it', 'its', 'me', 'my', 'our', 'she', 'their', 'them', 'they', 'us', 'we', 'you', 'your',
108
+ ]);
109
+ function capitalizeWord(word) {
110
+ return word[0].toUpperCase() + word.slice(1).toLowerCase();
111
+ }
112
+ /**
113
+ * capitalizes a word or a sentence.
114
+ *
115
+ * @category String
116
+ */
117
+ function capitalize(sentence) {
118
+ if (sentence.includes(' ')) {
119
+ return sentence.split(' ').map((word, index) => {
120
+ const lowercaseWord = word.toLowerCase();
121
+ if (index > 0 && lowercaseWords.has(lowercaseWord)) {
122
+ return lowercaseWord;
123
+ }
124
+ return capitalizeWord(word);
125
+ }).join(' ');
126
+ }
127
+ return capitalizeWord(sentence);
128
+ }
129
+
83
130
  /**
84
131
  * Lists the files that are present in the provided folder path.
85
132
  * @throws an error if the provided folder path does not exist.
@@ -190,4 +237,4 @@ async function readJSON(filePath, withComments = false) {
190
237
  return JSON.parse(stringJSON);
191
238
  }
192
239
 
193
- export { asserted, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, readJSON, remove, removeFiles, sleep, toString };
240
+ export { asserted, capitalize, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, sleep, toString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jeniex/utils",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "author": "JenieX",
5
5
  "homepage": "https://github.com/JenieX/utils",
6
6
  "type": "module",