From 344c33c54ca100af4bebfc9f123b0032ede813ed Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sat, 25 Sep 2021 21:41:06 +0200 Subject: [PATCH] Added localize implementation along with its tests --- spec/PKPass.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- src/PKPass.ts | 39 ++++++++++++++++++++++++++------------- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/spec/PKPass.ts b/spec/PKPass.ts index 0069842..fc813fa 100644 --- a/spec/PKPass.ts +++ b/spec/PKPass.ts @@ -1,4 +1,8 @@ -import { default as PKPass } from "../lib/PKPass"; +import { + default as PKPass, + localizationSymbol, + propsSymbol, +} from "../lib/PKPass"; describe("PKPass", () => { describe("setBeacons", () => { @@ -389,4 +393,48 @@ describe("PKPass", () => { expect(passCP.transitType).toBeUndefined(); */ }); }); + + describe("localize", () => { + const pass = new PKPass({}, {}, {}); + + it("should create a new language record inside class props", () => { + pass.localize("en"); + + expect(pass[localizationSymbol]["en"]).toEqual({}); + }); + + it("should save some translations to be exported later", () => { + pass.localize("it", { + say_hi: "ciao", + say_gb: "arrivederci", + }); + + expect(pass[localizationSymbol]["it"]).toEqual({ + say_hi: "ciao", + say_gb: "arrivederci", + }); + }); + + it("should accept later translations and merge them with existing ones", () => { + pass.localize("it", { + say_good_morning: "buongiorno", + say_good_evening: "buonasera", + }); + + expect(pass[localizationSymbol]["it"]).toEqual({ + say_hi: "ciao", + say_gb: "arrivederci", + say_good_morning: "buongiorno", + say_good_evening: "buonasera", + }); + }); + + it("should delete a language and its all translations when null is passed as parameter", () => { + pass.localize("it", null); + pass.localize("en", null); + + expect(pass[localizationSymbol]["it"]).toBeUndefined(); + expect(pass[localizationSymbol]["en"]).toBeUndefined(); + }); + }); }); diff --git a/src/PKPass.ts b/src/PKPass.ts index b775645..6bbb56e 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -5,10 +5,12 @@ import { getModelFolderContents } from "./parser"; import * as Schemas from "./schemas"; import { Stream } from "stream"; +/** Exporting for tests specs */ +export const propsSymbol = Symbol("props"); +export const localizationSymbol = Symbol("pass.l10n"); + const fieldKeysPoolSymbol = Symbol("fieldKeysPoolSymbol"); -const propsSymbol = Symbol("props"); const importMetadataSymbol = Symbol("import.pass.metadata"); -const localizationSymbol = Symbol("pass.l10n"); interface NamedBuffers { [key: string]: Buffer; @@ -363,6 +365,7 @@ export default class PKPass extends Bundle { * @TODO compile this pass into something usable * @TODO like _patch on old version * @TODO share implementation with getAsStream + * @TODO Build back translations */ return super.getAsBuffer(); @@ -381,6 +384,7 @@ export default class PKPass extends Bundle { * @TODO compile this pass into something usable * @TODO like _patch on old version * @TODO share implementation with getAsBuffer + * @TODO Build back translations */ return super.getAsStream(); @@ -392,24 +396,33 @@ export default class PKPass extends Bundle { /** * Allows to specify a language to be added to the - * final bundle, along with some optionals / additional - * translations. + * final bundle, along with some optionals translations. * - * If the language already exists in the origin source, - * translations will be added to the existing ones. + * If the language already exists, translations will be + * merged with the existing ones. + * + * Setting `translations` to `null`, fully deletes a language + * and its translations. * * @see https://developer.apple.com/documentation/walletpasses/creating_the_source_for_a_pass#3736718 * @param lang * @param translations */ - localize(lang: string, translations?: any): this { - /** - * @TODO change translations format - * @TODO specify a way to get current ones deleted - * @TODO Default languages from source - * @TODO print warning if lang is already in selection? - */ + localize( + lang: string, + translations?: { [key: string]: string } | null, + ): this { + if (translations === null) { + delete this[localizationSymbol][lang]; + return this; + } + + this[localizationSymbol][lang] ??= {}; + + if (typeof translations === "object" && !Array.isArray(translations)) { + Object.assign(this[localizationSymbol][lang], translations); + } return this; }