@alextheman/utility 2.0.0 → 2.1.1
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/dist/index.cjs +40 -2
- package/dist/index.d.cts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +38 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -60,9 +60,11 @@ __export(index_exports, {
|
|
|
60
60
|
omitProperties: () => omitProperties_default,
|
|
61
61
|
parseEmail: () => Email_default,
|
|
62
62
|
parseEnv: () => Env_default,
|
|
63
|
+
parseIntStrict: () => parseIntStrict_default,
|
|
63
64
|
parseUUID: () => UUID_default,
|
|
64
65
|
randomiseArray: () => randomiseArray_default,
|
|
65
66
|
range: () => range_default,
|
|
67
|
+
stringToBoolean: () => stringToBoolean_default,
|
|
66
68
|
truncate: () => truncate_default,
|
|
67
69
|
wait: () => wait_default
|
|
68
70
|
});
|
|
@@ -150,7 +152,7 @@ var formatDateAndTime_default = formatDateAndTime;
|
|
|
150
152
|
// src/functions/getRandomNumber.ts
|
|
151
153
|
function getRandomNumber(lowerBound, upperBound) {
|
|
152
154
|
if (lowerBound % 1 !== 0 || upperBound % 1 !== 0) {
|
|
153
|
-
throw new
|
|
155
|
+
throw new TypeError("NON_INTEGER_INPUTS");
|
|
154
156
|
}
|
|
155
157
|
return Math.floor(Math.random() * (upperBound - lowerBound + 1) + lowerBound);
|
|
156
158
|
}
|
|
@@ -159,7 +161,7 @@ var getRandomNumber_default = getRandomNumber;
|
|
|
159
161
|
// src/functions/isLeapYear.ts
|
|
160
162
|
function isLeapYear(year) {
|
|
161
163
|
if (year % 1 !== 0) {
|
|
162
|
-
throw new
|
|
164
|
+
throw new TypeError("NON_INTEGER_INPUT");
|
|
163
165
|
}
|
|
164
166
|
return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
|
|
165
167
|
}
|
|
@@ -213,6 +215,30 @@ function omitProperties(object, keysToOmit) {
|
|
|
213
215
|
}
|
|
214
216
|
var omitProperties_default = omitProperties;
|
|
215
217
|
|
|
218
|
+
// src/functions/parseIntStrict.ts
|
|
219
|
+
var IntegerParsingError = new TypeError("INTEGER_PARSING_ERROR");
|
|
220
|
+
function parseIntStrict(string, radix) {
|
|
221
|
+
const trimmedString = string.trim();
|
|
222
|
+
const pattern = radix && radix > 10 && radix <= 36 ? (
|
|
223
|
+
// String.fromCharCode() gets the maximum possible alphabetical character for a base above 10
|
|
224
|
+
new RegExp(`^[+-]?[0-9a-${String.fromCharCode(87 + radix - 1)}]+$`, "i")
|
|
225
|
+
) : /^[+-]?\d+$/;
|
|
226
|
+
if (!pattern.test(trimmedString)) {
|
|
227
|
+
throw IntegerParsingError;
|
|
228
|
+
}
|
|
229
|
+
if (radix && radix < 10 && [...trimmedString].some((character) => {
|
|
230
|
+
return parseInt(character) >= radix;
|
|
231
|
+
})) {
|
|
232
|
+
throw IntegerParsingError;
|
|
233
|
+
}
|
|
234
|
+
const parseIntResult = parseInt(trimmedString, radix);
|
|
235
|
+
if (isNaN(parseIntResult)) {
|
|
236
|
+
throw IntegerParsingError;
|
|
237
|
+
}
|
|
238
|
+
return parseIntResult;
|
|
239
|
+
}
|
|
240
|
+
var parseIntStrict_default = parseIntStrict;
|
|
241
|
+
|
|
216
242
|
// src/functions/randomiseArray.ts
|
|
217
243
|
function randomiseArray(array) {
|
|
218
244
|
const mutableArray = [...array];
|
|
@@ -249,6 +275,16 @@ function range(start, stop, step = 1) {
|
|
|
249
275
|
}
|
|
250
276
|
var range_default = range;
|
|
251
277
|
|
|
278
|
+
// src/functions/stringToBoolean.ts
|
|
279
|
+
function stringToBoolean(inputString) {
|
|
280
|
+
const normalisedString = inputString.toLowerCase();
|
|
281
|
+
if (normalisedString !== "true" && normalisedString !== "false") {
|
|
282
|
+
throw new TypeError("INVALID_BOOLEAN_STRING");
|
|
283
|
+
}
|
|
284
|
+
return normalisedString === "true";
|
|
285
|
+
}
|
|
286
|
+
var stringToBoolean_default = stringToBoolean;
|
|
287
|
+
|
|
252
288
|
// src/functions/truncate.ts
|
|
253
289
|
function truncate(stringToTruncate, maxLength = 5) {
|
|
254
290
|
return stringToTruncate.length > maxLength ? `${stringToTruncate.slice(0, maxLength)}...` : stringToTruncate;
|
|
@@ -350,9 +386,11 @@ var UUID_default = parseUUID;
|
|
|
350
386
|
omitProperties,
|
|
351
387
|
parseEmail,
|
|
352
388
|
parseEnv,
|
|
389
|
+
parseIntStrict,
|
|
353
390
|
parseUUID,
|
|
354
391
|
randomiseArray,
|
|
355
392
|
range,
|
|
393
|
+
stringToBoolean,
|
|
356
394
|
truncate,
|
|
357
395
|
wait
|
|
358
396
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -20,10 +20,14 @@ declare function isSameDate(firstDate: Date, secondDate: Date): boolean;
|
|
|
20
20
|
|
|
21
21
|
declare function omitProperties<T extends Record<string, unknown>, K extends keyof T>(object: T, keysToOmit: K | readonly K[]): Omit<T, K>;
|
|
22
22
|
|
|
23
|
+
declare function parseIntStrict(string: string, radix?: number): number;
|
|
24
|
+
|
|
23
25
|
declare function randomiseArray<T>(array: T[]): T[];
|
|
24
26
|
|
|
25
27
|
declare function range(start: number, stop: number, step?: number): number[];
|
|
26
28
|
|
|
29
|
+
declare function stringToBoolean(inputString: string): boolean;
|
|
30
|
+
|
|
27
31
|
declare function truncate(stringToTruncate: string, maxLength?: number): string;
|
|
28
32
|
|
|
29
33
|
declare function wait(seconds: number): Promise<void>;
|
|
@@ -58,4 +62,4 @@ type DisallowUndefined<T> = undefined extends T ? ["Error: Generic type cannot i
|
|
|
58
62
|
|
|
59
63
|
type NonUndefined<T> = T extends undefined ? never : T;
|
|
60
64
|
|
|
61
|
-
export { APIError, type DisallowUndefined, type Email, type Env, type HTTPErrorCodes, type NonUndefined, type UUID, addDaysToDate, appendSemicolon, convertFileToBase64, fillArray, formatDateAndTime, getRandomNumber, httpErrorCodeLookup, interpolateObjects, isLeapYear, isMonthlyMultiple, isSameDate, omitProperties, parseEmail, parseEnv, parseUUID, randomiseArray, range, truncate, wait };
|
|
65
|
+
export { APIError, type DisallowUndefined, type Email, type Env, type HTTPErrorCodes, type NonUndefined, type UUID, addDaysToDate, appendSemicolon, convertFileToBase64, fillArray, formatDateAndTime, getRandomNumber, httpErrorCodeLookup, interpolateObjects, isLeapYear, isMonthlyMultiple, isSameDate, omitProperties, parseEmail, parseEnv, parseIntStrict, parseUUID, randomiseArray, range, stringToBoolean, truncate, wait };
|
package/dist/index.d.ts
CHANGED
|
@@ -20,10 +20,14 @@ declare function isSameDate(firstDate: Date, secondDate: Date): boolean;
|
|
|
20
20
|
|
|
21
21
|
declare function omitProperties<T extends Record<string, unknown>, K extends keyof T>(object: T, keysToOmit: K | readonly K[]): Omit<T, K>;
|
|
22
22
|
|
|
23
|
+
declare function parseIntStrict(string: string, radix?: number): number;
|
|
24
|
+
|
|
23
25
|
declare function randomiseArray<T>(array: T[]): T[];
|
|
24
26
|
|
|
25
27
|
declare function range(start: number, stop: number, step?: number): number[];
|
|
26
28
|
|
|
29
|
+
declare function stringToBoolean(inputString: string): boolean;
|
|
30
|
+
|
|
27
31
|
declare function truncate(stringToTruncate: string, maxLength?: number): string;
|
|
28
32
|
|
|
29
33
|
declare function wait(seconds: number): Promise<void>;
|
|
@@ -58,4 +62,4 @@ type DisallowUndefined<T> = undefined extends T ? ["Error: Generic type cannot i
|
|
|
58
62
|
|
|
59
63
|
type NonUndefined<T> = T extends undefined ? never : T;
|
|
60
64
|
|
|
61
|
-
export { APIError, type DisallowUndefined, type Email, type Env, type HTTPErrorCodes, type NonUndefined, type UUID, addDaysToDate, appendSemicolon, convertFileToBase64, fillArray, formatDateAndTime, getRandomNumber, httpErrorCodeLookup, interpolateObjects, isLeapYear, isMonthlyMultiple, isSameDate, omitProperties, parseEmail, parseEnv, parseUUID, randomiseArray, range, truncate, wait };
|
|
65
|
+
export { APIError, type DisallowUndefined, type Email, type Env, type HTTPErrorCodes, type NonUndefined, type UUID, addDaysToDate, appendSemicolon, convertFileToBase64, fillArray, formatDateAndTime, getRandomNumber, httpErrorCodeLookup, interpolateObjects, isLeapYear, isMonthlyMultiple, isSameDate, omitProperties, parseEmail, parseEnv, parseIntStrict, parseUUID, randomiseArray, range, stringToBoolean, truncate, wait };
|
package/dist/index.js
CHANGED
|
@@ -98,7 +98,7 @@ var formatDateAndTime_default = formatDateAndTime;
|
|
|
98
98
|
// src/functions/getRandomNumber.ts
|
|
99
99
|
function getRandomNumber(lowerBound, upperBound) {
|
|
100
100
|
if (lowerBound % 1 !== 0 || upperBound % 1 !== 0) {
|
|
101
|
-
throw new
|
|
101
|
+
throw new TypeError("NON_INTEGER_INPUTS");
|
|
102
102
|
}
|
|
103
103
|
return Math.floor(Math.random() * (upperBound - lowerBound + 1) + lowerBound);
|
|
104
104
|
}
|
|
@@ -107,7 +107,7 @@ var getRandomNumber_default = getRandomNumber;
|
|
|
107
107
|
// src/functions/isLeapYear.ts
|
|
108
108
|
function isLeapYear(year) {
|
|
109
109
|
if (year % 1 !== 0) {
|
|
110
|
-
throw new
|
|
110
|
+
throw new TypeError("NON_INTEGER_INPUT");
|
|
111
111
|
}
|
|
112
112
|
return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
|
|
113
113
|
}
|
|
@@ -161,6 +161,30 @@ function omitProperties(object, keysToOmit) {
|
|
|
161
161
|
}
|
|
162
162
|
var omitProperties_default = omitProperties;
|
|
163
163
|
|
|
164
|
+
// src/functions/parseIntStrict.ts
|
|
165
|
+
var IntegerParsingError = new TypeError("INTEGER_PARSING_ERROR");
|
|
166
|
+
function parseIntStrict(string, radix) {
|
|
167
|
+
const trimmedString = string.trim();
|
|
168
|
+
const pattern = radix && radix > 10 && radix <= 36 ? (
|
|
169
|
+
// String.fromCharCode() gets the maximum possible alphabetical character for a base above 10
|
|
170
|
+
new RegExp(`^[+-]?[0-9a-${String.fromCharCode(87 + radix - 1)}]+$`, "i")
|
|
171
|
+
) : /^[+-]?\d+$/;
|
|
172
|
+
if (!pattern.test(trimmedString)) {
|
|
173
|
+
throw IntegerParsingError;
|
|
174
|
+
}
|
|
175
|
+
if (radix && radix < 10 && [...trimmedString].some((character) => {
|
|
176
|
+
return parseInt(character) >= radix;
|
|
177
|
+
})) {
|
|
178
|
+
throw IntegerParsingError;
|
|
179
|
+
}
|
|
180
|
+
const parseIntResult = parseInt(trimmedString, radix);
|
|
181
|
+
if (isNaN(parseIntResult)) {
|
|
182
|
+
throw IntegerParsingError;
|
|
183
|
+
}
|
|
184
|
+
return parseIntResult;
|
|
185
|
+
}
|
|
186
|
+
var parseIntStrict_default = parseIntStrict;
|
|
187
|
+
|
|
164
188
|
// src/functions/randomiseArray.ts
|
|
165
189
|
function randomiseArray(array) {
|
|
166
190
|
const mutableArray = [...array];
|
|
@@ -197,6 +221,16 @@ function range(start, stop, step = 1) {
|
|
|
197
221
|
}
|
|
198
222
|
var range_default = range;
|
|
199
223
|
|
|
224
|
+
// src/functions/stringToBoolean.ts
|
|
225
|
+
function stringToBoolean(inputString) {
|
|
226
|
+
const normalisedString = inputString.toLowerCase();
|
|
227
|
+
if (normalisedString !== "true" && normalisedString !== "false") {
|
|
228
|
+
throw new TypeError("INVALID_BOOLEAN_STRING");
|
|
229
|
+
}
|
|
230
|
+
return normalisedString === "true";
|
|
231
|
+
}
|
|
232
|
+
var stringToBoolean_default = stringToBoolean;
|
|
233
|
+
|
|
200
234
|
// src/functions/truncate.ts
|
|
201
235
|
function truncate(stringToTruncate, maxLength = 5) {
|
|
202
236
|
return stringToTruncate.length > maxLength ? `${stringToTruncate.slice(0, maxLength)}...` : stringToTruncate;
|
|
@@ -297,9 +331,11 @@ export {
|
|
|
297
331
|
omitProperties_default as omitProperties,
|
|
298
332
|
Email_default as parseEmail,
|
|
299
333
|
Env_default as parseEnv,
|
|
334
|
+
parseIntStrict_default as parseIntStrict,
|
|
300
335
|
UUID_default as parseUUID,
|
|
301
336
|
randomiseArray_default as randomiseArray,
|
|
302
337
|
range_default as range,
|
|
338
|
+
stringToBoolean_default as stringToBoolean,
|
|
303
339
|
truncate_default as truncate,
|
|
304
340
|
wait_default as wait
|
|
305
341
|
};
|