From ff33cc9d2e72e07d3c1c8a139e4a12746c508ba6 Mon Sep 17 00:00:00 2001 From: alexandercerutti Date: Mon, 20 Aug 2018 18:12:06 +0200 Subject: [PATCH] Added shouldOverwrite to Pass class options to overwrite or append to the existing ones, the properties in main buffer --- index.js | 21 +++++++++++++++++++-- schema.js | 3 ++- server.js | 5 +++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index b718671..b1792a3 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,7 @@ class Pass { this.model = ""; this.l10n = {}; this.props = {}; + this.shouldOverwrite = !(this.options.hasOwnProperty("shouldOverwrite") && !this.options.shouldOverwrite); fields.areas.forEach(a => this[a] = new fields.FieldsArea()); } @@ -370,8 +371,24 @@ class Pass { delete this.props["barcode"]; - // Merging the original and the new properties - Object.assign(passFile, this.props); + if (this.shouldOverwrite) { + Object.assign(passFile, this.props); + } else { + Object.keys(this.props).forEach(prop => { + if (passFile[prop]) { + if (passFile[prop] instanceof Array) { + passFile[prop].push(...this.props[prop]); + return; + } else if (passFile[prop] instanceof Object) { + Object.assign(passFile[prop], this.props[prop]); + return; + } + return; + } else { + passFile[prop] = this.props[prop]; + } + }); + } fields.areas.forEach(area => { if (this[area].fields.length) { diff --git a/schema.js b/schema.js index 804e588..1e23a19 100644 --- a/schema.js +++ b/schema.js @@ -10,7 +10,8 @@ let instance = Joi.object().keys({ passphrase: Joi.string().required(), }).required() }).required(), - overrides: Joi.object() + overrides: Joi.object(), + shouldOverwrite: Joi.boolean() }); let barcode = Joi.object().keys({ diff --git a/server.js b/server.js index a6527ba..9753c11 100644 --- a/server.js +++ b/server.js @@ -49,7 +49,8 @@ function manageRequest(request, response) { passphrase: "123456" } }, - overrides: overrides + overrides: overrides, + shouldOverwrite: true }); pass.localize("it", { @@ -75,7 +76,7 @@ function manageRequest(request, response) { longitude: 12.320653600000002 }]); - pass.relevance("maxDistance", 150); + pass.relevance("maxDistance", 10); //pass.localize("zu", {});