From f70257dca68b9ea6d7a0e33f4fff9d479ed172e0 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sun, 3 Oct 2021 22:03:52 +0200 Subject: [PATCH] Splitted strings utils to its own file --- src/PKPass.ts | 71 ++----------------------------------- src/StringsUtils.ts | 85 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 68 deletions(-) create mode 100644 src/StringsUtils.ts diff --git a/src/PKPass.ts b/src/PKPass.ts index 33c0c41..00e72ef 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -6,7 +6,7 @@ import { Stream } from "stream"; import { processDate } from "./processDate"; import forge from "node-forge"; import * as Signature from "./signature"; -import { EOL } from "os"; +import * as Strings from "./StringsUtils"; import { isValidRGB } from "./utils"; /** Exporting for tests specs */ @@ -430,7 +430,7 @@ export default class PKPass extends Bundle { Object.assign( (this[localizationSymbol][lang] ??= {}), - Object.fromEntries(parseStringsFile(buffer).translations), + Object.fromEntries(Strings.parse(buffer).translations), ); return; @@ -565,7 +565,7 @@ export default class PKPass extends Bundle { ) { const [lang, translations] = entry; - const stringsFile = createStringFile(translations); + const stringsFile = Strings.create(translations); if (stringsFile.length) { super.addBuffer(`${lang}.lproj/pass.strings`, stringsFile); @@ -946,68 +946,3 @@ function readPassMetadata(buffer: Buffer) { console.error(err); } } - -function parseStringsFile(buffer: Buffer) { - const fileAsString = buffer.toString("utf8"); - const translationRowRegex = /"(?.+)"\s+=\s+"(?.+)";\n?/; - const commentRowRegex = /\/\*\s*(.+)\s*\*\//; - - let translations: [placeholder: string, value: string][] = []; - let comments: string[] = []; - - let blockStartPoint = 0; - let blockEndPoint = 0; - - do { - if ( - /** New Line, new life */ - /\n/.test(fileAsString[blockEndPoint]) || - /** EOF */ - blockEndPoint === fileAsString.length - ) { - let match: RegExpMatchArray; - - const section = fileAsString.substring( - blockStartPoint, - blockEndPoint + 1, - ); - - if ((match = section.match(translationRowRegex))) { - const { - groups: { key, value }, - } = match; - - translations.push([key, value]); - } else if ((match = section.match(commentRowRegex))) { - const [, content] = match; - - comments.push(content.trimEnd()); - } - - /** Skipping \n and going to the next block. */ - blockEndPoint += 2; - blockStartPoint = blockEndPoint - 1; - } else { - blockEndPoint += 1; - } - } while (blockEndPoint <= fileAsString.length); - - return { - translations, - comments, - }; -} - -function createStringFile(translations: { [key: string]: string }): Buffer { - const stringContents = []; - - const translationsEntries = Object.entries(translations); - - for (let i = 0; i < translationsEntries.length; i++) { - const [key, value] = translationsEntries[i]; - - stringContents.push(`"${key}" = "${value}";`); - } - - return Buffer.from(stringContents.join(EOL)); -} diff --git a/src/StringsUtils.ts b/src/StringsUtils.ts new file mode 100644 index 0000000..3f394f2 --- /dev/null +++ b/src/StringsUtils.ts @@ -0,0 +1,85 @@ +import { EOL } from "os"; + +// ************************************ // +// *** UTILS FOR PASS.STRINGS FILES *** // +// ************************************ // + +/** + * Parses a string file to convert it to + * an object + * + * @param buffer + * @returns + */ + +export function parse(buffer: Buffer) { + const fileAsString = buffer.toString("utf8"); + const translationRowRegex = /"(?.+)"\s+=\s+"(?.+)";\n?/; + const commentRowRegex = /\/\*\s*(.+)\s*\*\//; + + let translations: [placeholder: string, value: string][] = []; + let comments: string[] = []; + + let blockStartPoint = 0; + let blockEndPoint = 0; + + do { + if ( + /** New Line, new life */ + /\n/.test(fileAsString[blockEndPoint]) || + /** EOF */ + blockEndPoint === fileAsString.length + ) { + let match: RegExpMatchArray; + + const section = fileAsString.substring( + blockStartPoint, + blockEndPoint + 1, + ); + + if ((match = section.match(translationRowRegex))) { + const { + groups: { key, value }, + } = match; + + translations.push([key, value]); + } else if ((match = section.match(commentRowRegex))) { + const [, content] = match; + + comments.push(content.trimEnd()); + } + + /** Skipping \n and going to the next block. */ + blockEndPoint += 2; + blockStartPoint = blockEndPoint - 1; + } else { + blockEndPoint += 1; + } + } while (blockEndPoint <= fileAsString.length); + + return { + translations, + comments, + }; +} + +/** + * Creates a strings file buffer + * + * @param translations + * @returns + */ + +export function create(translations: { [key: string]: string }): Buffer { + const stringContents = []; + + const translationsEntries = Object.entries(translations); + + for (let i = 0; i < translationsEntries.length; i++) { + const [key, value] = translationsEntries[i]; + + stringContents.push(`"${key}" = "${value}";`); + } + + return Buffer.from(stringContents.join(EOL)); +}