Feat/lhac 202 reshape images and money (#4)

* feat: add reshape functions for money and image
This commit is contained in:
leonmargaritis 2023-11-17 15:27:49 +01:00 committed by GitHub
parent dc586ce64a
commit 397efc9ff4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,67 @@
import { reshapeMoney, reshapeImage } from ".";
import {
CentPrecisionMoney as CommercetoolsCentPrecisionMoney,
HighPrecisionMoney as CommercetoolsHighPrecisionMoney
} from "@commercetools/platform-sdk";
describe("reshape & fetch functions", () => {
describe("reshapeMoney", () => {
it("returns correct amount for cent precision", () => {
const centPrecisionMoney: CommercetoolsCentPrecisionMoney = {
type: "centPrecision",
centAmount: 1001,
currencyCode: "EUR",
fractionDigits: 2
};
const result = reshapeMoney(centPrecisionMoney);
const expectedResult = { amount: "10.01", currencyCode: "EUR" };
expect(result).toEqual(expectedResult);
});
it("returns correct amount for high precision", () => {
const highPrecisionMoney: CommercetoolsHighPrecisionMoney = {
type: "highPrecision",
preciseAmount: 100001,
centAmount: 1000,
currencyCode: "EUR",
fractionDigits: 4
};
const result = reshapeMoney(highPrecisionMoney);
const expectedResult = { amount: "10.0001", currencyCode: "EUR" };
expect(result).toEqual(expectedResult);
});
});
describe("reshapeImage", () => {
it("returns correctly reshaped image with alt text", () => {
const image = {
url: "an-url",
dimensions: { w: 800, h: 600 },
label: "a-label"
};
const result = reshapeImage(image);
const expectedResult = { url: "an-url", width: 800, height: 600, altText: "a-label" };
expect(result).toEqual(expectedResult);
});
it("returns correctly reshaped image with empty string as alt text", () => {
const image = {
url: "an-url",
dimensions: { w: 800, h: 600 }
};
const result = reshapeImage(image);
const expectedResult = { url: "an-url", width: 800, height: 600, altText: "" };
expect(result).toEqual(expectedResult);
});
});
});

View File

@ -0,0 +1,25 @@
import {
TypedMoney as CommercetoolsTypedMoney,
Image as CommercetoolsImage,
CentPrecisionMoney,
HighPrecisionMoney,
TypedMoney
} from "@commercetools/platform-sdk";
import { Image, Money } from "./types";
export function reshapeMoney(typedMoney: TypedMoney): Money {
const { fractionDigits, currencyCode, type } = typedMoney;
const typedAmount = type === "centPrecision" ? typedMoney.centAmount : typedMoney.preciseAmount;
const amount = (typedAmount / Math.pow(10, fractionDigits)).toString();
return { amount, currencyCode };
}
export function reshapeImage(image: CommercetoolsImage): Image {
return {
url: image.url,
width: image.dimensions.w,
height: image.dimensions.h,
altText: image.label || ""
};
}