From 07151ec4c825f9ef717e01b1dda5c5b683070f27 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Mon, 18 Oct 2021 00:50:48 +0200 Subject: [PATCH] Added localize with translations to null to delete also files --- spec/PKPass.ts | 8 +++++++- src/PKPass.ts | 18 ++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/spec/PKPass.ts b/spec/PKPass.ts index 3f5e4bb..9ef872c 100644 --- a/spec/PKPass.ts +++ b/spec/PKPass.ts @@ -659,12 +659,18 @@ describe("PKPass", () => { }); }); - it("should delete a language and its all translations when null is passed as parameter", () => { + it("should delete a language, all of its translations and all of its files, when null is passed as parameter", () => { + pass.addBuffer("it.lproj/icon@3x.png", Buffer.alloc(0)); + pass.addBuffer("en.lproj/icon@3x.png", Buffer.alloc(0)); + pass.localize("it", null); pass.localize("en", null); expect(pass[localizationSymbol]["it"]).toBeUndefined(); expect(pass[localizationSymbol]["en"]).toBeUndefined(); + + expect(pass[filesSymbol]["it.lproj/icon@3x.png"]).toBeUndefined(); + expect(pass[filesSymbol]["en.lproj/icon@3x.png"]).toBeUndefined(); }); it("should always return undefined", () => { diff --git a/src/PKPass.ts b/src/PKPass.ts index 1883197..a1267c7 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -675,8 +675,8 @@ export default class PKPass extends Bundle { * If the language already exists, translations will be * merged with the existing ones. * - * Setting `translations` to `null`, fully deletes a language - * and its translations. + * Setting `translations` to `null`, fully deletes a language, + * its translations and its files. * * @see https://developer.apple.com/documentation/walletpasses/creating_the_source_for_a_pass#3736718 * @param lang @@ -695,6 +695,20 @@ export default class PKPass extends Bundle { if (translations === null) { delete this[localizationSymbol][lang]; + + const allFilesKeys = Object.keys(this[filesSymbol]); + const langFolderIdentifier = `${lang}.lproj`; + + for ( + let i = allFilesKeys.length, filePath: string; + (filePath = allFilesKeys[--i]); + + ) { + if (filePath.startsWith(langFolderIdentifier)) { + delete this[filesSymbol][filePath]; + } + } + return; }