@salespark/toolkit 2.1.1 → 2.1.3
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 +74 -38
- package/dist/index.cjs +32 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +31 -17
- package/dist/index.d.ts +31 -17
- package/dist/index.js +31 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -12
package/dist/index.d.cts
CHANGED
|
@@ -306,39 +306,39 @@ declare function round(n: number, decimals?: number): number;
|
|
|
306
306
|
* Safely converts a value to an integer. Returns defaultValue if parsing fails or results in NaN.
|
|
307
307
|
*
|
|
308
308
|
* Notes:
|
|
309
|
-
* Examples:
|
|
309
|
+
* Examples: safeParseInt("42") -> 42, safeParseInt("abc", 10) -> 10, safeParseInt(undefined) -> 0, safeParseInt(3.9) -> 3
|
|
310
310
|
* @param {unknown} value - Value to convert
|
|
311
311
|
* @param {Number} defaultValue - Default value if parsing fails
|
|
312
312
|
* History:
|
|
313
313
|
* 21-08-2025: Created
|
|
314
|
+
* 29-10-2025: Renamed from toInteger to safeParseInt
|
|
314
315
|
****************************************************/
|
|
315
|
-
declare function
|
|
316
|
-
|
|
317
|
-
*
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
* @param {Number} defaultValue - Default value if parsing fails
|
|
321
|
-
* History:
|
|
322
|
-
* 21-08-2025: Created
|
|
323
|
-
****************************************************/
|
|
324
|
-
declare const safeParseInt: typeof toInteger;
|
|
316
|
+
declare function safeParseInt(value: unknown, defaultValue?: number): number;
|
|
317
|
+
/**
|
|
318
|
+
* @deprecated Use `safeParseFloat` instead.
|
|
319
|
+
*/
|
|
320
|
+
declare const toInteger: typeof safeParseInt;
|
|
325
321
|
/******************************************************
|
|
326
322
|
* ##: Safe Number Conversion
|
|
327
323
|
* Safely parses a value into a number with optional decimal precision
|
|
328
324
|
*
|
|
329
325
|
* Notes:
|
|
330
326
|
* Handles commas as decimal/thousands separators. Returns 0 for null/undefined/empty string or invalid parsing.
|
|
331
|
-
* Examples:
|
|
332
|
-
* @param {unknown} value - Value to convert
|
|
327
|
+
* Examples: safeParseFloat("123.45") -> 123.45, safeParseFloat("123,45") -> 123.45, safeParseFloat("1,234.56") -> 1234.56, safeParseFloat("abc", 2) -> 0, safeParseFloat(42) -> 42 * @param {unknown} value - Value to convert
|
|
333
328
|
* @param {Number} decimals - Number of decimal places
|
|
334
329
|
* History:
|
|
335
330
|
* 21-08-2025: Created
|
|
331
|
+
* * 29-10-2025: Renamed from toNumber to safeParseFloat
|
|
336
332
|
****************************************************/
|
|
337
|
-
declare function
|
|
333
|
+
declare function safeParseFloat(value: unknown, decimals?: number): number;
|
|
334
|
+
/**
|
|
335
|
+
* @deprecated Use `safeParseFloat` instead.
|
|
336
|
+
*/
|
|
337
|
+
declare const toNumber: typeof safeParseFloat;
|
|
338
338
|
/**
|
|
339
|
-
* @deprecated Use `
|
|
339
|
+
* @deprecated Use `safeParseFloat` instead.
|
|
340
340
|
*/
|
|
341
|
-
declare const parseToNumber: typeof
|
|
341
|
+
declare const parseToNumber: typeof safeParseFloat;
|
|
342
342
|
/******************************************************
|
|
343
343
|
* ##: Random Digits Generator
|
|
344
344
|
* Generates a random string of digits with secure randomness when available
|
|
@@ -358,6 +358,19 @@ declare function randomDigits(length?: number, options?: {
|
|
|
358
358
|
* @deprecated Use `randomDigits` instead.
|
|
359
359
|
*/
|
|
360
360
|
declare const otp: typeof randomDigits;
|
|
361
|
+
/******************************************************
|
|
362
|
+
* ##: Decimal Number Formatter
|
|
363
|
+
* Formats a number with specified decimal places, handling comma/dot normalization.
|
|
364
|
+
*
|
|
365
|
+
* Intelligently handles European number formats (1.234,56) and US formats (1,234.56).
|
|
366
|
+
* Converts strings to numbers and applies decimal formatting.
|
|
367
|
+
* @param {number|string|null|undefined} value Number value to format
|
|
368
|
+
* @param {number} decimals Number of decimal places (default: 2)
|
|
369
|
+
* @returns {string} Formatted number string with specified decimals
|
|
370
|
+
* History:
|
|
371
|
+
* 16-10-2025: Created
|
|
372
|
+
****************************************************/
|
|
373
|
+
declare const formatDecimalNumber: (value: number | string | null | undefined, decimals?: number) => string;
|
|
361
374
|
|
|
362
375
|
/******************************************************
|
|
363
376
|
* ##: Debounce Function
|
|
@@ -399,6 +412,7 @@ declare const isNilText: (value: unknown) => boolean;
|
|
|
399
412
|
* @param {unknown} value - Value to check
|
|
400
413
|
* History:
|
|
401
414
|
* 21-08-2025: Created
|
|
415
|
+
* 18-10-2025: Trim before checking empty
|
|
402
416
|
****************************************************/
|
|
403
417
|
declare const isNilOrEmpty: (value: unknown) => boolean;
|
|
404
418
|
/******************************************************
|
|
@@ -681,4 +695,4 @@ declare const assessSecurityRisks: (risks: SecurityRisk[]) => {
|
|
|
681
695
|
declare const isBrowser: boolean;
|
|
682
696
|
declare const isNode: boolean;
|
|
683
697
|
|
|
684
|
-
export { type SecurityCheckResult, type SecurityRisk, addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, checkMarkdownSecurity, chunk, clamp, cleanObject, compact, currencyToSymbol, debounce, deburr, delay, difference, fill, flatten, flattenDepth, flattenDepthBase, flattenOnce, formatBytes, formatCurrency, getStringSimilarity, groupBy, hasNilOrEmpty, humanFileSize, intersection, isBrowser, isFlattenable, isNil, isNilEmptyOrZeroLen, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeParseInt, sanitize, sanitizeMarkdown, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
|
|
698
|
+
export { type SecurityCheckResult, type SecurityRisk, addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, 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, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeParseFloat, safeParseInt, sanitize, sanitizeMarkdown, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
|
package/dist/index.d.ts
CHANGED
|
@@ -306,39 +306,39 @@ declare function round(n: number, decimals?: number): number;
|
|
|
306
306
|
* Safely converts a value to an integer. Returns defaultValue if parsing fails or results in NaN.
|
|
307
307
|
*
|
|
308
308
|
* Notes:
|
|
309
|
-
* Examples:
|
|
309
|
+
* Examples: safeParseInt("42") -> 42, safeParseInt("abc", 10) -> 10, safeParseInt(undefined) -> 0, safeParseInt(3.9) -> 3
|
|
310
310
|
* @param {unknown} value - Value to convert
|
|
311
311
|
* @param {Number} defaultValue - Default value if parsing fails
|
|
312
312
|
* History:
|
|
313
313
|
* 21-08-2025: Created
|
|
314
|
+
* 29-10-2025: Renamed from toInteger to safeParseInt
|
|
314
315
|
****************************************************/
|
|
315
|
-
declare function
|
|
316
|
-
|
|
317
|
-
*
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
* @param {Number} defaultValue - Default value if parsing fails
|
|
321
|
-
* History:
|
|
322
|
-
* 21-08-2025: Created
|
|
323
|
-
****************************************************/
|
|
324
|
-
declare const safeParseInt: typeof toInteger;
|
|
316
|
+
declare function safeParseInt(value: unknown, defaultValue?: number): number;
|
|
317
|
+
/**
|
|
318
|
+
* @deprecated Use `safeParseFloat` instead.
|
|
319
|
+
*/
|
|
320
|
+
declare const toInteger: typeof safeParseInt;
|
|
325
321
|
/******************************************************
|
|
326
322
|
* ##: Safe Number Conversion
|
|
327
323
|
* Safely parses a value into a number with optional decimal precision
|
|
328
324
|
*
|
|
329
325
|
* Notes:
|
|
330
326
|
* Handles commas as decimal/thousands separators. Returns 0 for null/undefined/empty string or invalid parsing.
|
|
331
|
-
* Examples:
|
|
332
|
-
* @param {unknown} value - Value to convert
|
|
327
|
+
* Examples: safeParseFloat("123.45") -> 123.45, safeParseFloat("123,45") -> 123.45, safeParseFloat("1,234.56") -> 1234.56, safeParseFloat("abc", 2) -> 0, safeParseFloat(42) -> 42 * @param {unknown} value - Value to convert
|
|
333
328
|
* @param {Number} decimals - Number of decimal places
|
|
334
329
|
* History:
|
|
335
330
|
* 21-08-2025: Created
|
|
331
|
+
* * 29-10-2025: Renamed from toNumber to safeParseFloat
|
|
336
332
|
****************************************************/
|
|
337
|
-
declare function
|
|
333
|
+
declare function safeParseFloat(value: unknown, decimals?: number): number;
|
|
334
|
+
/**
|
|
335
|
+
* @deprecated Use `safeParseFloat` instead.
|
|
336
|
+
*/
|
|
337
|
+
declare const toNumber: typeof safeParseFloat;
|
|
338
338
|
/**
|
|
339
|
-
* @deprecated Use `
|
|
339
|
+
* @deprecated Use `safeParseFloat` instead.
|
|
340
340
|
*/
|
|
341
|
-
declare const parseToNumber: typeof
|
|
341
|
+
declare const parseToNumber: typeof safeParseFloat;
|
|
342
342
|
/******************************************************
|
|
343
343
|
* ##: Random Digits Generator
|
|
344
344
|
* Generates a random string of digits with secure randomness when available
|
|
@@ -358,6 +358,19 @@ declare function randomDigits(length?: number, options?: {
|
|
|
358
358
|
* @deprecated Use `randomDigits` instead.
|
|
359
359
|
*/
|
|
360
360
|
declare const otp: typeof randomDigits;
|
|
361
|
+
/******************************************************
|
|
362
|
+
* ##: Decimal Number Formatter
|
|
363
|
+
* Formats a number with specified decimal places, handling comma/dot normalization.
|
|
364
|
+
*
|
|
365
|
+
* Intelligently handles European number formats (1.234,56) and US formats (1,234.56).
|
|
366
|
+
* Converts strings to numbers and applies decimal formatting.
|
|
367
|
+
* @param {number|string|null|undefined} value Number value to format
|
|
368
|
+
* @param {number} decimals Number of decimal places (default: 2)
|
|
369
|
+
* @returns {string} Formatted number string with specified decimals
|
|
370
|
+
* History:
|
|
371
|
+
* 16-10-2025: Created
|
|
372
|
+
****************************************************/
|
|
373
|
+
declare const formatDecimalNumber: (value: number | string | null | undefined, decimals?: number) => string;
|
|
361
374
|
|
|
362
375
|
/******************************************************
|
|
363
376
|
* ##: Debounce Function
|
|
@@ -399,6 +412,7 @@ declare const isNilText: (value: unknown) => boolean;
|
|
|
399
412
|
* @param {unknown} value - Value to check
|
|
400
413
|
* History:
|
|
401
414
|
* 21-08-2025: Created
|
|
415
|
+
* 18-10-2025: Trim before checking empty
|
|
402
416
|
****************************************************/
|
|
403
417
|
declare const isNilOrEmpty: (value: unknown) => boolean;
|
|
404
418
|
/******************************************************
|
|
@@ -681,4 +695,4 @@ declare const assessSecurityRisks: (risks: SecurityRisk[]) => {
|
|
|
681
695
|
declare const isBrowser: boolean;
|
|
682
696
|
declare const isNode: boolean;
|
|
683
697
|
|
|
684
|
-
export { type SecurityCheckResult, type SecurityRisk, addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, checkMarkdownSecurity, chunk, clamp, cleanObject, compact, currencyToSymbol, debounce, deburr, delay, difference, fill, flatten, flattenDepth, flattenDepthBase, flattenOnce, formatBytes, formatCurrency, getStringSimilarity, groupBy, hasNilOrEmpty, humanFileSize, intersection, isBrowser, isFlattenable, isNil, isNilEmptyOrZeroLen, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeParseInt, sanitize, sanitizeMarkdown, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
|
|
698
|
+
export { type SecurityCheckResult, type SecurityRisk, addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, 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, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeParseFloat, safeParseInt, sanitize, sanitizeMarkdown, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
|
package/dist/index.js
CHANGED
|
@@ -251,7 +251,7 @@ function round(n, decimals = 0) {
|
|
|
251
251
|
const f = Math.pow(10, decimals);
|
|
252
252
|
return Math.round((n + Number.EPSILON) * f) / f;
|
|
253
253
|
}
|
|
254
|
-
function
|
|
254
|
+
function safeParseInt(value, defaultValue = 0) {
|
|
255
255
|
try {
|
|
256
256
|
const safeValue = parseInt(String(value), 10);
|
|
257
257
|
return isNaN(safeValue) ? defaultValue : safeValue;
|
|
@@ -259,8 +259,8 @@ function toInteger(value, defaultValue = 0) {
|
|
|
259
259
|
return defaultValue;
|
|
260
260
|
}
|
|
261
261
|
}
|
|
262
|
-
var
|
|
263
|
-
function
|
|
262
|
+
var toInteger = safeParseInt;
|
|
263
|
+
function safeParseFloat(value, decimals = 6) {
|
|
264
264
|
try {
|
|
265
265
|
if (value === void 0 || value === null || value === "") return 0;
|
|
266
266
|
let str = String(value);
|
|
@@ -276,7 +276,8 @@ function toNumber(value, decimals = 6) {
|
|
|
276
276
|
return 0;
|
|
277
277
|
}
|
|
278
278
|
}
|
|
279
|
-
var
|
|
279
|
+
var toNumber = safeParseFloat;
|
|
280
|
+
var parseToNumber = safeParseFloat;
|
|
280
281
|
function secureRandomIndex(max) {
|
|
281
282
|
if (max <= 0) return 0;
|
|
282
283
|
const g = globalThis;
|
|
@@ -308,6 +309,30 @@ function randomDigits(length = 6, options) {
|
|
|
308
309
|
return out;
|
|
309
310
|
}
|
|
310
311
|
var otp = randomDigits;
|
|
312
|
+
var formatDecimalNumber = (value, decimals = 2) => {
|
|
313
|
+
try {
|
|
314
|
+
let processedValue = value ?? 0;
|
|
315
|
+
if (typeof processedValue === "string") {
|
|
316
|
+
const trimmed = processedValue.trim();
|
|
317
|
+
if (trimmed.includes(",") && trimmed.includes(".")) {
|
|
318
|
+
const lastComma = trimmed.lastIndexOf(",");
|
|
319
|
+
const lastDot = trimmed.lastIndexOf(".");
|
|
320
|
+
processedValue = lastComma > lastDot ? trimmed.replace(/\./g, "").replace(",", ".") : trimmed.replace(/,/g, "");
|
|
321
|
+
} else if (trimmed.includes(",")) {
|
|
322
|
+
processedValue = trimmed.replace(/,/g, ".");
|
|
323
|
+
} else {
|
|
324
|
+
processedValue = trimmed;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
const numValue = parseFloat(String(processedValue));
|
|
328
|
+
if (isNaN(numValue)) {
|
|
329
|
+
return 0 .toFixed(Math.max(0, Math.floor(decimals)));
|
|
330
|
+
}
|
|
331
|
+
return numValue.toFixed(Math.max(0, Math.floor(decimals)));
|
|
332
|
+
} catch (error) {
|
|
333
|
+
return 0 .toFixed(Math.max(0, Math.floor(decimals)));
|
|
334
|
+
}
|
|
335
|
+
};
|
|
311
336
|
|
|
312
337
|
// src/utils/func.ts
|
|
313
338
|
function debounce(fn, wait = 250) {
|
|
@@ -346,7 +371,7 @@ var isNilText = (value) => {
|
|
|
346
371
|
};
|
|
347
372
|
var isNilOrEmpty = (value) => {
|
|
348
373
|
try {
|
|
349
|
-
return isNil(value) || value === "";
|
|
374
|
+
return isNil(value) || typeof value === "string" && value?.trim() === "";
|
|
350
375
|
} catch {
|
|
351
376
|
return true;
|
|
352
377
|
}
|
|
@@ -1590,6 +1615,6 @@ var assessSecurityRisks = (risks) => {
|
|
|
1590
1615
|
var isBrowser = typeof globalThis !== "undefined" && typeof globalThis.document !== "undefined";
|
|
1591
1616
|
var isNode = typeof process !== "undefined" && !!process.versions?.node;
|
|
1592
1617
|
|
|
1593
|
-
export { addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, checkMarkdownSecurity, chunk, clamp, cleanObject, compact, currencyToSymbol, debounce, deburr, delay, difference, fill, flatten, flattenDepth, flattenDepthBase, flattenOnce, formatBytes, formatCurrency, getStringSimilarity, groupBy, hasNilOrEmpty, humanFileSize, intersection, isBrowser, isFlattenable, isNil, isNilEmptyOrZeroLen, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeParseInt, sanitize, sanitizeMarkdown, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
|
|
1618
|
+
export { addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, assessSecurityRisks, basicSanitize, 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, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilText, isNilTextOrEmpty, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrEmptyEnforced, isNullUndefinedOrZero, isPTTaxId, isValidIBAN, isValidPTTaxId, objectToString, omit, otp, parseName, parseToBool, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeParseFloat, safeParseInt, sanitize, sanitizeMarkdown, shuffle, slugify, sortBy, stringSimilarity, symbolToCurrency, throttle, toBool, toInteger, toNumber, uniq, uniqBy };
|
|
1594
1619
|
//# sourceMappingURL=index.js.map
|
|
1595
1620
|
//# sourceMappingURL=index.js.map
|