.
This commit is contained in:
parent
5ea5010fd0
commit
33d7038e57
@ -1,6 +1,46 @@
|
||||
import { Quantity } from "./Quantity"; // Asegúrate de importar correctamente la clase Quantity.
|
||||
|
||||
describe("Quantity Value Object", () => {
|
||||
it("Should create a valid quantity (number)", () => {
|
||||
const validQuantity = Quantity.createFromFormattedValue(5);
|
||||
|
||||
expect(validQuantity.isSuccess).toBe(true);
|
||||
expect(validQuantity.object.toNumber()).toBe(5);
|
||||
});
|
||||
|
||||
it("Should create a valid quantity (number)", () => {
|
||||
const validQuantity = Quantity.createFromFormattedValue("5,31");
|
||||
|
||||
expect(validQuantity.isSuccess).toBe(true);
|
||||
expect(validQuantity.object.toNumber()).toBe(5.31);
|
||||
expect(validQuantity.object.toObject()).toStrictEqual({
|
||||
amount: 531,
|
||||
scale: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it("Should create a valid quantity (number)", () => {
|
||||
const validQuantity = Quantity.createFromFormattedValue("99,");
|
||||
|
||||
expect(validQuantity.isSuccess).toBe(true);
|
||||
expect(validQuantity.object.toNumber()).toBe(99);
|
||||
expect(validQuantity.object.toObject()).toStrictEqual({
|
||||
amount: 990,
|
||||
scale: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("Should create a valid quantity (number)", () => {
|
||||
const validQuantity = Quantity.createFromFormattedValue("9,1");
|
||||
|
||||
expect(validQuantity.isSuccess).toBe(true);
|
||||
expect(validQuantity.object.toNumber()).toBe(9.1);
|
||||
expect(validQuantity.object.toObject()).toStrictEqual({
|
||||
amount: 91,
|
||||
scale: 1,
|
||||
});
|
||||
});
|
||||
|
||||
// Prueba la creación de una cantidad válida.
|
||||
it("Should create a valid quantity (number)", () => {
|
||||
const validQuantity = Quantity.create({ amount: 5 });
|
||||
@ -68,7 +108,7 @@ describe("Quantity Value Object", () => {
|
||||
const quantity = Quantity.create({ amount: 1500 }).object;
|
||||
const result = quantity.toString();
|
||||
|
||||
expect(result).toBe("15");
|
||||
expect(result).toBe("15.00");
|
||||
});
|
||||
|
||||
// Prueba la operación de incremento.
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import Joi from "joi";
|
||||
import { isNull } from "lodash";
|
||||
import { NullOr } from "../../../../utilities";
|
||||
import { RuleValidator } from "../RuleValidator";
|
||||
import { INullableValueObjectOptions, NullableValueObject } from "./NullableValueObject";
|
||||
@ -21,7 +20,7 @@ interface IQuantity {
|
||||
}
|
||||
|
||||
export interface QuantityObject {
|
||||
amount: number;
|
||||
amount: number | null;
|
||||
scale: number;
|
||||
}
|
||||
|
||||
@ -43,9 +42,9 @@ export class Quantity extends NullableValueObject<IQuantity> {
|
||||
scale: NullOr<number>,
|
||||
options: IQuantityOptions = {}
|
||||
) {
|
||||
const ruleNull = RuleValidator.RULE_ALLOW_NULL_OR_UNDEFINED.default(
|
||||
defaultQuantityProps.amount
|
||||
);
|
||||
const ruleNull = RuleValidator.RULE_ALLOW_NULL_OR_UNDEFINED;
|
||||
|
||||
const ruleEmpty = RuleValidator.RULE_ALLOW_EMPTY;
|
||||
|
||||
const ruleNumber = RuleValidator.RULE_IS_TYPE_NUMBER.label(
|
||||
options.label ? options.label : "quantity"
|
||||
@ -60,7 +59,7 @@ export class Quantity extends NullableValueObject<IQuantity> {
|
||||
.max(Quantity.MAX_SCALE)
|
||||
.label(options.label ? options.label : "quantity");
|
||||
|
||||
const rules = Joi.alternatives(ruleNull, ruleNumber, ruleString);
|
||||
const rules = Joi.alternatives(ruleNull, ruleEmpty, ruleNumber, ruleString);
|
||||
|
||||
const validationResults = new ResultCollection([
|
||||
RuleValidator.validate<NullOr<number>>(
|
||||
@ -81,7 +80,7 @@ export class Quantity extends NullableValueObject<IQuantity> {
|
||||
let numericValue = typeof value === "string" ? parseInt(value, 10) : Number(value);
|
||||
|
||||
// Check if scale is null, and set to default if so
|
||||
let numericScale = isNull(scale) ? Quantity.DEFAULT_SCALE : Number(scale);
|
||||
let numericScale = scale === null ? Quantity.DEFAULT_SCALE : Number(scale);
|
||||
|
||||
// Calculate the adjusted value
|
||||
const adjustedValue = numericValue / Math.pow(10, numericScale);
|
||||
@ -113,11 +112,97 @@ export class Quantity extends NullableValueObject<IQuantity> {
|
||||
let _amount: NullOr<number> = Quantity._sanitize(amount);
|
||||
|
||||
const _props = {
|
||||
amount: isNull(_amount) ? 0 : _amount,
|
||||
amount: _amount === null ? 0 : _amount,
|
||||
scale,
|
||||
};
|
||||
|
||||
return Result.ok<Quantity>(new this(_props, isNull(_amount), options));
|
||||
console.log(_props);
|
||||
|
||||
return Result.ok<Quantity>(new Quantity(_props, _amount === null, options));
|
||||
}
|
||||
|
||||
public static createFromFormattedValue(
|
||||
value: NullOr<number | string>,
|
||||
_options: IQuantityOptions = {}
|
||||
) {
|
||||
if (value === null || value === "") {
|
||||
return Quantity.create({
|
||||
amount: null,
|
||||
scale: Quantity.DEFAULT_SCALE,
|
||||
});
|
||||
}
|
||||
|
||||
const valueStr = String(value);
|
||||
const [integerPart, decimalPart] = valueStr.split(",");
|
||||
|
||||
console.log(integerPart, decimalPart);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log({
|
||||
amount: _amount,
|
||||
scale: _scale,
|
||||
});
|
||||
|
||||
return Quantity.create(
|
||||
{
|
||||
amount: _amount,
|
||||
scale: _scale,
|
||||
},
|
||||
_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> {
|
||||
@ -132,14 +217,18 @@ export class Quantity extends NullableValueObject<IQuantity> {
|
||||
return _value;
|
||||
}
|
||||
|
||||
private static _toNumber(value: NullOr<number>, scale: number): number {
|
||||
if (isNull(value)) {
|
||||
return 0;
|
||||
private static _toString(value: NullOr<number>, scale: number): string {
|
||||
if (value === null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const factor = Math.pow(10, scale);
|
||||
const amount = Number(value) / factor;
|
||||
return Number(amount.toFixed(scale));
|
||||
const result = amount.toFixed(scale);
|
||||
|
||||
console.log({ result });
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
constructor(quantity: IQuantity, isNull: boolean, options: IQuantityOptions) {
|
||||
@ -172,12 +261,13 @@ export class Quantity extends NullableValueObject<IQuantity> {
|
||||
return this._isNull;
|
||||
};
|
||||
|
||||
public toNumber(): number {
|
||||
return Quantity._toNumber(this.amount, this.scale);
|
||||
public toString(): string {
|
||||
console.log("paso");
|
||||
return Quantity._toString(this.amount, this.scale);
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
return this.isNull() ? "" : String(this.toNumber());
|
||||
public toNumber(): number {
|
||||
return this.isNull() ? 0 : Number(this.toString());
|
||||
}
|
||||
|
||||
public toPrimitive(): NullOr<number> {
|
||||
@ -194,7 +284,7 @@ export class Quantity extends NullableValueObject<IQuantity> {
|
||||
|
||||
public toObject(): QuantityObject {
|
||||
return {
|
||||
amount: this.amount ? this.amount : 0,
|
||||
amount: this.amount,
|
||||
scale: this.scale,
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user