From 397efc9ff442e0e26da6ba18313d08283723483c Mon Sep 17 00:00:00 2001 From: leonmargaritis <141131511+leonmargaritis@users.noreply.github.com> Date: Fri, 17 Nov 2023 15:27:49 +0100 Subject: [PATCH] Feat/lhac 202 reshape images and money (#4) * feat: add reshape functions for money and image --- lib/commercetools/index.jest.test.ts | 67 ++++++++++++++++++++++++++++ lib/commercetools/index.ts | 25 +++++++++++ 2 files changed, 92 insertions(+) create mode 100644 lib/commercetools/index.jest.test.ts diff --git a/lib/commercetools/index.jest.test.ts b/lib/commercetools/index.jest.test.ts new file mode 100644 index 000000000..3a9b184d8 --- /dev/null +++ b/lib/commercetools/index.jest.test.ts @@ -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); + }); + }); +}); diff --git a/lib/commercetools/index.ts b/lib/commercetools/index.ts index e69de29bb..d98c5d087 100644 --- a/lib/commercetools/index.ts +++ b/lib/commercetools/index.ts @@ -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 || "" + }; +}