@jeniex/utils 1.2.0 → 1.4.0

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
@@ -48,16 +48,130 @@ function remove(array, object) {
48
48
  array.splice(index, 1);
49
49
  }
50
50
 
51
+ /**
52
+ * Asserts that an object is neither `null` nor `undefined`.
53
+ *
54
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
55
+ * Otherwise, TypeScript narrows the object to `T`.
56
+ *
57
+ * @template T The type of the object.
58
+ *
59
+ * @param object The object to validate.
60
+ *
61
+ * @throws {TypeError} If the object is `null` or `undefined`.
62
+ *
63
+ * @example
64
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
65
+ *
66
+ * assert(object);
67
+ *
68
+ * // `object` is now typed as `string`.
69
+ * console.log(object.length);
70
+ */
71
+ function assert(object) {
72
+ if (object === null || object === undefined) {
73
+ throw new TypeError('object is either null or undefined');
74
+ }
75
+ }
76
+ /**
77
+ * Asserts that an object satisfies the given type guard.
78
+ *
79
+ * If the type guard returns `false`, a `TypeError` is thrown.
80
+ * Otherwise, TypeScript narrows the object to the type accepted by
81
+ * the type guard.
82
+ *
83
+ * @template T The type checked by the type guard.
84
+ *
85
+ * @param object The object to validate.
86
+ * @param guard A type guard used to validate the object.
87
+ *
88
+ * @throws {TypeError} If the object does not satisfy the type guard.
89
+ *
90
+ * @example
91
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
92
+ *
93
+ * assertType(object, isNumber);
94
+ *
95
+ * // `object` is now typed as `number`.
96
+ * console.log(object.toFixed(2));
97
+ *
98
+ * @example
99
+ * const object: unknown = 'hello';
100
+ *
101
+ * assertType(object, isString);
102
+ *
103
+ * // `object` is now typed as `string`.
104
+ * console.log(object.toUpperCase());
105
+ */
106
+ function assertType(object, guard) {
107
+ if (!guard(object)) {
108
+ throw new TypeError('object has an invalid type');
109
+ }
110
+ }
111
+ /**
112
+ * Asserts that an object is neither `null` nor `undefined` and returns it.
113
+ *
114
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
115
+ * Otherwise, the object is returned with its type narrowed to `T`.
116
+ *
117
+ * @template T The type of the object.
118
+ *
119
+ * @param object The object to validate.
120
+ *
121
+ * @returns The validated object, narrowed to `T`.
122
+ *
123
+ * @throws {TypeError} If the object is `null` or `undefined`.
124
+ *
125
+ * @example
126
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
127
+ *
128
+ * const string = asserted(object);
129
+ *
130
+ * // `object` is now typed as `string`.
131
+ * console.log(string.length);
132
+ */
51
133
  function asserted(object) {
52
134
  if (object === null || object === undefined) {
53
- throw new Error('Value is either null or undefined.');
135
+ throw new TypeError('object is either null or undefined');
54
136
  }
55
137
  return object;
56
138
  }
