@leaderty/utils 3.0.0 → 3.2.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 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`.
@@ -110,6 +135,38 @@ Formats a number with two decimals and thousands separators. Rounds like `number
110
135
  numberToDecimalString(1234.56) // "1.234,56"
111
136
  ```
112
137
 
138
+ ### `isCbu(value)`
139
+
140
+ Indica si `value` es un CBU o una CVU válido: 22 dígitos con sus dos dígitos verificadores, calculados con la clave del BCRA (Com. "A" 8303). No dice si la cuenta existe.
141
+
142
+ **Parámetros:**
143
+ - `value` - `string`, solo dígitos (sin espacios ni guiones)
144
+
145
+ **Devuelve:** `boolean`
146
+
147
+ **Ejemplos:**
148
+ ```typescript
149
+ isCbu('2850590940090418135201') // true
150
+ isCbu('2850590940090418135202') // false: verificador de cuenta incorrecto
151
+ isCbu('28505909 40090418135201') // false
152
+ ```
153
+
154
+ ### `isAlias(value)`
155
+
156
+ Indica si `value` tiene la forma de un alias de CBU o CVU: de 6 a 20 caracteres entre letras sin ñ ni acentos, números, punto y guion. No dice si el alias existe.
157
+
158
+ **Parámetros:**
159
+ - `value` - `string`
160
+
161
+ **Devuelve:** `boolean`
162
+
163
+ **Ejemplos:**
164
+ ```typescript
165
+ isAlias('casa.sol.azul') // true
166
+ isAlias('año.casa') // false
167
+ isAlias('casa') // false: menos de 6 caracteres
168
+ ```
169
+
113
170
  ## Types
114
171
 
115
172
  `Currency`, `NumberToMoneyOptions` and `DateToShortOptions` are exported for consumers.
package/dist/bank.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Indica si `value` es un CBU o una CVU: 22 dígitos con sus dos dígitos verificadores
3
+ * (el de banco y sucursal, y el de cuenta).
4
+ */
5
+ export declare function isCbu(value: string): boolean;
6
+ /**
7
+ * Indica si `value` puede ser un alias de CBU o CVU: de 6 a 20 caracteres entre letras sin ñ ni acentos,
8
+ * números, punto y guion. No dice si el alias existe.
9
+ */
10
+ export declare function isAlias(value: string): boolean;
package/dist/bank.js ADDED
@@ -0,0 +1,27 @@
1
+ // Clave de verificación del BCRA (Com. "A" 8303, puntos 1.1 y 2.3): pesos 7139 repetidos, alineados a la
2
+ // derecha (de derecha a izquierda, 3 1 7 9); el verificador completa la suma hasta la decena siguiente.
3
+ const WEIGHTS = [3, 1, 7, 9];
4
+ function checkDigit(digits) {
5
+ let sum = 0;
6
+ for (let i = 0; i < digits.length; i++) {
7
+ sum += Number(digits[digits.length - 1 - i]) * WEIGHTS[i % WEIGHTS.length];
8
+ }
9
+ return (10 - (sum % 10)) % 10;
10
+ }
11
+ /**
12
+ * Indica si `value` es un CBU o una CVU: 22 dígitos con sus dos dígitos verificadores
13
+ * (el de banco y sucursal, y el de cuenta).
14
+ */
15
+ export function isCbu(value) {
16
+ if (!/^\d{22}$/.test(value))
17
+ return false;
18
+ return (checkDigit(value.slice(0, 7)) === Number(value[7]) &&
19
+ checkDigit(value.slice(8, 21)) === Number(value[21]));
20
+ }
21
+ /**
22
+ * Indica si `value` puede ser un alias de CBU o CVU: de 6 a 20 caracteres entre letras sin ñ ni acentos,
23
+ * números, punto y guion. No dice si el alias existe.
24
+ */
25
+ export function isAlias(value) {
26
+ return /^[A-Za-z0-9.-]{6,20}$/.test(value);
27
+ }
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,5 @@
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';
9
+ export { isAlias, isCbu } from './bank.js';
package/dist/index.js CHANGED
@@ -8,4 +8,5 @@ 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';
12
+ export { isAlias, isCbu } from './bank.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leaderty/utils",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",