This commit is contained in:
David Arranz 2024-07-16 18:05:52 +02:00
parent 33d7038e57
commit d4665a6de6
2 changed files with 49 additions and 44 deletions

View File

@ -121,6 +121,54 @@ export class Percentage extends NullableValueObject<IPercentage> {
return Result.ok<Percentage>(new this(_props, isNull(_amount), options));
}
public static createFromFormattedValue(
value: NullOr<number | string>,
_options: IPercentageOptions = {}
) {
if (value === null || value === "") {
return Percentage.create({
amount: null,
scale: Percentage.DEFAULT_SCALE,
});
}
const valueStr = String(value);
const [integerPart, decimalPart] = valueStr.split(",");
let _amount = integerPart;
let _scale = 2;
if (decimalPart === undefined) {
// 99
_scale = 0;
} else {
if (decimalPart === "") {
// 99,
_amount = integerPart + decimalPart.padEnd(1, "0");
_scale = 1;
}
if (decimalPart.length === 1) {
// 99,1
_amount = integerPart + decimalPart.padEnd(1, "0");
_scale = 1;
} else {
if (decimalPart.length === 2) {
// 99,12
_amount = integerPart + decimalPart.padEnd(2, "0");
_scale = 2;
}
}
}
return Percentage.create(
{
amount: _amount,
scale: _scale,
},
_options
);
}
private static _sanitize(value: NullOr<number | string>): NullOr<number> {
let _value: NullOr<number> = null;

View File

@ -116,8 +116,6 @@ export class Quantity extends NullableValueObject<IQuantity> {
scale,
};
console.log(_props);
return Result.ok<Quantity>(new Quantity(_props, _amount === null, options));
}
@ -135,8 +133,6 @@ export class Quantity extends NullableValueObject<IQuantity> {
const valueStr = String(value);
const [integerPart, decimalPart] = valueStr.split(",");
console.log(integerPart, decimalPart);
let _amount = integerPart;
let _scale = 2;
@ -162,11 +158,6 @@ export class Quantity extends NullableValueObject<IQuantity> {
}
}
console.log({
amount: _amount,
scale: _scale,
});
return Quantity.create(
{
amount: _amount,
@ -174,35 +165,6 @@ export class Quantity extends NullableValueObject<IQuantity> {
},
_options
);
/*const _amountArray = String(value).split(",") ?? [];
let _amount = _amountArray.join("");
let _scale = 0;
if (_amountArray.length === 2) {
// Hay parte decimal, rellena o no -> 1,34 / 14,3 / 99,
_scale = 2;
if (_amountArray[1].length === 0) {
// 99,
_amount = `${_amount}00`;
}
if (_amountArray[1].length === 1) {
// 99,
_amount = `${_amount}0`;
}
} else {
_scale = 0;
}
const _params = {
amount: _amount,
scale: _scale,
};
return Quantity.create(_params, _options);*/
}
private static _sanitize(value: NullOr<number | string>): NullOr<number> {
@ -224,11 +186,7 @@ export class Quantity extends NullableValueObject<IQuantity> {
const factor = Math.pow(10, scale);
const amount = Number(value) / factor;
const result = amount.toFixed(scale);
console.log({ result });
return result;
return amount.toFixed(scale);
}
constructor(quantity: IQuantity, isNull: boolean, options: IQuantityOptions) {
@ -262,7 +220,6 @@ export class Quantity extends NullableValueObject<IQuantity> {
};
public toString(): string {
console.log("paso");
return Quantity._toString(this.amount, this.scale);
}