57
- function stringOrThrow(value) {
58
- if (!isString(value)) {
59
- throw new Error('Expected a string.');
139
+ /**
140
+ * Asserts that an object satisfies the given type guard and returns it
141
+ * with the narrowed type.
142
+ *
143
+ * If the type guard returns `false`, a `TypeError` is thrown.
144
+ *
145
+ * @template T The type checked by the type guard.
146
+ *
147
+ * @param object The object to validate.
148
+ * @param guard A type guard used to validate the object.
149
+ *
150
+ * @returns The validated object, narrowed to `T`.
151
+ *
152
+ * @throws {TypeError} If the object does not satisfy the type guard.
153
+ *
154
+ * @example
155
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
156
+ *
157
+ * const number = assertedType(object, isNumber);
158
+ *
159
+ * // `number` is typed as `number`.
160
+ * console.log(number.toFixed(2));
161
+ *
162
+ * @example
163
+ * const object: unknown = 'hello';
164
+ *
165
+ * const text = assertedType(object, isString);
166
+ *
167
+ * // `text` is typed as `string`.
168
+ * console.log(text.toUpperCase());
169
+ */
170
+ function assertedType(object, guard) {
171
+ if (!guard(object)) {
172
+ throw new TypeError('object has an invalid type');
60
173
  }
174
+ return object;
61
175
  }
62
176
  function isTruthy(object) {
63
177
  return Boolean(object) === true;
@@ -585,7 +699,10 @@ exports.$ = $;
585
699
  exports.$$ = $$;
586
700
  exports.addStyle = addStyle;
587
701
  exports.alert = alert;
702
+ exports.assert = assert;
703
+ exports.assertType = assertType;
588
704
  exports.asserted = asserted;
705
+ exports.assertedType = assertedType;
589
706
  exports.capitalize = capitalize;
590
707
  exports.checkVPN = checkVPN;
591
708
  exports.checkVPNRandomly = checkVPNRandomly;
@@ -621,6 +738,5 @@ exports.saveFile = saveFile;
621
738
  exports.scriptName = scriptName;
622
739
  exports.setSearchParam = setSearchParam;
623
740
  exports.sleep = sleep;
624
- exports.stringOrThrow = stringOrThrow;
625
741
  exports.tabURL = tabURL;
626
742
  exports.toString = toString;
@@ -16,8 +16,113 @@ declare function remove<T>(array: T[], object: T): void;
16
16
  declare function noop(): void;
17
17
  declare function toString(object: unknown): string;
18
18
 
19
+ /**
20
+ * Asserts that an object is neither `null` nor `undefined`.
21
+ *
22
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
23
+ * Otherwise, TypeScript narrows the object to `T`.
24
+ *
25
+ * @template T The type of the object.
26
+ *
27
+ * @param object The object to validate.
28
+ *
29
+ * @throws {TypeError} If the object is `null` or `undefined`.
30
+ *
31
+ * @example
32
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
33
+ *
34
+ * assert(object);
35
+ *
36
+ * // `object` is now typed as `string`.
37
+ * console.log(object.length);
38
+ */
39
+ declare function assert<T>(object: T | null | undefined): asserts object is T;
40
+ /**
41
+ * Asserts that an object satisfies the given type guard.
42
+ *
43
+ * If the type guard returns `false`, a `TypeError` is thrown.
44
+ * Otherwise, TypeScript narrows the object to the type accepted by
45
+ * the type guard.
46
+ *
47
+ * @template T The type checked by the type guard.
48
+ *
49
+ * @param object The object to validate.
50
+ * @param guard A type guard used to validate the object.
51
+ *
52
+ * @throws {TypeError} If the object does not satisfy the type guard.
53
+ *
54
+ * @example
55
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
56
+ *
57
+ * assertType(object, isNumber);
58
+ *
59
+ * // `object` is now typed as `number`.
60
+ * console.log(object.toFixed(2));
61
+ *
62
+ * @example
63
+ * const object: unknown = 'hello';
64
+ *
65
+ * assertType(object, isString);
66
+ *
67
+ * // `object` is now typed as `string`.
68
+ * console.log(object.toUpperCase());
69
+ */
70
+ declare function assertType<T>(object: unknown, guard: (object: unknown) => object is T): asserts object is T;
71
+ /**
72
+ * Asserts that an object is neither `null` nor `undefined` and returns it.
73
+ *
74
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
75
+ * Otherwise, the object is returned with its type narrowed to `T`.
76
+ *
77
+ * @template T The type of the object.
78
+ *
79
+ * @param object The object to validate.
80
+ *
81
+ * @returns The validated object, narrowed to `T`.
82
+ *
83
+ * @throws {TypeError} If the object is `null` or `undefined`.
84
+ *
85
+ * @example
86
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
87
+ *
88
+ * const string = asserted(object);
89
+ *
90
+ * // `object` is now typed as `string`.
91
+ * console.log(string.length);
92
+ */
19
93
  declare function asserted<T>(object: T | null | undefined): T;
20
- declare function stringOrThrow(value: unknown): asserts value is string;
94
+ /**
95
+ * Asserts that an object satisfies the given type guard and returns it
96
+ * with the narrowed type.
97
+ *
98
+ * If the type guard returns `false`, a `TypeError` is thrown.
99
+ *
100
+ * @template T The type checked by the type guard.
101
+ *
102
+ * @param object The object to validate.
103
+ * @param guard A type guard used to validate the object.
104
+ *
105
+ * @returns The validated object, narrowed to `T`.
106
+ *
107
+ * @throws {TypeError} If the object does not satisfy the type guard.
108
+ *
109
+ * @example
110
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
111
+ *
112
+ * const number = assertedType(object, isNumber);
113
+ *
114
+ * // `number` is typed as `number`.
115
+ * console.log(number.toFixed(2));
116
+ *
117
+ * @example
118
+ * const object: unknown = 'hello';
119
+ *
120
+ * const text = assertedType(object, isString);
121
+ *
122
+ * // `text` is typed as `string`.
123
+ * console.log(text.toUpperCase());
124
+ */
125
+ declare function assertedType<T>(object: unknown, guard: (object: unknown) => object is T): T;
21
126
  declare function isTruthy<T>(object: T): object is Exclude<T, '' | 0 | false | null | undefined>;
22
127
  declare function isFalsy(object: unknown): object is '' | 0 | false | null | undefined;
23
128
  declare function isNullish(object: unknown): object is null | undefined;
@@ -203,4 +308,4 @@ declare function rx(url: string, options?: RxOptions): RqRequest;
203
308
 
204
309
  declare function createRequest<TOptions>(transport: Transport<TOptions>, url: string, options?: TOptions): RqRequest;
205
310
 
206
- export { $, $$, Flag, Fn, Nullable, Prettify, addStyle, alert, asserted, capitalize, checkVPN, checkVPNRandomly, confirm, customizeConsole, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, rq, createRequest as rqc, rx, saveFile, scriptName, setSearchParam, sleep, stringOrThrow, tabURL, toString };
311
+ export { $, $$, Flag, Fn, Nullable, Prettify, addStyle, alert, assert, assertType, asserted, assertedType, capitalize, checkVPN, checkVPNRandomly, confirm, customizeConsole, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, rq, createRequest as rqc, rx, saveFile, scriptName, setSearchParam, sleep, tabURL, toString };
package/browser/index.js CHANGED
@@ -46,16 +46,130 @@ function remove(array, object) {
46
46
  array.splice(index, 1);
47
47
  }
48
48
 
49
+ /**
50
+ * Asserts that an object is neither `null` nor `undefined`.
51
+ *
52
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
53
+ * Otherwise, TypeScript narrows the object to `T`.
54
+ *
55
+ * @template T The type of the object.
56
+ *
57
+ * @param object The object to validate.
58
+ *
59
+ * @throws {TypeError} If the object is `null` or `undefined`.
60
+ *
61
+ * @example
62
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
63
+ *
64
+ * assert(object);
65
+ *
66
+ * // `object` is now typed as `string`.
67
+ * console.log(object.length);
68
+ */
69
+ function assert(object) {
70
+ if (object === null || object === undefined) {
71
+ throw new TypeError('object is either null or undefined');
72
+ }
73
+ }
74
+ /**
75
+ * Asserts that an object satisfies the given type guard.
76
+ *
77
+ * If the type guard returns `false`, a `TypeError` is thrown.
78
+ * Otherwise, TypeScript narrows the object to the type accepted by
79
+ * the type guard.
80
+ *
81
+ * @template T The type checked by the type guard.
82
+ *
83
+ * @param object The object to validate.
84
+ * @param guard A type guard used to validate the object.
85
+ *
86
+ * @throws {TypeError} If the object does not satisfy the type guard.
87
+ *
88
+ * @example
89
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
90
+ *
91
+ * assertType(object, isNumber);
92
+ *
93
+ * // `object` is now typed as `number`.
94
+ * console.log(object.toFixed(2));
95
+ *
96
+ * @example
97
+ * const object: unknown = 'hello';
98
+ *
99
+ * assertType(object, isString);
100
+ *
101
+ * // `object` is now typed as `string`.
102
+ * console.log(object.toUpperCase());
103
+ */
104
+ function assertType(object, guard) {
105
+ if (!guard(object)) {
106
+ throw new TypeError('object has an invalid type');
107
+ }
108
+ }
109
+ /**
110
+ * Asserts that an object is neither `null` nor `undefined` and returns it.
111
+ *
112
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
113
+ * Otherwise, the object is returned with its type narrowed to `T`.
114
+ *
115
+ * @template T The type of the object.
116
+ *
117
+ * @param object The object to validate.
118
+ *
119
+ * @returns The validated object, narrowed to `T`.
120
+ *
121
+ * @throws {TypeError} If the object is `null` or `undefined`.
122
+ *
123
+ * @example
124
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
125
+ *
126
+ * const string = asserted(object);
127
+ *
128
+ * // `object` is now typed as `string`.
129
+ * console.log(string.length);
130
+ */
49
131
  function asserted(object) {
50
132
  if (object === null || object === undefined) {
51
- throw new Error('Value is either null or undefined.');
133
+ throw new TypeError('object is either null or undefined');
52
134
  }
53
135
  return object;
54
136
  }
55
- function stringOrThrow(value) {
56
- if (!isString(value)) {
57
- throw new Error('Expected a string.');
137
+ /**
138
+ * Asserts that an object satisfies the given type guard and returns it
139
+ * with the narrowed type.
140
+ *
141
+ * If the type guard returns `false`, a `TypeError` is thrown.
142
+ *
143
+ * @template T The type checked by the type guard.
144
+ *
145
+ * @param object The object to validate.
146
+ * @param guard A type guard used to validate the object.
147
+ *
148
+ * @returns The validated object, narrowed to `T`.
149
+ *
150
+ * @throws {TypeError} If the object does not satisfy the type guard.
151
+ *
152
+ * @example
153
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
154
+ *
155
+ * const number = assertedType(object, isNumber);
156
+ *
157
+ * // `number` is typed as `number`.
158
+ * console.log(number.toFixed(2));
159
+ *
160
+ * @example
161
+ * const object: unknown = 'hello';
162
+ *
163
+ * const text = assertedType(object, isString);
164
+ *
165
+ * // `text` is typed as `string`.
166
+ * console.log(text.toUpperCase());
167
+ */
168
+ function assertedType(object, guard) {
169
+ if (!guard(object)) {
170
+ throw new TypeError('object has an invalid type');
58
171
  }
172
+ return object;
59
173
  }
60
174
  function isTruthy(object) {
61
175
  return Boolean(object) === true;
@@ -579,4 +693,4 @@ function rx(url, options) {
579
693
  return createRequest(gmTransport, url, options);
580
694
  }
581
695
 
582
- export { $, $$, addStyle, alert, asserted, capitalize, checkVPN, checkVPNRandomly, confirm, customizeConsole, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, rq, createRequest as rqc, rx, saveFile, scriptName, setSearchParam, sleep, stringOrThrow, tabURL, toString };
696
+ export { $, $$, addStyle, alert, assert, assertType, asserted, assertedType, capitalize, checkVPN, checkVPNRandomly, confirm, customizeConsole, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, rq, createRequest as rqc, rx, saveFile, scriptName, setSearchParam, sleep, tabURL, toString };
package/node/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var fs = require('node:fs');
4
3
  var fsp = require('node:fs/promises');
4
+ var fs = require('node:fs');
5
5
  var path = require('node:path');
6
6
 
7
7
  function noop() { }
@@ -52,16 +52,130 @@ function remove(array, object) {
52
52
  array.splice(index, 1);
53
53
  }
54
54
 
55
+ /**
56
+ * Asserts that an object is neither `null` nor `undefined`.
57
+ *
58
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
59
+ * Otherwise, TypeScript narrows the object to `T`.
60
+ *
61
+ * @template T The type of the object.
62
+ *
63
+ * @param object The object to validate.
64
+ *
65
+ * @throws {TypeError} If the object is `null` or `undefined`.
66
+ *
67
+ * @example
68
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
69
+ *
70
+ * assert(object);
71
+ *
72
+ * // `object` is now typed as `string`.
73
+ * console.log(object.length);
74
+ */
75
+ function assert(object) {
76
+ if (object === null || object === undefined) {
77
+ throw new TypeError('object is either null or undefined');
78
+ }
79
+ }
80
+ /**
81
+ * Asserts that an object satisfies the given type guard.
82
+ *
83
+ * If the type guard returns `false`, a `TypeError` is thrown.
84
+ * Otherwise, TypeScript narrows the object to the type accepted by
85
+ * the type guard.
86
+ *
87
+ * @template T The type checked by the type guard.
88
+ *
89
+ * @param object The object to validate.
90
+ * @param guard A type guard used to validate the object.
91
+ *
92
+ * @throws {TypeError} If the object does not satisfy the type guard.
93
+ *
94
+ * @example
95
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
96
+ *
97
+ * assertType(object, isNumber);
98
+ *
99
+ * // `object` is now typed as `number`.
100
+ * console.log(object.toFixed(2));
101
+ *
102
+ * @example
103
+ * const object: unknown = 'hello';
104
+ *
105
+ * assertType(object, isString);
106
+ *
107
+ * // `object` is now typed as `string`.
108
+ * console.log(object.toUpperCase());
109
+ */
110
+ function assertType(object, guard) {
111
+ if (!guard(object)) {
112
+ throw new TypeError('object has an invalid type');
113
+ }
114
+ }
115
+ /**
116
+ * Asserts that an object is neither `null` nor `undefined` and returns it.
117
+ *
118
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
119
+ * Otherwise, the object is returned with its type narrowed to `T`.
120
+ *
121
+ * @template T The type of the object.
122
+ *
123
+ * @param object The object to validate.
124
+ *
125
+ * @returns The validated object, narrowed to `T`.
126
+ *
127
+ * @throws {TypeError} If the object is `null` or `undefined`.
128
+ *
129
+ * @example
130
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
131
+ *
132
+ * const string = asserted(object);
133
+ *
134
+ * // `object` is now typed as `string`.
135
+ * console.log(string.length);
136
+ */
55
137
  function asserted(object) {
56
138
  if (object === null || object === undefined) {
57
- throw new Error('Value is either null or undefined.');
139
+ throw new TypeError('object is either null or undefined');
58
140
  }
59
141
  return object;
60
142
  }
61
- function stringOrThrow(value) {
62
- if (!isString(value)) {
63
- throw new Error('Expected a string.');
143
+ /**
144
+ * Asserts that an object satisfies the given type guard and returns it
145
+ * with the narrowed type.
146
+ *
147
+ * If the type guard returns `false`, a `TypeError` is thrown.
148
+ *
149
+ * @template T The type checked by the type guard.
150
+ *
151
+ * @param object The object to validate.
152
+ * @param guard A type guard used to validate the object.
153
+ *
154
+ * @returns The validated object, narrowed to `T`.
155
+ *
156
+ * @throws {TypeError} If the object does not satisfy the type guard.
157
+ *
158
+ * @example
159
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
160
+ *
161
+ * const number = assertedType(object, isNumber);
162
+ *
163
+ * // `number` is typed as `number`.
164
+ * console.log(number.toFixed(2));
165
+ *
166
+ * @example
167
+ * const object: unknown = 'hello';
168
+ *
169
+ * const text = assertedType(object, isString);
170
+ *
171
+ * // `text` is typed as `string`.
172
+ * console.log(text.toUpperCase());
173
+ */
174
+ function assertedType(object, guard) {
175
+ if (!guard(object)) {
176
+ throw new TypeError('object has an invalid type');
64
177
  }
178
+ return object;
65
179
  }
66
180
  function isTruthy(object) {
67
181
  return Boolean(object) === true;
@@ -134,6 +248,64 @@ function capitalize(sentence) {
134
248
  return capitalizeWord(sentence);
135
249
  }
136
250
 
251
+ async function exists(path) {
252
+ try {
253
+ await fsp.access(path);
254
+ return true;
255
+ }
256
+ catch {
257
+ return false;
258
+ }
259
+ }
260
+
261
+ async function existsFile(path) {
262
+ try {
263
+ const stats = await fsp.stat(path);
264
+ return stats.isFile();
265
+ }
266
+ catch {
267
+ return false;
268
+ }
269
+ }
270
+
271
+ function existsFileSync(path) {
272
+ try {
273
+ return fs.statSync(path).isFile();
274
+ }
275
+ catch {
276
+ return false;
277
+ }
278
+ }
279
+
280
+ async function existsFolder(path) {
281
+ try {
282
+ const stats = await fsp.stat(path);
283
+ return stats.isDirectory();
284
+ }
285
+ catch {
286
+ return false;
287
+ }
288
+ }
289
+
290
+ function existsFolderSync(path) {
291
+ try {
292
+ return fs.statSync(path).isDirectory();
293
+ }
294
+ catch {
295
+ return false;
296
+ }
297
+ }
298
+
299
+ function existsSync(path) {
300
+ try {
301
+ fs.accessSync(path);
302
+ return true;
303
+ }
304
+ catch {
305
+ return false;
306
+ }
307
+ }
308
+
137
309
  /**
138
310
  * Lists the files that are present in the provided folder path.
139
311
  * @throws an error if the provided folder path does not exist.
@@ -315,8 +487,17 @@ function rq(url, options) {
315
487
  return createRequest(fetchTransport, url, options);
316
488
  }
317
489
 
490
+ exports.assert = assert;
491
+ exports.assertType = assertType;
318
492
  exports.asserted = asserted;
493
+ exports.assertedType = assertedType;
319
494
  exports.capitalize = capitalize;
495
+ exports.exists = exists;
496
+ exports.existsFile = existsFile;
497
+ exports.existsFileSync = existsFileSync;
498
+ exports.existsFolder = existsFolder;
499
+ exports.existsFolderSync = existsFolderSync;
500
+ exports.existsSync = existsSync;
320
501
  exports.isBoolean = isBoolean;
321
502
  exports.isFalsy = isFalsy;
322
503
  exports.isFunction = isFunction;
@@ -338,5 +519,4 @@ exports.removeFiles = removeFiles;
338
519
  exports.rq = rq;
339
520
  exports.rqc = createRequest;
340
521
  exports.sleep = sleep;
341
- exports.stringOrThrow = stringOrThrow;
342
522
  exports.toString = toString;
package/node/index.d.ts CHANGED
@@ -16,8 +16,113 @@ declare function remove<T>(array: T[], object: T): void;
16
16
  declare function noop(): void;
17
17
  declare function toString(object: unknown): string;
18
18
 
19
+ /**
20
+ * Asserts that an object is neither `null` nor `undefined`.
21
+ *
22
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
23
+ * Otherwise, TypeScript narrows the object to `T`.
24
+ *
25
+ * @template T The type of the object.
26
+ *
27
+ * @param object The object to validate.
28
+ *
29
+ * @throws {TypeError} If the object is `null` or `undefined`.
30
+ *
31
+ * @example
32
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
33
+ *
34
+ * assert(object);
35
+ *
36
+ * // `object` is now typed as `string`.
37
+ * console.log(object.length);
38
+ */
39
+ declare function assert<T>(object: T | null | undefined): asserts object is T;
40
+ /**
41
+ * Asserts that an object satisfies the given type guard.
42
+ *
43
+ * If the type guard returns `false`, a `TypeError` is thrown.
44
+ * Otherwise, TypeScript narrows the object to the type accepted by
45
+ * the type guard.
46
+ *
47
+ * @template T The type checked by the type guard.
48
+ *
49
+ * @param object The object to validate.
50
+ * @param guard A type guard used to validate the object.
51
+ *
52
+ * @throws {TypeError} If the object does not satisfy the type guard.
53
+ *
54
+ * @example
55
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
56
+ *
57
+ * assertType(object, isNumber);
58
+ *
59
+ * // `object` is now typed as `number`.
60
+ * console.log(object.toFixed(2));
61
+ *
62
+ * @example
63
+ * const object: unknown = 'hello';
64
+ *
65
+ * assertType(object, isString);
66
+ *
67
+ * // `object` is now typed as `string`.
68
+ * console.log(object.toUpperCase());
69
+ */
70
+ declare function assertType<T>(object: unknown, guard: (object: unknown) => object is T): asserts object is T;
71
+ /**
72
+ * Asserts that an object is neither `null` nor `undefined` and returns it.
73
+ *
74
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
75
+ * Otherwise, the object is returned with its type narrowed to `T`.
76
+ *
77
+ * @template T The type of the object.
78
+ *
79
+ * @param object The object to validate.
80
+ *
81
+ * @returns The validated object, narrowed to `T`.
82
+ *
83
+ * @throws {TypeError} If the object is `null` or `undefined`.
84
+ *
85
+ * @example
86
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
87
+ *
88
+ * const string = asserted(object);
89
+ *
90
+ * // `object` is now typed as `string`.
91
+ * console.log(string.length);
92
+ */
19
93
  declare function asserted<T>(object: T | null | undefined): T;
20
- declare function stringOrThrow(value: unknown): asserts value is string;
94
+ /**
95
+ * Asserts that an object satisfies the given type guard and returns it
96
+ * with the narrowed type.
97
+ *
98
+ * If the type guard returns `false`, a `TypeError` is thrown.
99
+ *
100
+ * @template T The type checked by the type guard.
101
+ *
102
+ * @param object The object to validate.
103
+ * @param guard A type guard used to validate the object.
104
+ *
105
+ * @returns The validated object, narrowed to `T`.
106
+ *
107
+ * @throws {TypeError} If the object does not satisfy the type guard.
108
+ *
109
+ * @example
110
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
111
+ *
112
+ * const number = assertedType(object, isNumber);
113
+ *
114
+ * // `number` is typed as `number`.
115
+ * console.log(number.toFixed(2));
116
+ *
117
+ * @example
118
+ * const object: unknown = 'hello';
119
+ *
120
+ * const text = assertedType(object, isString);
121
+ *
122
+ * // `text` is typed as `string`.
123
+ * console.log(text.toUpperCase());
124
+ */
125
+ declare function assertedType<T>(object: unknown, guard: (object: unknown) => object is T): T;
21
126
  declare function isTruthy<T>(object: T): object is Exclude<T, '' | 0 | false | null | undefined>;
22
127
  declare function isFalsy(object: unknown): object is '' | 0 | false | null | undefined;
23
128
  declare function isNullish(object: unknown): object is null | undefined;
@@ -56,6 +161,18 @@ type Prettify<T> = {
56
161
  [KeyType in keyof T]: T[KeyType];
57
162
  } & {};
58
163
 
164
+ declare function exists(path: string): Promise<boolean>;
165
+
166
+ declare function existsFile(path: string): Promise<boolean>;
167
+
168
+ declare function existsFileSync(path: string): boolean;
169
+
170
+ declare function existsFolder(path: string): Promise<boolean>;
171
+
172
+ declare function existsFolderSync(path: string): boolean;
173
+
174
+ declare function existsSync(path: string): boolean;
175
+
59
176
  interface ListBaseOpt {
60
177
  /** The absolute path of the target folder. */
61
178
  folderPath: string;
@@ -134,4 +251,4 @@ declare function rq(url: string, options?: RequestInit): RqRequest;
134
251
 
135
252
  declare function createRequest<TOptions>(transport: Transport<TOptions>, url: string, options?: TOptions): RqRequest;
136
253
 
137
- export { Fn, Nullable, Prettify, asserted, capitalize, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, rq, createRequest as rqc, sleep, stringOrThrow, toString };
254
+ export { Fn, Nullable, Prettify, assert, assertType, asserted, assertedType, capitalize, exists, existsFile, existsFileSync, existsFolder, existsFolderSync, existsSync, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, rq, createRequest as rqc, sleep, toString };
package/node/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import fs from 'node:fs';
2
- import fsp from 'node:fs/promises';
1
+ import fsp, { access, stat } from 'node:fs/promises';
2
+ import fs, { statSync, accessSync } from 'node:fs';
3
3
  import path from 'node:path';
4
4
 
5
5
  function noop() { }
@@ -50,16 +50,130 @@ function remove(array, object) {
50
50
  array.splice(index, 1);
51
51
  }
52
52
 
53
+ /**
54
+ * Asserts that an object is neither `null` nor `undefined`.
55
+ *
56
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
57
+ * Otherwise, TypeScript narrows the object to `T`.
58
+ *
59
+ * @template T The type of the object.
60
+ *
61
+ * @param object The object to validate.
62
+ *
63
+ * @throws {TypeError} If the object is `null` or `undefined`.
64
+ *
65
+ * @example
66
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
67
+ *
68
+ * assert(object);
69
+ *
70
+ * // `object` is now typed as `string`.
71
+ * console.log(object.length);
72
+ */
73
+ function assert(object) {
74
+ if (object === null || object === undefined) {
75
+ throw new TypeError('object is either null or undefined');
76
+ }
77
+ }
78
+ /**
79
+ * Asserts that an object satisfies the given type guard.
80
+ *
81
+ * If the type guard returns `false`, a `TypeError` is thrown.
82
+ * Otherwise, TypeScript narrows the object to the type accepted by
83
+ * the type guard.
84
+ *
85
+ * @template T The type checked by the type guard.
86
+ *
87
+ * @param object The object to validate.
88
+ * @param guard A type guard used to validate the object.
89
+ *
90
+ * @throws {TypeError} If the object does not satisfy the type guard.
91
+ *
92
+ * @example
93
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
94
+ *
95
+ * assertType(object, isNumber);
96
+ *
97
+ * // `object` is now typed as `number`.
98
+ * console.log(object.toFixed(2));
99
+ *
100
+ * @example
101
+ * const object: unknown = 'hello';
102
+ *
103
+ * assertType(object, isString);
104
+ *
105
+ * // `object` is now typed as `string`.
106
+ * console.log(object.toUpperCase());
107
+ */
108
+ function assertType(object, guard) {
109
+ if (!guard(object)) {
110
+ throw new TypeError('object has an invalid type');
111
+ }
112
+ }
113
+ /**
114
+ * Asserts that an object is neither `null` nor `undefined` and returns it.
115
+ *
116
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
117
+ * Otherwise, the object is returned with its type narrowed to `T`.
118
+ *
119
+ * @template T The type of the object.
120
+ *
121
+ * @param object The object to validate.
122
+ *
123
+ * @returns The validated object, narrowed to `T`.
124
+ *
125
+ * @throws {TypeError} If the object is `null` or `undefined`.
126
+ *
127
+ * @example
128
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
129
+ *
130
+ * const string = asserted(object);
131
+ *
132
+ * // `object` is now typed as `string`.
133
+ * console.log(string.length);
134
+ */
53
135
  function asserted(object) {
54
136
  if (object === null || object === undefined) {
55
- throw new Error('Value is either null or undefined.');
137
+ throw new TypeError('object is either null or undefined');
56
138
  }
57
139
  return object;
58
140
  }
59
- function stringOrThrow(value) {
60
- if (!isString(value)) {
61
- throw new Error('Expected a string.');
141
+ /**
142
+ * Asserts that an object satisfies the given type guard and returns it
143
+ * with the narrowed type.
144
+ *
145
+ * If the type guard returns `false`, a `TypeError` is thrown.
146
+ *
147
+ * @template T The type checked by the type guard.
148
+ *
149
+ * @param object The object to validate.
150
+ * @param guard A type guard used to validate the object.
151
+ *
152
+ * @returns The validated object, narrowed to `T`.
153
+ *
154
+ * @throws {TypeError} If the object does not satisfy the type guard.
155
+ *
156
+ * @example
157
+ * const object: number | string = Math.random() < 0.5 ? 1 : 'Hello';
158
+ *
159
+ * const number = assertedType(object, isNumber);
160
+ *
161
+ * // `number` is typed as `number`.
162
+ * console.log(number.toFixed(2));
163
+ *
164
+ * @example
165
+ * const object: unknown = 'hello';
166
+ *
167
+ * const text = assertedType(object, isString);
168
+ *
169
+ * // `text` is typed as `string`.
170
+ * console.log(text.toUpperCase());
171
+ */
172
+ function assertedType(object, guard) {
173
+ if (!guard(object)) {
174
+ throw new TypeError('object has an invalid type');
62
175
  }
176
+ return object;
63
177
  }
64
178
  function isTruthy(object) {
65
179
  return Boolean(object) === true;
@@ -132,6 +246,64 @@ function capitalize(sentence) {
132
246
  return capitalizeWord(sentence);
133
247
  }
134
248
 
249
+ async function exists(path) {
250
+ try {
251
+ await access(path);
252
+ return true;
253
+ }
254
+ catch {
255
+ return false;
256
+ }
257
+ }
258
+
259
+ async function existsFile(path) {
260
+ try {
261
+ const stats = await stat(path);
262
+ return stats.isFile();
263
+ }
264
+ catch {
265
+ return false;
266
+ }
267
+ }
268
+
269
+ function existsFileSync(path) {
270
+ try {
271
+ return statSync(path).isFile();
272
+ }
273
+ catch {
274
+ return false;
275
+ }
276
+ }
277
+
278
+ async function existsFolder(path) {
279
+ try {
280
+ const stats = await stat(path);
281
+ return stats.isDirectory();
282
+ }
283
+ catch {
284
+ return false;
285
+ }
286
+ }
287
+
288
+ function existsFolderSync(path) {
289
+ try {
290
+ return statSync(path).isDirectory();
291
+ }
292
+ catch {
293
+ return false;
294
+ }
295
+ }
296
+
297
+ function existsSync(path) {
298
+ try {
299
+ accessSync(path);
300
+ return true;
301
+ }
302
+ catch {
303
+ return false;
304
+ }
305
+ }
306
+
135
307
  /**
136
308
  * Lists the files that are present in the provided folder path.
137
309
  * @throws an error if the provided folder path does not exist.
@@ -313,4 +485,4 @@ function rq(url, options) {
313
485
  return createRequest(fetchTransport, url, options);
314
486
  }
315
487
 
316
- export { asserted, capitalize, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, rq, createRequest as rqc, sleep, stringOrThrow, toString };
488
+ export { assert, assertType, asserted, assertedType, capitalize, exists, existsFile, existsFileSync, existsFolder, existsFolderSync, existsSync, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, rq, createRequest as rqc, sleep, toString };
@@ -42,6 +42,26 @@ async function confirm(message, defaultAnswer = false) {
42
42
  return confirmValue;
43
43
  }
44
44
 
45
+ /**
46
+ * Asserts that an object is neither `null` nor `undefined`.
47
+ *
48
+ * If the object is `null` or `undefined`, a `TypeError` is thrown.
49
+ * Otherwise, TypeScript narrows the object to `T`.
50
+ *
51
+ * @template T The type of the object.
52
+ *
53
+ * @param object The object to validate.
54
+ *
55
+ * @throws {TypeError} If the object is `null` or `undefined`.
56
+ *
57
+ * @example
58
+ * const object: string | undefined = Math.random() < 0.5 ? undefined : 'Hello';
59
+ *
60
+ * assert(object);
61
+ *
62
+ * // `object` is now typed as `string`.
63
+ * console.log(object.length);
64
+ */
45
65
  function isFalsy(object) {
46
66
  return Boolean(object) === false;
47
67
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jeniex/utils",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "author": "JenieX",
5
5
  "homepage": "https://github.com/JenieX/utils",
6
6
  "type": "module",