This is a style guide for writing code in JavaScript using Node.js. This includes all APIs created from the Express API Skeleton.
As a base guide, the Airbnb JavaScript Style Guide should be followed. Express APIs enforce this by using ESLint rules.
When naming things using camel case such as variables or keys, acronyms should not be capitalized. If a single-letter word is used such as "A" or "I", the word is capitalized and the word following it is also capitalized.
Example:
const exampleJsonSerializer;
const osuId;
const idOsu;
const oracleDb;
const exampleOfAJsonSerializer;
Functions should be documented using JSDoc with a format similar to this example:
/**
* @summary Checks if two strings are equal
* @function
* @param {string} paramA One string
* @param {string} paramB Another string
* @returns {boolean} true if strings are equal, false otherwise
* @throws Will throw an error if either parameter is falsy
*/
const function(paramA, paramB) {
if (!paramA || !paramB) {
throw new Error('Both strings must be non-empty');
}
return (paramA === paramB);
}
When listing more than 3 properties of an object, each property should be on its own line.
Example:
const obj1 = { prop1, prop2, prop3 };
const obj2 = {
prop1,
prop2,
prop3,
prop4,
};
ES6 imports are preferred over commonjs require
statements. However, for some situations such as
conditional imports, require
should be used instead.
Whenever possible, the following should be grouped together, separated by a blank line at the top of a module in this order:
-
import
a third-party module -
import
a local module -
require
or use other method for importing module -
Declare global variables and/or call functions
Each line within groups 1-3 should be ordered alphabetically by the first alphanumeric character in the name of the package/file path in the statement.
Example:
import a from 'a';
import b from 'b';
import * as file1 from './file1';
import { method } from './file2';
const c = someCondition ? require('c') : null;
const str = 'abc';
From ESLint's global-require
rule, a ternary require is allowed
Example from above link:
var logger = DEBUG ? require('dev-logger') : require('logger');
or
const package = packageIsNeeded ? require('package') : null;
Nested destructure assignments can be done either by nesting curly braces or by destructuring properties of objects.
Example:
const { b: { c } } = a;
or
const { c } = a.b;
Multiline ternary operations should use the following indentation:
query.primaryInd !== undefined
? `AND ROW ${query.primaryInd ? "= 'Y'" : 'IS NULL'}`
: ''