cafe-utility 8.0.2 → 9.0.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.
Files changed (3) hide show
  1. package/index.d.ts +7 -5
  2. package/index.js +39 -15
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -12,8 +12,7 @@ declare function initializeArray<T>(count: number, initializer: (index: number)
12
12
  declare function rotate2DArray<T>(array: T[][]): T[][];
13
13
  declare function initialize2DArray<T>(width: number, height: number, initialValue: T): T[][];
14
14
  declare function containsShape<T>(array2D: T[][], shape: T[][], x: number, y: number): boolean;
15
- declare function takeRandomly<T>(array: T[], count: number, generator?: () => number): T[];
16
- declare function pickRandomIndices<T>(array: T[], count: number): number[];
15
+ declare function pickRandomIndices<T>(array: T[], count: number, generator?: () => number): number[];
17
16
  declare function pluck<T, K extends keyof T>(array: T[], key: K): T[K][];
18
17
  declare function makeSeededRng(seed: number): () => number;
19
18
  declare function intBetween(min: number, max: number): number;
@@ -21,6 +20,8 @@ declare function floatBetween(min: number, max: number): number;
21
20
  declare function signedRandom(): number;
22
21
  declare function chance(threshold: number): boolean;
23
22
  declare function pick<T>(array: T[], generator?: () => number): T;
23
+ declare function pickMany<T>(array: T[], count: number, generator?: () => number): T[];
24
+ declare function pickManyUnique<T>(array: T[], count: number, equalityFunction: (a: T, b: T) => boolean, generator?: () => number): T[];
24
25
  declare function last<T>(array: T[]): T;
25
26
  declare function pickWeighted<T>(array: T[], weights: number[], randomNumber?: number): T;
26
27
  declare function sortWeighted<T>(array: T[], weights: number[], generator?: () => number): T[];
@@ -316,8 +317,6 @@ export declare const Arrays: {
316
317
  onlyOrNull: typeof onlyOrNull;
317
318
  firstOrNull: typeof firstOrNull;
318
319
  shuffle: typeof shuffle;
319
- takeRandomly: typeof takeRandomly;
320
- pickRandomIndices: typeof pickRandomIndices;
321
320
  initialize: typeof initializeArray;
322
321
  initialize2D: typeof initialize2DArray;
323
322
  rotate2D: typeof rotate2DArray;
@@ -325,8 +324,11 @@ export declare const Arrays: {
325
324
  glue: typeof glue;
326
325
  pluck: typeof pluck;
327
326
  pick: typeof pick;
328
- last: typeof last;
327
+ pickMany: typeof pickMany;
328
+ pickManyUnique: typeof pickManyUnique;
329
329
  pickWeighted: typeof pickWeighted;
330
+ pickRandomIndices: typeof pickRandomIndices;
331
+ last: typeof last;
330
332
  sortWeighted: typeof sortWeighted;
331
333
  pushAll: typeof pushAll;
332
334
  unshiftAll: typeof unshiftAll;
package/index.js CHANGED
@@ -104,12 +104,8 @@ function containsShape(array2D, shape, x, y) {
104
104
  return true
105
105
  }
106
106
 
107
- function takeRandomly(array, count, generator = Math.random) {
108
- return shuffle(array, generator).slice(0, count)
109
- }
110
-
111
- function pickRandomIndices(array, count) {
112
- return shuffle(range(0, array.length - 1)).slice(0, count)
107
+ function pickRandomIndices(array, count, generator = Math.random) {
108
+ return shuffle(range(0, array.length - 1), generator).slice(0, count)
113
109
  }
114
110
 
115
111
  function pluck(array, key) {
@@ -150,6 +146,29 @@ function pick(array, generator = Math.random) {
150
146
  return array[Math.floor(array.length * generator())]
151
147
  }
152
148
 
149
+ function pickMany(array, count, generator = Math.random) {
150
+ if (count > array.length) {
151
+ throw new Error(`Count (${count}) is greater than array length (${array.length})`)
152
+ }
153
+ const indices = pickRandomIndices(array, count, generator)
154
+ return indices.map(index => array[index])
155
+ }
156
+
157
+ function pickManyUnique(array, count, equalityFunction, generator = Math.random) {
158
+ if (count > array.length) {
159
+ throw new Error(`Count (${count}) is greater than array length (${array.length})`)
160
+ }
161
+ const results = []
162
+ while (results.length < count) {
163
+ const candidate = pick(array, generator)
164
+ if (results.some(result => equalityFunction(result, candidate))) {
165
+ continue
166
+ }
167
+ results.push(candidate)
168
+ }
169
+ return results
170
+ }
171
+
153
172
  function last(array) {
154
173
  return array[array.length - 1]
155
174
  }
@@ -292,12 +311,16 @@ function convertBytes(bytes) {
292
311
  }
293
312
 
294
313
  function isObject(value) {
295
- return (
296
- value !== null &&
297
- typeof value === 'object' &&
298
- !value.constructor.isBuffer &&
299
- value.constructor.name !== 'Uint8Array'
300
- )
314
+ if (!value) {
315
+ return false
316
+ }
317
+ if (value._readableState) {
318
+ return false
319
+ }
320
+ if (value.constructor && (value.constructor.isBuffer || value.constructor.name == 'Uint8Array')) {
321
+ return false
322
+ }
323
+ return typeof value === 'object'
301
324
  }
302
325
 
303
326
  function isStrictlyObject(value) {
@@ -2164,8 +2187,6 @@ exports.Arrays = {
2164
2187
  onlyOrNull,
2165
2188
  firstOrNull,
2166
2189
  shuffle,
2167
- takeRandomly,
2168
- pickRandomIndices,
2169
2190
  initialize: initializeArray,
2170
2191
  initialize2D: initialize2DArray,
2171
2192
  rotate2D: rotate2DArray,
@@ -2173,8 +2194,11 @@ exports.Arrays = {
2173
2194
  glue,
2174
2195
  pluck,
2175
2196
  pick,
2176
- last,
2197
+ pickMany,
2198
+ pickManyUnique,
2177
2199
  pickWeighted,
2200
+ pickRandomIndices,
2201
+ last,
2178
2202
  sortWeighted,
2179
2203
  pushAll,
2180
2204
  unshiftAll,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "8.0.2",
3
+ "version": "9.0.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {