atomic-fns 0.2.2 → 0.3.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 (70) hide show
  1. package/.firebaserc +5 -0
  2. package/README.md +9 -4
  3. package/firebase.json +10 -0
  4. package/package.json +9 -9
  5. package/src/collections/index.d.ts +672 -0
  6. package/src/collections/index.js +1091 -0
  7. package/src/collections/index.ts +1186 -0
  8. package/src/datetime/index.d.ts +6 -0
  9. package/src/datetime/index.js +6 -0
  10. package/src/datetime/index.ts +7 -0
  11. package/src/functools/index.d.ts +107 -0
  12. package/src/functools/index.js +219 -0
  13. package/src/functools/index.ts +255 -0
  14. package/src/globals/index.d.ts +286 -0
  15. package/src/globals/index.js +388 -0
  16. package/src/globals/index.ts +451 -0
  17. package/src/index.d.ts +8 -7
  18. package/src/index.js +8 -7
  19. package/src/index.ts +8 -7
  20. package/src/itertools/index.d.ts +225 -0
  21. package/src/itertools/index.js +449 -0
  22. package/src/itertools/index.ts +471 -0
  23. package/src/math/index.d.ts +97 -0
  24. package/src/math/index.js +92 -0
  25. package/src/math/index.ts +152 -0
  26. package/src/{operators.d.ts → operators/index.d.ts} +11 -9
  27. package/src/operators/index.js +171 -0
  28. package/src/operators/index.ts +175 -0
  29. package/src/random/index.d.ts +42 -0
  30. package/src/random/index.js +65 -0
  31. package/src/random/index.ts +66 -0
  32. package/tests/collections.spec.ts +42 -25
  33. package/tests/globals.spec.ts +56 -60
  34. package/tests/itertools.spec.ts +176 -4
  35. package/tests/math.spec.ts +25 -0
  36. package/tests/operators.spec.ts +1 -26
  37. package/tests/random.spec.ts +46 -0
  38. package/tsconfig.json +12 -4
  39. package/src/collections.d.ts +0 -228
  40. package/src/collections.js +0 -553
  41. package/src/collections.ts +0 -641
  42. package/src/datetime.d.ts +0 -5
  43. package/src/datetime.js +0 -5
  44. package/src/datetime.ts +0 -6
  45. package/src/functools.d.ts +0 -40
  46. package/src/functools.js +0 -78
  47. package/src/functools.ts +0 -93
  48. package/src/globals.d.ts +0 -179
  49. package/src/globals.js +0 -309
  50. package/src/globals.ts +0 -377
  51. package/src/itertools.d.ts +0 -20
  52. package/src/itertools.js +0 -98
  53. package/src/itertools.ts +0 -116
  54. package/src/math.d.ts +0 -16
  55. package/src/math.js +0 -70
  56. package/src/math.ts +0 -77
  57. package/src/objects.d.ts +0 -0
  58. package/src/objects.js +0 -1
  59. package/src/objects.ts +0 -0
  60. package/src/operators.js +0 -109
  61. package/src/operators.ts +0 -130
  62. package/src/random.d.ts +0 -22
  63. package/src/random.js +0 -48
  64. package/src/random.ts +0 -53
  65. package/src/strings.d.ts +0 -0
  66. package/src/strings.js +0 -1
  67. package/src/strings.ts +0 -0
  68. package/src/util.d.ts +0 -0
  69. package/src/util.js +0 -1
  70. package/src/util.ts +0 -0
