@salespark/toolkit 2.1.11 → 2.1.13

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.cts CHANGED
@@ -437,7 +437,7 @@ declare const toInteger: typeof safeParseInt;
437
437
  * Handles commas as decimal/thousands separators. Returns 0 for null/undefined/empty string or invalid parsing.
438
438
  * Examples: safeParseFloat("123.45") -> 123.45, safeParseFloat("123,45") -> 123.45, safeParseFloat("1,234.56") -> 1234.56, safeParseFloat("abc", 2) -> 0, safeParseFloat(42) -> 42
439
439
  * @param {unknown} value - Value to convert
440
- * @param {number} decimals - Number of decimal places
440
+ * @param {number} decimals - Number of decimal places (default 6)
441
441
  * History:
442
442
  * 21-08-2025: Created
443
443
  * 29-10-2025: Renamed from toNumber to safeParseFloat
@@ -828,8 +828,69 @@ declare const assessSecurityRisks: (risks: SecurityRisk[]) => {
828
828
  recommendations: string[];
829
829
  };
830
830
 
831
+ type DeferFn = () => void | Promise<void>;
832
+ /******************************************************************
833
+ * ##: Defer work to run right after return using a microtask
834
+ * Schedules work after the current call stack, using queueMicrotask or
835
+ * Promise.then, and swallows sync/async errors to avoid impacting callers.
836
+ *
837
+ * TL;DR: Runs post-return work ASAP without blocking the caller.
838
+ * Best for small but important tasks that must run soon.
839
+ * @param {DeferFn} fn - Function to execute after the current stack completes
840
+ * @returns {void} - Does not return a value
841
+ * History:
842
+ * 09-01-2026: Created
843
+ ******************************************************************/
844
+ declare const deferPostReturn: (fn: DeferFn) => void;
845
+ /******************************************************************
846
+ * ##: Defer non-critical work with low priority scheduling
847
+ * Schedules work after more urgent tasks, using setImmediate when available
848
+ * (or setTimeout), and swallows sync/async errors to keep execution safe.
849
+ *
850
+ * TL;DR: Runs low-priority work as late as possible.
851
+ * Ideal for logs, metrics, and cleanup that must not compete with requests.
852
+ * @param {DeferFn} fn - Function to execute with low priority scheduling
853
+ * @returns {void} - Does not return a value
854
+ * History:
855
+ * 09-01-2026: Created
856
+ ******************************************************************/
857
+ declare const deferNonCritical: (fn: DeferFn) => void;
858
+ type HttpResponseLike = {
859
+ once: (event: "finish" | "close", listener: (...args: any[]) => void) => void;
860
+ writableEnded?: boolean;
861
+ };
862
+ /******************************************************************
863
+ * ##: Defer work until after the HTTP response finishes
864
+ * Runs work only after the response is completed (finish/close), ensures it
865
+ * executes once, and schedules via microtask to avoid impacting response time.
866
+ *
867
+ * TL;DR: Runs work after sending the response, exactly once.
868
+ * Use for post-response tasks like logging, metrics, or async side effects.
869
+ * @param {HttpResponseLike} res - Response-like object that emits finish/close events
870
+ * @param {DeferFn} fn - Function to execute after the response has completed
871
+ * @returns {void} - Does not return a value
872
+ * History:
873
+ * 09-01-2026: Created
874
+ ******************************************************************/
875
+ declare const deferAfterResponse: (res: HttpResponseLike, fn: DeferFn) => void;
876
+ /******************************************************************
877
+ * ##: Defer low-priority work until after the HTTP response finishes
878
+ * Runs work only after the response is completed (finish/close), ensures it
879
+ * executes once, and schedules with low priority to avoid competing with traffic.
880
+ *
881
+ * TL;DR: Runs low-priority work after sending the response, exactly once.
882
+ * Best for non-urgent metrics, logs, and cleanup after request completion.
883
+ * @param {HttpResponseLike} res - Response-like object that emits finish/close events
884
+ * @param {DeferFn} fn - Function to execute after the response has completed
885
+ * @returns {void} - Does not return a value
886
+ * History:
887
+ * 09-01-2025: Created
888
+ * 09-01-2025: Fixed duplicate execution bug (finish + close both firing)
889
+ ******************************************************************/
890
+ declare const deferAfterResponseNonCritical: (res: HttpResponseLike, fn: DeferFn) => void;
891
+
831
892
  /** Environment helpers (runtime-agnostic checks) */
832
893
  declare const isBrowser: boolean;
833
894
  declare const isNode: boolean;
