cafe-utility 10.21.0 → 10.23.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.
Files changed (3) hide show
  1. package/index.d.ts +3 -1
  2. package/index.js +22 -7
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -53,7 +53,7 @@ declare function isNumber(value: any): value is number;
53
53
  declare function isBoolean(value: any): value is boolean;
54
54
  declare function isDate(value: any): value is Date;
55
55
  declare function isBlank(value: any): boolean;
56
- declare function isId(value: any): value is number | string;
56
+ declare function isId(value: any): value is number;
57
57
  declare function randomLetterString(length: number): string;
58
58
  declare function randomAlphanumericString(length: number): string;
59
59
  declare function randomRichAsciiString(length: number): string;
@@ -64,6 +64,7 @@ declare function asNumber(number: any): number;
64
64
  declare function asBoolean(bool: any): boolean;
65
65
  declare function asDate(date: any): Date;
66
66
  declare function asNullableString(string: any): string | null;
67
+ declare function asEmptiableString(string: any): string;
67
68
  declare function asId(value: any): number;
68
69
  declare function asTime(value: any): string;
69
70
  declare function asArray(value: any): unknown[];
@@ -547,6 +548,7 @@ export declare const Types: {
547
548
  asBoolean: typeof asBoolean;
548
549
  asDate: typeof asDate;
549
550
  asNullableString: typeof asNullableString;
551
+ asEmptiableString: typeof asEmptiableString;
550
552
  asId: typeof asId;
551
553
  asTime: typeof asTime;
552
554
  asArray: typeof asArray;
package/index.js CHANGED
@@ -383,7 +383,7 @@ function isPromise(value) {
383
383
  }
384
384
 
385
385
  function isNumber(value) {
386
- return !isNaN(value) && String(value) === String(parseFloat(value))
386
+ return typeof value === 'number' && !isNaN(value)
387
387
  }
388
388
 
389
389
  function isBoolean(value) {
@@ -399,8 +399,7 @@ function isBlank(value) {
399
399
  }
400
400
 
401
401
  function isId(value) {
402
- const numeric = typeof value === 'string' ? parseInt(value, 10) : value
403
- return Number.isInteger(numeric) && numeric >= 1
402
+ return isNumber(value) && Number.isInteger(value) && value >= 1
404
403
  }
405
404
 
406
405
  const alphabet = 'abcdefghijklmnopqrstuvwxyz'
@@ -456,10 +455,14 @@ function asString(string) {
456
455
  }
457
456
 
458
457
  function asNumber(number) {
459
- if (!isNumber(number)) {
458
+ if (isNumber(number)) {
459
+ return number
460
+ }
461
+ const parsed = parseFloat(number)
462
+ if (!isNumber(parsed)) {
460
463
  throw new TypeError('Expected number, got: ' + number)
461
464
  }
462
- return number
465
+ return parsed
463
466
  }
464
467
 
465
468
  function asBoolean(bool) {
@@ -483,11 +486,22 @@ function asNullableString(string) {
483
486
  return string
484
487
  }
485
488
 
489
+ function asEmptiableString(string) {
490
+ if (!isString(string)) {
491
+ throw new TypeError('Expected string, got: ' + string)
492
+ }
493
+ return string
494
+ }
495
+
486
496
  function asId(value) {
487
- if (!isId(value)) {
497
+ if (isId(value)) {
498
+ return value
499
+ }
500
+ const parsed = parseInt(value, 10)
501
+ if (!isId(parsed)) {
488
502
  throw new TypeError('Expected id, got: ' + value)
489
503
  }
490
- return typeof value === 'string' ? parseInt(value, 10) : value
504
+ return parsed
491
505
  }
492
506
 
493
507
  function asTime(value) {
@@ -2872,6 +2886,7 @@ exports.Types = {
2872
2886
  asBoolean,
2873
2887
  asDate,
2874
2888
  asNullableString,
2889
+ asEmptiableString,
2875
2890
  asId,
2876
2891
  asTime,
2877
2892
  asArray,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "10.21.0",
3
+ "version": "10.23.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {