@salespark/toolkit 2.1.22 → 2.1.23

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/README.md CHANGED
@@ -28,6 +28,7 @@ npm i @salespark/toolkit
28
28
  - **Object utilities**: pick, omit, clean objects, deep merge, etc.
29
29
  - **String utilities**: slugify, template fill, deburr, sanitize, capitalize words/sentences, SMS length.
30
30
  - **Number utilities**: clamp, round, safe arithmetic/comparisons, safe parse (locale-aware), random digits, etc.
31
+ - **Random utilities**: token generation helpers (non-crypto), random IDs, shuffle helpers
31
32
  - **Function utilities**: debounce, throttle, safeJSONParse, formatCurrency, parseName, currency conversions, etc.
32
33
  - **Defer utilities**: post-return microtask scheduling, non-critical timers, after-response hooks.
33
34
  - **Boolean utilities**: safe boolean conversion with common representations
@@ -482,6 +483,58 @@ formatDecimalNumber("invalid", 2); // "0.00"
482
483
  - **`parseToNumber`** → Use `safeParseFloat` instead
483
484
  - **`otp`** → Use `randomDigits` instead
484
485
 
486
+ ### 🎲 Random Utilities
487
+
488
+ **`createToken(options?: { withUppercase?: boolean; withLowercase?: boolean; withNumbers?: boolean; withSymbols?: boolean; length?: number; alphabet?: string }): string`** — Generates a random token string using Math.random (non-crypto) and a configurable alphabet.
489
+
490
+ ```javascript
491
+ createToken(); // 64-char token (letters + numbers)
492
+ createToken({ length: 16, withSymbols: true });
493
+ createToken({ alphabet: "ABCDEF0123456789", length: 24 }); // custom alphabet
494
+ ```
495
+
496
+ **`random(): number`** — Returns a random float in the range [0, 1) using Math.random (non-crypto).
497
+
498
+ ```javascript
499
+ random(); // 0.123456...
500
+ ```
501
+
502
+ **`randFromArray<T>(array: T[]): T`** — Picks a random element from an array.
503
+
504
+ ```javascript
505
+ randFromArray(["a", "b", "c"]); // random element
506
+ ```
507
+
508
+ **`randIntFromInterval(min: number, max: number): number`** — Returns a random integer in [min, max).
509
+
510
+ ```javascript
511
+ randIntFromInterval(10, 20); // 10..19
512
+ ```
513
+
514
+ **`shuffleArray<T>(array: T[]): T[]`** — Returns a shuffled copy of the input array.
515
+
516
+ ```javascript
517
+ shuffleArray([1, 2, 3]); // new shuffled array
518
+ ```
519
+
520
+ **`shuffleArrayMutate<T>(array: T[]): T[]`** — Shuffles an array in place (mutates input).
521
+
522
+ ```javascript
523
+ shuffleArrayMutate([1, 2, 3]); // same array instance, shuffled
524
+ ```
525
+
526
+ **`shuffleString(str: string, delimiter?: string): string`** — Shuffles a string by splitting and rejoining.
527
+
528
+ ```javascript
529
+ shuffleString("abc"); // "bca" (random order)
530
+ ```
531
+
532
+ **`generateRandomId(): string`** — Generates a short id with prefix "id-" and a base36 segment.
533
+
534
+ ```javascript
535
+ generateRandomId(); // "id-5gk9x1c2qz" (random)
536
+ ```
537
+
485
538
  ### ✅ Boolean Utilities
486
539
 
487
540
  **`toBool(value: unknown, def?: boolean): boolean`** — Converts a value to boolean, supporting common string/number representations.
package/dist/index.cjs CHANGED
@@ -2677,6 +2677,43 @@ var smsLength = (text, singleOverrides, multiOverrides) => {
2677
2677
  };
2678
2678
  };
2679
2679
 
