@salespark/toolkit 2.1.20 → 2.1.21

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
@@ -26,7 +26,7 @@ npm i @salespark/toolkit
26
26
 
27
27
  - **Array utilities**: chunk, uniqBy, deep equality, flatten, groupBy, etc.
28
28
  - **Object utilities**: pick, omit, clean objects, deep merge, etc.
29
- - **String utilities**: slugify, template fill, deburr, sanitize, capitalize words/sentences.
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
31
  - **Function utilities**: debounce, throttle, safeJSONParse, formatCurrency, parseName, currency conversions, etc.
32
32
  - **Defer utilities**: post-return microtask scheduling, non-critical timers, after-response hooks.
@@ -54,6 +54,7 @@ import {
54
54
  isBrowser,
55
55
  toBool,
56
56
  generatePassword,
57
+ smsLength,
57
58
  uuidv4,
58
59
  } from "@salespark/toolkit";
59
60
 
@@ -80,6 +81,9 @@ const bool = toBool("yes"); // true
80
81
  // Generate a password (sync by default)
81
82
  const recoveryToken = generatePassword(96, false);
82
83
 
84
+ // SMS length and segmentation
85
+ const sms = smsLength("hello");
86
+
83
87
  // Generate UUID v4
84
88
  const id = uuidv4();
85
89
 
@@ -361,6 +365,16 @@ sanitize("<script>alert('hack')</script>Hello World!", 20);
361
365
  // Result: "Hello World!"
362
366
  ```
363
367
 
368
+ **`smsLength(text: string, singleOverrides?, multiOverrides?): SmsLengthResult`** — Calculates SMS encoding, length, and segmentation details (overrides are optional).
369
+
370
+ ```javascript
371
+ const sms = smsLength("hello");
372
+ // Result: { encoding: "GSM_7BIT", length: 5, messages: 1, remaining: 155, ... }
373
+
374
+ const custom = smsLength("hello", { GSM_7BIT: 10 }, { GSM_7BIT: 5 });
375
+ // Result: characterPerMessage uses overrides
376
+ ```
377
+
364
378
  ### 🔢 Number Utilities
365
379
 
366
380
  **`clamp(n: number, min: number, max: number): number`** — Restricts a number to be within the min and max bounds.
@@ -1093,5 +1107,5 @@ MIT © [SalesPark](https://salespark.io)
1093
1107
 
1094
1108
  ---
1095
1109
 
1096
- _Document version: 17_
1110
+ _Document version: 18_
1097
1111
  _Last update: 14-03-2026_
package/dist/index.cjs CHANGED
@@ -2620,6 +2620,62 @@ var generatePasswordWithOptions = (options) => {
2620
2620
  return requiresAsync ? generatePasswordAsync(options) : generatePasswordSync(options);
2621
2621
  };
2622
2622
 
2623
+ // src/utils/sms.ts
2624
+ var GSM_7BIT_REGEXP = /^[@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&'()*+,\-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà]*$/;
2625
+ var GSM_7BIT_EXT_REGEXP = /^[@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&'()*+,\-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà\^{}\\\[\]~|€]*$/;
2626
+ var GSM_7BIT_EXT_CHAR_REGEXP = /[\^{}\\\[\]~|€]/g;
2627
+ var messageLength = {
2628
+ GSM_7BIT: 160,
2629
+ GSM_7BIT_EXT: 160,
2630
+ UTF16: 70
2631
+ };
2632
+ var multiMessageLength = {
2633
+ GSM_7BIT: 153,
2634
+ GSM_7BIT_EXT: 153,
2635
+ UTF16: 67
2636
+ };
2637
+ var detectEncoding = (text) => {
2638
+ if (GSM_7BIT_REGEXP.test(text)) {
2639
+ return "GSM_7BIT";
2640
+ }
2641
+ if (GSM_7BIT_EXT_REGEXP.test(text)) {
2642
+ return "GSM_7BIT_EXT";
2643
+ }
2644
+ return "UTF16";
2645
+ };
2646
+ var smsLength = (text, singleOverrides, multiOverrides) => {
2647
+ const singleLengths = { ...messageLength, ...singleOverrides };
2648
+ const multiLengths = { ...multiMessageLength, ...multiOverrides };
2649
+ if (text.length === 0) {
2650
+ return {
2651
+ encoding: "GSM_7BIT",
2652
+ length: 0,
2653
+ characterPerMessage: singleLengths.GSM_7BIT,
2654
+ inCurrentMessage: 0,
2655
+ remaining: singleLengths.GSM_7BIT,
2656
+ messages: 0
2657
+ };
2658
+ }
2659
+ const encoding = detectEncoding(text);
2660
+ const extCharCount = encoding === "GSM_7BIT_EXT" ? (text.match(GSM_7BIT_EXT_CHAR_REGEXP) ?? []).length : 0;
2661
+ const length = text.length + extCharCount;
2662
+ let characterPerMessage = singleLengths[encoding];
2663
+ if (length > characterPerMessage) {
2664
+ characterPerMessage = multiLengths[encoding];
2665
+ }
2666
+ const messages = Math.ceil(length / characterPerMessage);
2667
+ const inCurrentMessage = length - characterPerMessage * (messages - 1);
2668
+ const remaining = characterPerMessage * messages - length;
2669
+ return {
2670
+ encoding,
2671
+ length,
2672
+ characterPerMessage,
2673
+ inCurrentMessage,
2674
+ remaining,
2675
+ messages
2676
+ };
2677
+ };
2678
+
2623
2679
  // src/index.ts
2624
2680
  var isBrowser = typeof globalThis !== "undefined" && typeof globalThis.document !== "undefined";
2625
2681
  var isNode = typeof process !== "undefined" && !!process.versions?.node;
@@ -2628,6 +2684,9 @@ Object.defineProperty(exports, "uuidv4", {
2628
2684
  enumerable: true,
2629
2685
  get: function () { return uuid.v4; }
2630
2686
  });
2687
+ exports.GSM_7BIT_EXT_CHAR_REGEXP = GSM_7BIT_EXT_CHAR_REGEXP;
2688
+ exports.GSM_7BIT_EXT_REGEXP = GSM_7BIT_EXT_REGEXP;
2689
+ exports.GSM_7BIT_REGEXP = GSM_7BIT_REGEXP;
2631
2690
  exports.addSpaceBetweenNumbers = addSpaceBetweenNumbers;
2632
2691
  exports.addThousandsSpace = addThousandsSpace;
2633
2692
  exports.areArraysDeepEqualUnordered = areArraysDeepEqualUnordered;
@@ -2721,6 +2780,7 @@ exports.sanitizeMarkdown = sanitizeMarkdown;
2721
2780
  exports.sentenceCase = sentenceCase;
2722
2781
  exports.shuffle = shuffle;
2723
2782
  exports.slugify = slugify;
2783
+ exports.smsLength = smsLength;
2724
2784
  exports.sortBy = sortBy;
2725
2785
  exports.stringSimilarity = stringSimilarity;
2726
2786
  exports.symbolToCurrency = symbolToCurrency;