qznt 1.0.1 → 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.ts CHANGED
@@ -178,11 +178,37 @@ declare const date: {
178
178
  parse: typeof parse;
179
179
  };
180
180
 
181
+ interface ReadDirOptions {
182
+ /** Whether to scan subdirectories. [default: true] */
183
+ recursive?: boolean;
184
+ }
185
+ /**
186
+ * Recursively (or shallowly) scans a directory and returns an array of relative file paths.
187
+ * @param path The path to the directory.
188
+ * @param options Options for the readDir function.
189
+ */
190
+ declare function readDir(path: string, options?: ReadDirOptions): string[];
191
+ declare const fs: {
192
+ readDir: typeof readDir;
193
+ };
194
+
181
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
+ }
182
204
  interface RndStrOptions {
183
- casing?: AlphaCasing;
205
+ /** Optional seed for RNG. */
184
206
  seed?: number;
207
+ /** Character casing mode. */
208
+ casing?: AlphaCasing;
209
+ /** Custom character pool. */
185
210
  customChars?: string;
211
+ /** Characters to exclude from the pool. */
186
212
  exclude?: string | string[];
187
213
  }
188
214
  /**
@@ -194,9 +220,9 @@ declare function chance(percent?: number, seed?: number): boolean;
194
220
  /**
195
221
  * Returns a random item from the given array.
196
222
  * @param array Array of items to choose from.
197
- * @param seed Optional seed for RNG.
223
+ * @param options Options for the choice function.
198
224
  */
199
- declare function choice<T>(array: T[], seed?: number): T;
225
+ declare function choice<T>(array: T[], options?: RndArrayOptions): T;
200
226
  /**
201
227
  * Returns a random item from the given array based on their corresponding weights.
202
228
  * @param array Array of items to choose from.
@@ -236,9 +262,9 @@ declare function float(min: number, max: number, seed?: number): number;
236
262
  /**
237
263
  * Returns a random index from the given array.
238
264
  * @param array The array to generate an index for.
239
- * @param seed Optional seed for RNG.
265
+ * @param options Options for the index function.
240
266
  */
241
- declare function index(array: any[], seed?: number): number;
267
+ declare function index(array: any[], options?: RndArrayOptions): number;
242
268
  /**
243
269
  * Generates a random integer between the given minimum and maximum values.
244
270
  * @param min The minimum value (inclusive) for the random integer.
@@ -607,7 +633,7 @@ declare function secs(num: number): number;
607
633
  * @param selector Function that selects the item's weight.
608
634
  * @param ignoreNaN If true, NaN values will not throw an error.
609
635
  */
