Moved fieldsArray to typescript

This commit is contained in:
Alexander Cerutti
2019-05-11 21:07:27 +02:00
parent 464f23e965
commit 7ee36c6f87

View File

@@ -1,5 +1,7 @@
const schema = require("./schema"); import * as schema from "./schema";
const debug = require("debug")("passkit:fields"); import debug from "debug";
const fieldsDebug = debug("passkit:fields");
/** /**
* Class to represent lower-level keys pass fields * Class to represent lower-level keys pass fields
@@ -8,9 +10,8 @@ const debug = require("debug")("passkit:fields");
const poolSymbol = Symbol("pool"); const poolSymbol = Symbol("pool");
class FieldsArray extends Array { export default class FieldsArray extends Array {
constructor(pool: Set<string>, ...args: any[]) {
constructor(pool,...args) {
super(...args); super(...args);
this[poolSymbol] = pool; this[poolSymbol] = pool;
} }
@@ -20,18 +21,18 @@ class FieldsArray extends Array {
* also uniqueKeys set. * also uniqueKeys set.
*/ */
push(...fieldsData) { push(...fieldsData: schema.Field[]): number {
const validFields = fieldsData.reduce((acc, current) => { const validFields = fieldsData.reduce((acc: schema.Field[], current: schema.Field) => {
if (!(typeof current === "object") || !schema.isValid(current, "field")) { if (!(typeof current === "object") || !schema.isValid(current, "field")) {
return acc; return acc;
} }
if (acc.some(e => e.key === current.key) || this[poolSymbol].has(current.key)) { if (acc.some(e => e.key === current.key) || this[poolSymbol].has(current.key)) {
debug(`Field with key "${key}" discarded: fields must be unique in pass scope.`); fieldsDebug(`Field with key "${current.key}" discarded: fields must be unique in pass scope.`);
} } else {
this[poolSymbol].add(current.key);
this[poolSymbol].add(current.key) acc.push(current);
acc.push(current) }
return acc; return acc;
}, []); }, []);
@@ -44,9 +45,9 @@ class FieldsArray extends Array {
* also uniqueKeys set * also uniqueKeys set
*/ */
pop() { pop(): schema.Field {
const element = Array.prototype.pop.call(this); const element: schema.Field = Array.prototype.pop.call(this);
this[poolSymbol].delete(element.key) this[poolSymbol].delete(element.key);
return element; return element;
} }
@@ -55,16 +56,14 @@ class FieldsArray extends Array {
* also uniqueKeys set * also uniqueKeys set
*/ */
splice(start, deleteCount, ...items) { splice(start: number, deleteCount: number, ...items: schema.Field[]): schema.Field[] {
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[poolSymbol].delete(item.key));
return Array.prototype.splice.call(this, start, deleteCount, items); return Array.prototype.splice.call(this, start, deleteCount, items);
} }
get length() { get length(): number {
return this.length; return this.length;
} }
} }
module.exports = FieldsArray;