2680
+ // src/utils/random.ts
2681
+ var random = () => Math.random();
2682
+ var randFromArray = (array) => array[Math.floor(random() * array.length)];
2683
+ var randIntFromInterval = (min, max) => Math.floor(random() * (max - min) + min);
2684
+ function shuffleArrayMutate(array) {
2685
+ for (let i = array.length - 1; i > 0; i--) {
2686
+ const j = Math.floor(Math.random() * (i + 1));
2687
+ [array[i], array[j]] = [array[j], array[i]];
2688
+ }
2689
+ return array;
2690
+ }
2691
+ var shuffleArray = (array) => shuffleArrayMutate([...array]);
2692
+ var shuffleString = (str, delimiter = "") => shuffleArrayMutate(str.split(delimiter)).join(delimiter);
2693
+ function createToken({
2694
+ withUppercase = true,
2695
+ withLowercase = true,
2696
+ withNumbers = true,
2697
+ withSymbols = false,
2698
+ length = 64,
2699
+ alphabet
2700
+ } = {}) {
2701
+ const allAlphabet = alphabet ?? [
2702
+ withUppercase ? "ABCDEFGHIJKLMOPQRSTUVWXYZ" : "",
2703
+ withLowercase ? "abcdefghijklmopqrstuvwxyz" : "",
2704
+ withNumbers ? "0123456789" : "",
2705
+ withSymbols ? `.,;:!?./-"'#{([-|\\@)]=}*+` : ""
2706
+ ].join("");
2707
+ const safeLength = Number.isFinite(length) ? Math.max(0, Math.floor(length)) : 0;
2708
+ if (!allAlphabet || safeLength === 0) return "";
2709
+ let token = "";
2710
+ for (let i = 0; i < safeLength; i++) {
2711
+ token += allAlphabet[Math.floor(random() * allAlphabet.length)];
2712
+ }
2713
+ return token;
2714
+ }
2715
+ var generateRandomId = () => `id-${random().toString(36).substring(2, 12)}`;
2716
+
2680
2717
  // src/index.ts
2681
2718
  var isBrowser = typeof globalThis !== "undefined" && typeof globalThis.document !== "undefined";
2682
2719
  var isNode = typeof process !== "undefined" && !!process.versions?.node;
@@ -2701,6 +2738,7 @@ exports.chunk = chunk;
2701
2738
  exports.clamp = clamp;
2702
2739
  exports.cleanObject = cleanObject;
2703
2740
  exports.compact = compact;
2741
+ exports.createToken = createToken;
2704
2742
  exports.currencyToSymbol = currencyToSymbol;
2705
2743
  exports.debounce = debounce;
2706
2744
  exports.deburr = deburr;
@@ -2727,6 +2765,7 @@ exports.formatCurrencyPro = formatCurrencyPro;
2727
2765
  exports.formatDecimalNumber = formatDecimalNumber;
2728
2766
  exports.generatePassword = generatePassword;
2729
2767
  exports.generatePasswordWithOptions = generatePasswordWithOptions;
2768
+ exports.generateRandomId = generateRandomId;
2730
2769
  exports.getStringSimilarity = getStringSimilarity;
2731
2770
  exports.groupBy = groupBy;
2732
2771
  exports.hasNilOrEmpty = hasNilOrEmpty;
@@ -2766,6 +2805,9 @@ exports.parseToNumber = parseToNumber;
2766
2805
  exports.pick = pick;
2767
2806
  exports.pluck = pluck;
2768
2807
  exports.pushAll = pushAll;
2808
+ exports.randFromArray = randFromArray;
2809
+ exports.randIntFromInterval = randIntFromInterval;
2810
+ exports.random = random;
2769
2811
  exports.randomDigits = randomDigits;
2770
2812
  exports.removeDiacritics = removeDiacritics;
2771
2813
  exports.round = round;
@@ -2780,6 +2822,9 @@ exports.sanitize = sanitize;
2780
2822
  exports.sanitizeMarkdown = sanitizeMarkdown;
2781
2823
  exports.sentenceCase = sentenceCase;
2782
2824
  exports.shuffle = shuffle;
2825
+ exports.shuffleArray = shuffleArray;
2826
+ exports.shuffleArrayMutate = shuffleArrayMutate;
2827
+ exports.shuffleString = shuffleString;
2783
2828
  exports.slugify = slugify;
2784
2829
  exports.smsLength = smsLength;
2785
2830
  exports.sortBy = sortBy;