mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 15:25:20 +00:00
Changed implementation of FieldsArray to accept passInstance instead of pool, to check frozen instance
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import PKPass from "./PKPass";
|
||||
import { fieldKeysPoolSymbol } from "./PKPass";
|
||||
import * as Schemas from "./schemas";
|
||||
import * as Utils from "./utils";
|
||||
import formatMessage, * as Messages from "./messages";
|
||||
|
||||
/**
|
||||
@@ -6,14 +9,17 @@ import formatMessage, * as Messages from "./messages";
|
||||
* @see https://apple.co/2wkUBdh
|
||||
*/
|
||||
|
||||
const poolSymbol = Symbol("pool");
|
||||
const passInstanceSymbol = Symbol("passInstance");
|
||||
|
||||
export default class FieldsArray extends Array<Schemas.Field> {
|
||||
private [poolSymbol]: Set<string>;
|
||||
private [passInstanceSymbol]: InstanceType<typeof PKPass>;
|
||||
|
||||
constructor(pool: Set<string>, ...args: Schemas.Field[]) {
|
||||
constructor(
|
||||
passInstance: InstanceType<typeof PKPass>,
|
||||
...args: Schemas.Field[]
|
||||
) {
|
||||
super(...args);
|
||||
this[poolSymbol] = pool;
|
||||
this[passInstanceSymbol] = passInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,6 +28,8 @@ export default class FieldsArray extends Array<Schemas.Field> {
|
||||
*/
|
||||
|
||||
push(...fieldsData: Schemas.Field[]): number {
|
||||
Utils.assertUnfrozen(this[passInstanceSymbol]);
|
||||
|
||||
const validFields = fieldsData.reduce(
|
||||
(acc: Schemas.Field[], current: Schemas.Field) => {
|
||||
try {
|
||||
@@ -35,7 +43,9 @@ export default class FieldsArray extends Array<Schemas.Field> {
|
||||
return acc;
|
||||
}
|
||||
|
||||
if (this[poolSymbol].has(current.key)) {
|
||||
const pool = this[passInstanceSymbol][fieldKeysPoolSymbol];
|
||||
|
||||
if (pool.has(current.key)) {
|
||||
console.warn(
|
||||
formatMessage(
|
||||
Messages.FIELDS.REPEATED_KEY,
|
||||
@@ -45,7 +55,7 @@ export default class FieldsArray extends Array<Schemas.Field> {
|
||||
return acc;
|
||||
}
|
||||
|
||||
this[poolSymbol].add(current.key);
|
||||
pool.add(current.key);
|
||||
return [...acc, current];
|
||||
},
|
||||
[],
|
||||
@@ -60,8 +70,10 @@ export default class FieldsArray extends Array<Schemas.Field> {
|
||||
*/
|
||||
|
||||
pop(): Schemas.Field {
|
||||
Utils.assertUnfrozen(this[passInstanceSymbol]);
|
||||
|
||||
const element: Schemas.Field = super.pop();
|
||||
this[poolSymbol].delete(element.key);
|
||||
this[passInstanceSymbol][fieldKeysPoolSymbol].delete(element.key);
|
||||
return element;
|
||||
}
|
||||
|
||||
@@ -75,8 +87,12 @@ export default class FieldsArray extends Array<Schemas.Field> {
|
||||
deleteCount: number,
|
||||
...items: Schemas.Field[]
|
||||
): Schemas.Field[] {
|
||||
Utils.assertUnfrozen(this[passInstanceSymbol]);
|
||||
|
||||
const removeList = this.slice(start, deleteCount + start);
|
||||
removeList.forEach((item) => this[poolSymbol].delete(item.key));
|
||||
removeList.forEach((item) =>
|
||||
this[passInstanceSymbol][fieldKeysPoolSymbol].delete(item.key),
|
||||
);
|
||||
|
||||
return super.splice(start, deleteCount, ...items);
|
||||
}
|
||||
|
||||
@@ -305,11 +305,11 @@ export default class PKPass extends Bundle {
|
||||
|
||||
this[passTypeSymbol] = type;
|
||||
this[propsSymbol][this[passTypeSymbol]] = {
|
||||
headerFields /******/: new FieldsArray(this[fieldKeysPoolSymbol]),
|
||||
primaryFields /*****/: new FieldsArray(this[fieldKeysPoolSymbol]),
|
||||
secondaryFields /***/: new FieldsArray(this[fieldKeysPoolSymbol]),
|
||||
auxiliaryFields /***/: new FieldsArray(this[fieldKeysPoolSymbol]),
|
||||
backFields /********/: new FieldsArray(this[fieldKeysPoolSymbol]),
|
||||
headerFields /******/: new FieldsArray(this),
|
||||
primaryFields /*****/: new FieldsArray(this),
|
||||
secondaryFields /***/: new FieldsArray(this),
|
||||
auxiliaryFields /***/: new FieldsArray(this),
|
||||
backFields /********/: new FieldsArray(this),
|
||||
transitType: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import * as Messages from "./messages";
|
||||
import type Bundle from "./Bundle";
|
||||
|
||||
/**
|
||||
* Acts as a wrapper for converting date to W3C string
|
||||
* @param date
|
||||
@@ -105,3 +108,9 @@ export function cloneRecursive(object: Object) {
|
||||
|
||||
return objectCopy;
|
||||
}
|
||||
|
||||
export function assertUnfrozen(instance: InstanceType<typeof Bundle>) {
|
||||
if (instance.isFrozen) {
|
||||
throw new Error(Messages.BUNDLE.CLOSED);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user