@salespark/toolkit 2.1.21 → 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.
@@ -883,9 +936,16 @@ assessSecurityRisks([]);
883
936
  ```typescript
884
937
  import { generatePassword } from "@salespark/toolkit";
885
938
 
886
- // Sync usage (current default behavior)
939
+ // Sync usage (positional signature skips security recommendations)
887
940
  const recoveryToken = generatePassword(96, false);
888
941
 
942
+ // Enforce security recommendations by using options
943
+ const strict = generatePassword({
944
+ length: 12,
945
+ memorable: true,
946
+ ignoreSecurityRecommendations: false,
947
+ });
948
+
889
949
  // Memorable password (override security recommendations if needed)
890
950
  const memorable = generatePassword({
891
951
  length: 12,
package/dist/index.cjs CHANGED
@@ -2613,6 +2613,7 @@ function generatePassword(lengthOrOptions, memorable, pattern, prefix) {
2613
2613
  if (memorable !== void 0) options.memorable = memorable;
2614
2614
  if (pattern !== void 0) options.pattern = pattern;
2615
2615
  if (prefix !== void 0) options.prefix = prefix;
2616
+ options.ignoreSecurityRecommendations = true;
2616
2617
  return generatePasswordSync(options);
2617
2618
  }
2618
2619
  var generatePasswordWithOptions = (options) => {
@@ -2676,6 +2677,43 @@ var smsLength = (text, singleOverrides, multiOverrides) => {
2676
2677
  };
2677
2678
  };
2678
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
+
2679
2717
  // src/index.ts
2680
2718
  var isBrowser = typeof globalThis !== "undefined" && typeof globalThis.document !== "undefined";
2681
2719
  var isNode = typeof process !== "undefined" && !!process.versions?.node;
@@ -2700,6 +2738,7 @@ exports.chunk = chunk;
2700
2738
  exports.clamp = clamp;
2701
2739
  exports.cleanObject = cleanObject;
2702
2740
  exports.compact = compact;
2741
+ exports.createToken = createToken;
2703
2742
  exports.currencyToSymbol = currencyToSymbol;
2704
2743
  exports.debounce = debounce;
2705
2744
  exports.deburr = deburr;
@@ -2726,6 +2765,7 @@ exports.formatCurrencyPro = formatCurrencyPro;
2726
2765
  exports.formatDecimalNumber = formatDecimalNumber;
2727
2766
  exports.generatePassword = generatePassword;
2728
2767
  exports.generatePasswordWithOptions = generatePasswordWithOptions;
2768
+ exports.generateRandomId = generateRandomId;
2729
2769
  exports.getStringSimilarity = getStringSimilarity;
2730
2770
  exports.groupBy = groupBy;
2731
2771
  exports.hasNilOrEmpty = hasNilOrEmpty;
@@ -2765,6 +2805,9 @@ exports.parseToNumber = parseToNumber;
2765
2805
  exports.pick = pick;
2766
2806
  exports.pluck = pluck;
2767
2807
  exports.pushAll = pushAll;
2808
+ exports.randFromArray = randFromArray;
2809
+ exports.randIntFromInterval = randIntFromInterval;
2810
+ exports.random = random;
2768
2811
  exports.randomDigits = randomDigits;
2769
2812
  exports.removeDiacritics = removeDiacritics;
2770
2813
  exports.round = round;
@@ -2779,6 +2822,9 @@ exports.sanitize = sanitize;
2779
2822
  exports.sanitizeMarkdown = sanitizeMarkdown;
2780
2823
  exports.sentenceCase = sentenceCase;
2781
2824
  exports.shuffle = shuffle;
2825
+ exports.shuffleArray = shuffleArray;
2826
+ exports.shuffleArrayMutate = shuffleArrayMutate;
2827
+ exports.shuffleString = shuffleString;
2782
2828
  exports.slugify = slugify;
2783
2829
  exports.smsLength = smsLength;
2784
2830
  exports.sortBy = sortBy;