@@ -0,0 +1,152 @@
1
+ /**
2
+ * Math and algebraic operations.
3
+ *
4
+ * @module Math
5
+ */
6
+
7
+ import { call, Predicate } from '../globals/index.js'
8
+ import { reduce } from '../itertools/index.js'
9
+ import { gt, id, lt } from '../operators/index.js'
10
+
11
+ /**
12
+ * Returns the absolute value of a number. The argument may be a number or an object implementing the `abs()`.
13
+ * @param x The value.
14
+ * @returns The absolute value.
15
+ */
16
+ export function abs(x): number {
17
+ const op = call(x, 'abs')
18
+ if (op != null) return op
19
+ return Math.abs(x)
20
+ }
21
+
22
+ /**
23
+ * Round `x` to the number of digits after the decimal point. If `digits` is
24
+ * omitted, it returns the nearest integer to x.
25
+ *
26
+ * @param {number} x
27
+ * @param {number} [digits=0]
28
+ *
29
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round Math.round()}
30
+ */
31
+ export function round(x: number, digits = 0) {
32
+ const base = 10 ** digits
33
+ return Math.round(x * base) / base
34
+ }
35
+
36
+ /**
37
+ * Rounds down and returns the largest integer less than or equal to a given number.
38
+ * @param x A number.
39
+ * @returns Nearest integer less than or equal to number.
40
+ *
41
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor Math.floor()}
42
+ */
43
+ export const floor = (x: number) => Math.floor(x)
44
+
45
+ /**
46
+ * Rounds up and returns the largest integer greater than or equal to a given number.
47
+ * @param x A number.
48
+ * @returns Nearest integer greater than or equal to number.
49
+ *
50
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil Math.ceil()}
51
+ */
52
+ export const ceil = (x: number) => Math.ceil(x)
53
+
54
+ /**
55
+ * Returns a tuple like `[x / y, x % y]`.
56
+ *
57
+ * @param x
58
+ * @param y
59
+ * @returns
60
+ */
61
+ export function divmod(x: number, y: number) {
62
+ return [Math.floor(x / y), x % y]
63
+ }
64
+
65
+ export function log2(x) {
66
+ if (x > 0) {
67
+ return Math.log(x) * 1.442695
68
+ }
69
+ return Number.NaN
70
+ }
71
+
72
+ export function logBase(x, y) {
73
+ if (x > 0 && y > 0) {
74
+ return Math.log(y) / Math.log(x)
75
+ }
76
+ return Number.NaN
77
+ }
78
+
79
+ /**
80
+ * Returns the smallest element in an iterable. The optional `key` argument specifies a transform on the elements before comparing them. If the elements in the iterable are objects that implement a custom `lt` method, this will be called to compare.
81
+ * @param iterable The iterable to inspect.
82
+ * @param key Optional key function to transform the elements (default `id`).
83
+ * @returns The smallest element in the iterable.
84
+ */
85
+ export function min(iterable: number[]): number
86
+ export function min<T>(iterable: Iterable<T>, key?: Predicate<T>): T
87
+ export function min<T>(iterable: Iterable<T>, key = id) {
88
+ return reduce(iterable, (x, y) => (lt(key(x), key(y)) ? x : y))
89
+ }
90
+
91
+ /**
92
+ * Returns the largest element in an iterable. The optional `key` argument specifies a transform on the elements before comparing them. If the elements in the iterable are objects that implement a custom `gt` method, this will be called to compare.
93
+ * @param iterable The iterable to inspect.
94
+ * @param key Optional key function to transform the elements (default `id`).
95
+ * @returns The largest element in the iterable.
96
+ */
97
+ export function max(iterable: number[]): number
98
+ export function max<T>(iterable: Iterable<T>, key?: Predicate<T>): T
99
+ export function max<T>(iterable: Iterable<T>, key = id) {
100
+ return reduce(iterable, (x, y) => (gt(key(x), key(y)) ? x : y))
101
+ }
102
+
103
+ /**
104
+ * Computes the mean of the values in `iterable` and accepts an optional `key` function which is invoked for each element to generate the values to be averaged.
105
+ * @param {Array<number>|Iterable<T>} iterable
106
+ * @param {Function} [key] Optional iteratee invoked per element.
107
+ * @returns Returns the mean value or `false` if no elements.
108
+ * @template T
109
+ * @example
110
+ ```js
111
+ let objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }]
112
+
113
+ mean(objects, (o) => o.n)
114
+ // => 5
115
+ ```
116
+ */
117
+ export function mean(iterable: number[]): false | number
118
+ export function mean<T>(iterable: Iterable<T>, key?: Predicate<T>): false | number
119
+ export function mean<T>(iterable: Iterable<T>, key = id): false | number {
120
+ let total = 0
121
+ let count = 0
122
+ for (const value of iterable) {
123
+ total += key(value)
124
+ count++
125
+ }
126
+ return count !== 0 && total / count
127
+ }
128
+
129
+ /**
130
+ * Sums `initial` and the items of an iterable and returns the total.
131
+ * @param args The elements to add to `initial`.
132
+ * @param initial The initial value to add to (default `0`).
133
+ * @returns The total sum of initial and iterable elements.
134
+ * @example
135
+ ```js
136
+ sum([1, 2, 3, 4])
137
+ // => 10
138
+
139
+ sum([1, 2, 3, 4], 5)
140
+ // => 15
141
+ ```
142
+ */
143
+ export function sum(iterable: number[]): number
144
+ export function sum<T>(iterable: Iterable<T>, key?: Predicate<T>): T
145
+ export function sum<T>(iterable: Iterable<T>, key?: Predicate<T>): T
146
+ export function sum<T>(args: Iterable<T>, key: Predicate<T> = id) {
147
+ let initial = 0
148
+ for (const x of args) {
149
+ initial += key(x)
150
+ }
151
+ return initial
152
+ }
@@ -1,21 +1,21 @@
1
+ /**
2
+ * Generic operations for both values and objects.
3
+ *
4
+ * @module Operators
5
+ */
1
6
  export declare const id: (x: any) => any;
