@atlantjs/arch 13.3.0 → 14.0.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.
- package/@tool-box/utils/datatypes/boolean-utils.d.ts +7 -0
- package/@tool-box/utils/datatypes/boolean-utils.js +146 -4
- package/@tool-box/utils/datatypes/string-utils.d.ts +32 -4
- package/@tool-box/utils/datatypes/string-utils.js +303 -14
- package/@tool-box/utils/ducts/common.d.ts +49 -0
- package/@tool-box/utils/ducts/common.js +36 -3
- package/@tool-box/utils/ducts/optional-type.d.ts +85 -0
- package/@tool-box/utils/ducts/optional-type.js +79 -0
- package/@tool-box/utils/ducts/return-type.d.ts +17 -17
- package/@tool-box/utils/ducts/return-type.js +14 -4
- package/@tool-box/utils/http-provider/http-provider.d.ts +6 -0
- package/@tool-box/utils/http-provider/http-provider.js +43 -14
- package/@tool-box/utils/map/map.abstract.d.ts +39 -0
- package/@tool-box/utils/map/map.abstract.js +70 -6
- package/@tool-box/utils/random/random.d.ts +168 -0
- package/@tool-box/utils/random/random.js +235 -0
- package/@tool-box/utils/type-guard/guardian.d.ts +450 -7
- package/@tool-box/utils/type-guard/guardian.js +539 -35
- package/objects/@common/edges/cron-expression.edge.d.ts +30 -2
- package/objects/@common/edges/cron-expression.edge.js +77 -5
- package/objects/@common/edges/email.edge.d.ts +39 -1
- package/objects/@common/edges/email.edge.js +80 -2
- package/objects/@common/edges/ulid.sketch.edge.d.ts +48 -1
- package/objects/@common/edges/ulid.sketch.edge.js +75 -4
- package/objects/@common/edges/url.edge.d.ts +182 -0
- package/objects/@common/edges/url.edge.js +249 -0
- package/objects/@common/edges/username.edge.d.ts +9 -0
- package/objects/@common/edges/username.edge.js +34 -0
- package/objects/@common/edges/uuid.sketch.edge.d.ts +97 -1
- package/objects/@common/edges/uuid.sketch.edge.js +127 -6
- package/objects/amount/amount-value.edge.d.ts +39 -0
- package/objects/amount/amount-value.edge.js +69 -0
- package/objects/amount/amount.edge.d.ts +378 -2
- package/objects/amount/amount.edge.js +493 -4
- package/objects/datetime/edges/datetime.edge.d.ts +422 -4
- package/objects/datetime/edges/datetime.edge.js +538 -33
- package/objects/password/password.edge.d.ts +90 -0
- package/objects/password/password.edge.js +140 -6
- package/objects/primitives/boolean.edge.sketch.d.ts +105 -3
- package/objects/primitives/boolean.edge.sketch.js +132 -6
- package/objects/primitives/number.edge.sketch.d.ts +236 -0
- package/objects/primitives/number.edge.sketch.js +310 -24
- package/objects/primitives/string.edge.sketch.d.ts +148 -0
- package/objects/primitives/string.edge.sketch.js +191 -7
- package/objects/schedule/schedule.edge.d.ts +194 -0
- package/objects/schedule/schedule.edge.js +269 -2
- package/objects/time/time.edge.d.ts +285 -2
- package/objects/time/time.edge.js +385 -6
- package/package.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -19,10 +19,72 @@ class AmountEdge {
|
|
|
19
19
|
get unit() {
|
|
20
20
|
return this._unit;
|
|
21
21
|
}
|
|
22
|
+
// ==================== Arredondamento e Precisão ====================
|
|
23
|
+
/**
|
|
24
|
+
* Arredonda o valor para o inteiro mais próximo.
|
|
25
|
+
* @returns this para permitir encadeamento de métodos
|
|
26
|
+
* @example
|
|
27
|
+
* const amount = new AmountEdge(5.7, MassUnitPoint.gram);
|
|
28
|
+
* amount.round(); // valor agora é 6
|
|
29
|
+
*/
|
|
22
30
|
round() {
|
|
23
31
|
this._value = Math.round(this._value);
|
|
24
32
|
return this;
|
|
25
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Arredonda o valor para cima (sempre para o próximo inteiro maior).
|
|
36
|
+
* @returns this para permitir encadeamento de métodos
|
|
37
|
+
* @example
|
|
38
|
+
* const amount = new AmountEdge(5.2, MassUnitPoint.gram);
|
|
39
|
+
* amount.ceil(); // valor agora é 6
|
|
40
|
+
*/
|
|
41
|
+
ceil() {
|
|
42
|
+
this._value = Math.ceil(this._value);
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Arredonda o valor para baixo (sempre para o próximo inteiro menor).
|
|
47
|
+
* @returns this para permitir encadeamento de métodos
|
|
48
|
+
* @example
|
|
49
|
+
* const amount = new AmountEdge(5.9, MassUnitPoint.gram);
|
|
50
|
+
* amount.floor(); // valor agora é 5
|
|
51
|
+
*/
|
|
52
|
+
floor() {
|
|
53
|
+
this._value = Math.floor(this._value);
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Arredonda o valor com uma precisão decimal específica.
|
|
58
|
+
* @param precision - Número de casas decimais desejadas
|
|
59
|
+
* @returns this para permitir encadeamento de métodos
|
|
60
|
+
* @example
|
|
61
|
+
* const amount = new AmountEdge(5.6789, MassUnitPoint.gram);
|
|
62
|
+
* amount.toFixed(2); // valor agora é 5.68
|
|
63
|
+
*/
|
|
64
|
+
toFixed(precision) {
|
|
65
|
+
this._value = Number(this._value.toFixed(precision));
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Retorna o valor absoluto (sem sinal).
|
|
70
|
+
* @returns this para permitir encadeamento de métodos
|
|
71
|
+
* @example
|
|
72
|
+
* const amount = new AmountEdge(-5.5, MassUnitPoint.gram);
|
|
73
|
+
* amount.abs(); // valor agora é 5.5
|
|
74
|
+
*/
|
|
75
|
+
abs() {
|
|
76
|
+
this._value = Math.abs(this._value);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
// ==================== Compactação de Unidades ====================
|
|
80
|
+
/**
|
|
81
|
+
* Compacta a quantidade para uma unidade mais apropriada baseado no valor.
|
|
82
|
+
* Por exemplo, 1000 gramas é convertido para 1 quilograma.
|
|
83
|
+
* @returns Nova instância com a unidade compactada
|
|
84
|
+
* @example
|
|
85
|
+
* const amount = new AmountEdge(1500, MassUnitPoint.gram);
|
|
86
|
+
* const compact = amount.compact(); // 1.5 kg
|
|
87
|
+
*/
|
|
26
88
|
compact() {
|
|
27
89
|
const amountValue = new amount_value_edge_1.AmountValueEdge(this._value);
|
|
28
90
|
// Mass
|
|
@@ -63,14 +125,42 @@ class AmountEdge {
|
|
|
63
125
|
}
|
|
64
126
|
return this;
|
|
65
127
|
}
|
|
128
|
+
// ==================== Operações Aritméticas ====================
|
|
129
|
+
/**
|
|
130
|
+
* Soma outra quantidade ao valor atual.
|
|
131
|
+
* @param amount - Quantidade a ser somada
|
|
132
|
+
* @returns this para permitir encadeamento de métodos
|
|
133
|
+
* @example
|
|
134
|
+
* const amount1 = new AmountEdge(5, MassUnitPoint.gram);
|
|
135
|
+
* const amount2 = new AmountEdge(3, MassUnitPoint.gram);
|
|
136
|
+
* amount1.sum(amount2); // valor agora é 8
|
|
137
|
+
*/
|
|
66
138
|
sum(amount) {
|
|
67
139
|
this._value += amount.value;
|
|
68
140
|
return this;
|
|
69
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Subtrai outra quantidade do valor atual.
|
|
144
|
+
* @param amount - Quantidade a ser subtraída
|
|
145
|
+
* @returns this para permitir encadeamento de métodos
|
|
146
|
+
* @example
|
|
147
|
+
* const amount1 = new AmountEdge(8, MassUnitPoint.gram);
|
|
148
|
+
* const amount2 = new AmountEdge(3, MassUnitPoint.gram);
|
|
149
|
+
* amount1.subtract(amount2); // valor agora é 5
|
|
150
|
+
*/
|
|
70
151
|
subtract(amount) {
|
|
71
152
|
this._value -= amount.value;
|
|
72
153
|
return this;
|
|
73
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Divide o valor por um número ou outra quantidade.
|
|
157
|
+
* @param amount - Divisor (número ou AmountEdge)
|
|
158
|
+
* @returns this para permitir encadeamento de métodos
|
|
159
|
+
* @example
|
|
160
|
+
* const amount = new AmountEdge(10, MassUnitPoint.gram);
|
|
161
|
+
* amount.divideBy(2); // valor agora é 5
|
|
162
|
+
* amount.divideBy(new AmountEdge(2, MassUnitPoint.gram)); // valor agora é 2.5
|
|
163
|
+
*/
|
|
74
164
|
divideBy(amount) {
|
|
75
165
|
if (typeof amount === "number") {
|
|
76
166
|
this._value /= amount;
|
|
@@ -80,6 +170,15 @@ class AmountEdge {
|
|
|
80
170
|
}
|
|
81
171
|
return this;
|
|
82
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Multiplica o valor por um número ou outra quantidade.
|
|
175
|
+
* @param amount - Multiplicador (número ou AmountEdge)
|
|
176
|
+
* @returns this para permitir encadeamento de métodos
|
|
177
|
+
* @example
|
|
178
|
+
* const amount = new AmountEdge(5, MassUnitPoint.gram);
|
|
179
|
+
* amount.multiplyBy(2); // valor agora é 10
|
|
180
|
+
* amount.multiplyBy(new AmountEdge(3, MassUnitPoint.gram)); // valor agora é 30
|
|
181
|
+
*/
|
|
83
182
|
multiplyBy(amount) {
|
|
84
183
|
if (typeof amount === "number") {
|
|
85
184
|
this._value *= amount;
|
|
@@ -89,15 +188,277 @@ class AmountEdge {
|
|
|
89
188
|
}
|
|
90
189
|
return this;
|
|
91
190
|
}
|
|
92
|
-
|
|
93
|
-
|
|
191
|
+
/**
|
|
192
|
+
* Calcula o resto da divisão (módulo) do valor atual.
|
|
193
|
+
* @param amount - Divisor (número ou AmountEdge)
|
|
194
|
+
* @returns this para permitir encadeamento de métodos
|
|
195
|
+
* @example
|
|
196
|
+
* const amount = new AmountEdge(10, MassUnitPoint.gram);
|
|
197
|
+
* amount.modBy(3); // valor agora é 1
|
|
198
|
+
*/
|
|
199
|
+
modBy(amount) {
|
|
200
|
+
if (typeof amount === "number") {
|
|
201
|
+
this._value %= amount;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
this._value %= amount.value;
|
|
205
|
+
}
|
|
206
|
+
return this;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Calcula a diferença absoluta entre esta quantidade e outra.
|
|
210
|
+
* @param amount - Quantidade para comparação
|
|
211
|
+
* @returns Nova instância com a diferença absoluta
|
|
212
|
+
* @example
|
|
213
|
+
* const amount1 = new AmountEdge(10, MassUnitPoint.gram);
|
|
214
|
+
* const amount2 = new AmountEdge(3, MassUnitPoint.gram);
|
|
215
|
+
* const diff = amount1.difference(amount2); // 7
|
|
216
|
+
*/
|
|
217
|
+
difference(amount) {
|
|
218
|
+
const diff = Math.abs(this._value - amount.value);
|
|
219
|
+
return new AmountEdge(diff, this._unit);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Aplica uma porcentagem sobre o valor atual.
|
|
223
|
+
* @param percentage - Porcentagem a aplicar (0-100)
|
|
224
|
+
* @returns this para permitir encadeamento de métodos
|
|
225
|
+
* @example
|
|
226
|
+
* const amount = new AmountEdge(100, MassUnitPoint.gram);
|
|
227
|
+
* amount.applyPercentage(20); // valor agora é 120 (100 + 20%)
|
|
228
|
+
*/
|
|
229
|
+
applyPercentage(percentage) {
|
|
230
|
+
const percentageValue = (this._value * percentage) / 100;
|
|
231
|
+
this._value += percentageValue;
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Calcula a porcentagem que esta quantidade representa de um total.
|
|
236
|
+
* @param total - Quantidade total
|
|
237
|
+
* @returns Porcentagem como número (0-100)
|
|
238
|
+
* @example
|
|
239
|
+
* const amount = new AmountEdge(25, MassUnitPoint.gram);
|
|
240
|
+
* const total = new AmountEdge(100, MassUnitPoint.gram);
|
|
241
|
+
* const percentage = amount.percentageOf(total); // 25
|
|
242
|
+
*/
|
|
243
|
+
percentageOf(total) {
|
|
244
|
+
if (total.value === 0)
|
|
245
|
+
return 0;
|
|
246
|
+
return (this._value / total.value) * 100;
|
|
247
|
+
}
|
|
248
|
+
// ==================== Comparações ====================
|
|
249
|
+
/**
|
|
250
|
+
* Verifica se esta quantidade é igual a outra.
|
|
251
|
+
* @param amount - Quantidade para comparação
|
|
252
|
+
* @returns true se as quantidades são iguais (valor e unidade), false caso contrário
|
|
253
|
+
* @example
|
|
254
|
+
* const amount1 = new AmountEdge(5, MassUnitPoint.gram);
|
|
255
|
+
* const amount2 = new AmountEdge(5, MassUnitPoint.gram);
|
|
256
|
+
* amount1.isEqual(amount2); // true
|
|
257
|
+
*/
|
|
258
|
+
isEqual(amount) {
|
|
259
|
+
return (guardian_1._.isEqual(this._value, amount.value) && guardian_1._.isEqual(this._unit, amount.unit));
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Verifica se esta quantidade é diferente de outra.
|
|
263
|
+
* @param amount - Quantidade para comparação
|
|
264
|
+
* @returns true se as quantidades são diferentes, false caso contrário
|
|
265
|
+
* @example
|
|
266
|
+
* const amount1 = new AmountEdge(5, MassUnitPoint.gram);
|
|
267
|
+
* const amount2 = new AmountEdge(3, MassUnitPoint.gram);
|
|
268
|
+
* amount1.isDifferent(amount2); // true
|
|
269
|
+
*/
|
|
270
|
+
isDifferent(amount) {
|
|
271
|
+
return !this.isEqual(amount);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Verifica se esta quantidade é maior que outra.
|
|
275
|
+
* @param amount - Quantidade para comparação
|
|
276
|
+
* @returns true se this.value > amount.value, false caso contrário
|
|
277
|
+
* @example
|
|
278
|
+
* const amount1 = new AmountEdge(10, MassUnitPoint.gram);
|
|
279
|
+
* const amount2 = new AmountEdge(5, MassUnitPoint.gram);
|
|
280
|
+
* amount1.isGreaterThan(amount2); // true
|
|
281
|
+
*/
|
|
282
|
+
isGreaterThan(amount) {
|
|
283
|
+
return this._value > amount.value;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Verifica se esta quantidade é menor que outra.
|
|
287
|
+
* @param amount - Quantidade para comparação
|
|
288
|
+
* @returns true se this.value < amount.value, false caso contrário
|
|
289
|
+
* @example
|
|
290
|
+
* const amount1 = new AmountEdge(3, MassUnitPoint.gram);
|
|
291
|
+
* const amount2 = new AmountEdge(5, MassUnitPoint.gram);
|
|
292
|
+
* amount1.isLessThan(amount2); // true
|
|
293
|
+
*/
|
|
294
|
+
isLessThan(amount) {
|
|
295
|
+
return this._value < amount.value;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Verifica se esta quantidade é maior ou igual a outra.
|
|
299
|
+
* @param amount - Quantidade para comparação
|
|
300
|
+
* @returns true se this.value >= amount.value, false caso contrário
|
|
301
|
+
* @example
|
|
302
|
+
* const amount1 = new AmountEdge(5, MassUnitPoint.gram);
|
|
303
|
+
* const amount2 = new AmountEdge(5, MassUnitPoint.gram);
|
|
304
|
+
* amount1.isGreaterThanOrEqual(amount2); // true
|
|
305
|
+
*/
|
|
306
|
+
isGreaterThanOrEqual(amount) {
|
|
307
|
+
return this._value >= amount.value;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Verifica se esta quantidade é menor ou igual a outra.
|
|
311
|
+
* @param amount - Quantidade para comparação
|
|
312
|
+
* @returns true se this.value <= amount.value, false caso contrário
|
|
313
|
+
* @example
|
|
314
|
+
* const amount1 = new AmountEdge(5, MassUnitPoint.gram);
|
|
315
|
+
* const amount2 = new AmountEdge(5, MassUnitPoint.gram);
|
|
316
|
+
* amount1.isLessThanOrEqual(amount2); // true
|
|
317
|
+
*/
|
|
318
|
+
isLessThanOrEqual(amount) {
|
|
319
|
+
return this._value <= amount.value;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Verifica se esta quantidade está entre dois limites (inclusive).
|
|
323
|
+
* @param min - Quantidade mínima
|
|
324
|
+
* @param max - Quantidade máxima
|
|
325
|
+
* @returns true se o valor está entre min e max, false caso contrário
|
|
326
|
+
* @example
|
|
327
|
+
* const amount = new AmountEdge(5, MassUnitPoint.gram);
|
|
328
|
+
* const min = new AmountEdge(1, MassUnitPoint.gram);
|
|
329
|
+
* const max = new AmountEdge(10, MassUnitPoint.gram);
|
|
330
|
+
* amount.isBetween(min, max); // true
|
|
331
|
+
*/
|
|
332
|
+
isBetween(min, max) {
|
|
333
|
+
return this.isGreaterThanOrEqual(min) && this.isLessThanOrEqual(max);
|
|
94
334
|
}
|
|
95
|
-
|
|
96
|
-
|
|
335
|
+
// ==================== Validações ====================
|
|
336
|
+
/**
|
|
337
|
+
* Verifica se o valor é zero.
|
|
338
|
+
* @returns true se this.value === 0, false caso contrário
|
|
339
|
+
* @example
|
|
340
|
+
* const amount = new AmountEdge(0, MassUnitPoint.gram);
|
|
341
|
+
* amount.isZero(); // true
|
|
342
|
+
*/
|
|
343
|
+
isZero() {
|
|
344
|
+
return this._value === 0;
|
|
97
345
|
}
|
|
346
|
+
/**
|
|
347
|
+
* Verifica se o valor é positivo (maior que zero).
|
|
348
|
+
* @returns true se this.value > 0, false caso contrário
|
|
349
|
+
* @example
|
|
350
|
+
* const amount = new AmountEdge(5, MassUnitPoint.gram);
|
|
351
|
+
* amount.isPositive(); // true
|
|
352
|
+
*/
|
|
353
|
+
isPositive() {
|
|
354
|
+
return this._value > 0;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Verifica se o valor é negativo (menor que zero).
|
|
358
|
+
* @returns true se this.value < 0, false caso contrário
|
|
359
|
+
* @example
|
|
360
|
+
* const amount = new AmountEdge(-5, MassUnitPoint.gram);
|
|
361
|
+
* amount.isNegative(); // true
|
|
362
|
+
*/
|
|
363
|
+
isNegative() {
|
|
364
|
+
return this._value < 0;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Verifica se a unidade é igual a uma unidade específica.
|
|
368
|
+
* @param unit - Unidade a comparar
|
|
369
|
+
* @returns true se a unidade é igual, false caso contrário
|
|
370
|
+
* @example
|
|
371
|
+
* const amount = new AmountEdge(5, MassUnitPoint.gram);
|
|
372
|
+
* amount.hasUnit(MassUnitPoint.gram); // true
|
|
373
|
+
* amount.hasUnit(MassUnitPoint.kilogram); // false
|
|
374
|
+
*/
|
|
375
|
+
hasUnit(unit) {
|
|
376
|
+
return guardian_1._.isEqual(this._unit, unit);
|
|
377
|
+
}
|
|
378
|
+
// ==================== Clonagem e Imutabilidade ====================
|
|
379
|
+
/**
|
|
380
|
+
* Cria uma cópia independente desta quantidade.
|
|
381
|
+
* Útil para evitar mutações indesejadas.
|
|
382
|
+
* @returns Nova instância com os mesmos valores
|
|
383
|
+
* @example
|
|
384
|
+
* const amount1 = new AmountEdge(5, MassUnitPoint.gram);
|
|
385
|
+
* const amount2 = amount1.clone();
|
|
386
|
+
* amount2.multiplyBy(2); // amount1 continua 5, amount2 é 10
|
|
387
|
+
*/
|
|
388
|
+
clone() {
|
|
389
|
+
return new AmountEdge(this._value, this._unit);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Retorna uma nova instância com o valor alterado.
|
|
393
|
+
* Mantém a imutabilidade da instância original.
|
|
394
|
+
* @param value - Novo valor
|
|
395
|
+
* @returns Nova instância com o valor alterado
|
|
396
|
+
* @example
|
|
397
|
+
* const amount = new AmountEdge(5, MassUnitPoint.gram);
|
|
398
|
+
* const newAmount = amount.withValue(10); // amount continua 5, newAmount é 10
|
|
399
|
+
*/
|
|
400
|
+
withValue(value) {
|
|
401
|
+
return new AmountEdge(value, this._unit);
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Retorna uma nova instância com a unidade alterada.
|
|
405
|
+
* Mantém a imutabilidade da instância original.
|
|
406
|
+
* @param unit - Nova unidade
|
|
407
|
+
* @returns Nova instância com a unidade alterada
|
|
408
|
+
* @example
|
|
409
|
+
* const amount = new AmountEdge(5, MassUnitPoint.gram);
|
|
410
|
+
* const newAmount = amount.withUnit(MassUnitPoint.kilogram);
|
|
411
|
+
*/
|
|
412
|
+
withUnit(unit) {
|
|
413
|
+
return new AmountEdge(this._value, unit);
|
|
414
|
+
}
|
|
415
|
+
// ==================== Formatação ====================
|
|
416
|
+
/**
|
|
417
|
+
* Retorna uma representação em string da quantidade.
|
|
418
|
+
* @returns String no formato "valor unidade"
|
|
419
|
+
* @example
|
|
420
|
+
* const amount = new AmountEdge(5, MassUnitPoint.gram);
|
|
421
|
+
* amount.toString(); // "5 g"
|
|
422
|
+
*/
|
|
98
423
|
toString() {
|
|
99
424
|
return `${this._value.toString()} ${this._unit}`;
|
|
100
425
|
}
|
|
426
|
+
/**
|
|
427
|
+
* Formata a quantidade como string com separador de milhar e precisão decimal.
|
|
428
|
+
* @param locale - Localização para formatação (ex: 'en-US', 'pt-BR')
|
|
429
|
+
* @param precision - Número de casas decimais
|
|
430
|
+
* @returns String formatada
|
|
431
|
+
* @example
|
|
432
|
+
* const amount = new AmountEdge(1234.5678, MassUnitPoint.gram);
|
|
433
|
+
* amount.toFormattedString('pt-BR', 2); // "1.234,57 g"
|
|
434
|
+
*/
|
|
435
|
+
toFormattedString(locale = "pt-BR", precision = 2) {
|
|
436
|
+
const formatted = this._value.toLocaleString(locale, {
|
|
437
|
+
minimumFractionDigits: precision,
|
|
438
|
+
maximumFractionDigits: precision,
|
|
439
|
+
});
|
|
440
|
+
return `${formatted} ${this._unit}`;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Retorna apenas o valor como string com precisão específica.
|
|
444
|
+
* @param precision - Número de casas decimais
|
|
445
|
+
* @returns String do valor formatado
|
|
446
|
+
* @example
|
|
447
|
+
* const amount = new AmountEdge(5.6789, MassUnitPoint.gram);
|
|
448
|
+
* amount.toValueString(2); // "5.68"
|
|
449
|
+
*/
|
|
450
|
+
toValueString(precision = 2) {
|
|
451
|
+
return this._value.toFixed(precision);
|
|
452
|
+
}
|
|
453
|
+
// ==================== Conversão ====================
|
|
454
|
+
/**
|
|
455
|
+
* Converte a quantidade para um objeto Polygon.
|
|
456
|
+
* @param precision - Número de casas decimais
|
|
457
|
+
* @returns Objeto polygon com valor e unidade
|
|
458
|
+
* @example
|
|
459
|
+
* const amount = new AmountEdge(5.6789, MassUnitPoint.gram);
|
|
460
|
+
* amount.toPolygon(2); // { value: 5.68, unit: MassUnitPoint.gram }
|
|
461
|
+
*/
|
|
101
462
|
toPolygon(precision = 2) {
|
|
102
463
|
return {
|
|
103
464
|
value: this._unit === mass_unit_point_1.MassUnitPoint.gram
|
|
@@ -106,5 +467,133 @@ class AmountEdge {
|
|
|
106
467
|
unit: this._unit,
|
|
107
468
|
};
|
|
108
469
|
}
|
|
470
|
+
/**
|
|
471
|
+
* Converte a quantidade para um objeto JSON.
|
|
472
|
+
* @returns Objeto JSON com value e unit
|
|
473
|
+
* @example
|
|
474
|
+
* const amount = new AmountEdge(5, MassUnitPoint.gram);
|
|
475
|
+
* amount.toJSON(); // { value: 5, unit: "g" }
|
|
476
|
+
*/
|
|
477
|
+
toJSON() {
|
|
478
|
+
return {
|
|
479
|
+
value: this._value,
|
|
480
|
+
unit: this._unit,
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
// ==================== Métodos Estáticos ====================
|
|
484
|
+
/**
|
|
485
|
+
* Cria uma nova quantidade com valor zero.
|
|
486
|
+
* @template T - Tipo da unidade
|
|
487
|
+
* @param unit - Unidade da quantidade
|
|
488
|
+
* @returns Nova instância com valor zero
|
|
489
|
+
* @example
|
|
490
|
+
* const zero = AmountEdge.zero(MassUnitPoint.gram); // 0 g
|
|
491
|
+
*/
|
|
492
|
+
static zero(unit) {
|
|
493
|
+
return new AmountEdge(0, unit);
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Soma uma lista de quantidades.
|
|
497
|
+
* @template T - Tipo da unidade
|
|
498
|
+
* @param amounts - Array de quantidades a somar
|
|
499
|
+
* @returns Nova instância com a soma total
|
|
500
|
+
* @example
|
|
501
|
+
* const amounts = [
|
|
502
|
+
* new AmountEdge(5, MassUnitPoint.gram),
|
|
503
|
+
* new AmountEdge(3, MassUnitPoint.gram),
|
|
504
|
+
* new AmountEdge(2, MassUnitPoint.gram),
|
|
505
|
+
* ];
|
|
506
|
+
* const total = AmountEdge.sumAll(amounts); // 10 g
|
|
507
|
+
*/
|
|
508
|
+
static sumAll(amounts) {
|
|
509
|
+
if (amounts.length === 0) {
|
|
510
|
+
throw new Error("Cannot sum an empty array of amounts");
|
|
511
|
+
}
|
|
512
|
+
const total = amounts[0].clone();
|
|
513
|
+
for (let i = 1; i < amounts.length; i++) {
|
|
514
|
+
total.sum(amounts[i]);
|
|
515
|
+
}
|
|
516
|
+
return total;
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Retorna a quantidade máxima de uma lista.
|
|
520
|
+
* @template T - Tipo da unidade
|
|
521
|
+
* @param amounts - Array de quantidades
|
|
522
|
+
* @returns Nova instância com o maior valor
|
|
523
|
+
* @example
|
|
524
|
+
* const amounts = [
|
|
525
|
+
* new AmountEdge(5, MassUnitPoint.gram),
|
|
526
|
+
* new AmountEdge(10, MassUnitPoint.gram),
|
|
527
|
+
* new AmountEdge(3, MassUnitPoint.gram),
|
|
528
|
+
* ];
|
|
529
|
+
* const max = AmountEdge.max(amounts); // 10 g
|
|
530
|
+
*/
|
|
531
|
+
static max(amounts) {
|
|
532
|
+
if (amounts.length === 0) {
|
|
533
|
+
throw new Error("Cannot find max of an empty array of amounts");
|
|
534
|
+
}
|
|
535
|
+
let maxAmount = amounts[0].clone();
|
|
536
|
+
for (let i = 1; i < amounts.length; i++) {
|
|
537
|
+
if (amounts[i].isGreaterThan(maxAmount)) {
|
|
538
|
+
maxAmount = amounts[i].clone();
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
return maxAmount;
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Retorna a quantidade mínima de uma lista.
|
|
545
|
+
* @template T - Tipo da unidade
|
|
546
|
+
* @param amounts - Array de quantidades
|
|
547
|
+
* @returns Nova instância com o menor valor
|
|
548
|
+
* @example
|
|
549
|
+
* const amounts = [
|
|
550
|
+
* new AmountEdge(5, MassUnitPoint.gram),
|
|
551
|
+
* new AmountEdge(10, MassUnitPoint.gram),
|
|
552
|
+
* new AmountEdge(3, MassUnitPoint.gram),
|
|
553
|
+
* ];
|
|
554
|
+
* const min = AmountEdge.min(amounts); // 3 g
|
|
555
|
+
*/
|
|
556
|
+
static min(amounts) {
|
|
557
|
+
if (amounts.length === 0) {
|
|
558
|
+
throw new Error("Cannot find min of an empty array of amounts");
|
|
559
|
+
}
|
|
560
|
+
let minAmount = amounts[0].clone();
|
|
561
|
+
for (let i = 1; i < amounts.length; i++) {
|
|
562
|
+
if (amounts[i].isLessThan(minAmount)) {
|
|
563
|
+
minAmount = amounts[i].clone();
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return minAmount;
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Calcula a média de uma lista de quantidades.
|
|
570
|
+
* @template T - Tipo da unidade
|
|
571
|
+
* @param amounts - Array de quantidades
|
|
572
|
+
* @returns Nova instância com a média dos valores
|
|
573
|
+
* @example
|
|
574
|
+
* const amounts = [
|
|
575
|
+
* new AmountEdge(10, MassUnitPoint.gram),
|
|
576
|
+
* new AmountEdge(20, MassUnitPoint.gram),
|
|
577
|
+
* new AmountEdge(30, MassUnitPoint.gram),
|
|
578
|
+
* ];
|
|
579
|
+
* const average = AmountEdge.average(amounts); // 20 g
|
|
580
|
+
*/
|
|
581
|
+
static average(amounts) {
|
|
582
|
+
const total = AmountEdge.sumAll(amounts);
|
|
583
|
+
total.divideBy(amounts.length);
|
|
584
|
+
return total;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Cria uma quantidade a partir de um objeto Polygon.
|
|
588
|
+
* @template T - Tipo da unidade
|
|
589
|
+
* @param polygon - Objeto polygon com value e unit
|
|
590
|
+
* @returns Nova instância de AmountEdge
|
|
591
|
+
* @example
|
|
592
|
+
* const polygon = { value: 5, unit: MassUnitPoint.gram };
|
|
593
|
+
* const amount = AmountEdge.fromPolygon(polygon); // 5 g
|
|
594
|
+
*/
|
|
595
|
+
static fromPolygon(polygon) {
|
|
596
|
+
return new AmountEdge(polygon.value, polygon.unit);
|
|
597
|
+
}
|
|
109
598
|
}
|
|
110
599
|
exports.AmountEdge = AmountEdge;
|