-2
\$\begingroup\$

I'm looking for any advice on the way I have implemented methods (explained below). More specifically, is it a good practice or not? And if not, any suggestions?


NOTE - communications.js is used in many places throughout the app.


Search_user.js

This code searches for cookies and returns a user object if any matches found.


function searchForAUser () {

let userName = "userName=";
let userId = "userId=";
let user = {};

let decodedCookie = decodeURIComponent(document.cookie);

let cookieArr = decodedCookie.split(';');

for(var i=0; i< cookieArr.length; i++) {
        var c = cookieArr[i];
        while (c.charAt(0) === " ") {
            c= c.substring(1);
        }

        if (c.indexOf(userName) === 0) {
            let userNameDecoded =  c.substring(userName.length, c.length);          
            user.userName = userNameDecoded;
            console.log(userNameDecoded);
        }

        if (c.indexOf(userId) === 0 ) {
            var userIdDecoded = c.substring(userId.length, c.length);
            user.userId = userIdDecoded;
            console.log(user.userId);
        }
return user;

main.js

//used for xmlHttpRequests
let comms = require("./communications.js");
//searches a user
let serachUser = require("./search_user.js");

var main = {
    user : {},
    init : async function () {
        this.user = await searchForAUser();
        console.log(this.user);
    },

}


//verifies the user.t against a database
async function searchForAUser () {
    let user =  serachUser();
    let verify = await comms.sendUserData(user,"../../backend/Log_in.php");
    if (user.t === verify) {
        user.loggedInStatus = true;
    }
    else {
        user.loggedInStatus = false;
    }
    return user;
}

My question is, whether above searchForAUser function is unnecessary? And, is it good practice to use below one instead of above function?


Passing comms to Search_User.js


Search_User.js

function searchForAUser (comms) {

let userName = "userName=";
let userId = "userId=";
let user = {};
let decodedCookie = decodeURIComponent(document.cookie);

let cookieArr = decodedCookie.split(';');

for(var i=0; i< cookieArr.length; i++) {
        var c = cookieArr[i];
        while (c.charAt(0) === " ") {
            c= c.substring(1);
        }

        if (c.indexOf(userName) === 0) {
            let userNameDecoded =  c.substring(userName.length, c.length);          
            user.userName = userNameDecoded;
            console.log(userNameDecoded);
        }

        if (c.indexOf(userId) === 0 ) {
            var userIdDecoded = c.substring(userId.length, c.length);
            user.userId = userIdDecoded;
            console.log(user.userId);
        }

//communication is also done in the same function

let verify = comms.sendUserData(user,"../../backend/Log_in.php");
if (user.t === verify) {
        user.loggedInStatus = true;
    }
return user;

main.js

let comms = require("./communications.js");
let serachUser = require("./search_user.js");

var main = {
    user : {},
    init : async function () {
        //passing comms to the searchuser function

        this.user = searchUser(comms);
        console.log(this.user);
    },

}

Passing the comms method to the searchUser function and executing it inside the searchUser.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Welcome to Code Review. This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does. \$\endgroup\$
    – L. F.
    Mar 15, 2020 at 4:34
  • \$\begingroup\$ @L.F. thank you for advice. I have edited and added more details. Basically I need to know is, whether passing the comms to searchUser is a good practice? I have to use more functions of this pattern throughout the app. \$\endgroup\$ Mar 15, 2020 at 16:21
  • \$\begingroup\$ Are all those console.log statements part of your actual code? Please take a look at the help center. \$\endgroup\$
    – Mast
    Mar 16, 2020 at 8:57
  • \$\begingroup\$ @Mast No, console.log are only for testing purpose. \$\endgroup\$ Mar 16, 2020 at 12:17

1 Answer 1

3
\$\begingroup\$

In the matter of simplify Search_User.js instead:

var c = cookieArr[i];
while (c.charAt(0) === " ") {
    c= c.substring(1);
}

you can use trim()

var c = cookieArr[i].trim();

and in IF's use slice or substring with start argument only

if (c.indexOf(userName) === 0) { user.userName = c.slice(userName.length) }
if (c.indexOf(userId) === 0) { user.userId = c.substring(userId.length) }
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.