2
7
  export declare const bool: (x: any) => boolean;
3
8
  export declare const not: (x: any) => boolean;
4
9
  export declare const isinstance: (x: any, y: any) => boolean;
5
- export declare const all: (arr: any[], fn?: (x: any) => boolean) => boolean;
6
- export declare const any: (arr: any[], fn?: (x: any) => boolean) => boolean;
7
- export declare const and: (arr: any[], fn?: (x: any) => boolean) => boolean;
8
- export declare const or: (arr: any[], fn?: (x: any) => boolean) => boolean;
9
- export declare function comp(x: any, y: any): any;
10
+ export declare function comp(x: any, y: any): 0 | 1 | -1;
10
11
  export declare function eq(x: any, y: any): any;
11
- export declare function shallowEqual(x: any, y: any): any;
12
12
  export declare function lt(x: any, y: any): any;
13
- export declare function le(x: any, y: any): any;
13
+ export declare function lte(x: any, y: any): any;
14
14
  export declare function gt(x: any, y: any): any;
15
- export declare function ge(x: any, y: any): any;
15
+ export declare function gte(x: any, y: any): any;
16
16
  export declare function add(x: any, y: any): any;
17
17
  export declare function sub(x: any, y: any): any;
18
- export declare function mult(x: any, y: any): any;
18
+ export declare function mul(x: any, y: any): any;
19
19
  export declare function div(x: any, y: any): any;
20
20
  export declare function mod(x: any, y: any): any;
21
21
  /**
@@ -27,3 +27,5 @@ export declare function mod(x: any, y: any): any;
27
27
  * @return `x` raised to the power `y`
28
28
  */
29
29
  export declare function pow(x: any, y: any): any;
