mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 21:25:26 +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 Schemas from "./schemas";
|
||||||
|
import * as Utils from "./utils";
|
||||||
import formatMessage, * as Messages from "./messages";
|
import formatMessage, * as Messages from "./messages";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -6,14 +9,17 @@ import formatMessage, * as Messages from "./messages";
|
|||||||
* @see https://apple.co/2wkUBdh
|
* @see https://apple.co/2wkUBdh
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const poolSymbol = Symbol("pool");
|
const passInstanceSymbol = Symbol("passInstance");
|
||||||
|
|
||||||
export default class FieldsArray extends Array<Schemas.Field> {
|
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);
|
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 {
|
push(...fieldsData: Schemas.Field[]): number {
|
||||||
|
Utils.assertUnfrozen(this[passInstanceSymbol]);
|
||||||
|
|
||||||
const validFields = fieldsData.reduce(
|
const validFields = fieldsData.reduce(
|
||||||
(acc: Schemas.Field[], current: Schemas.Field) => {
|
(acc: Schemas.Field[], current: Schemas.Field) => {
|
||||||
try {
|
try {
|
||||||
@@ -35,7 +43,9 @@ export default class FieldsArray extends Array<Schemas.Field> {
|
|||||||
return acc;
|
return acc;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this[poolSymbol].has(current.key)) {
|
const pool = this[passInstanceSymbol][fieldKeysPoolSymbol];
|
||||||
|
|
||||||
|
if (pool.has(current.key)) {
|
||||||
console.warn(
|
console.warn(
|
||||||
formatMessage(
|
formatMessage(
|
||||||
Messages.FIELDS.REPEATED_KEY,
|
Messages.FIELDS.REPEATED_KEY,
|
||||||
@@ -45,7 +55,7 @@ export default class FieldsArray extends Array<Schemas.Field> {
|
|||||||
return acc;
|
return acc;
|
||||||
}
|
}
|
||||||
|
|
||||||
this[poolSymbol].add(current.key);
|
pool.add(current.key);
|
||||||
return [...acc, current];
|
return [...acc, current];
|
||||||
},
|
},
|
||||||
[],
|
[],
|
||||||
@@ -60,8 +70,10 @@ export default class FieldsArray extends Array<Schemas.Field> {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
pop(): Schemas.Field {
|
pop(): Schemas.Field {
|
||||||
|
Utils.assertUnfrozen(this[passInstanceSymbol]);
|
||||||
|
|
||||||
const element: Schemas.Field = super.pop();
|
const element: Schemas.Field = super.pop();
|
||||||
this[poolSymbol].delete(element.key);
|
this[passInstanceSymbol][fieldKeysPoolSymbol].delete(element.key);
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,8 +87,12 @@ export default class FieldsArray extends Array<Schemas.Field> {
|
|||||||
deleteCount: number,
|
deleteCount: number,
|
||||||
...items: Schemas.Field[]
|
...items: Schemas.Field[]
|
||||||
): Schemas.Field[] {
|
): Schemas.Field[] {
|
||||||
|
Utils.assertUnfrozen(this[passInstanceSymbol]);
|
||||||
|
|
||||||
const removeList = this.slice(start, deleteCount + start);
|
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);
|
return super.splice(start, deleteCount, ...items);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -305,11 +305,11 @@ export default class PKPass extends Bundle {
|
|||||||
|
|
||||||
this[passTypeSymbol] = type;
|
this[passTypeSymbol] = type;
|
||||||
this[propsSymbol][this[passTypeSymbol]] = {
|
this[propsSymbol][this[passTypeSymbol]] = {
|
||||||
headerFields /******/: new FieldsArray(this[fieldKeysPoolSymbol]),
|
headerFields /******/: new FieldsArray(this),
|
||||||
primaryFields /*****/: new FieldsArray(this[fieldKeysPoolSymbol]),
|
primaryFields /*****/: new FieldsArray(this),
|
||||||
secondaryFields /***/: new FieldsArray(this[fieldKeysPoolSymbol]),
|
secondaryFields /***/: new FieldsArray(this),
|
||||||
auxiliaryFields /***/: new FieldsArray(this[fieldKeysPoolSymbol]),
|
auxiliaryFields /***/: new FieldsArray(this),
|
||||||
backFields /********/: new FieldsArray(this[fieldKeysPoolSymbol]),
|
backFields /********/: new FieldsArray(this),
|
||||||
transitType: undefined,
|
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
|
* Acts as a wrapper for converting date to W3C string
|
||||||
* @param date
|
* @param date
|
||||||
@@ -105,3 +108,9 @@ export function cloneRecursive(object: Object) {
|
|||||||
|
|
||||||
return objectCopy;
|
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