cafe-utility 4.13.2 → 4.15.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 +5 -1
  2. package/index.js +15 -2
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -20,7 +20,7 @@ declare function randomIntInclusive(min: number, max: number): number;
20
20
  declare function randomBetween(min: number, max: number): number;
21
21
  declare function signedRandom(): number;
22
22
  declare function chance(threshold: number): boolean;
23
- declare function pick<T>(array: T[]): T;
23
+ declare function pick<T>(array: T[], generator?: () => number): T;
24
24
  declare function last<T>(array: T[]): T;
25
25
  declare function pickWeighted<T>(array: T[], weights: number[], randomNumber?: number): T;
26
26
  declare function sortWeighted<T>(array: T[], weights: number[]): T[];
@@ -57,6 +57,7 @@ declare function isUndefined(value: any): value is undefined;
57
57
  declare function isFunction(value: any): value is Function;
58
58
  declare function isString(value: any): value is string;
59
59
  declare function isNumber(value: any): value is number;
60
+ declare function isBoolean(value: any): value is boolean;
60
61
  declare function isDate(value: any): value is Date;
61
62
  declare function isBlank(value: any): boolean;
62
63
  declare function isId(value: any): value is number | string;
@@ -67,6 +68,7 @@ declare function randomUnicodeString(length: number): string;
67
68
  declare function randomHexString(length: number): string;
68
69
  declare function asString(string: any): string;
69
70
  declare function asNumber(number: any): number;
71
+ declare function asBoolean(bool: any): boolean;
70
72
  declare function asDate(date: any): Date;
71
73
  declare function asNullableString(string: any): string | null;
72
74
  declare function asId(value: any): number;
@@ -445,11 +447,13 @@ export declare const Types: {
445
447
  isUndefined: typeof isUndefined;
446
448
  isString: typeof isString;
447
449
  isNumber: typeof isNumber;
450
+ isBoolean: typeof isBoolean;
448
451
  isDate: typeof isDate;
449
452
  isBlank: typeof isBlank;
450
453
  isId: typeof isId;
451
454
  asString: typeof asString;
452
455
  asNumber: typeof asNumber;
456
+ asBoolean: typeof asBoolean;
453
457
  asDate: typeof asDate;
454
458
  asNullableString: typeof asNullableString;
455
459
  asId: typeof asId;
package/index.js CHANGED
@@ -112,8 +112,8 @@ function chance(threshold) {
112
112
  return Math.random() < threshold
113
113
  }
114
114
 
115
- function pick(array) {
116
- return array[Math.floor(array.length * Math.random())]
115
+ function pick(array, generator = Math.random) {
116
+ return array[Math.floor(array.length * generator())]
117
117
  }
118
118
 
119
119
  function last(array) {
@@ -387,6 +387,10 @@ function isNumber(value) {
387
387
  return !isNaN(value) && String(value) === String(parseFloat(value))
388
388
  }
389
389
 
390
+ function isBoolean(value) {
391
+ return value === true || value === false
392
+ }
393
+
390
394
  function isDate(value) {
391
395
  return Object.prototype.toString.call(value) === '[object Date]'
392
396
  }
@@ -459,6 +463,13 @@ function asNumber(number) {
459
463
  return number
460
464
  }
461
465
 
466
+ function asBoolean(bool) {
467
+ if (!isBoolean(bool)) {
468
+ throw new TypeError('Expected boolean, got: ' + bool)
469
+ }
470
+ return bool
471
+ }
472
+
462
473
  function asDate(date) {
463
474
  if (!isDate(date)) {
464
475
  throw new TypeError('Expected date, got: ' + date)
@@ -1931,11 +1942,13 @@ exports.Types = {
1931
1942
  isUndefined,
1932
1943
  isString,
1933
1944
  isNumber,
1945
+ isBoolean,
1934
1946
  isDate,
1935
1947
  isBlank,
1936
1948
  isId,
1937
1949
  asString,
1938
1950
  asNumber,
1951
+ asBoolean,
1939
1952
  asDate,
1940
1953
  asNullableString,
1941
1954
  asId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "4.13.2",
3
+ "version": "4.15.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {