From 4e1bc5c3fb025d4255d8408b5a9cde7b1a4803e2 Mon Sep 17 00:00:00 2001 From: alexandercerutti Date: Wed, 29 Aug 2018 19:54:15 +0200 Subject: [PATCH] Now excluding fields with the same key --- fields.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/fields.js b/fields.js index c75ccd4..c88e6a8 100644 --- a/fields.js +++ b/fields.js @@ -7,6 +7,7 @@ const schema = require("./schema"); class FieldsContainer { constructor() { + this.uniqueKeys = []; this.fields = []; } @@ -25,7 +26,15 @@ class FieldsContainer { fields = fields[0]; } - let validFields = fields.filter(f => typeof f === "object" && schema.isValid(f, "field")); + let validFields = fields.filter(f => { + if (this.uniqueKeys.includes(f.key)) { + return false; + } + + this.uniqueKeys.push(f.key); + + return typeof f === "object" && schema.isValid(f, "field"); + }); this.fields.push(...validFields); @@ -49,10 +58,12 @@ class FieldsContainer { if (quantity > -1) { let removedElements = this.fields.slice(quantity); this.fields = this.fields.slice(0, this.fields.length - quantity); + this.uniqueKeys = this.uniqueKeys.slice(0, this.uniqueKeys - quantity); return removedElements; } + this.uniqueKeys.pop(); return this.fields.pop(); } }