From bdbbf235d267344ff26d175ba8c3a43445c62e82 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sun, 19 Sep 2021 18:55:43 +0200 Subject: [PATCH] Added propsSymbol and prop getter implementation --- src/PKPass.ts | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/PKPass.ts b/src/PKPass.ts index 99df1fc..839f317 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -6,6 +6,7 @@ import * as Schemas from "./schemas"; import { Stream } from "stream"; const fieldKeysPoolSymbol = Symbol("fieldKeysPoolSymbol"); +const propsSymbol = Symbol("props"); interface NamedBuffers { [key: string]: Buffer; @@ -21,6 +22,7 @@ type TransitTypes = `PKTransitType${ export class PKPass extends Bundle { private certificates: Certificates; private [fieldKeysPoolSymbol] = new Set(); + private [propsSymbol]: Schemas.ValidPass = {}; public primaryFields /*****/ = new FieldsArray(this[fieldKeysPoolSymbol]); public secondaryFields /***/ = new FieldsArray(this[fieldKeysPoolSymbol]); public auxiliaryFields /***/ = new FieldsArray(this[fieldKeysPoolSymbol]); @@ -128,11 +130,7 @@ export class PKPass extends Bundle { */ get props(): Readonly { - /** - * @TODO implement - */ - - return undefined; + return freezeRecusive(this[propsSymbol]); } /** @@ -369,3 +367,28 @@ export class PKPass extends Bundle { return this; } } + +function freezeRecusive(object: Object) { + const objectCopy = {}; + const objectEntries = Object.entries(object); + + for (let i = 0; i < objectEntries.length; i++) { + const [key, value] = objectEntries[i]; + + if (value && typeof value === "object") { + if (Array.isArray(value)) { + objectCopy[key] = value.slice(); + + for (let j = 0; j < value.length; j++) { + objectCopy[key][j] = freezeRecusive(value[j]); + } + } else { + objectCopy[key] = freezeRecusive(value); + } + } else { + objectCopy[key] = value; + } + } + + return Object.freeze(objectCopy); +}