@salespark/toolkit 2.1.12 → 2.1.14

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
@@ -584,14 +584,17 @@ isNilOrNaN("abc");
584
584
  // Result: true (coerced to NaN)
585
585
  ```
586
586
 
587
- **`formatBytes(bytes: number, si?: boolean, dp?: number): string`** — Formats bytes as human-readable text.
587
+ **`formatBytes(bytes: number, si?: boolean, dp?: number, noSpace?: boolean): string`** — Formats bytes as human-readable text.
588
588
 
589
589
  ```javascript
590
+ formatBytes(999, true);
591
+ // Result: "999 Bytes"
592
+
590
593
  formatBytes(1024);
591
594
  // Result: "1.0 KiB"
592
595
 
593
- formatBytes(1000, true);
594
- // Result: "1.0 kB"
596
+ formatBytes(1000, true, 1, true);
597
+ // Result: "1.0kB"
595
598
  ```
596
599
 
597
600
  **`stringSimilarity(s1: string, s2: string): number`** — Returns the similarity between two strings (0..1).
package/dist/index.cjs CHANGED
@@ -553,11 +553,11 @@ var isNullOrUndefinedInArray = hasNilOrEmpty;
553
553
  var isNullOrUndefinedEmptyOrZero = isNilEmptyOrZeroLen;
554
554
  var isNullUndefinedOrZero = isNilOrZeroLen;
555
555
  var isNullOrUndefinedOrNaN = isNilOrNaN;
556
- var formatBytes = (bytes, si = false, dp = 1) => {
556
+ var formatBytes = (bytes, si = false, dp = 1, noSpace = false) => {
557
557
  if (!Number.isFinite(bytes)) return "NaN";
558
558
  const thresh = si ? 1e3 : 1024;
559
559
  const abs = Math.abs(bytes);
560
- if (abs < thresh) return `${bytes} B`;
560
+ if (abs < thresh) return `${bytes} Bytes`;
561
561
  const units = si ? ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] : ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
562
562
  let u = -1;
563
563
  const r = 10 ** dp;
@@ -566,7 +566,7 @@ var formatBytes = (bytes, si = false, dp = 1) => {
566
566
  value /= thresh;
567
567
  ++u;
568
568
  } while (Math.round(Math.abs(value) * r) / r >= thresh && u < units.length - 1);
569
- return `${value.toFixed(dp)} ${units[u]}`;
569
+ return `${value.toFixed(dp)}${noSpace ? "" : " "}${units[u]}`;
570
570
  };
571
571
  var humanFileSize = formatBytes;
572
572
  var levenshtein = (a, b) => {
@@ -1745,6 +1745,90 @@ var assessSecurityRisks = (risks) => {
1745
1745
  return { score, level, recommendations };
1746
1746
  };
1747
1747
 
1748
+ // src/utils/defer.ts
1749
+ var swallow = (p) => p.catch(() => {
1750
+ });
1751
+ var deferPostReturn = (fn) => {
1752
+ if (typeof fn !== "function") return;
1753
+ const run = () => {
1754
+ try {
1755
+ const result = fn();
1756
+ swallow(Promise.resolve(result));
1757
+ } catch {
1758
+ }
1759
+ };
1760
+ if (typeof queueMicrotask === "function") {
1761
+ queueMicrotask(run);
1762
+ } else {
1763
+ Promise.resolve().then(run);
1764
+ }
1765
+ };
1766
+ var deferNonCritical = (fn) => {
1767
+ if (typeof fn !== "function") return;
1768
+ const run = () => {
1769
+ try {
1770
+ const result = fn();
1771
+ swallow(Promise.resolve(result));
1772
+ } catch {
1773
+ }
1774
+ };
1775
+ if (typeof setImmediate === "function") {
1776
+ setImmediate(run);
1777
+ } else {
1778
+ setTimeout(run, 0);
1779
+ }
1780
+ };
1781
+ var deferAfterResponse = (res, fn) => {
1782
+ if (typeof fn !== "function") return;
1783
+ if (res?.writableEnded === true) {
1784
+ deferPostReturn(fn);
1785
+ return;
1786
+ }
1787
+ if (!res || typeof res.once !== "function") {
1788
+ deferPostReturn(fn);
1789
+ return;
1790
+ }
1791
+ let executed = false;
1792
+ const executeOnce = () => {
1793
+ if (executed) return;
1794
+ executed = true;
1795
+ deferPostReturn(fn);
1796
+ };
1797
+ try {
1798
+ res.once("finish", executeOnce);
1799
+ res.once("close", executeOnce);
1800
+ } catch {
1801
+ if (!executed) {
1802
+ deferPostReturn(fn);
1803
+ }
1804
+ }
1805
+ };
1806
+ var deferAfterResponseNonCritical = (res, fn) => {
1807
+ if (typeof fn !== "function") return;
1808
+ if (res?.writableEnded === true) {
1809
+ deferNonCritical(fn);
1810
+ return;
1811
+ }
1812
+ if (!res || typeof res.once !== "function") {
1813
+ deferNonCritical(fn);
1814
+ return;
1815
+ }
1816
+ let executed = false;
1817
+ const executeOnce = () => {
1818
+ if (executed) return;
1819
+ executed = true;
1820
+ deferNonCritical(fn);
1821
+ };
1822
+ try {
1823
+ res.once("finish", executeOnce);
1824
+ res.once("close", executeOnce);
1825
+ } catch {
1826
+ if (!executed) {
1827
+ deferNonCritical(fn);
1828
+ }
1829
+ }
1830
+ };
1831
+
1748
1832
  // src/index.ts
1749
1833
  var isBrowser = typeof globalThis !== "undefined" && typeof globalThis.document !== "undefined";
1750
1834
  var isNode = typeof process !== "undefined" && !!process.versions?.node;
@@ -1765,6 +1849,10 @@ exports.compact = compact;
1765
1849
  exports.currencyToSymbol = currencyToSymbol;
1766
1850
  exports.debounce = debounce;
1767
1851
  exports.deburr = deburr;
1852
+ exports.deferAfterResponse = deferAfterResponse;
1853
+ exports.deferAfterResponseNonCritical = deferAfterResponseNonCritical;
1854
+ exports.deferNonCritical = deferNonCritical;
1855
+ exports.deferPostReturn = deferPostReturn;
1768
1856
  exports.delay = delay;
1769
1857
  exports.difference = difference;
1770
1858
  exports.fill = fill;