@leaderty/utils 3.0.0 → 3.1.0
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 +25 -0
- package/dist/format.d.ts +13 -0
- package/dist/format.js +21 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,6 +38,31 @@ numberToMoney(-1234.5, { decimals: 2 }) // "
|
|
|
38
38
|
numberToMoney(undefined, { currency: 'ARS' }) // "-"
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
+
### `moneyToNumber(value)`
|
|
42
|
+
|
|
43
|
+
Lee un monto escrito a la manera argentina: coma para los decimales y sin separador de miles. Es la inversa de lo que un usuario tipea en un campo de plata.
|
|
44
|
+
|
|
45
|
+
**Comportamiento:**
|
|
46
|
+
- Rechaza cualquier punto: `350.5` y `350.000` no se distinguen sin adivinar.
|
|
47
|
+
- Admite un monto a medio escribir (`1234,`) y hasta dos decimales. Con más decimales devuelve `null`: no redondea lo que escribió el usuario.
|
|
48
|
+
- Admite el signo menos y hasta 13 cifras enteras, para que cada centavo sea exacto en un `number`.
|
|
49
|
+
- Ignora los espacios alrededor del monto.
|
|
50
|
+
|
|
51
|
+
**Parámetros:**
|
|
52
|
+
- `value` - `string`
|
|
53
|
+
|
|
54
|
+
**Devuelve:** `number`, o `null` si `value` está vacío o no es un monto válido
|
|
55
|
+
|
|
56
|
+
**Ejemplos:**
|
|
57
|
+
```typescript
|
|
58
|
+
moneyToNumber('357672,50') // 357672.5
|
|
59
|
+
moneyToNumber('350000') // 350000
|
|
60
|
+
moneyToNumber('1234,') // 1234
|
|
61
|
+
moneyToNumber('450.000') // null
|
|
62
|
+
moneyToNumber('1,2,3') // null
|
|
63
|
+
moneyToNumber('') // null
|
|
64
|
+
```
|
|
65
|
+
|
|
41
66
|
### `dateToShort(value, options?)`
|
|
42
67
|
|
|
43
68
|
Formats a date as `dd/mm/aaaa`, or `dd/mm/aaaa hh:mm` (24-hour) with `time: true`.
|
package/dist/format.d.ts
CHANGED
|
@@ -32,6 +32,19 @@ export interface DateToShortOptions {
|
|
|
32
32
|
* numberToMoney(-1234.5, { currency: 'USD', currencyDisplay: 'symbol', decimals: 2 }) // "-US$ 1.234,50"
|
|
33
33
|
*/
|
|
34
34
|
export declare function numberToMoney(amount: number | null | undefined, options?: NumberToMoneyOptions): string;
|
|
35
|
+
/**
|
|
36
|
+
* Reads an amount typed the Argentine way: `,` for decimals and no thousands separator.
|
|
37
|
+
* Any dot is rejected: `350.5` and `350.000` can't be told apart without guessing.
|
|
38
|
+
*
|
|
39
|
+
* @param value - Text of a money field
|
|
40
|
+
* @returns The amount, or `null` when `value` has no amount or is not a valid one
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* moneyToNumber('357672,50') // 357672.5
|
|
44
|
+
* moneyToNumber('1234,') // 1234
|
|
45
|
+
* moneyToNumber('450.000') // null
|
|
46
|
+
*/
|
|
47
|
+
export declare function moneyToNumber(value: string): number | null;
|
|
35
48
|
/**
|
|
36
49
|
* Formats a date as `dd/mm/aaaa`, optionally with the time as `dd/mm/aaaa hh:mm`.
|
|
37
50
|
*
|
package/dist/format.js
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
/** Shown instead of a value that is missing or not a valid number/date. */
|
|
7
7
|
const PLACEHOLDER = '-';
|
|
8
8
|
const CURRENCY_SYMBOLS = { ARS: '$', USD: 'US$' };
|
|
9
|
+
/** Up to 13 integer digits: with two decimals, 15 significant digits are always exact to the cent in a `number`. */
|
|
10
|
+
const MONEY_INPUT_PATTERN = /^-?(?:\d{1,13}(?:,\d{0,2})?|,\d{1,2})$/;
|
|
9
11
|
/** Argentina is UTC-3 all year (no daylight saving time since 2009). */
|
|
10
12
|
const BUENOS_AIRES_OFFSET_MS = -3 * 60 * 60 * 1000;
|
|
11
13
|
/** `aaaa-mm-dd`, optionally followed by a time and no time zone: a calendar date or Argentine wall-clock time. */
|
|
@@ -53,6 +55,25 @@ export function numberToMoney(amount, options = {}) {
|
|
|
53
55
|
const prefix = currencyDisplay === 'symbol' ? CURRENCY_SYMBOLS[currency] : currency;
|
|
54
56
|
return `${sign}${prefix} ${number}`;
|
|
55
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Reads an amount typed the Argentine way: `,` for decimals and no thousands separator.
|
|
60
|
+
* Any dot is rejected: `350.5` and `350.000` can't be told apart without guessing.
|
|
61
|
+
*
|
|
62
|
+
* @param value - Text of a money field
|
|
63
|
+
* @returns The amount, or `null` when `value` has no amount or is not a valid one
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* moneyToNumber('357672,50') // 357672.5
|
|
67
|
+
* moneyToNumber('1234,') // 1234
|
|
68
|
+
* moneyToNumber('450.000') // null
|
|
69
|
+
*/
|
|
70
|
+
export function moneyToNumber(value) {
|
|
71
|
+
const text = value.trim();
|
|
72
|
+
if (!MONEY_INPUT_PATTERN.test(text))
|
|
73
|
+
return null;
|
|
74
|
+
// `|| 0` turns -0 into 0
|
|
75
|
+
return Number(text.replace(',', '.')) || 0;
|
|
76
|
+
}
|
|
56
77
|
/**
|
|
57
78
|
* Formats a date as `dd/mm/aaaa`, optionally with the time as `dd/mm/aaaa hh:mm`.
|
|
58
79
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -5,4 +5,4 @@
|
|
|
5
5
|
export declare function numberToDecimalString(numero: number): string;
|
|
6
6
|
/** @deprecated Renamed to {@link numberToDecimalString}. */
|
|
7
7
|
export declare const numberToStringWithDecimals: typeof numberToDecimalString;
|
|
8
|
-
export { numberToMoney, dateToShort, dateToYMD, dateToMonthYearLong, type Currency, type NumberToMoneyOptions, type DateToShortOptions, } from './format.js';
|
|
8
|
+
export { numberToMoney, moneyToNumber, dateToShort, dateToYMD, dateToMonthYearLong, type Currency, type NumberToMoneyOptions, type DateToShortOptions, } from './format.js';
|
package/dist/index.js
CHANGED
|
@@ -8,4 +8,4 @@ export function numberToDecimalString(numero) {
|
|
|
8
8
|
}
|
|
9
9
|
/** @deprecated Renamed to {@link numberToDecimalString}. */
|
|
10
10
|
export const numberToStringWithDecimals = numberToDecimalString;
|
|
11
|
-
export { numberToMoney, dateToShort, dateToYMD, dateToMonthYearLong, } from './format.js';
|
|
11
|
+
export { numberToMoney, moneyToNumber, dateToShort, dateToYMD, dateToMonthYearLong, } from './format.js';
|