qznt 1.0.2 → 1.0.3

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.d.mts CHANGED
@@ -193,10 +193,22 @@ declare const fs: {
193
193
  };
194
194
 
195
195
  type AlphaCasing = "lower" | "upper" | "mixed";
196
+ interface RndArrayOptions {
197
+ /** Optional seed for RNG. */
198
+ seed?: number;
199
+ /** Reroll if the result is equal to this value. */
200
+ not?: any;
201
+ /** Maximum number of times to reroll if `not` is specified. [default: 10] */
202
+ maxRerolls?: number;
203
+ }
196
204
  interface RndStrOptions {
197
- casing?: AlphaCasing;
205
+ /** Optional seed for RNG. */
198
206
  seed?: number;
207
+ /** Character casing mode. */
208
+ casing?: AlphaCasing;
209
+ /** Custom character pool. */
199
210
  customChars?: string;
211
+ /** Characters to exclude from the pool. */
200
212
  exclude?: string | string[];
201
213
  }
202
214
  /**
@@ -208,9 +220,9 @@ declare function chance(percent?: number, seed?: number): boolean;
208
220
  /**
209
221
  * Returns a random item from the given array.
210
222
  * @param array Array of items to choose from.
211
- * @param seed Optional seed for RNG.
223
+ * @param options Options for the choice function.
212
224
  */
213
- declare function choice<T>(array: T[], seed?: number): T;
225
+ declare function choice<T>(array: T[], options?: RndArrayOptions): T;
214
226
  /**
215
227
  * Returns a random item from the given array based on their corresponding weights.
216
228
  * @param array Array of items to choose from.
@@ -250,9 +262,9 @@ declare function float(min: number, max: number, seed?: number): number;
250
262
  /**
251
263
  * Returns a random index from the given array.
252
264
  * @param array The array to generate an index for.
253
- * @param seed Optional seed for RNG.
265
+ * @param options Options for the index function.
254
266
  */
255
- declare function index(array: any[], seed?: number): number;
267
+ declare function index(array: any[], options?: RndArrayOptions): number;
256
268
  /**
257
269
  * Generates a random integer between the given minimum and maximum values.
258
270
  * @param min The minimum value (inclusive) for the random integer.
@@ -732,14 +744,14 @@ declare const qznt: {
732
744
  };
733
745
  rnd: {
734
746
  chance: (percent?: number, seed?: number) => boolean;
735
- choice: <T>(array: T[], seed?: number) => T;
747
+ choice: <T>(array: T[], options?: RndArrayOptions) => T;
736
748
  weighted: <T>(array: T[], selector: (item: T) => number, seed?: number) => T;
737
749
  sampler: <T>(items: T[], selector: (item: T) => number, seed?: number) => {
738
750
  pick: (seed?: number) => T | undefined;
739
751
  };
740
752
  prng: (seed: number) => () => number;
741
753
  float: (min: number, max: number, seed?: number) => number;
742
- index: (array: any[], seed?: number) => number;
754
+ index: (array: any[], options?: RndArrayOptions) => number;
743
755
  int: (min: number, max: number, seed?: number) => number;
744
756
  str: (len: number, mode: "number" | "alpha" | "alphanumeric" | "custom", options?: RndStrOptions) => string;
745
757
  };
@@ -856,14 +868,14 @@ declare const $: {
856
868
  };
857
869
  rnd: {
858
870
  chance: (percent?: number, seed?: number) => boolean;
859
- choice: <T>(array: T[], seed?: number) => T;
871
+ choice: <T>(array: T[], options?: RndArrayOptions) => T;
860
872
  weighted: <T>(array: T[], selector: (item: T) => number, seed?: number) => T;
861
873
  sampler: <T>(items: T[], selector: (item: T) => number, seed?: number) => {
862
874
  pick: (seed?: number) => T | undefined;
863
875
  };
864
876
  prng: (seed: number) => () => number;
865
877
  float: (min: number, max: number, seed?: number) => number;
866
- index: (array: any[], seed?: number) => number;
878
+ index: (array: any[], options?: RndArrayOptions) => number;
867
879
  int: (min: number, max: number, seed?: number) => number;
868
880
  str: (len: number, mode: "number" | "alpha" | "alphanumeric" | "custom", options?: RndStrOptions) => string;
869
881
  };
@@ -962,4 +974,4 @@ declare const $: {
962
974
  };
963
975
  };
964
976
 
965
- export { $, type AlphaCasing, type AnyFunc, Cache, type DateOptions, type DebouncedFunction, type DeepPartial, Loop, type MemoizedFunction, type ParseOptions, Pipe, type ReadDirOptions, type RndStrOptions, type SequentialMapContext, Storage, type ToRecordContext, type TypedEmitter, arr, async, date, qznt as default, fn, format, fs, is, math, obj, rnd, str, timing, to };
977
+ export { $, type AlphaCasing, type AnyFunc, Cache, type DateOptions, type DebouncedFunction, type DeepPartial, Loop, type MemoizedFunction, type ParseOptions, Pipe, type ReadDirOptions, type RndArrayOptions, type RndStrOptions, type SequentialMapContext, Storage, type ToRecordContext, type TypedEmitter, arr, async, date, qznt as default, fn, format, fs, is, math, obj, rnd, str, timing, to };
package/dist/index.d.ts CHANGED
@@ -193,10 +193,22 @@ declare const fs: {
193
193
  };
194
194
 
195
195
  type AlphaCasing = "lower" | "upper" | "mixed";
196
+ interface RndArrayOptions {
197
+ /** Optional seed for RNG. */
198
+ seed?: number;
199
+ /** Reroll if the result is equal to this value. */
200
+ not?: any;
201
+ /** Maximum number of times to reroll if `not` is specified. [default: 10] */
202
+ maxRerolls?: number;
203
+ }
196
204
  interface RndStrOptions {
197
- casing?: AlphaCasing;
205
+ /** Optional seed for RNG. */
198
206
  seed?: number;
207
+ /** Character casing mode. */
208
+ casing?: AlphaCasing;
209
+ /** Custom character pool. */
199
210
  customChars?: string;
211
+ /** Characters to exclude from the pool. */
200
212
  exclude?: string | string[];
201
213
  }
