@sankhyalabs/core 5.4.0 → 5.4.1
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.
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
- [getValueOrZero](NumberUtils.md#getvalueorzero)
|
|
20
20
|
- [keepOnlyDecimalSeparator](NumberUtils.md#keeponlydecimalseparator)
|
|
21
21
|
- [round](NumberUtils.md#round)
|
|
22
|
+
- [safeFormat](NumberUtils.md#safeformat)
|
|
22
23
|
- [stringToNumber](NumberUtils.md#stringtonumber)
|
|
23
24
|
|
|
24
25
|
## Constructors
|
|
@@ -61,7 +62,7 @@ Informo: "27.99" | Obtenho: "27,99"
|
|
|
61
62
|
|
|
62
63
|
#### Defined in
|
|
63
64
|
|
|
64
|
-
src/utils/NumberUtils.ts:
|
|
65
|
+
src/utils/NumberUtils.ts:186
|
|
65
66
|
|
|
66
67
|
___
|
|
67
68
|
|
|
@@ -86,13 +87,13 @@ Determina a ordem de umeros.
|
|
|
86
87
|
|
|
87
88
|
#### Defined in
|
|
88
89
|
|
|
89
|
-
src/utils/NumberUtils.ts:
|
|
90
|
+
src/utils/NumberUtils.ts:267
|
|
90
91
|
|
|
91
92
|
___
|
|
92
93
|
|
|
93
94
|
### format
|
|
94
95
|
|
|
95
|
-
▸ `Static` **format**(`value`, `precision`, `prettyPrecision?`): `string`
|
|
96
|
+
▸ `Static` **format**(`value`, `precision`, `prettyPrecision?`, `defaultValue?`): `string`
|
|
96
97
|
|
|
97
98
|
Formata o numeral com a precisão informada.
|
|
98
99
|
|
|
@@ -100,6 +101,8 @@ Formata o numeral com a precisão informada.
|
|
|
100
101
|
|
|
101
102
|
```ts
|
|
102
103
|
Informado: ('10,9845444', 3, 3) | Retorna: '10,985'
|
|
104
|
+
Informado: (undefined, 3, 3) | Retorna: NaN
|
|
105
|
+
Informado: (undefined, 3, 3, '0,00') | Retorna: 0,00
|
|
103
106
|
```
|
|
104
107
|
|
|
105
108
|
#### Parameters
|
|
@@ -109,6 +112,7 @@ Informado: ('10,9845444', 3, 3) | Retorna: '10,985'
|
|
|
109
112
|
| `value` | `string` | `undefined` | Numeral em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico - sem separador de milhares: ######.##). |
|
|
110
113
|
| `precision` | `number` | `undefined` | Quantidade de casas decimais. |
|
|
111
114
|
| `prettyPrecision` | `number` | `NaN` | Quantidade de zeros nos decimais. |
|
|
115
|
+
| `defaultValue` | `string` | `undefined` | Valor padrão caso o value não seja um valor numérico válido. |
|
|
112
116
|
|
|
113
117
|
#### Returns
|
|
114
118
|
|
|
@@ -118,7 +122,7 @@ Informado: ('10,9845444', 3, 3) | Retorna: '10,985'
|
|
|
118
122
|
|
|
119
123
|
#### Defined in
|
|
120
124
|
|
|
121
|
-
src/utils/NumberUtils.ts:
|
|
125
|
+
src/utils/NumberUtils.ts:77
|
|
122
126
|
|
|
123
127
|
___
|
|
124
128
|
|
|
@@ -161,7 +165,7 @@ Informo: value: "30abc", defaultValue: 0 | Obtenho: 0
|
|
|
161
165
|
|
|
162
166
|
#### Defined in
|
|
163
167
|
|
|
164
|
-
src/utils/NumberUtils.ts:
|
|
168
|
+
src/utils/NumberUtils.ts:208
|
|
165
169
|
|
|
166
170
|
___
|
|
167
171
|
|
|
@@ -185,7 +189,7 @@ Obtém o valor ou zero, caso o valor seja inválido(NaN/undefined).
|
|
|
185
189
|
|
|
186
190
|
#### Defined in
|
|
187
191
|
|
|
188
|
-
src/utils/NumberUtils.ts:
|
|
192
|
+
src/utils/NumberUtils.ts:227
|
|
189
193
|
|
|
190
194
|
___
|
|
191
195
|
|
|
@@ -216,7 +220,7 @@ Informado: '95.12' | Retorna: '9512'
|
|
|
216
220
|
|
|
217
221
|
#### Defined in
|
|
218
222
|
|
|
219
|
-
src/utils/NumberUtils.ts:
|
|
223
|
+
src/utils/NumberUtils.ts:158
|
|
220
224
|
|
|
221
225
|
___
|
|
222
226
|
|
|
@@ -260,7 +264,40 @@ Informo: ("100.16", 3) | 100.16
|
|
|
260
264
|
|
|
261
265
|
#### Defined in
|
|
262
266
|
|
|
263
|
-
src/utils/NumberUtils.ts:
|
|
267
|
+
src/utils/NumberUtils.ts:255
|
|
268
|
+
|
|
269
|
+
___
|
|
270
|
+
|
|
271
|
+
### safeFormat
|
|
272
|
+
|
|
273
|
+
▸ `Static` **safeFormat**(`value`, `precision`, `prettyPrecision?`): `string`
|
|
274
|
+
|
|
275
|
+
Formata o numeral com a precisão informada e o valor default 0,00 caso o value não seja um numero valido.
|
|
276
|
+
|
|
277
|
+
**`Example`**
|
|
278
|
+
|
|
279
|
+
```ts
|
|
280
|
+
Informado: ('10,9845444', 3, 3) | Retorna: '10,985'
|
|
281
|
+
Informado: (undefined, 3, 3) | Retorna: 0,00
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
#### Parameters
|
|
285
|
+
|
|
286
|
+
| Name | Type | Default value | Description |
|
|
287
|
+
| :------ | :------ | :------ | :------ |
|
|
288
|
+
| `value` | `string` | `undefined` | Numeral em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico - sem separador de milhares: ######.##). |
|
|
289
|
+
| `precision` | `number` | `undefined` | Quantidade de casas decimais. |
|
|
290
|
+
| `prettyPrecision` | `number` | `NaN` | Quantidade de zeros nos decimais. |
|
|
291
|
+
|
|
292
|
+
#### Returns
|
|
293
|
+
|
|
294
|
+
`string`
|
|
295
|
+
|
|
296
|
+
- Numeral em formato de String formatado em PT-BR.
|
|
297
|
+
|
|
298
|
+
#### Defined in
|
|
299
|
+
|
|
300
|
+
src/utils/NumberUtils.ts:143
|
|
264
301
|
|
|
265
302
|
___
|
|
266
303
|
|
|
@@ -22,12 +22,28 @@ export declare class NumberUtils {
|
|
|
22
22
|
* @param value - Numeral em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico - sem separador de milhares: ######.##).
|
|
23
23
|
* @param precision - Quantidade de casas decimais.
|
|
24
24
|
* @param prettyPrecision - Quantidade de zeros nos decimais.
|
|
25
|
+
* @param defaultValue - Valor padrão caso o value não seja um valor numérico válido.
|
|
25
26
|
*
|
|
26
27
|
* @returns - Numeral em formato de String formatado em PT-BR.
|
|
27
28
|
* @example
|
|
28
29
|
* Informado: ('10,9845444', 3, 3) | Retorna: '10,985'
|
|
30
|
+
* Informado: (undefined, 3, 3) | Retorna: NaN
|
|
31
|
+
* Informado: (undefined, 3, 3, '0,00') | Retorna: 0,00
|
|
29
32
|
*/
|
|
30
|
-
static format: (value: string, precision: number, prettyPrecision?: number) => string;
|
|
33
|
+
static format: (value: string, precision: number, prettyPrecision?: number, defaultValue?: string) => string;
|
|
34
|
+
/**
|
|
35
|
+
* Formata o numeral com a precisão informada e o valor default 0,00 caso o value não seja um numero valido.
|
|
36
|
+
*
|
|
37
|
+
* @param value - Numeral em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico - sem separador de milhares: ######.##).
|
|
38
|
+
* @param precision - Quantidade de casas decimais.
|
|
39
|
+
* @param prettyPrecision - Quantidade de zeros nos decimais.
|
|
40
|
+
*
|
|
41
|
+
* @returns - Numeral em formato de String formatado em PT-BR.
|
|
42
|
+
* @example
|
|
43
|
+
* Informado: ('10,9845444', 3, 3) | Retorna: '10,985'
|
|
44
|
+
* Informado: (undefined, 3, 3) | Retorna: 0,00
|
|
45
|
+
*/
|
|
46
|
+
static safeFormat: (value: string, precision: number, prettyPrecision?: number) => string;
|
|
31
47
|
/**
|
|
32
48
|
* Retira os separadores de milhar de um numeral em formato de string.
|
|
33
49
|
*
|
|
@@ -105,18 +105,21 @@ NumberUtils.stringToNumber = (value) => {
|
|
|
105
105
|
* @param value - Numeral em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico - sem separador de milhares: ######.##).
|
|
106
106
|
* @param precision - Quantidade de casas decimais.
|
|
107
107
|
* @param prettyPrecision - Quantidade de zeros nos decimais.
|
|
108
|
+
* @param defaultValue - Valor padrão caso o value não seja um valor numérico válido.
|
|
108
109
|
*
|
|
109
110
|
* @returns - Numeral em formato de String formatado em PT-BR.
|
|
110
111
|
* @example
|
|
111
112
|
* Informado: ('10,9845444', 3, 3) | Retorna: '10,985'
|
|
113
|
+
* Informado: (undefined, 3, 3) | Retorna: NaN
|
|
114
|
+
* Informado: (undefined, 3, 3, '0,00') | Retorna: 0,00
|
|
112
115
|
*/
|
|
113
|
-
NumberUtils.format = (value, precision, prettyPrecision = NaN) => {
|
|
116
|
+
NumberUtils.format = (value, precision, prettyPrecision = NaN, defaultValue = NaN.toString()) => {
|
|
114
117
|
if (value === '' || value === undefined || value === "NaN") {
|
|
115
|
-
return
|
|
118
|
+
return defaultValue;
|
|
116
119
|
}
|
|
117
120
|
const newValue = NumberUtils.stringToNumber(value);
|
|
118
121
|
if (isNaN(newValue)) {
|
|
119
|
-
return
|
|
122
|
+
return defaultValue;
|
|
120
123
|
}
|
|
121
124
|
//Validation "precision":
|
|
122
125
|
// Case1: precision < 0 => does not use precision
|
|
@@ -153,6 +156,21 @@ NumberUtils.format = (value, precision, prettyPrecision = NaN) => {
|
|
|
153
156
|
return newValueStr;
|
|
154
157
|
};
|
|
155
158
|
/**
|
|
159
|
+
* Formata o numeral com a precisão informada e o valor default 0,00 caso o value não seja um numero valido.
|
|
160
|
+
*
|
|
161
|
+
* @param value - Numeral em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico - sem separador de milhares: ######.##).
|
|
162
|
+
* @param precision - Quantidade de casas decimais.
|
|
163
|
+
* @param prettyPrecision - Quantidade de zeros nos decimais.
|
|
164
|
+
*
|
|
165
|
+
* @returns - Numeral em formato de String formatado em PT-BR.
|
|
166
|
+
* @example
|
|
167
|
+
* Informado: ('10,9845444', 3, 3) | Retorna: '10,985'
|
|
168
|
+
* Informado: (undefined, 3, 3) | Retorna: 0,00
|
|
169
|
+
*/
|
|
170
|
+
NumberUtils.safeFormat = (value, precision, prettyPrecision = NaN) => {
|
|
171
|
+
return NumberUtils.format(value, precision, prettyPrecision, "0,00");
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
156
174
|
* Retira os separadores de milhar de um numeral em formato de string.
|
|
157
175
|
*
|
|
158
176
|
* @param value - Numeral em formato de string a ser convertido.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NumberUtils.js","sourceRoot":"","sources":["../../src/utils/NumberUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,MAAM,mCAAmC,GAAW,qCAAqC,CAAC;AAE1F;;GAEG;AACH,MAAM,OAAO,WAAW;
|
|
1
|
+
{"version":3,"file":"NumberUtils.js","sourceRoot":"","sources":["../../src/utils/NumberUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,MAAM,mCAAmC,GAAW,qCAAqC,CAAC;AAE1F;;GAEG;AACH,MAAM,OAAO,WAAW;IA+NpB;;;;;;;;;;;;;;;;;;;;;;;MAuBE;IACF,MAAM,CAAC,KAAK,CAAC,KAAa,EAAE,WAAmB,CAAC;QAC5C,MAAM,UAAU,GAAG,SAAA,EAAE,EAAI,QAAQ,CAAA,CAAC;QAClC,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC,GAAG,UAAU,CAAC;IACvD,CAAC;IAEA;;;;;;MAME;IACH,MAAM,CAAC,OAAO,CAAC,CAAS,EAAE,CAAS;QAE/B,IAAG,CAAC,KAAK,SAAS,EAAC;YACf,OAAO,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;SAClC;aAAM,IAAG,CAAC,KAAK,SAAS,EAAC;YACtB,OAAO,CAAC,CAAC,CAAC;SACb;QAED,IAAG,CAAC,KAAK,IAAI,EAAC;YACV,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;SAC7B;aAAM,IAAG,CAAC,KAAK,IAAI,EAAC;YACjB,OAA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 -66,21 +66,24 @@ export class NumberUtils {
|
|
|
66
66
|
* @param value - Numeral em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico - sem separador de milhares: ######.##).
|
|
67
67
|
* @param precision - Quantidade de casas decimais.
|
|
68
68
|
* @param prettyPrecision - Quantidade de zeros nos decimais.
|
|
69
|
+
* @param defaultValue - Valor padrão caso o value não seja um valor numérico válido.
|
|
69
70
|
*
|
|
70
71
|
* @returns - Numeral em formato de String formatado em PT-BR.
|
|
71
72
|
* @example
|
|
72
73
|
* Informado: ('10,9845444', 3, 3) | Retorna: '10,985'
|
|
74
|
+
* Informado: (undefined, 3, 3) | Retorna: NaN
|
|
75
|
+
* Informado: (undefined, 3, 3, '0,00') | Retorna: 0,00
|
|
73
76
|
*/
|
|
74
|
-
static format = (value: string, precision: number, prettyPrecision: number = NaN): string => {
|
|
77
|
+
static format = (value: string, precision: number, prettyPrecision: number = NaN, defaultValue:string = NaN.toString()): string => {
|
|
75
78
|
|
|
76
79
|
if (value === '' || value === undefined || value === "NaN") {
|
|
77
|
-
return
|
|
80
|
+
return defaultValue;
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
const newValue: number = NumberUtils.stringToNumber(value);
|
|
81
84
|
|
|
82
85
|
if (isNaN(newValue)) {
|
|
83
|
-
return
|
|
86
|
+
return defaultValue;
|
|
84
87
|
}
|
|
85
88
|
|
|
86
89
|
//Validation "precision":
|
|
@@ -122,9 +125,24 @@ export class NumberUtils {
|
|
|
122
125
|
|
|
123
126
|
|
|
124
127
|
return newValueStr;
|
|
128
|
+
}
|
|
125
129
|
|
|
126
130
|
|
|
127
|
-
|
|
131
|
+
/**
|
|
132
|
+
* Formata o numeral com a precisão informada e o valor default 0,00 caso o value não seja um numero valido.
|
|
133
|
+
*
|
|
134
|
+
* @param value - Numeral em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico - sem separador de milhares: ######.##).
|
|
135
|
+
* @param precision - Quantidade de casas decimais.
|
|
136
|
+
* @param prettyPrecision - Quantidade de zeros nos decimais.
|
|
137
|
+
*
|
|
138
|
+
* @returns - Numeral em formato de String formatado em PT-BR.
|
|
139
|
+
* @example
|
|
140
|
+
* Informado: ('10,9845444', 3, 3) | Retorna: '10,985'
|
|
141
|
+
* Informado: (undefined, 3, 3) | Retorna: 0,00
|
|
142
|
+
*/
|
|
143
|
+
static safeFormat = (value: string, precision: number, prettyPrecision: number = NaN): string => {
|
|
144
|
+
return NumberUtils.format(value, precision, prettyPrecision, "0,00");
|
|
145
|
+
}
|
|
128
146
|
|
|
129
147
|
|
|
130
148
|
/**
|