@alextheman/utility 2.0.0 → 2.1.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/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 Error("NON_INTEGER_INPUTS");
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 Error("NON_INTEGER_INPUT");
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,21 @@ function omitProperties(object, keysToOmit) {
213
215
  }
214
216
  var omitProperties_default = omitProperties;
215
217
 
218
+ // src/functions/parseIntStrict.ts
219
+ function parseIntStrict(string, radix) {
220
+ const trimmedString = string.trim();
221
+ const pattern = radix && radix > 10 && radix <= 36 ? (
222
+ // String.fromCharCode() gets the maximum possible alphabetical character for a base above 10
223
+ new RegExp(`^[+-]?[0-9a-${String.fromCharCode(87 + radix - 1)}]+$`, "i")
224
+ ) : /^[+-]?\d+$/;
225
+ const parseIntResult = parseInt(trimmedString, radix);
226
+ if (isNaN(parseIntResult) || !pattern.test(trimmedString)) {
227
+ throw new TypeError("INTEGER_PARSING_ERROR");
228
+ }
229
+ return parseIntResult;
230
+ }
231
+ var parseIntStrict_default = parseIntStrict;
232
+
216
233
  // src/functions/randomiseArray.ts
217
234
  function randomiseArray(array) {
218
235
  const mutableArray = [...array];
@@ -249,6 +266,16 @@ function range(start, stop, step = 1) {
249
266
  }
250
267
  var range_default = range;
251
268
 
269
+ // src/functions/stringToBoolean.ts
270
+ function stringToBoolean(inputString) {
271
+ const normalisedString = inputString.toLowerCase();
272
+ if (normalisedString !== "true" && normalisedString !== "false") {
273
+ throw new TypeError("INVALID_BOOLEAN_STRING");
274
+ }
275
+ return normalisedString === "true";
276
+ }
277
+ var stringToBoolean_default = stringToBoolean;
278
+
252
279
  // src/functions/truncate.ts
253
280
  function truncate(stringToTruncate, maxLength = 5) {
254
281
  return stringToTruncate.length > maxLength ? `${stringToTruncate.slice(0, maxLength)}...` : stringToTruncate;
@@ -350,9 +377,11 @@ var UUID_default = parseUUID;
350
377
  omitProperties,
351
378
  parseEmail,
352
379
  parseEnv,
380
+ parseIntStrict,
353
381
  parseUUID,
354
382
  randomiseArray,
355
383
  range,
384
+ stringToBoolean,
356
385
  truncate,
357
386
  wait
358
387
  });
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 Error("NON_INTEGER_INPUTS");
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 Error("NON_INTEGER_INPUT");
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,21 @@ function omitProperties(object, keysToOmit) {
161
161
  }
162
162
  var omitProperties_default = omitProperties;
163
163
 
164
+ // src/functions/parseIntStrict.ts
165
+ function parseIntStrict(string, radix) {
166
+ const trimmedString = string.trim();
167
+ const pattern = radix && radix > 10 && radix <= 36 ? (
168
+ // String.fromCharCode() gets the maximum possible alphabetical character for a base above 10
169
+ new RegExp(`^[+-]?[0-9a-${String.fromCharCode(87 + radix - 1)}]+$`, "i")
170
+ ) : /^[+-]?\d+$/;
171
+ const parseIntResult = parseInt(trimmedString, radix);
172
+ if (isNaN(parseIntResult) || !pattern.test(trimmedString)) {
173
+ throw new TypeError("INTEGER_PARSING_ERROR");
174
+ }
175
+ return parseIntResult;
176
+ }
177
+ var parseIntStrict_default = parseIntStrict;
178
+
164
179
  // src/functions/randomiseArray.ts
165
180
  function randomiseArray(array) {
166
181
  const mutableArray = [...array];
@@ -197,6 +212,16 @@ function range(start, stop, step = 1) {
197
212
  }
198
213
  var range_default = range;
199
214
 
215
+ // src/functions/stringToBoolean.ts
216
+ function stringToBoolean(inputString) {
217
+ const normalisedString = inputString.toLowerCase();
218
+ if (normalisedString !== "true" && normalisedString !== "false") {
219
+ throw new TypeError("INVALID_BOOLEAN_STRING");
220
+ }
221
+ return normalisedString === "true";
222
+ }
223
+ var stringToBoolean_default = stringToBoolean;
224
+
200
225
  // src/functions/truncate.ts
201
226
  function truncate(stringToTruncate, maxLength = 5) {
202
227
  return stringToTruncate.length > maxLength ? `${stringToTruncate.slice(0, maxLength)}...` : stringToTruncate;
@@ -297,9 +322,11 @@ export {
297
322
  omitProperties_default as omitProperties,
298
323
  Email_default as parseEmail,
299
324
  Env_default as parseEnv,
325
+ parseIntStrict_default as parseIntStrict,
300
326
  UUID_default as parseUUID,
301
327
  randomiseArray_default as randomiseArray,
302
328
  range_default as range,
329
+ stringToBoolean_default as stringToBoolean,
303
330
  truncate_default as truncate,
304
331
  wait_default as wait
305
332
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alextheman/utility",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Helpful utility functions",
5
5
  "license": "ISC",
6
6
  "author": "alextheman",