202
214
  /**
@@ -208,9 +220,9 @@ declare function chance(percent?: number, seed?: number): boolean;
208
220
  /**
209
221
  * Returns a random item from the given array.
210
222
  * @param array Array of items to choose from.
211
- * @param seed Optional seed for RNG.
223
+ * @param options Options for the choice function.
212
224
  */
213
- declare function choice<T>(array: T[], seed?: number): T;
225
+ declare function choice<T>(array: T[], options?: RndArrayOptions): T;
214
226
  /**
215
227
  * Returns a random item from the given array based on their corresponding weights.
216
228
  * @param array Array of items to choose from.
@@ -250,9 +262,9 @@ declare function float(min: number, max: number, seed?: number): number;
250
262
  /**
251
263
  * Returns a random index from the given array.
252
264
  * @param array The array to generate an index for.
253
- * @param seed Optional seed for RNG.
265
+ * @param options Options for the index function.
254
266
  */
255
- declare function index(array: any[], seed?: number): number;
267
+ declare function index(array: any[], options?: RndArrayOptions): number;
256
268
  /**
257
269
  * Generates a random integer between the given minimum and maximum values.
258
270
  * @param min The minimum value (inclusive) for the random integer.
@@ -732,14 +744,14 @@ declare const qznt: {
732
744
  };
733
745
  rnd: {
734
746
  chance: (percent?: number, seed?: number) => boolean;
735
- choice: <T>(array: T[], seed?: number) => T;
747
+ choice: <T>(array: T[], options?: RndArrayOptions) => T;
736
748
  weighted: <T>(array: T[], selector: (item: T) => number, seed?: number) => T;
737
749
  sampler: <T>(items: T[], selector: (item: T) => number, seed?: number) => {
738
750
  pick: (seed?: number) => T | undefined;
739
751
  };
740
752
  prng: (seed: number) => () => number;
741
753
  float: (min: number, max: number, seed?: number) => number;
742
- index: (array: any[], seed?: number) => number;
754
+ index: (array: any[], options?: RndArrayOptions) => number;
743
755
  int: (min: number, max: number, seed?: number) => number;
744
756
  str: (len: number, mode: "number" | "alpha" | "alphanumeric" | "custom", options?: RndStrOptions) => string;
745
757
  };
@@ -856,14 +868,14 @@ declare const $: {
856
868
  };
857
869
  rnd: {
858
870
  chance: (percent?: number, seed?: number) => boolean;
859
- choice: <T>(array: T[], seed?: number) => T;
871
+ choice: <T>(array: T[], options?: RndArrayOptions) => T;
860
872
  weighted: <T>(array: T[], selector: (item: T) => number, seed?: number) => T;
861
873
  sampler: <T>(items: T[], selector: (item: T) => number, seed?: number) => {
862
874
  pick: (seed?: number) => T | undefined;
863
875
  };
864
876
  prng: (seed: number) => () => number;
865
877
  float: (min: number, max: number, seed?: number) => number;
866
- index: (array: any[], seed?: number) => number;
878
+ index: (array: any[], options?: RndArrayOptions) => number;
867
879
  int: (min: number, max: number, seed?: number) => number;
868
880
  str: (len: number, mode: "number" | "alpha" | "alphanumeric" | "custom", options?: RndStrOptions) => string;
869
881
  };
@@ -962,4 +974,4 @@ declare const $: {
962
974
  };
963
975
  };
964
976
 
965
- export { $, type AlphaCasing, type AnyFunc, Cache, type DateOptions, type DebouncedFunction, type DeepPartial, Loop, type MemoizedFunction, type ParseOptions, Pipe, type ReadDirOptions, type RndStrOptions, type SequentialMapContext, Storage, type ToRecordContext, type TypedEmitter, arr, async, date, qznt as default, fn, format, fs, is, math, obj, rnd, str, timing, to };
977
+ export { $, type AlphaCasing, type AnyFunc, Cache, type DateOptions, type DebouncedFunction, type DeepPartial, Loop, type MemoizedFunction, type ParseOptions, Pipe, type ReadDirOptions, type RndArrayOptions, type RndStrOptions, type SequentialMapContext, Storage, type ToRecordContext, type TypedEmitter, arr, async, date, qznt as default, fn, format, fs, is, math, obj, rnd, str, timing, to };
package/dist/index.js CHANGED
@@ -24,9 +24,19 @@ function chance(percent2 = 0.5, seed) {
24
24
  const random = seed !== void 0 ? prng(seed) : Math.random;
25
25
  return random() < percent2;
26
26
  }
27
- function choice(array, seed) {
27
+ function choice(array, options = {}) {
28
+ const { seed, not, maxRerolls = 10 } = options;
28
29
  const random = seed !== void 0 ? prng(seed) : Math.random;
29
- return array[Math.floor(random() * array.length)];
30
+ const rnd2 = () => array[Math.floor(random() * array.length)];
31
+ let result = rnd2();
32
+ if (seed !== void 0 && array.length > 1) {
33
+ let rerolls = 0;
34
+ while (not !== void 0 && result === not && rerolls < maxRerolls) {
35
+ result = rnd2();
36
+ rerolls++;
37
+ }
38
+ }
39
+ return result;
30
40
  }
31
41
  function weighted(array, selector, seed) {
32
42
  const random = seed !== void 0 ? prng(seed) : Math.random;
@@ -38,9 +48,9 @@ function weighted(array, selector, seed) {
38
48
  cumulativeWeights.push(currentSum);
39
49
  }
40
50
  const decider = random() * currentSum;
41
- let index2 = cumulativeWeights.findIndex((w) => w >= decider);
51
+ let index2;
42
52
  if (array.length < 20) {
43
- cumulativeWeights.findIndex((w) => w >= decider);
53
+ index2 = cumulativeWeights.findIndex((w) => w >= decider);
44
54
  } else {
45
55
  let low = 0;
46
56
  let high = cumulativeWeights.length - 1;
@@ -101,9 +111,19 @@ function float(min, max, seed) {
101
111
  const random = seed !== void 0 ? prng(seed) : Math.random;
102
112
  return random() * (max - min) + min;
103
113
  }
104
- function index(array, seed) {
114
+ function index(array, options = {}) {
115
+ const { seed, not, maxRerolls = 10 } = options;
105
116
  const random = seed !== void 0 ? prng(seed) : Math.random;
106
- return Math.floor(random() * array.length);
117
+ const rnd2 = () => Math.floor(random() * array.length);
118
+ let result = rnd2();
119
+ if (seed !== void 0 && array.length > 1) {
120
+ let rerolls = 0;
121
+ while (not !== void 0 && result === not && rerolls < maxRerolls) {
122
+ result = rnd2();
123
+ rerolls++;
124
+ }
125
+ }
126
+ return result;
107
127
  }
108
128
  function int(min, max, seed) {
109
129
  const random = seed !== void 0 ? prng(seed) : Math.random;