Skip to content

Commit

Permalink
code review: let should only be used when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
A-312 committed Dec 31, 2019
1 parent acb44e9 commit 8b571e4
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions lib/convict.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const types = {
assert(isIP(x), 'must be an IP address');
},
duration: function(x) {
let err_msg = 'must be a positive integer or human readable string (e.g. 3000, "5 days")';
const err_msg = 'must be a positive integer or human readable string (e.g. 3000, "5 days")';
if (Number.isInteger(x)) {
assert(x >= 0, err_msg);
} else {
Expand All @@ -103,13 +103,11 @@ const ALLOWED_OPTION_STRICT = 'strict';
const ALLOWED_OPTION_WARN = 'warn';

function flatten(obj, useProperties) {
let stack = Object.keys(obj);
let key;

let entries = [];
const stack = Object.keys(obj);
const entries = [];

while (stack.length) {
key = stack.shift();
let key = stack.shift();
let val = walk(obj, key);
if (typeof val === 'object' && !Array.isArray(val) && val != null) {
if (useProperties) {
Expand All @@ -121,7 +119,7 @@ function flatten(obj, useProperties) {
continue;
}
}
let subkeys = Object.keys(val);
const subkeys = Object.keys(val);

// Don't filter out empty objects
if (subkeys.length > 0) {
Expand All @@ -134,7 +132,7 @@ function flatten(obj, useProperties) {
entries.push([key, val]);
}

let flattened = {};
const flattened = {};
entries.forEach(function(entry) {
let key = entry[0];
if (useProperties) {
Expand All @@ -148,7 +146,7 @@ function flatten(obj, useProperties) {
}

function validate(instance, schema, strictValidation) {
let errors = {
const errors = {
undeclared: [],
invalid_type: [],
missing: []
Expand Down Expand Up @@ -281,7 +279,7 @@ function normalizeSchema(name, rawSchema, props, fullName, getter, sensitive) {

// store original format function
let format = schema.format;
let newFormat = (() => {
const newFormat = (() => {
if (BUILT_INS.indexOf(format) >= 0 || BUILT_IN_NAMES.indexOf(format) >= 0) {
// if the format property is a built-in JavaScript constructor,
// assert that the value is of that type
Expand Down Expand Up @@ -402,10 +400,10 @@ function applyValues(from, to, schema) {
}

function traverseSchema(schema, path) {
let ar = path.split('.');
const ar = path.split('.');
let o = schema;
while (ar.length > 0) {
let k = ar.shift();
const k = ar.shift();
if (o && o.properties && o.properties[k]) {
o = o.properties[k];
} else {
Expand Down Expand Up @@ -448,7 +446,7 @@ function getCoerceMethod(format) {
case 'duration':
return (v) => {
if (isStr(v)) {
let split = v.split(' ');
const split = v.split(' ');
if (split.length == 1) {
// It must be an integer in string form.
return parseInt(v, 10);
Expand Down Expand Up @@ -482,9 +480,9 @@ function loadFile(path) {

function walk(obj, path, initializeMissing) {
if (path) {
let ar = path.split('.');
const ar = path.split('.');
while (ar.length) {
let k = ar.shift();
const k = ar.shift();
if (initializeMissing && obj[k] == null) {
obj[k] = {};
obj = obj[k];
Expand All @@ -502,11 +500,11 @@ function walk(obj, path, initializeMissing) {
/**
* @returns a config object
*/
let convict = function convict(def, opts) {
const convict = function convict(def, opts) {

// TODO: Rename this `rv` variable (supposedly "return value") into something
// more meaningful.
let rv = {
const rv = {
/**
* Gets the array of process arguments, using the override passed to the
* convict function or process.argv if no override was passed.
Expand Down Expand Up @@ -536,12 +534,12 @@ let convict = function convict(def, opts) {
* even if they aren't set, to avoid revealing any information.
*/
toString: function() {
let clone = cloneDeep(this._instance);
const clone = cloneDeep(this._instance);
this._sensitive.forEach(function(key) {
let path = key.split('.');
let childKey = path.pop();
let parentKey = path.join('.');
let parent = walk(clone, parentKey);
const path = key.split('.');
const childKey = path.pop();
const parentKey = path.join('.');
const parent = walk(clone, parentKey);
parent[childKey] = '[Sensitive]';
});
return JSON.stringify(clone, null, 2);
Expand All @@ -566,7 +564,7 @@ let convict = function convict(def, opts) {
* notation to reference nested values
*/
get: function(path) {
let o = walk(this._instance, path);
const o = walk(this._instance, path);
return cloneDeep(o);
},

Expand All @@ -576,7 +574,7 @@ let convict = function convict(def, opts) {
*/
getOrigin: function(path) {
path = (path.split('.').join('.properties.')) + '._cvtGetOrigin';
let o = walk(this._schema.properties, path);
const o = walk(this._schema.properties, path);
return o ? o() : null;
},

Expand All @@ -588,7 +586,7 @@ let convict = function convict(def, opts) {
// The default value for FOO.BAR.BAZ is stored in `_schema.properties` at:
// FOO.properties.BAR.properties.BAZ.default
path = (path.split('.').join('.properties.')) + '.default';
let o = walk(this._schema.properties, path);
const o = walk(this._schema.properties, path);
return cloneDeep(o);
},

Expand All @@ -604,7 +602,7 @@ let convict = function convict(def, opts) {
*/
has: function(path) {
try {
let r = this.get(path);
const r = this.get(path);
// values that are set but undefined return false
return typeof r !== 'undefined';
} catch (e) {
Expand Down Expand Up @@ -644,7 +642,7 @@ let convict = function convict(def, opts) {
* Loads and merges one or multiple JSON configuration files into config
*/
loadFile: function(paths) {
let self = this;
const self = this;
if (!Array.isArray(paths)) paths = [paths];
paths.forEach(function(path) {
// Support empty config files #253
Expand All @@ -670,14 +668,14 @@ let convict = function convict(def, opts) {

const output_function = options.output || global.console.log;

let errors = validate(this._instance, this._schema, options.allowed);
const errors = validate(this._instance, this._schema, options.allowed);

if (errors.invalid_type.length + errors.undeclared.length + errors.missing.length) {
let sensitive = this._sensitive;
const sensitive = this._sensitive;

let fillErrorBuffer = function(errors) {
let err_buf = '';
const fillErrorBuffer = function(errors) {
for (let i = 0; i < errors.length; i++) {
let err_buf = '';

if (err_buf.length) err_buf += '\n';

Expand All @@ -698,7 +696,7 @@ let convict = function convict(def, opts) {
const params_err_buf = fillErrorBuffer(errors.undeclared);
const missing_err_buf = fillErrorBuffer(errors.missing);

let output_err_bufs = [types_err_buf, missing_err_buf];
const output_err_bufs = [types_err_buf, missing_err_buf];

if (options.allowed === ALLOWED_OPTION_WARN && params_err_buf.length) {
let warning = 'Warning:';
Expand All @@ -713,7 +711,7 @@ let convict = function convict(def, opts) {
output_err_bufs.push(params_err_buf);
}

let output = output_err_bufs
const output = output_err_bufs
.filter(function(str) { return str.length; })
.join('\n');

Expand Down

0 comments on commit 8b571e4

Please sign in to comment.