.
This commit is contained in:
parent
33d7038e57
commit
d4665a6de6
@ -121,6 +121,54 @@ export class Percentage extends NullableValueObject<IPercentage> {
|
|||||||
return Result.ok<Percentage>(new this(_props, isNull(_amount), options));
|
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> {
|
private static _sanitize(value: NullOr<number | string>): NullOr<number> {
|
||||||
let _value: NullOr<number> = null;
|
let _value: NullOr<number> = null;
|
||||||
|
|
||||||
|
|||||||
@ -116,8 +116,6 @@ export class Quantity extends NullableValueObject<IQuantity> {
|
|||||||
scale,
|
scale,
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log(_props);
|
|
||||||
|
|
||||||
return Result.ok<Quantity>(new Quantity(_props, _amount === null, options));
|
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 valueStr = String(value);
|
||||||
const [integerPart, decimalPart] = valueStr.split(",");
|
const [integerPart, decimalPart] = valueStr.split(",");
|
||||||
|
|
||||||
console.log(integerPart, decimalPart);
|
|
||||||
|
|
||||||
let _amount = integerPart;
|
let _amount = integerPart;
|
||||||
let _scale = 2;
|
let _scale = 2;
|
||||||
|
|
||||||
@ -162,11 +158,6 @@ export class Quantity extends NullableValueObject<IQuantity> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log({
|
|
||||||
amount: _amount,
|
|
||||||
scale: _scale,
|
|
||||||
});
|
|
||||||
|
|
||||||
return Quantity.create(
|
return Quantity.create(
|
||||||
{
|
{
|
||||||
amount: _amount,
|
amount: _amount,
|
||||||
@ -174,35 +165,6 @@ export class Quantity extends NullableValueObject<IQuantity> {
|
|||||||
},
|
},
|
||||||
_options
|
_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> {
|
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 factor = Math.pow(10, scale);
|
||||||
const amount = Number(value) / factor;
|
const amount = Number(value) / factor;
|
||||||
const result = amount.toFixed(scale);
|
return amount.toFixed(scale);
|
||||||
|
|
||||||
console.log({ result });
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(quantity: IQuantity, isNull: boolean, options: IQuantityOptions) {
|
constructor(quantity: IQuantity, isNull: boolean, options: IQuantityOptions) {
|
||||||
@ -262,7 +220,6 @@ export class Quantity extends NullableValueObject<IQuantity> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
public toString(): string {
|
public toString(): string {
|
||||||
console.log("paso");
|
|
||||||
return Quantity._toString(this.amount, this.scale);
|
return Quantity._toString(this.amount, this.scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user