834
895
 
835
- export { type CapitalizeFirstOptions, type CapitalizeWordsOptions, type SecurityCheckResult, type SecurityRisk, type SentenceCaseOptions, addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, capitalizeFirst, capitalizeWords, checkMarkdownSecurity, chunk, clamp, cleanObject, compact, currencyToSymbol, debounce, deburr, delay, difference, fill, flatten, flattenDepth, flattenDepthBase, flattenOnce, formatBytes, formatCurrency, formatDecimalNumber, getStringSimilarity, groupBy, hasNilOrEmpty, humanFileSize, intersection, isBrowser, isFlattenable, isNil, isNilEmptyOrZeroLen, isNilEmptyOrZeroLength, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilOrZeroLength, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, numbersEqual, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeAdd, safeDivide, safeJSONParse, safeMultiply, safeParseFloat, safeParseInt, safeSubtract, sanitize, sanitizeMarkdown, sentenceCase, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
896
+ export { type CapitalizeFirstOptions, type CapitalizeWordsOptions, type DeferFn, type HttpResponseLike, type SecurityCheckResult, type SecurityRisk, type SentenceCaseOptions, addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, capitalizeFirst, capitalizeWords, checkMarkdownSecurity, chunk, clamp, cleanObject, compact, currencyToSymbol, debounce, deburr, deferAfterResponse, deferAfterResponseNonCritical, deferNonCritical, deferPostReturn, delay, difference, fill, flatten, flattenDepth, flattenDepthBase, flattenOnce, formatBytes, formatCurrency, formatDecimalNumber, getStringSimilarity, groupBy, hasNilOrEmpty, humanFileSize, intersection, isBrowser, isFlattenable, isNil, isNilEmptyOrZeroLen, isNilEmptyOrZeroLength, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilOrZeroLength, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, numbersEqual, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeAdd, safeDivide, safeJSONParse, safeMultiply, safeParseFloat, safeParseInt, safeSubtract, sanitize, sanitizeMarkdown, sentenceCase, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
package/dist/index.d.ts CHANGED
@@ -437,7 +437,7 @@ declare const toInteger: typeof safeParseInt;
437
437
  * Handles commas as decimal/thousands separators. Returns 0 for null/undefined/empty string or invalid parsing.
438
438
  * Examples: safeParseFloat("123.45") -> 123.45, safeParseFloat("123,45") -> 123.45, safeParseFloat("1,234.56") -> 1234.56, safeParseFloat("abc", 2) -> 0, safeParseFloat(42) -> 42
439
439
  * @param {unknown} value - Value to convert
440
- * @param {number} decimals - Number of decimal places
440
+ * @param {number} decimals - Number of decimal places (default 6)
441
441
  * History:
442
442
  * 21-08-2025: Created
443
443
  * 29-10-2025: Renamed from toNumber to safeParseFloat
@@ -828,8 +828,69 @@ declare const assessSecurityRisks: (risks: SecurityRisk[]) => {
828
828
  recommendations: string[];
829
829
  };
830
830
 
831
+ type DeferFn = () => void | Promise<void>;
832
+ /******************************************************************
833
+ * ##: Defer work to run right after return using a microtask
834
+ * Schedules work after the current call stack, using queueMicrotask or
835
+ * Promise.then, and swallows sync/async errors to avoid impacting callers.
836
+ *
837
+ * TL;DR: Runs post-return work ASAP without blocking the caller.
838
+ * Best for small but important tasks that must run soon.
839
+ * @param {DeferFn} fn - Function to execute after the current stack completes
840
+ * @returns {void} - Does not return a value
841
+ * History:
842
+ * 09-01-2026: Created
843
+ ******************************************************************/
844
+ declare const deferPostReturn: (fn: DeferFn) => void;
845
+ /******************************************************************
846
+ * ##: Defer non-critical work with low priority scheduling
847
+ * Schedules work after more urgent tasks, using setImmediate when available
848
+ * (or setTimeout), and swallows sync/async errors to keep execution safe.
849
+ *
850
+ * TL;DR: Runs low-priority work as late as possible.
851
+ * Ideal for logs, metrics, and cleanup that must not compete with requests.
852
+ * @param {DeferFn} fn - Function to execute with low priority scheduling
853
+ * @returns {void} - Does not return a value
854
+ * History:
855
+ * 09-01-2026: Created
856
+ ******************************************************************/
857
+ declare const deferNonCritical: (fn: DeferFn) => void;
858
+ type HttpResponseLike = {
859
+ once: (event: "finish" | "close", listener: (...args: any[]) => void) => void;
860
+ writableEnded?: boolean;
861
+ };
862
+ /******************************************************************
863
+ * ##: Defer work until after the HTTP response finishes
864
+ * Runs work only after the response is completed (finish/close), ensures it
865
+ * executes once, and schedules via microtask to avoid impacting response time.
866
+ *
867
+ * TL;DR: Runs work after sending the response, exactly once.
868
+ * Use for post-response tasks like logging, metrics, or async side effects.
869
+ * @param {HttpResponseLike} res - Response-like object that emits finish/close events
870
+ * @param {DeferFn} fn - Function to execute after the response has completed
871
+ * @returns {void} - Does not return a value
872
+ * History:
873
+ * 09-01-2026: Created
874
+ ******************************************************************/
875
+ declare const deferAfterResponse: (res: HttpResponseLike, fn: DeferFn) => void;
876
+ /******************************************************************
877
+ * ##: Defer low-priority work until after the HTTP response finishes
878
+ * Runs work only after the response is completed (finish/close), ensures it
879
+ * executes once, and schedules with low priority to avoid competing with traffic.
880
+ *
881
+ * TL;DR: Runs low-priority work after sending the response, exactly once.
882
+ * Best for non-urgent metrics, logs, and cleanup after request completion.
883
+ * @param {HttpResponseLike} res - Response-like object that emits finish/close events
884
+ * @param {DeferFn} fn - Function to execute after the response has completed
885
+ * @returns {void} - Does not return a value
886
+ * History:
887
+ * 09-01-2025: Created
888
+ * 09-01-2025: Fixed duplicate execution bug (finish + close both firing)
889
+ ******************************************************************/
890
+ declare const deferAfterResponseNonCritical: (res: HttpResponseLike, fn: DeferFn) => void;
891
+
831
892
  /** Environment helpers (runtime-agnostic checks) */
832
893
  declare const isBrowser: boolean;
833
894
  declare const isNode: boolean;
834
895
 
835
- export { type CapitalizeFirstOptions, type CapitalizeWordsOptions, type SecurityCheckResult, type SecurityRisk, type SentenceCaseOptions, addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, capitalizeFirst, capitalizeWords, checkMarkdownSecurity, chunk, clamp, cleanObject, compact, currencyToSymbol, debounce, deburr, delay, difference, fill, flatten, flattenDepth, flattenDepthBase, flattenOnce, formatBytes, formatCurrency, formatDecimalNumber, getStringSimilarity, groupBy, hasNilOrEmpty, humanFileSize, intersection, isBrowser, isFlattenable, isNil, isNilEmptyOrZeroLen, isNilEmptyOrZeroLength, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilOrZeroLength, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, numbersEqual, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeAdd, safeDivide, safeJSONParse, safeMultiply, safeParseFloat, safeParseInt, safeSubtract, sanitize, sanitizeMarkdown, sentenceCase, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
896
+ export { type CapitalizeFirstOptions, type CapitalizeWordsOptions, type DeferFn, type HttpResponseLike, type SecurityCheckResult, type SecurityRisk, type SentenceCaseOptions, addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, capitalizeFirst, capitalizeWords, checkMarkdownSecurity, chunk, clamp, cleanObject, compact, currencyToSymbol, debounce, deburr, deferAfterResponse, deferAfterResponseNonCritical, deferNonCritical, deferPostReturn, delay, difference, fill, flatten, flattenDepth, flattenDepthBase, flattenOnce, formatBytes, formatCurrency, formatDecimalNumber, getStringSimilarity, groupBy, hasNilOrEmpty, humanFileSize, intersection, isBrowser, isFlattenable, isNil, isNilEmptyOrZeroLen, isNilEmptyOrZeroLength, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilOrZeroLength, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, numbersEqual, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeAdd, safeDivide, safeJSONParse, safeMultiply, safeParseFloat, safeParseInt, safeSubtract, sanitize, sanitizeMarkdown, sentenceCase, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
package/dist/index.js CHANGED
@@ -1743,10 +1743,94 @@ var assessSecurityRisks = (risks) => {
1743
1743
  return { score, level, recommendations };
1744
1744
  };
1745
1745
 
1746
+ // src/utils/defer.ts
1747
+ var swallow = (p) => p.catch(() => {
1748
+ });
1749
+ var deferPostReturn = (fn) => {
1750
+ if (typeof fn !== "function") return;
1751
+ const run = () => {
1752
+ try {
1753
+ const result = fn();
1754
+ swallow(Promise.resolve(result));
1755
+ } catch {
1756
+ }
1757
+ };
1758
+ if (typeof queueMicrotask === "function") {
1759
+ queueMicrotask(run);
1760
+ } else {
1761
+ Promise.resolve().then(run);
1762
+ }
1763
+ };
1764
+ var deferNonCritical = (fn) => {
1765
+ if (typeof fn !== "function") return;
1766
+ const run = () => {
1767
+ try {
1768
+ const result = fn();
1769
+ swallow(Promise.resolve(result));
1770
+ } catch {
1771
+ }
1772
+ };
1773
+ if (typeof setImmediate === "function") {
1774
+ setImmediate(run);
1775
+ } else {
1776
+ setTimeout(run, 0);
1777
+ }
1778
+ };
1779
+ var deferAfterResponse = (res, fn) => {
1780
+ if (typeof fn !== "function") return;
1781
+ if (res?.writableEnded === true) {
1782
+ deferPostReturn(fn);
1783
+ return;
1784
+ }
1785
+ if (!res || typeof res.once !== "function") {
1786
+ deferPostReturn(fn);
1787
+ return;
1788
+ }
1789
+ let executed = false;
1790
+ const executeOnce = () => {
1791
+ if (executed) return;
1792
+ executed = true;
1793
+ deferPostReturn(fn);
1794
+ };
1795
+ try {
1796
+ res.once("finish", executeOnce);
1797
+ res.once("close", executeOnce);
1798
+ } catch {
1799
+ if (!executed) {
1800
+ deferPostReturn(fn);
1801
+ }
1802
+ }
1803
+ };
1804
+ var deferAfterResponseNonCritical = (res, fn) => {
1805
+ if (typeof fn !== "function") return;
1806
+ if (res?.writableEnded === true) {
1807
+ deferNonCritical(fn);
1808
+ return;
1809
+ }
1810
+ if (!res || typeof res.once !== "function") {
1811
+ deferNonCritical(fn);
1812
+ return;
1813
+ }
1814
+ let executed = false;
1815
+ const executeOnce = () => {
1816
+ if (executed) return;
1817
+ executed = true;
1818
+ deferNonCritical(fn);
1819
+ };
1820
+ try {
1821
+ res.once("finish", executeOnce);
1822
+ res.once("close", executeOnce);
1823
+ } catch {
1824
+ if (!executed) {
1825
+ deferNonCritical(fn);
1826
+ }
1827
+ }
1828
+ };
1829
+
1746
1830
  // src/index.ts
1747
1831
  var isBrowser = typeof globalThis !== "undefined" && typeof globalThis.document !== "undefined";
1748
1832
  var isNode = typeof process !== "undefined" && !!process.versions?.node;
1749
1833
 
1750
- export { addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, capitalizeFirst, capitalizeWords, checkMarkdownSecurity, chunk, clamp, cleanObject, compact, currencyToSymbol, debounce, deburr, delay, difference, fill, flatten, flattenDepth, flattenDepthBase, flattenOnce, formatBytes, formatCurrency, formatDecimalNumber, getStringSimilarity, groupBy, hasNilOrEmpty, humanFileSize, intersection, isBrowser, isFlattenable, isNil, isNilEmptyOrZeroLen, isNilEmptyOrZeroLength, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilOrZeroLength, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, numbersEqual, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeAdd, safeDivide, safeJSONParse, safeMultiply, safeParseFloat, safeParseInt, safeSubtract, sanitize, sanitizeMarkdown, sentenceCase, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
1834
+ export { addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, capitalizeFirst, capitalizeWords, checkMarkdownSecurity, chunk, clamp, cleanObject, compact, currencyToSymbol, debounce, deburr, deferAfterResponse, deferAfterResponseNonCritical, deferNonCritical, deferPostReturn, delay, difference, fill, flatten, flattenDepth, flattenDepthBase, flattenOnce, formatBytes, formatCurrency, formatDecimalNumber, getStringSimilarity, groupBy, hasNilOrEmpty, humanFileSize, intersection, isBrowser, isFlattenable, isNil, isNilEmptyOrZeroLen, isNilEmptyOrZeroLength, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilOrZeroLength, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, numbersEqual, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeAdd, safeDivide, safeJSONParse, safeMultiply, safeParseFloat, safeParseInt, safeSubtract, sanitize, sanitizeMarkdown, sentenceCase, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
1751
1835
  //# sourceMappingURL=index.js.map
1752
1836
  //# sourceMappingURL=index.js.map