From 01e977167ab0bdafd181a304c0e36c5498884b68 Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Sun, 1 Sep 2024 14:59:55 +0300 Subject: [PATCH 1/5] changing from else-if to if in function --- src/meta/configs.js | 93 +++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 33 deletions(-) diff --git a/src/meta/configs.js b/src/meta/configs.js index 80159490c4..da3a50a4a1 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -17,42 +17,69 @@ Meta.config = {}; // called after data is loaded from db function deserialize(config) { - const deserialized = {}; - Object.keys(config).forEach((key) => { - const defaultType = typeof defaults[key]; - const type = typeof config[key]; - const number = parseFloat(config[key]); + const deserialized = {}; - if (defaultType === 'string' && type === 'number') { - deserialized[key] = String(config[key]); - } else if (defaultType === 'number' && type === 'string') { - if (!isNaN(number) && isFinite(config[key])) { - deserialized[key] = number; - } else { - deserialized[key] = defaults[key]; - } - } else if (config[key] === 'true') { - deserialized[key] = true; - } else if (config[key] === 'false') { - deserialized[key] = false; - } else if (config[key] === null) { - deserialized[key] = defaults[key]; - } else if (defaultType === 'undefined' && !isNaN(number) && isFinite(config[key])) { - deserialized[key] = number; - } else if (Array.isArray(defaults[key]) && !Array.isArray(config[key])) { - try { - deserialized[key] = JSON.parse(config[key] || '[]'); - } catch (err) { - winston.error(err.stack); - deserialized[key] = defaults[key]; - } - } else { - deserialized[key] = config[key]; - } - }); - return deserialized; + Object.keys(config).forEach((key) => { + const defaultType = typeof defaults[key]; + const type = typeof config[key]; + const number = parseFloat(config[key]); + + deserialized[key] = handleTypeConversion(defaultType, type, config[key], number, key); + }); + + return deserialized; } +function handleTypeConversion(defaultType, type, value, number, key) { + if (defaultType === 'string' && type === 'number') { + return String(value); + } + + if (defaultType === 'number' && type === 'string') { + return handleNumberConversion(number, value, key); + } + + if (value === 'true') { + return true; + } + + if (value === 'false') { + return false; + } + + if (value === null) { + return defaults[key]; + } + + if (defaultType === 'undefined' && !isNaN(number) && isFinite(value)) { + return number; + } + + if (Array.isArray(defaults[key]) && !Array.isArray(value)) { + return handleArrayConversion(value, key); + } + + return value; +} + +function handleNumberConversion(number, value, key) { + if (!isNaN(number) && isFinite(value)) { + return number; + } else { + return defaults[key]; + } +} + +function handleArrayConversion(value, key) { + try { + return JSON.parse(value || '[]'); + } catch (err) { + winston.error(err.stack); + return defaults[key]; + } +} + + // called before data is saved to db function serialize(config) { const serialized = {}; From 765a1e84cf6740c5e83fc93497d5473c5c3c780b Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Sun, 1 Sep 2024 19:30:55 +0300 Subject: [PATCH 2/5] fixing indentation mistakes --- src/meta/configs.js | 81 ++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/src/meta/configs.js b/src/meta/configs.js index da3a50a4a1..fa64a1c251 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -17,66 +17,65 @@ Meta.config = {}; // called after data is loaded from db function deserialize(config) { - const deserialized = {}; + const deserialized = {}; - Object.keys(config).forEach((key) => { - const defaultType = typeof defaults[key]; - const type = typeof config[key]; - const number = parseFloat(config[key]); + Object.keys(config).forEach((key) => { + const defaultType = typeof defaults[key]; + const type = typeof config[key]; + const number = parseFloat(config[key]); - deserialized[key] = handleTypeConversion(defaultType, type, config[key], number, key); - }); + deserialized[key] = handleTypeConversion(defaultType, type, config[key], number, key); + }); - return deserialized; + return deserialized; } function handleTypeConversion(defaultType, type, value, number, key) { - if (defaultType === 'string' && type === 'number') { - return String(value); - } + if (defaultType === 'string' && type === 'number') { + return String(value); + } - if (defaultType === 'number' && type === 'string') { - return handleNumberConversion(number, value, key); - } + if (defaultType === 'number' && type === 'string') { + return handleNumberConversion(number, value, key); + } - if (value === 'true') { - return true; - } + if (value === 'true') { + return true; + } - if (value === 'false') { - return false; - } + if (value === 'false') { + return false; + } - if (value === null) { - return defaults[key]; - } + if (value === null) { + return defaults[key]; + } - if (defaultType === 'undefined' && !isNaN(number) && isFinite(value)) { - return number; - } + if (defaultType === 'undefined' && !isNaN(number) && isFinite(value)) { + return number; + } - if (Array.isArray(defaults[key]) && !Array.isArray(value)) { - return handleArrayConversion(value, key); - } + if (Array.isArray(defaults[key]) && !Array.isArray(value)) { + return handleArrayConversion(value, key); + } - return value; + return value; } function handleNumberConversion(number, value, key) { - if (!isNaN(number) && isFinite(value)) { - return number; - } else { - return defaults[key]; - } + if (!isNaN(number) && isFinite(value)) { + return number; + } + return defaults[key]; } function handleArrayConversion(value, key) { - try { - return JSON.parse(value || '[]'); - } catch (err) { - winston.error(err.stack); - return defaults[key]; - } + try { + return JSON.parse(value || '[]'); + } catch (err) { + winston.error(err.stack); + return defaults[key]; + } } From 3d385d51ba428369d1e35b0bda81d3548dbe73fb Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Mon, 2 Sep 2024 09:25:05 +0300 Subject: [PATCH 3/5] added comments for function created --- src/meta/configs.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/meta/configs.js b/src/meta/configs.js index fa64a1c251..61158ec5d8 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -30,6 +30,7 @@ function deserialize(config) { return deserialized; } +//got this code from ChatGPT function handleTypeConversion(defaultType, type, value, number, key) { if (defaultType === 'string' && type === 'number') { return String(value); From 61dfeb5100b228130a76cf95bc73e01d2857fc67 Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Mon, 2 Sep 2024 09:27:31 +0300 Subject: [PATCH 4/5] fixing comment style --- src/meta/configs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/meta/configs.js b/src/meta/configs.js index 61158ec5d8..8be72bf722 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -30,7 +30,7 @@ function deserialize(config) { return deserialized; } -//got this code from ChatGPT +// got this code from ChatGPT function handleTypeConversion(defaultType, type, value, number, key) { if (defaultType === 'string' && type === 'number') { return String(value); From e0c88240d3046fa7f3d87642d5cafd572625d9fe Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Wed, 4 Sep 2024 20:57:09 +0300 Subject: [PATCH 5/5] making log print work --- src/meta/configs.js | 3 +++ src/posts/create.js | 1 + src/user/create.js | 2 ++ test/meta.js | 4 ++++ 4 files changed, 10 insertions(+) diff --git a/src/meta/configs.js b/src/meta/configs.js index 8be72bf722..ee7576414d 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -15,6 +15,7 @@ const Configs = module.exports; Meta.config = {}; + // called after data is loaded from db function deserialize(config) { const deserialized = {}; @@ -63,6 +64,8 @@ function handleTypeConversion(defaultType, type, value, number, key) { return value; } +console.log("Reem") + function handleNumberConversion(number, value, key) { if (!isNaN(number) && isFinite(value)) { return number; diff --git a/src/posts/create.js b/src/posts/create.js index d541564c2e..aa209931dc 100644 --- a/src/posts/create.js +++ b/src/posts/create.js @@ -71,6 +71,7 @@ module.exports = function (Posts) { return result.post; }; + async function addReplyTo(postData, timestamp) { if (!postData.toPid) { return; diff --git a/src/user/create.js b/src/user/create.js index 610d614e81..6c9f037149 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -122,6 +122,8 @@ module.exports = function (User) { return userData.uid; } + console.log("Reem") + async function storePassword(uid, password) { if (!password) { return; diff --git a/test/meta.js b/test/meta.js index 77667ddbf2..9628b15441 100644 --- a/test/meta.js +++ b/test/meta.js @@ -11,6 +11,7 @@ const User = require('../src/user'); const Groups = require('../src/groups'); const request = require('../src/request'); + describe('meta', () => { let fooUid; let bazUid; @@ -571,6 +572,9 @@ describe('meta', () => { assert.equal(response.headers['access-control-allow-origin'], 'mydomain.com'); meta.config['access-control-allow-origin-regex'] = oldValue; }); + + + }); it('should log targets', (done) => {