30
+ export declare function shallowEqual(obj: any, other: any): boolean;
31
+ export declare function deepEqual(obj: any, other: any, checker?: typeof eq): boolean;
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Generic operations for both values and objects.
3
+ *
4
+ * @module Operators
5
+ */
6
+ import { call, isNumber, isObject } from '../globals/index.js';
7
+ export const id = (x) => x;
8
+ export const bool = (x) => !!x;
9
+ export const not = (x) => !x;
10
+ export const isinstance = (x, y) => x instanceof y;
11
+ export function comp(x, y) {
12
+ const op = call(x, 'compare', y);
13
+ if (isNumber(op))
14
+ return op;
15
+ if (eq(x, y))
16
+ return 0;
17
+ if (lte(x, y))
18
+ return -1;
19
+ if (gte(x, y))
20
+ return 1;
21
+ return 0;
22
+ }
23
+ export function eq(x, y) {
24
+ const op = call(x, 'eq', y);
25
+ if (op != null)
26
+ return op;
27
+ return x === y;
28
+ }
29
+ export function lt(x, y) {
30
+ const op = call(x, 'lt', y);
31
+ if (op != null)
32
+ return op;
33
+ return x < y;
34
+ }
35
+ export function lte(x, y) {
36
+ let op = call(x, 'lte', y);
37
+ if (op != null)
38
+ return op;
39
+ op = call(x, 'eq', y);
40
+ if (op === true)
41
+ return op;
42
+ op = call(x, 'lt', y);
43
+ if (op != null)
44
+ return op;
45
+ return x <= y;
46
+ }
47
+ export function gt(x, y) {
48
+ const op = call(x, 'gt', y);
49
+ if (op != null)
50
+ return op;
51
+ return x > y;
52
+ }
53
+ export function gte(x, y) {
54
+ let op = call(x, 'gte', y);
55
+ if (op != null)
56
+ return op;
57
+ op = call(x, 'eq', y);
58
+ if (op === true)
59
+ return op;
60
+ op = call(x, 'gt', y);
61
+ if (op != null)
62
+ return op;
63
+ return x >= y;
64
+ }
65
+ export function add(x, y) {
66
+ const op = call(x, 'add', y);
67
+ if (op != null)
68
+ return op;
69
+ return x + y;
70
+ }
71
+ export function sub(x, y) {
72
+ const op = call(x, 'sub', y);
73
+ if (op != null)
74
+ return op;
75
+ return x - y;
76
+ }
77
+ export function mul(x, y) {
78
+ const op = call(x, 'mul', y);
79
+ if (op != null)
80
+ return op;
81
+ return x * y;
82
+ }
83
+ export function div(x, y) {
84
+ const op = call(x, 'div', y);
85
+ if (op != null)
86
+ return op;
87
+ return x / y;
88
+ }
89
+ export function mod(x, y) {
90
+ const op = call(x, 'mod', y);
91
+ if (op != null)
92
+ return op;
93
+ return x % y;
94
+ }
95
+ /**
96
+ * Calculates `x` to the power of `y`. If `x` implements a custom `pow` operator
97
+ * it will return `x.pow(y)`.
98
+ *
99
+ * @param {*} x
100
+ * @param {*} y
101
+ * @return `x` raised to the power `y`
102
+ */
103
+ export function pow(x, y) {
104
+ const op = call(x, 'pow', y);
105
+ if (op != null)
106
+ return op;
107
+ return x ** y;
108
+ }
109
+ export function shallowEqual(obj, other) {
110
+ if (obj === other)
111
+ return true;
112
+ if (!obj || !other)
113
+ return false;
114
+ if (Array.isArray(obj)) {
115
+ // compare the arrays
116
+ if (obj.length !== other.length)
117
+ return false;
118
+ for (let i = 0; i < obj.length; i++) {
119
+ if (obj[i] !== other[i]) {
120
+ return false;
121
+ }
122
+ }
123
+ return true;
124
+ }
125
+ else if (isObject(obj)) {
126
+ // compare the object keys
127
+ const objKeys = Object.keys(obj);
128
+ const otherKeys = Object.keys(other);
129
+ if (objKeys.length !== otherKeys.length)
130
+ return false;
131
+ for (const key of objKeys) {
132
+ if (!Object.prototype.hasOwnProperty.call(other, key) || obj[key] !== other[key]) {
133
+ return false;
134
+ }
135
+ }
136
+ return true;
137
+ }
138
+ return false;
139
+ }
140
+ export function deepEqual(obj, other, checker = eq) {
141
+ if (checker(obj, other))
142
+ return true;
143
+ if (!obj || !other)
144
+ return false;
145
+ if (Array.isArray(obj)) {
146
+ // compare the arrays
147
+ if (obj.length !== other.length)
148
+ return false;
149
+ for (let i = 0; i < obj.length; i++) {
150
+ if (!deepEqual(obj[i], other[i], checker)) {
151
+ return false;
152
+ }
153
+ }
154
+ return true;
155
+ }
156
+ else if (isObject(obj)) {
157
+ // compare the object keys
158
+ const objKeys = Object.keys(obj);
159
+ const otherKeys = Object.keys(other);
160
+ if (objKeys.length !== otherKeys.length)
161
+ return false;
162
+ for (const key of objKeys) {
163
+ if (!Object.prototype.hasOwnProperty.call(other, key) ||
164
+ !deepEqual(obj[key], other[key], checker)) {
165
+ return false;
166
+ }
167
+ }
168
+ return true;
169
+ }
170
+ return false;
171
+ }
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Generic operations for both values and objects.
3
+ *
4
+ * @module Operators
5
+ */
6
+
7
+ import { call, isNumber, isObject } from '../globals/index.js'
8
+
9
+ export const id = (x) => x
10
+
11
+ export const bool = (x) => !!x
12
+
13
+ export const not = (x) => !x
14
+
15
+ export const isinstance = (x, y) => x instanceof y
16
+
17
+ export function comp(x, y) {
18
+ const op = call(x, 'compare', y)
19
+ if (isNumber(op)) return op as 0 | 1 | -1
20
+ if (eq(x, y)) return 0
21
+ if (lte(x, y)) return -1
22
+ if (gte(x, y)) return 1
23
+ return 0
24
+ }
25
+
26
+ export function eq(x, y) {
27
+ const op = call(x, 'eq', y)
28
+ if (op != null) return op
29
+ return x === y
30
+ }
31
+
32
+ export function lt(x, y) {
33
+ const op = call(x, 'lt', y)
34
+ if (op != null) return op
35
+ return x < y
36
+ }
37
+
38
+ export function lte(x, y) {
39
+ let op = call(x, 'lte', y)
40
+ if (op != null) return op
41
+ op = call(x, 'eq', y)
42
+ if (op === true) return op
43
+ op = call(x, 'lt', y)
44
+ if (op != null) return op
45
+ return x <= y
46
+ }
47
+
48
+ export function gt(x, y) {
49
+ const op = call(x, 'gt', y)
50
+ if (op != null) return op
51
+ return x > y
52
+ }
53
+
54
+ export function gte(x, y) {
55
+ let op = call(x, 'gte', y)
56
+ if (op != null) return op
57
+ op = call(x, 'eq', y)
58
+ if (op === true) return op
59
+ op = call(x, 'gt', y)
60
+ if (op != null) return op
61
+ return x >= y
62
+ }
63
+
64
+ export function add(x, y) {
65
+ const op = call(x, 'add', y)
66
+ if (op != null) return op
67
+ return x + y
68
+ }
69
+
70
+ export function sub(x, y) {
71
+ const op = call(x, 'sub', y)
72
+ if (op != null) return op
73
+ return x - y
74
+ }
75
+
76
+ export function mul(x, y) {
77
+ const op = call(x, 'mul', y)
78
+ if (op != null) return op
79
+ return x * y
80
+ }
81
+
82
+ export function div(x, y) {
83
+ const op = call(x, 'div', y)
84
+ if (op != null) return op
85
+ return x / y
86
+ }
87
+
88
+ export function mod(x, y) {
89
+ const op = call(x, 'mod', y)
90
+ if (op != null) return op
91
+ return x % y
92
+ }
93
+
94
+ /**
95
+ * Calculates `x` to the power of `y`. If `x` implements a custom `pow` operator
96
+ * it will return `x.pow(y)`.
97
+ *
98
+ * @param {*} x
99
+ * @param {*} y
100
+ * @return `x` raised to the power `y`
101
+ */
102
+ export function pow(x, y) {
103
+ const op = call(x, 'pow', y)
104
+ if (op != null) return op
105
+ return x ** y
106
+ }
107
+
108
+ export function shallowEqual(obj, other) {
109
+ if (obj === other) return true
110
+
111
+ if (!obj || !other) return false
112
+
113
+ if (Array.isArray(obj)) {
114
+ // compare the arrays
115
+ if (obj.length !== other.length) return false
116
+
117
+ for (let i = 0; i < obj.length; i++) {
118
+ if (obj[i] !== other[i]) {
119
+ return false
120
+ }
121
+ }
122
+ return true
123
+ } else if (isObject(obj)) {
124
+ // compare the object keys
125
+ const objKeys = Object.keys(obj)
126
+ const otherKeys = Object.keys(other)
127
+
128
+ if (objKeys.length !== otherKeys.length) return false
129
+
130
+ for (const key of objKeys) {
131
+ if (!Object.prototype.hasOwnProperty.call(other, key) || obj[key] !== other[key]) {
132
+ return false
133
+ }
134
+ }
135
+ return true
136
+ }
137
+
138
+ return false
139
+ }
140
+
141
+ export function deepEqual(obj, other, checker = eq) {
142
+ if (checker(obj, other)) return true
143
+
144
+ if (!obj || !other) return false
145
+
146
+ if (Array.isArray(obj)) {
147
+ // compare the arrays
148
+ if (obj.length !== other.length) return false
149
+
150
+ for (let i = 0; i < obj.length; i++) {
151
+ if (!deepEqual(obj[i], other[i], checker)) {
152
+ return false
153
+ }
154
+ }
155
+ return true
156
+ } else if (isObject(obj)) {
157
+ // compare the object keys
158
+ const objKeys = Object.keys(obj)
159
+ const otherKeys = Object.keys(other)
160
+
161
+ if (objKeys.length !== otherKeys.length) return false
162
+
163
+ for (const key of objKeys) {
164
+ if (
165
+ !Object.prototype.hasOwnProperty.call(other, key) ||
166
+ !deepEqual(obj[key], other[key], checker)
167
+ ) {
168
+ return false
169
+ }
170
+ }
171
+ return true
172
+ }
173
+
174
+ return false
175
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * This module implements pseudo-random generators for various distributions.
3
+ *
4
+ * @module Random
5
+ */
6
+ /**
7
+ * Returns a random float between a [min, max) value (max exclusive).
8
+ * @param {number} min
9
+ * @param {number} max
10
+ * @returns {number}
11
+ */
12
+ export declare const random: (min?: number, max?: number) => number;
13
+ /**
14
+ * Returns a random integer between a [min, max] value (inclusive).
15
+ * @param {number} min
16
+ * @param {number} max
17
+ * @returns {number}
18
+ */
19
+ export declare const randomInt: (min?: number, max?: number) => number;
20
+ /**
21
+ * Returns the item at some random index in a sequence.
22
+ * @returns {T}
23
+ * @template T
24
+ */
25
+ export declare const choice: <T>(arr: T[]) => T | undefined;
26
+ /**
27
+ * Returns a random sample of size k from a list of items.
28
+ * @param {Array} arr
29
+ * @param {number} size
30
+ * @returns A new array with `size` random elements from `arr`.
31
+ * @template T
32
+ */
33
+ export declare function sample<T>(arr: T[], size: number): T[];
34
+ /**
35
+ * Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
36
+ *
37
+ * @param {Array} arr
38
+ * @param {number} size
39
+ * @returns The shuffled array.
40
+ * @template T
41
+ */
42
+ export declare function shuffle<T>(arr: T[], size?: number): T[];
@@ -0,0 +1,65 @@
1
+ /**
2
+ * This module implements pseudo-random generators for various distributions.
3
+ *
4
+ * @module Random
5
+ */
6
+ import { isObject } from '../globals/index.js';
7
+ /**
8
+ * Returns a random float between a [min, max) value (max exclusive).
9
+ * @param {number} min
10
+ * @param {number} max
11
+ * @returns {number}
12
+ */
13
+ export const random = (min = 0, max = 1) => Math.random() * (max - min) + min;
14
+ /**
15
+ * Returns a random integer between a [min, max] value (inclusive).
16
+ * @param {number} min
17
+ * @param {number} max
18
+ * @returns {number}
19
+ */
20
+ export const randomInt = (min = 0, max = 1) => Math.round(random(min, max));
21
+ /**
22
+ * Returns the item at some random index in a sequence.
23
+ * @returns {T}
24
+ * @template T
25
+ */
26
+ export const choice = (arr) => {
27
+ if (!arr || !arr.length)
28
+ return;
29
+ return arr[randomInt(0, arr.length - 1)];
30
+ };
31
+ /**
32
+ * Returns a random sample of size k from a list of items.
33
+ * @param {Array} arr
34
+ * @param {number} size
35
+ * @returns A new array with `size` random elements from `arr`.
36
+ * @template T
37
+ */
38
+ export function sample(arr, size) {
39
+ if (!arr || !arr.length || arr.length < size)
40
+ return [];
41
+ return shuffle(arr.slice()).slice(-size);
42
+ }
43
+ /**
44
+ * Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
45
+ *
46
+ * @param {Array} arr
47
+ * @param {number} size
48
+ * @returns The shuffled array.
49
+ * @template T
50
+ */
51
+ export function shuffle(arr, size) {
52
+ if (!arr || !arr.length)
53
+ return arr;
54
+ if (isObject(arr))
55
+ arr = Object.values(arr);
56
+ if (size == null)
57
+ size = arr.length - 1;
58
+ for (let i = 0; i <= size; i++) {
59
+ const j = randomInt(i, size);
60
+ const temp = arr[j];
61
+ arr[j] = arr[i];
62
+ arr[i] = temp;
63
+ }
64
+ return arr;
65
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * This module implements pseudo-random generators for various distributions.
3
+ *
4
+ * @module Random
5
+ */
6
+
7
+ import { isObject } from '../globals/index.js'
8
+
9
+ /**
10
+ * Returns a random float between a [min, max) value (max exclusive).
11
+ * @param {number} min
12
+ * @param {number} max
13
+ * @returns {number}
14
+ */
15
+ export const random = (min = 0, max = 1) => Math.random() * (max - min) + min
16
+
17
+ /**
18
+ * Returns a random integer between a [min, max] value (inclusive).
19
+ * @param {number} min
20
+ * @param {number} max
21
+ * @returns {number}
22
+ */
23
+ export const randomInt = (min = 0, max = 1) => Math.round(random(min, max))
24
+
25
+ /**
26
+ * Returns the item at some random index in a sequence.
27
+ * @returns {T}
28
+ * @template T
29
+ */
30
+ export const choice = <T>(arr: T[]) => {
31
+ if (!arr || !arr.length) return
32
+ return arr[randomInt(0, arr.length - 1)]
33
+ }
34
+
35
+ /**
36
+ * Returns a random sample of size k from a list of items.
37
+ * @param {Array} arr
38
+ * @param {number} size
39
+ * @returns A new array with `size` random elements from `arr`.
40
+ * @template T
41
+ */
42
+ export function sample<T>(arr: T[], size: number) {
43
+ if (!arr || !arr.length || arr.length < size) return []
44
+ return shuffle(arr.slice()).slice(-size)
45
+ }
46
+
47
+ /**
48
+ * Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
49
+ *
50
+ * @param {Array} arr
51
+ * @param {number} size
52
+ * @returns The shuffled array.
53
+ * @template T
54
+ */
55
+ export function shuffle<T>(arr: T[], size?: number) {
56
+ if (!arr || !arr.length) return arr
57
+ if (isObject(arr)) arr = Object.values(arr)
58
+ if (size == null) size = arr.length - 1
59
+ for (let i = 0; i <= size; i++) {
60
+ const j = randomInt(i, size)
61
+ const temp = arr[j]
62
+ arr[j] = arr[i]
63
+ arr[i] = temp
64
+ }
65
+ return arr
66
+ }