Hot answers tagged

6

There's definitely a lot of duplicate code in those 2 ifs. I'd extract it into function and pass different things as parameters. Then code would look let's say: //not sure about name of function and parameters function nodeFieldPerType(filePath, getNodePath) { if ( node.internal.type === "Mdx" && node.fileAbsolutePath.includes(filePath) ...


4

I know this is a couple weeks old, and you already have a good solution. I just wanted to point out a method that is a bit more deterministic/declarative. You are already using multiple instances of gatsby-source-filesystem, and they each have their own name property. That becomes sourceInstanceName on the File nodes. You can use that value to simplify ...


3

It is fine to write things out as you did, but you want to keep DRY(Dont Repeat Yourself) in mind. That is where defining your own methods/functions comes in when you wan to start cleaning things up. Starting with this: Math.floor(Math.random() * 50) + 3 Math.floor(Math.random() * 90 - 1) + 1 This can be put into its own function to calculate ...


3

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 ...


2

Regarding your validations - extract each of your validations in validation object. You can do something like: class Validator { isValid(obj) { //return false if obj is not valid } message(obj) { //return error object if obj is not valid } status() { //error status if validation fails } } Then you can have like EmailValidator, Required, etc. ...


2

I'd look at python decorators as something, that accepts function and returns function, which is usually original function wrapped in another function and some added behaviour. Change signature of your "decorator" functions to match that and you got something like decorators. Don't have much time at the moment to write examples, but you get the idea :-) ...


1

If you want to check whether every item in an array passes a test, you should use .every for that, not .reduce. Eg data.reduce((acc, el) => acc && el.length > 0, true) can be switched with data.every(arr => arr.length) and data.reduce((acc, el) => acc && el[0] === null, true) can be switched with data.every(arr => arr[0] ...


1

If the part you don't like it the repetition of calling customFieldsServer.getAccountCustomFields(...) in two separate places, then you can avoid that without using async/await like this by create a common promise variable that each branch of the if/else fills in so after the if/else progress can continue using the common promise: let p; if ( account....


1

Thank you so much for the input! It gave me great insight into solving this problem. Thanks @Jeremy Albright for your insight and advice. It inspired me to rewrite my node.js file. My setup so far: // gatsby-node.js exports.createPages = require("./gatsby/node/createPages") exports.onCreateNode = require("./gatsby/node/onCreateNode") I'm still learning ...


Only top voted, non community-wiki answers of a minimum length are eligible