Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring code in src/meta/config.js #36

Open
wants to merge 5 commits into
base: f24
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 56 additions & 26 deletions src/meta/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,74 @@

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]);

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];
}
deserialized[key] = handleTypeConversion(defaultType, type, config[key], number, key);
});

return deserialized;
}

// got this code from ChatGPT
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;
}

console.log("Reem")

Check failure on line 67 in src/meta/configs.js

View workflow job for this annotation

GitHub Actions / test

Strings must use singlequote

Check failure on line 67 in src/meta/configs.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

function handleNumberConversion(number, value, 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];
}
}


// called before data is saved to db
function serialize(config) {
const serialized = {};
Expand Down
1 change: 1 addition & 0 deletions src/posts/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module.exports = function (Posts) {
return result.post;
};


async function addReplyTo(postData, timestamp) {
if (!postData.toPid) {
return;
Expand Down
2 changes: 2 additions & 0 deletions src/user/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
return userData.uid;
}

console.log("Reem")

Check failure on line 125 in src/user/create.js

View workflow job for this annotation

GitHub Actions / test

Strings must use singlequote

Check failure on line 125 in src/user/create.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

async function storePassword(uid, password) {
if (!password) {
return;
Expand Down
4 changes: 4 additions & 0 deletions test/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
const Groups = require('../src/groups');
const request = require('../src/request');


describe('meta', () => {
let fooUid;
let bazUid;
Expand Down Expand Up @@ -570,7 +571,10 @@
});
assert.equal(response.headers['access-control-allow-origin'], 'mydomain.com');
meta.config['access-control-allow-origin-regex'] = oldValue;
});

Check failure on line 574 in test/meta.js

View workflow job for this annotation

GitHub Actions / test

Block must not be padded by blank lines


Check failure on line 576 in test/meta.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed

});

it('should log targets', (done) => {
Expand Down
Loading