610
- declare function sum<T>(array: T[], selector: (item: T) => number, ignoreNaN?: boolean): number;
636
+ declare function sum<T>(array: T[], selector?: (item: T) => number): number;
611
637
  declare const math: {
612
638
  clamp: typeof clamp;
613
639
  invLerp: typeof invLerp;
@@ -718,14 +744,14 @@ declare const qznt: {
718
744
  };
719
745
  rnd: {
720
746
  chance: (percent?: number, seed?: number) => boolean;
721
- choice: <T>(array: T[], seed?: number) => T;
747
+ choice: <T>(array: T[], options?: RndArrayOptions) => T;
722
748
  weighted: <T>(array: T[], selector: (item: T) => number, seed?: number) => T;
723
749
  sampler: <T>(items: T[], selector: (item: T) => number, seed?: number) => {
724
750
  pick: (seed?: number) => T | undefined;
725
751
  };
726
752
  prng: (seed: number) => () => number;
727
753
  float: (min: number, max: number, seed?: number) => number;
728
- index: (array: any[], seed?: number) => number;
754
+ index: (array: any[], options?: RndArrayOptions) => number;
729
755
  int: (min: number, max: number, seed?: number) => number;
730
756
  str: (len: number, mode: "number" | "alpha" | "alphanumeric" | "custom", options?: RndStrOptions) => string;
731
757
  };
@@ -759,7 +785,7 @@ declare const qznt: {
759
785
  percent: (a: number, b: number, round?: boolean) => number;
760
786
  remap: (value: number, inMin: number, inMax: number, outMin: number, outMax: number) => number;
761
787
  secs: (num: number) => number;
762
- sum: <T>(array: T[], selector: (item: T) => number, ignoreNaN?: boolean) => number;
788
+ sum: <T>(array: T[], selector?: (item: T) => number) => number;
763
789
  };
764
790
  is: {
765
791
  defined: <T>(val: T | undefined | null) => val is T;
@@ -773,6 +799,9 @@ declare const qznt: {
773
799
  string: (val: unknown) => val is string;
774
800
  today: (date: number | Date) => boolean;
775
801
  };
802
+ fs: {
803
+ readDir: (path: string, options?: ReadDirOptions) => string[];
804
+ };
776
805
  format: {
777
806
  currency: (num: number, options?: {
778
807
  currency?: string;
@@ -839,14 +868,14 @@ declare const $: {
839
868
  };
840
869
  rnd: {
841
870
  chance: (percent?: number, seed?: number) => boolean;
842
- choice: <T>(array: T[], seed?: number) => T;
871
+ choice: <T>(array: T[], options?: RndArrayOptions) => T;
843
872
  weighted: <T>(array: T[], selector: (item: T) => number, seed?: number) => T;
844
873
  sampler: <T>(items: T[], selector: (item: T) => number, seed?: number) => {
845
874
  pick: (seed?: number) => T | undefined;
846
875
  };
847
876
  prng: (seed: number) => () => number;
848
877
  float: (min: number, max: number, seed?: number) => number;
849
- index: (array: any[], seed?: number) => number;
878
+ index: (array: any[], options?: RndArrayOptions) => number;
850
879
  int: (min: number, max: number, seed?: number) => number;
851
880
  str: (len: number, mode: "number" | "alpha" | "alphanumeric" | "custom", options?: RndStrOptions) => string;
852
881
  };
@@ -880,7 +909,7 @@ declare const $: {
880
909
  percent: (a: number, b: number, round?: boolean) => number;
881
910
  remap: (value: number, inMin: number, inMax: number, outMin: number, outMax: number) => number;
882
911
  secs: (num: number) => number;
883
- sum: <T>(array: T[], selector: (item: T) => number, ignoreNaN?: boolean) => number;
912
+ sum: <T>(array: T[], selector?: (item: T) => number) => number;
884
913
  };
885
914
  is: {
886
915
  defined: <T>(val: T | undefined | null) => val is T;
@@ -894,6 +923,9 @@ declare const $: {
894
923
  string: (val: unknown) => val is string;
895
924
  today: (date: number | Date) => boolean;
896
925
  };
926
+ fs: {
927
+ readDir: (path: string, options?: ReadDirOptions) => string[];
928
+ };
897
929
  format: {
898
930
  currency: (num: number, options?: {
899
931
  currency?: string;
@@ -942,4 +974,4 @@ declare const $: {
942
974
  };
943
975
  };
944
976
 
945
- export { $, type AlphaCasing, type AnyFunc, Cache, type DateOptions, type DebouncedFunction, type DeepPartial, Loop, type MemoizedFunction, type ParseOptions, Pipe, type RndStrOptions, type SequentialMapContext, Storage, type ToRecordContext, type TypedEmitter, arr, async, date, qznt as default, fn, format, 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;
@@ -463,6 +483,41 @@ var format = {
463
483
  compactNumber
464
484
  };
465
485
 
486
+ // src/fs.ts
487
+ var fs_exports = {};
488
+ __export(fs_exports, {
489
+ fs: () => fs
490
+ });
491
+ import _fs from "fs";
492
+ import { join } from "path";
493
+ function readDir(path2, options = {}) {
494
+ const { recursive = true } = options;
495
+ if (!_fs.existsSync(path2)) return [];
496
+ if (!recursive) {
497
+ return _fs.readdirSync(path2).filter((fn2) => {
498
+ return _fs.statSync(join(path2, fn2)).isFile();
499
+ });
500
+ }
501
+ const walk = (dir, base = "") => {
502
+ const results = [];
503
+ const entries = _fs.readdirSync(dir, { withFileTypes: true });
504
+ for (const entry of entries) {
505
+ const relativePath = base ? join(base, entry.name) : entry.name;
506
+ const fullPath = join(dir, entry.name);
507
+ if (entry.isDirectory()) {
508
+ results.push(...walk(fullPath, relativePath));
509
+ } else if (entry.isFile()) {
510
+ results.push(relativePath);
511
+ }
512
+ }
513
+ return results;
514
+ };
515
+ return walk(path2);
516
+ }
517
+ var fs = {
518
+ readDir
519
+ };
520
+
466
521
  // src/is.ts
467
522
  var is_exports = {};
468
523
  __export(is_exports, {
@@ -543,10 +598,12 @@ function remap(value, inMin, inMax, outMin, outMax) {
543
598
  function secs(num) {
544
599
  return Math.floor(num / 1e3);
545
600
  }
546
- function sum(array, selector, ignoreNaN) {
547
- return array.map(selector).reduce((a, b) => {
548
- const invalid = isNaN(b) && !ignoreNaN;
549
- if (invalid) throw new TypeError(`sum: '${b}' is not a valid number.`);
601
+ function sum(array, selector) {
602
+ const _array = selector ? array.map(selector) : array;
603
+ if (!_array.every((v) => typeof v === "number")) {
604
+ throw new TypeError(`sum: Array must only contain numbers.`);
605
+ }
606
+ return _array.reduce((a, b) => {
550
607
  return (isNaN(b) ? 0 : b) < 0 ? a - -b : a + (b || 0);
551
608
  }, 0);
552
609
  }
@@ -774,16 +831,16 @@ function Pipe(value, ...fns) {
774
831
  }
775
832
 
776
833
  // src/Storage.ts
777
- import fs from "fs";
834
+ import fs2 from "fs";
778
835
  import path from "path";
779
836
  var Storage = class {
780
837
  isNode = typeof window === "undefined";
781
838
  filePath = null;
782
839
  memoryCache = /* @__PURE__ */ new Map();
783
840
  loadFromFile() {
784
- if (this.filePath && fs.existsSync(this.filePath)) {
841
+ if (this.filePath && fs2.existsSync(this.filePath)) {
785
842
  try {
786
- const data = JSON.parse(fs.readFileSync(this.filePath, "utf-8"));
843
+ const data = JSON.parse(fs2.readFileSync(this.filePath, "utf-8"));
787
844
  Object.entries(data).forEach(([k, v]) => this.memoryCache.set(k, JSON.stringify(v)));
788
845
  } catch (err) {
789
846
  console.error(`Store: Failed to load file '${this.filePath}'`, err);
@@ -794,7 +851,7 @@ var Storage = class {
794
851
  if (this.isNode && this.filePath) {
795
852
  const out = {};
796
853
  this.memoryCache.forEach((v, k) => out[k] = JSON.parse(v));
797
- fs.writeFileSync(this.filePath, JSON.stringify(out, null, 2));
854
+ fs2.writeFileSync(this.filePath, JSON.stringify(out, null, 2));
798
855
  }
799
856
  }
800
857
  /**
@@ -1012,6 +1069,7 @@ var qznt = {
1012
1069
  ...fn_exports,
1013
1070
  ...date_exports,
1014
1071
  ...format_exports,
1072
+ ...fs_exports,
1015
1073
  ...is_exports,
1016
1074
  ...math_exports,
1017
1075
  ...obj_exports,
@@ -1038,6 +1096,7 @@ export {
1038
1096
  index_default as default,
1039
1097
  fn,
1040
1098
  format,
1099
+ fs,
1041
1100
  is,
1042
1101
  math,
1043
1102
  obj,