From 960331d7371ba00cc72625cc112010e9db30c804 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sun, 7 Feb 2021 17:23:33 +0100 Subject: [PATCH] Changed splitBufferBundle to use Object.entries and added tests for it --- package.json | 2 +- spec/index.ts | 3 ++- spec/utils.ts | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.ts | 26 +++++++++++++++++------ 4 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 spec/utils.ts diff --git a/package.json b/package.json index 1fd477d..6c7786b 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "build:examples": "cd examples && npm run build", "build:spec": "rm -rf ./spec/*.js && npx tsc", "prepublishOnly": "npm run build", - "test": "npm run build:spec && npm run build:src && jasmine spec/index.js", + "test": "npm run build:spec && npm run build:src && jasmine spec/*.js", "example": "npm run build:src && npm --prefix examples run example" }, "author": "Alexander Patrick Cerutti", diff --git a/spec/index.ts b/spec/index.ts index 532ee86..4edb1d6 100644 --- a/spec/index.ts +++ b/spec/index.ts @@ -113,10 +113,11 @@ describe("Passkit-generator", function () { }); describe("expiration()", () => { - it("Won't apply changes withouta valid argument", () => { + it("Won't apply changes without a valid argument", () => { // @ts-expect-error pass.expiration(); expect(pass.props["expirationDate"]).toBe(undefined); + // @ts-expect-error pass.expiration(42); expect(pass.props["expirationDate"]).toBe(undefined); diff --git a/spec/utils.ts b/spec/utils.ts new file mode 100644 index 0000000..e956f5a --- /dev/null +++ b/spec/utils.ts @@ -0,0 +1,59 @@ +import { splitBufferBundle } from "../lib/utils"; +import type { BundleUnit } from "../src/schema"; + +describe("splitBufferBundle", () => { + it("should split the bundle in language-organized files buffers and normal files with valid bundleUnit passed", () => { + const zeroBuffer = Buffer.alloc(0); + const payload: BundleUnit = { + "en.lproj/thumbnail@2x.png": zeroBuffer, + "de.lproj/background@2x.png": zeroBuffer, + "it.lproj/thumbnail@2x.png": zeroBuffer, + "thumbnail@2x.png": zeroBuffer, + "background.png": zeroBuffer + }; + + const result = splitBufferBundle(payload); + + expect(result).toBeDefined(); + expect(result.length).toBe(2); + expect(result[0]).toEqual({ + "en.lproj": { + "thumbnail@2x.png": zeroBuffer, + }, + "de.lproj": { + "background@2x.png": zeroBuffer, + }, + "it.lproj": { + "thumbnail@2x.png": zeroBuffer, + } + }); + expect(result[1]).toEqual({ + "thumbnail@2x.png": zeroBuffer, + "background.png": zeroBuffer + }); + }); + + it("should return empty partitionedBufferBundle if BundleUnit is empty object", () => { + const result = splitBufferBundle({}); + + expect(result).toBeDefined(); + expect(result.length).toBe(2); + expect(result[0]).toEqual({}); + expect(result[1]).toEqual({}); + }); + + it("should return empty partitionedBufferBundle if BundleUnit is undefined", () => { + const resultUndefined = splitBufferBundle(undefined); + const resultNull = splitBufferBundle(null); + + expect(resultUndefined).toBeDefined(); + expect(resultUndefined.length).toBe(2); + expect(resultUndefined[0]).toEqual({}); + expect(resultUndefined[1]).toEqual({}); + + expect(resultNull).toBeDefined(); + expect(resultNull.length).toBe(2); + expect(resultNull[0]).toEqual({}); + expect(resultNull[1]).toEqual({}); + }); +}); diff --git a/src/utils.ts b/src/utils.ts index 2bad300..0fb9fdb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -91,20 +91,32 @@ export function generateStringFile(lang: { [index: string]: string }): Buffer { type PartitionedBundleElements = [PartitionedBundle["l10nBundle"], PartitionedBundle["bundle"]]; -export function splitBufferBundle(origin: any): PartitionedBundleElements { - return Object.keys(origin).reduce(([l10n, bundle], current) => { - if (!current.includes(".lproj")) { - return [l10n, { ...bundle, [current]: origin[current] }]; +export function splitBufferBundle(origin: BundleUnit): PartitionedBundleElements { + const initialValue: PartitionedBundleElements = [{}, {}]; + + if (!origin) { + return initialValue; + } + + return Object.entries(origin).reduce(([l10n, bundle], [key, buffer]) => { + if (!key.includes(".lproj")) { + return [ + l10n, + { + ...bundle, + [key]: buffer + } + ]; } - const pathComponents = current.split(sep); + const pathComponents = key.split(sep); const lang = pathComponents[0]; const file = pathComponents.slice(1).join("/"); - (l10n[lang] || (l10n[lang] = {}))[file] = origin[current]; + (l10n[lang] || (l10n[lang] = {}))[file] = buffer; return [l10n, bundle]; - }, [{}, {}]); + }, initialValue); } type StringSearchMode = "includes" | "startsWith" | "endsWith";