@jeniex/utils 1.0.15 → 1.0.16

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,17 +1,52 @@
1
1
  'use strict';
2
2
 
3
+ function noop() { }
4
+ function toString(object) {
5
+ return Object.prototype.toString.call(object);
6
+ }
7
+
3
8
  function isString(object) {
4
9
  return typeof object === 'string';
5
10
  }
11
+ function isNumber(object) {
12
+ return typeof object === 'number';
13
+ }
14
+ function isBoolean(object) {
15
+ return typeof object === 'boolean';
16
+ }
17
+ function isObject(object) {
18
+ return toString(object) === '[object Object]';
19
+ }
20
+ // eslint-disable-next-line @typescript-eslint/ban-types
21
+ function isFunction(object) {
22
+ return typeof object === 'function';
23
+ }
6
24
 
7
- function ensureJoin(object, separator = ',') {
25
+ /**
26
+ * Joins an array's items or do nothing if it is joined already.
27
+ *
28
+ * @category Array
29
+ */
30
+ function join(object, separator = ',') {
8
31
  if (isString(object)) {
9
32
  return object;
10
33
  }
11
34
  return object.join(separator);
12
35
  }
13
36
 
14
- function noop() { }
37
+ /**
38
+ * Removes an item from an array by mutating it.
39
+ * @throws An error if the object to be removed does not exist inside the array.
40
+ *
41
+ * @category Array
42
+ */
43
+ function remove(array, object) {
44
+ const index = array.indexOf(object);
45
+ if (index === -1) {
46
+ throw new Error('Provided value does not exist inside the array.');
47
+ }
48
+ array.splice(index, 1);
49
+ }
15
50
 
16
51
  function asserted(object) {
17
52
  if (object === null || object === undefined) {
@@ -32,6 +67,11 @@ function isNotNullish(object) {
32
67
  return object !== null && object !== undefined;
33
68
  }
34
69
 
70
+ /**
71
+ * A wrapper around `setTimeout`.
72
+ *
73
+ * @category Promise
74
+ */
35
75
  async function sleep(milliSeconds) {
36
76
  return new Promise((resolve) => {
37
77
  setTimeout(resolve, milliSeconds);
@@ -154,14 +194,14 @@ const fishX = {
154
194
  };
155
195
 
156
196
  function $(selectors, parent) {
157
- const element = (parent ?? document).querySelector(ensureJoin(selectors));
197
+ const element = (parent ?? document).querySelector(join(selectors));
158
198
  if (element === null) {
159
199
  throw new Error(`Couldn't find the element with the selector ${selectors}`);
160
200
  }
161
201
  return element;
162
202
  }
163
203
  function $$(selectors, parent) {
164
- const elements = (parent ?? document).querySelectorAll(ensureJoin(selectors));
204
+ const elements = (parent ?? document).querySelectorAll(join(selectors));
165
205
  if (elements.length === 0) {
166
206
  throw new Error(`Couldn't find any element with the selector ${selectors}`);
167
207
  }
@@ -334,7 +374,6 @@ exports.addStyle = addStyle;
334
374
  exports.alert = alert;
335
375
  exports.asserted = asserted;
336
376
  exports.confirm = confirm;
337
- exports.ensureJoin = ensureJoin;
338
377
  exports.fish = fish;
339
378
  exports.fishResponse = fishResponse;
340
379
  exports.fishX = fishX;
@@ -342,16 +381,23 @@ exports.fishXResponse = fishXResponse;
342
381
  exports.getOptions = getOptions;
343
382
  exports.getSearchParam = getSearchParam;
344
383
  exports.imageLoad = imageLoad;
384
+ exports.isBoolean = isBoolean;
345
385
  exports.isFalsy = isFalsy;
386
+ exports.isFunction = isFunction;
346
387
  exports.isNotNullish = isNotNullish;
347
388
  exports.isNullish = isNullish;
389
+ exports.isNumber = isNumber;
390
+ exports.isObject = isObject;
348
391
  exports.isString = isString;
349
392
  exports.isTruthy = isTruthy;
393
+ exports.join = join;
350
394
  exports.logId = logId;
351
395
  exports.noop = noop;
352
396
  exports.pageLoad = pageLoad;
353
397
  exports.prompt = prompt;
398
+ exports.remove = remove;
354
399
  exports.scriptName = scriptName;
355
400
  exports.setSearchParam = setSearchParam;
356
401
  exports.sleep = sleep;
357
402
  exports.tabURL = tabURL;
403
+ exports.toString = toString;
@@ -1,6 +1,20 @@
1
- declare function ensureJoin(object: string[] | string, separator?: string): string;
1
+ /**
2
+ * Joins an array's items or do nothing if it is joined already.
3
+ *
4
+ * @category Array
5
+ */
6
+ declare function join(object: string[] | string, separator?: string): string;
7
+
8
+ /**
9
+ * Removes an item from an array by mutating it.
10
+ * @throws An error if the object to be removed does not exist inside the array.
11
+ *
12
+ * @category Array
13
+ */
14
+ declare function remove<T>(array: T[], object: T): void;
2
15
 
3
16
  declare function noop(): void;
17
+ declare function toString(object: unknown): string;
4
18
 
5
19
  declare function asserted<T>(object: T | null | undefined): T;
6
20
  declare function isTruthy<T>(object: T): object is Exclude<T, '' | 0 | false | null | undefined>;
@@ -9,7 +23,16 @@ declare function isNullish(object: unknown): object is null | undefined;
9
23
  declare function isNotNullish<T>(object: T): object is Exclude<T, null | undefined>;
10
24
 
11
25
  declare function isString(object: unknown): object is string;
26
+ declare function isNumber(object: unknown): object is number;
27
+ declare function isBoolean(object: unknown): object is boolean;
28
+ declare function isObject(object: unknown): object is object;
29
+ declare function isFunction<T extends Function>(object: unknown): object is T;
12
30
 
31
+ /**
32
+ * A wrapper around `setTimeout`.
33
+ *
34
+ * @category Promise
35
+ */
13
36
  declare function sleep(milliSeconds: number): Promise<void>;
14
37
 
15
38
  type Nullable<T> = T | null | undefined;
@@ -106,4 +129,4 @@ type Options = Record<string, FlagValue>;
106
129
 
107
130
  declare function getOptions(flags: Flag[]): Promise<Options>;
108
131
 
109
- export { $, $$, Flag, Fn, Nullable, addStyle, alert, asserted, confirm, ensureJoin, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isFalsy, isNotNullish, isNullish, isString, isTruthy, logId, noop, pageLoad, prompt, scriptName, setSearchParam, sleep, tabURL };
132
+ export { $, $$, Flag, Fn, Nullable, 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 };
package/browser/index.js CHANGED
@@ -1,15 +1,50 @@
1
+ function noop() { }
2
+ function toString(object) {
3
+ return Object.prototype.toString.call(object);
4
+ }
5
+
1
6
  function isString(object) {
2
7
  return typeof object === 'string';
3
8
  }
9
+ function isNumber(object) {
10
+ return typeof object === 'number';
11
+ }
12
+ function isBoolean(object) {
13
+ return typeof object === 'boolean';
14
+ }
15
+ function isObject(object) {
16
+ return toString(object) === '[object Object]';
17
+ }
18
+ // eslint-disable-next-line @typescript-eslint/ban-types
19
+ function isFunction(object) {
20
+ return typeof object === 'function';
21
+ }
4
22
 
5
- function ensureJoin(object, separator = ',') {
23
+ /**
24
+ * Joins an array's items or do nothing if it is joined already.
25
+ *
26
+ * @category Array
27
+ */
28
+ function join(object, separator = ',') {
6
29
  if (isString(object)) {
7
30
  return object;
8
31
  }
9
32
  return object.join(separator);
10
33
  }
11
34
 
12
- function noop() { }
35
+ /**
36
+ * Removes an item from an array by mutating it.
37
+ * @throws An error if the object to be removed does not exist inside the array.
38
+ *
39
+ * @category Array
40
+ */
41
+ function remove(array, object) {
42
+ const index = array.indexOf(object);
43
+ if (index === -1) {
44
+ throw new Error('Provided value does not exist inside the array.');
45
+ }
46
+ array.splice(index, 1);
47
+ }
13
48
 
14
49
  function asserted(object) {
15
50
  if (object === null || object === undefined) {
@@ -30,6 +65,11 @@ function isNotNullish(object) {
30
65
  return object !== null && object !== undefined;
31
66
  }
32
67
 
68
+ /**
69
+ * A wrapper around `setTimeout`.
70
+ *
71
+ * @category Promise
72
+ */
33
73
  async function sleep(milliSeconds) {
34
74
  return new Promise((resolve) => {
35
75
  setTimeout(resolve, milliSeconds);
@@ -152,14 +192,14 @@ const fishX = {
152
192
  };
153
193
 
154
194
  function $(selectors, parent) {
155
- const element = (parent ?? document).querySelector(ensureJoin(selectors));
195
+ const element = (parent ?? document).querySelector(join(selectors));
156
196
  if (element === null) {
157
197
  throw new Error(`Couldn't find the element with the selector ${selectors}`);
158
198
  }
159
199
  return element;
160
200
  }
161
201
  function $$(selectors, parent) {
162
- const elements = (parent ?? document).querySelectorAll(ensureJoin(selectors));
202
+ const elements = (parent ?? document).querySelectorAll(join(selectors));
163
203
  if (elements.length === 0) {
164
204
  throw new Error(`Couldn't find any element with the selector ${selectors}`);
165
205
  }
@@ -326,4 +366,4 @@ async function getOptions(flags) {
326
366
  return options;
327
367
  }
328
368
 
329
- export { $, $$, addStyle, alert, asserted, confirm, ensureJoin, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isFalsy, isNotNullish, isNullish, isString, isTruthy, logId, noop, pageLoad, prompt, scriptName, setSearchParam, sleep, tabURL };
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 };
package/node/index.cjs CHANGED
@@ -4,18 +4,53 @@ var fs = require('node:fs');
4
4
  var fsp = require('node:fs/promises');
5
5
  var path = require('node:path');
6
6
 
7
+ function noop() { }
8
+ function toString(object) {
9
+ return Object.prototype.toString.call(object);
10
+ }
11
+
7
12
  function isString(object) {
8
13
  return typeof object === 'string';
9
14
  }
15
+ function isNumber(object) {
16
+ return typeof object === 'number';
17
+ }
18
+ function isBoolean(object) {
19
+ return typeof object === 'boolean';
20
+ }
21
+ function isObject(object) {
22
+ return toString(object) === '[object Object]';
23
+ }
24
+ // eslint-disable-next-line @typescript-eslint/ban-types
25
+ function isFunction(object) {
26
+ return typeof object === 'function';
27
+ }
10
28
 
11
- function ensureJoin(object, separator = ',') {
29
+ /**
30
+ * Joins an array's items or do nothing if it is joined already.
31
+ *
32
+ * @category Array
33
+ */
34
+ function join(object, separator = ',') {
12
35
  if (isString(object)) {
13
36
  return object;
14
37
  }
15
38
  return object.join(separator);
16
39
  }
17
40
 
18
- function noop() { }
41
+ /**
42
+ * Removes an item from an array by mutating it.
43
+ * @throws An error if the object to be removed does not exist inside the array.
44
+ *
45
+ * @category Array
46
+ */
47
+ function remove(array, object) {
48
+ const index = array.indexOf(object);
49
+ if (index === -1) {
50
+ throw new Error('Provided value does not exist inside the array.');
51
+ }
52
+ array.splice(index, 1);
53
+ }
19
54
 
20
55
  function asserted(object) {
21
56
  if (object === null || object === undefined) {
@@ -36,6 +71,11 @@ function isNotNullish(object) {
36
71
  return object !== null && object !== undefined;
37
72
  }
38
73
 
74
+ /**
75
+ * A wrapper around `setTimeout`.
76
+ *
77
+ * @category Promise
78
+ */
39
79
  async function sleep(milliSeconds) {
40
80
  return new Promise((resolve) => {
41
81
  setTimeout(resolve, milliSeconds);
@@ -125,16 +165,37 @@ async function removeFiles({ folderPath, filter }) {
125
165
  }
126
166
  }
127
167
 
168
+ async function readJSON(filePath, withComments = false) {
169
+ const stringJSON = await fsp.readFile(filePath, 'utf-8');
170
+ if (withComments === true) {
171
+ try {
172
+ const { default: JSON5 } = await import('json5');
173
+ return JSON5.parse(stringJSON);
174
+ }
175
+ catch (exception) {
176
+ throw new Error(exception.message);
177
+ }
178
+ }
179
+ return JSON.parse(stringJSON);
180
+ }
181
+
128
182
  exports.asserted = asserted;
129
- exports.ensureJoin = ensureJoin;
183
+ exports.isBoolean = isBoolean;
130
184
  exports.isFalsy = isFalsy;
185
+ exports.isFunction = isFunction;
131
186
  exports.isNotNullish = isNotNullish;
132
187
  exports.isNullish = isNullish;
188
+ exports.isNumber = isNumber;
189
+ exports.isObject = isObject;
133
190
  exports.isString = isString;
134
191
  exports.isTruthy = isTruthy;
192
+ exports.join = join;
135
193
  exports.listFiles = listFiles;
136
194
  exports.listFolders = listFolders;
137
195
  exports.listFoldersSync = listFoldersSync;
138
196
  exports.noop = noop;
197
+ exports.readJSON = readJSON;
198
+ exports.remove = remove;
139
199
  exports.removeFiles = removeFiles;
140
200
  exports.sleep = sleep;
201
+ exports.toString = toString;
package/node/index.d.ts CHANGED
@@ -1,6 +1,20 @@
1
- declare function ensureJoin(object: string[] | string, separator?: string): string;
1
+ /**
2
+ * Joins an array's items or do nothing if it is joined already.
3
+ *
4
+ * @category Array
5
+ */
6
+ declare function join(object: string[] | string, separator?: string): string;
7
+
8
+ /**
9
+ * Removes an item from an array by mutating it.
10
+ * @throws An error if the object to be removed does not exist inside the array.
11
+ *
12
+ * @category Array
13
+ */
14
+ declare function remove<T>(array: T[], object: T): void;
2
15
 
3
16
  declare function noop(): void;
17
+ declare function toString(object: unknown): string;
4
18
 
5
19
  declare function asserted<T>(object: T | null | undefined): T;
6
20
  declare function isTruthy<T>(object: T): object is Exclude<T, '' | 0 | false | null | undefined>;
@@ -9,7 +23,16 @@ declare function isNullish(object: unknown): object is null | undefined;
9
23
  declare function isNotNullish<T>(object: T): object is Exclude<T, null | undefined>;
10
24
 
11
25
  declare function isString(object: unknown): object is string;
26
+ declare function isNumber(object: unknown): object is number;
27
+ declare function isBoolean(object: unknown): object is boolean;
28
+ declare function isObject(object: unknown): object is object;
29
+ declare function isFunction<T extends Function>(object: unknown): object is T;
12
30
 
31
+ /**
32
+ * A wrapper around `setTimeout`.
33
+ *
34
+ * @category Promise
35
+ */
13
36
  declare function sleep(milliSeconds: number): Promise<void>;
14
37
 
15
38
  type Nullable<T> = T | null | undefined;
@@ -54,4 +77,6 @@ declare function listFoldersSync({ folderPath, getFullPath }: ListFoldersOpt): s
54
77
 
55
78
  declare function removeFiles({ folderPath, filter }: RemoveFilesOpt): Promise<void>;
56
79
 
57
- export { Fn, Nullable, asserted, ensureJoin, isFalsy, isNotNullish, isNullish, isString, isTruthy, listFiles, listFolders, listFoldersSync, noop, removeFiles, sleep };
80
+ declare function readJSON<T = object>(filePath: string, withComments?: boolean): Promise<T>;
81
+
82
+ export { Fn, Nullable, asserted, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, readJSON, remove, removeFiles, sleep, toString };
package/node/index.js CHANGED
@@ -2,18 +2,53 @@ import fs from 'node:fs';
2
2
  import fsp from 'node:fs/promises';
3
3
  import path from 'node:path';
4
4
 
5
+ function noop() { }
6
+ function toString(object) {
7
+ return Object.prototype.toString.call(object);
8
+ }
9
+
5
10
  function isString(object) {
6
11
  return typeof object === 'string';
7
12
  }
13
+ function isNumber(object) {
14
+ return typeof object === 'number';
15
+ }
16
+ function isBoolean(object) {
17
+ return typeof object === 'boolean';
18
+ }
19
+ function isObject(object) {
20
+ return toString(object) === '[object Object]';
21
+ }
22
+ // eslint-disable-next-line @typescript-eslint/ban-types
23
+ function isFunction(object) {
24
+ return typeof object === 'function';
25
+ }
8
26
 
9
- function ensureJoin(object, separator = ',') {
27
+ /**
28
+ * Joins an array's items or do nothing if it is joined already.
29
+ *
30
+ * @category Array
31
+ */
32
+ function join(object, separator = ',') {
10
33
  if (isString(object)) {
11
34
  return object;
12
35
  }
13
36
  return object.join(separator);
14
37
  }
15
38
 
16
- function noop() { }
39
+ /**
40
+ * Removes an item from an array by mutating it.
41
+ * @throws An error if the object to be removed does not exist inside the array.
42
+ *
43
+ * @category Array
44
+ */
45
+ function remove(array, object) {
46
+ const index = array.indexOf(object);
47
+ if (index === -1) {
48
+ throw new Error('Provided value does not exist inside the array.');
49
+ }
50
+ array.splice(index, 1);
51
+ }
17
52
 
18
53
  function asserted(object) {
19
54
  if (object === null || object === undefined) {
@@ -34,6 +69,11 @@ function isNotNullish(object) {
34
69
  return object !== null && object !== undefined;
35
70
  }
36
71
 
72
+ /**
73
+ * A wrapper around `setTimeout`.
74
+ *
75
+ * @category Promise
76
+ */
37
77
  async function sleep(milliSeconds) {
38
78
  return new Promise((resolve) => {
39
79
  setTimeout(resolve, milliSeconds);
@@ -123,4 +163,18 @@ async function removeFiles({ folderPath, filter }) {
123
163
  }
124
164
  }
125
165
 
126
- export { asserted, ensureJoin, isFalsy, isNotNullish, isNullish, isString, isTruthy, listFiles, listFolders, listFoldersSync, noop, removeFiles, sleep };
166
+ async function readJSON(filePath, withComments = false) {
167
+ const stringJSON = await fsp.readFile(filePath, 'utf-8');
168
+ if (withComments === true) {
169
+ try {
170
+ const { default: JSON5 } = await import('json5');
171
+ return JSON5.parse(stringJSON);
172
+ }
173
+ catch (exception) {
174
+ throw new Error(exception.message);
175
+ }
176
+ }
177
+ return JSON.parse(stringJSON);
178
+ }
179
+
180
+ export { asserted, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, readJSON, remove, removeFiles, sleep, toString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jeniex/utils",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "author": "JenieX",
5
5
  "homepage": "https://github.com/JenieX/utils",
6
6
  "type": "module",