@jeniex/utils 1.0.20 → 1.0.22

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;
@@ -197,14 +244,14 @@ const fishX = {
197
244
  function $(selectors, parent) {
198
245
  const element = (parent ?? document).querySelector(join(selectors));
199
246
  if (element === null) {
200
- 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}`);
201
248
  }
202
249
  return element;
203
250
  }
204
251
  function $$(selectors, parent) {
205
252
  const elements = (parent ?? document).querySelectorAll(join(selectors));
206
253
  if (elements.length === 0) {
207
- 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}`);
208
255
  }
209
256
  return elements;
210
257
  }
@@ -239,6 +286,26 @@ function setSearchParam(key, value, fullURL) {
239
286
  }
240
287
  return updatedURL.join('');
241
288
  }
289
+ /**
290
+ * @param keys The names of the param in the search query.
291
+ * @param fullURL If provided, it will be our target, otherwise it is the current location.
292
+ */
293
+ function removeSearchParams(keys, fullURL) {
294
+ const updatedURL = [];
295
+ const { origin, pathname, search, hash } = new URL(fullURL ?? window.location.href);
296
+ const searchParams = new URLSearchParams(search);
297
+ for (const key of keys) {
298
+ searchParams.delete(key);
299
+ }
300
+ updatedURL.push(origin, pathname);
301
+ if (searchParams.toString() !== '') {
302
+ updatedURL.push('?', searchParams.toString());
303
+ }
304
+ if (hash !== '') {
305
+ updatedURL.push(hash);
306
+ }
307
+ return updatedURL.join('');
308
+ }
242
309
 
243
310
  function addStyle(css, parent = document.documentElement) {
244
311
  const style = document.createElement('style');
@@ -374,6 +441,7 @@ exports.$$ = $$;
374
441
  exports.addStyle = addStyle;
375
442
  exports.alert = alert;
376
443
  exports.asserted = asserted;
444
+ exports.capitalize = capitalize;
377
445
  exports.confirm = confirm;
378
446
  exports.fish = fish;
379
447
  exports.fishResponse = fishResponse;
@@ -394,9 +462,11 @@ exports.isTruthy = isTruthy;
394
462
  exports.join = join;
395
463
  exports.logId = logId;
396
464
  exports.noop = noop;
465
+ exports.padZeros = padZeros;
397
466
  exports.pageLoad = pageLoad;
398
467
  exports.prompt = prompt;
399
468
  exports.remove = remove;
469
+ exports.removeSearchParams = removeSearchParams;
400
470
  exports.scriptName = scriptName;
401
471
  exports.setSearchParam = setSearchParam;
402
472
  exports.sleep = sleep;
@@ -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> = {
@@ -86,6 +100,11 @@ declare function getSearchParam(key: string, fullURL?: string): string | undefin
86
100
  * @param fullURL If provided, it will be our target, otherwise it is the current location.
87
101
  */
88
102
  declare function setSearchParam(key: string, value: string, fullURL?: string): string;
103
+ /**
104
+ * @param keys The names of the param in the search query.
105
+ * @param fullURL If provided, it will be our target, otherwise it is the current location.
106
+ */
107
+ declare function removeSearchParams(keys: string[], fullURL?: string): string;
89
108
 
90
109
  declare const scriptName: string;
91
110
  /** The identifier of the script to be used in logging. */
@@ -132,4 +151,4 @@ type Options = Record<string, FlagValue>;
132
151
 
133
152
  declare function getOptions(flags: Flag[]): Promise<Options>;
134
153
 
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 };
154
+ 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, removeSearchParams, 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;
@@ -195,14 +242,14 @@ const fishX = {
195
242
  function $(selectors, parent) {
196
243
  const element = (parent ?? document).querySelector(join(selectors));
197
244
  if (element === null) {
198
- 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}`);
199
246
  }
200
247
  return element;
201
248
  }
202
249
  function $$(selectors, parent) {
203
250
  const elements = (parent ?? document).querySelectorAll(join(selectors));
204
251
  if (elements.length === 0) {
205
- 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}`);
206
253
  }
207
254
  return elements;
208
255
  }
@@ -237,6 +284,26 @@ function setSearchParam(key, value, fullURL) {
237
284
  }
238
285
  return updatedURL.join('');
239
286
  }
287
+ /**
288
+ * @param keys The names of the param in the search query.
289
+ * @param fullURL If provided, it will be our target, otherwise it is the current location.
290
+ */
291
+ function removeSearchParams(keys, fullURL) {
292
+ const updatedURL = [];
293
+ const { origin, pathname, search, hash } = new URL(fullURL ?? window.location.href);
294
+ const searchParams = new URLSearchParams(search);
295
+ for (const key of keys) {
296
+ searchParams.delete(key);
297
+ }
298
+ updatedURL.push(origin, pathname);
299
+ if (searchParams.toString() !== '') {
300
+ updatedURL.push('?', searchParams.toString());
301
+ }
302
+ if (hash !== '') {
303
+ updatedURL.push(hash);
304
+ }
305
+ return updatedURL.join('');
306
+ }
240
307
 
241
308
  function addStyle(css, parent = document.documentElement) {
242
309
  const style = document.createElement('style');
@@ -367,4 +434,4 @@ async function getOptions(flags) {
367
434
  return options;
368
435
  }
369
436
 
370
- 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 };
437
+ 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, removeSearchParams, 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.20",
3
+ "version": "1.0.22",
4
4
  "author": "JenieX",
5
5
  "homepage": "https://github.com/JenieX/utils",
6
6
  "type": "module",