Skip to content

Commit

Permalink
Merge c2e97a7 into 5a16dd1
Browse files Browse the repository at this point in the history
  • Loading branch information
madarche committed Apr 11, 2020
2 parents 5a16dd1 + c2e97a7 commit 5205849
Show file tree
Hide file tree
Showing 53 changed files with 1,448 additions and 1,014 deletions.
102 changes: 88 additions & 14 deletions .eslintrc.js
Expand Up @@ -9,28 +9,102 @@ module.exports = {
mocha: true,
},
rules: {
strict: ['error', 'global'],
//*** Programming best practices ***

// JavaScript core
'no-array-constructor': 'error',
'no-unused-vars': ['error', {'args': 'none'}],
strict: ['error', 'safe'],
semi: ['error', 'never'],

'no-unused-vars': ['error', {args: 'none'}],

// Don't use "var", only use "let" and "const"
'no-var': 'error',
// Use const if a variable is never reassigned
'prefer-const': 'error',

// No redeclaration of existing restricted names and builtins
'no-shadow-restricted-names': 'error',
// No redeclaration of existing variables in outer scope
//'no-shadow': ['error', {builtinGlobals: true}],
//
// Interesting but produces less readable code …
//'no-shadow': ['error', {builtinGlobals: true, hoist: 'all'}],

'no-array-constructor': ['error'],

// No dead code
'no-unreachable': 'error',
'no-fallthrough': 'error',

// Take full advantage of JavaScript flexibility by being able, in a
// function, to return different types (for exemple sometimes a boolean
// and sometimes an object).
'consistent-return': 'off',

// Disallow gratuitous parentheses
'no-extra-parens': ['error', 'all', {conditionalAssign: false}],

// Error best practices:
// Only throw Error instances
'no-throw-literal': 'error',

// Switch-case best practices
'default-case': 'error',
'no-fallthrough': 'error',
'no-case-declarations': 'error',
'consistent-return': 'off',

// Node.js specifics
// Enforces return statements in callbacks of array's methods
'array-callback-return': 'error',

'no-console': 'error',

//*** Presentation style ***

indent: ['error', 2],
quotes: ['error', 'single', {avoidEscape: true}],
'quote-props': ['error', 'as-needed'],

// Use as much as possible snake_case for variable names and camelCase for
// function names.
camelcase: 'off',

'new-cap': 'error',

'no-multiple-empty-lines': ['error', {max: 2}],
'no-trailing-spaces': 'error',
'comma-spacing': ['error', {before: false, after: true}],
'space-in-parens': ['error', 'never'],
'keyword-spacing': 'error',
'space-before-blocks': 'error',
'space-infix-ops': 'error',
'space-before-function-paren': ['error', 'never'],
'no-spaced-func': 'error',
'no-multi-spaces': 'error',
'space-unary-ops': 'error',
'object-curly-spacing': ['error', 'never'],
'array-bracket-spacing': ['error', 'never'],
'brace-style': ['error', '1tbs'],
curly: ['error', 'all'],

//*** Node.js specifics ***

'no-process-exit': 'error',

// Use "require" statements in the global scope context (eg. no
// "require" statements inside functions, etc.)
//'global-require': 'error',

// Use Buffer.from, Buffer.alloc, and Buffer.allocUnsafe instead of the
// Buffer constructor (security vulnerabilities).
'no-buffer-constructor': 'error',

// No Sync methods are they degrade perfs
'no-sync': 'error',

// Disallow string concatenation when using __dirname and __filename
'no-path-concat': 'error',

// Presentation
indent: ['error', 2],
quotes: ['error', 'single', 'avoid-escape'],
'new-cap': 'error',
camelcase: 'off',
'no-underscore-dangle': 'off',
'space-before-function-paren': ['warn', 'never'],
// Enforce Callback Error Handling
'handle-callback-err': 'error',

'no-new-require': 'error',
},
};
26 changes: 13 additions & 13 deletions example/server.js
@@ -1,12 +1,12 @@
'use strict';
'use strict'

const path = require('path');
const http = require('http');
const convict = require('../lib/convict.js');
const path = require('path')
const http = require('http')
const convict = require('../lib/convict.js')

convict.addFormat(require('convict-format-with-validator').ipaddress);
convict.addFormat(require('convict-format-with-validator').ipaddress)

let conf = convict({
const conf = convict({
ip: {
doc: 'The IP Address to bind.',
format: 'ipaddress',
Expand All @@ -19,12 +19,12 @@ let conf = convict({
default: 0,
env: 'PORT'
}
}).loadFile(path.join(__dirname, 'config.json')).validate();
}).loadFile(path.join(__dirname, 'config.json')).validate()

let server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
const server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('Hello World\n')
}).listen(conf.get('port'), conf.get('ip'), function() {
let addy = server.address();
console.log('running on http://%s:%d', addy.address, addy.port); // eslint-disable-line no-console
});
const addy = server.address()
console.log('running on http://%s:%d', addy.address, addy.port) // eslint-disable-line no-console
})
102 changes: 88 additions & 14 deletions packages/convict-format-with-moment/.eslintrc.js
Expand Up @@ -9,28 +9,102 @@ module.exports = {
mocha: true,
},
rules: {
strict: ['error', 'global'],
//*** Programming best practices ***

// JavaScript core
'no-array-constructor': 'error',
'no-unused-vars': ['error', {'args': 'none'}],
strict: ['error', 'safe'],
semi: ['error', 'never'],

'no-unused-vars': ['error', {args: 'none'}],

// Don't use "var", only use "let" and "const"
'no-var': 'error',
// Use const if a variable is never reassigned
'prefer-const': 'error',

// No redeclaration of existing restricted names and builtins
'no-shadow-restricted-names': 'error',
// No redeclaration of existing variables in outer scope
//'no-shadow': ['error', {builtinGlobals: true}],
//
// Interesting but produces less readable code …
//'no-shadow': ['error', {builtinGlobals: true, hoist: 'all'}],

'no-array-constructor': ['error'],

// No dead code
'no-unreachable': 'error',
'no-fallthrough': 'error',

// Take full advantage of JavaScript flexibility by being able, in a
// function, to return different types (for exemple sometimes a boolean
// and sometimes an object).
'consistent-return': 'off',

// Disallow gratuitous parentheses
'no-extra-parens': ['error', 'all', {conditionalAssign: false}],

// Error best practices:
// Only throw Error instances
'no-throw-literal': 'error',

// Switch-case best practices
'default-case': 'error',
'no-fallthrough': 'error',
'no-case-declarations': 'error',
'consistent-return': 'off',

// Node.js specifics
// Enforces return statements in callbacks of array's methods
'array-callback-return': 'error',

'no-console': 'error',

//*** Presentation style ***

indent: ['error', 2],
quotes: ['error', 'single', {avoidEscape: true}],
'quote-props': ['error', 'as-needed'],

// Use as much as possible snake_case for variable names and camelCase for
// function names.
camelcase: 'off',

'new-cap': 'error',

'no-multiple-empty-lines': ['error', {max: 2}],
'no-trailing-spaces': 'error',
'comma-spacing': ['error', {before: false, after: true}],
'space-in-parens': ['error', 'never'],
'keyword-spacing': 'error',
'space-before-blocks': 'error',
'space-infix-ops': 'error',
'space-before-function-paren': ['error', 'never'],
'no-spaced-func': 'error',
'no-multi-spaces': 'error',
'space-unary-ops': 'error',
'object-curly-spacing': ['error', 'never'],
'array-bracket-spacing': ['error', 'never'],
'brace-style': ['error', '1tbs'],
curly: ['error', 'all'],

//*** Node.js specifics ***

'no-process-exit': 'error',

// Use "require" statements in the global scope context (eg. no
// "require" statements inside functions, etc.)
//'global-require': 'error',

// Use Buffer.from, Buffer.alloc, and Buffer.allocUnsafe instead of the
// Buffer constructor (security vulnerabilities).
'no-buffer-constructor': 'error',

// No Sync methods are they degrade perfs
'no-sync': 'error',

// Disallow string concatenation when using __dirname and __filename
'no-path-concat': 'error',

// Presentation
indent: ['error', 2],
quotes: ['error', 'single', 'avoid-escape'],
'new-cap': 'error',
camelcase: 'off',
'no-underscore-dangle': 'off',
'space-before-function-paren': ['warn', 'never'],
// Enforce Callback Error Handling
'handle-callback-err': 'error',

'no-new-require': 'error',
},
};
8 changes: 5 additions & 3 deletions packages/convict-format-with-moment/lib/index.js
Expand Up @@ -15,19 +15,21 @@ function assert(assertion, err_msg) {
const duration = {
name: 'duration',
coerce: (v) => {
let split = v.split(' ')
const split = v.split(' ')
if (split.length == 1) {
// It must be an integer in string form.
v = parseInt(v, 10)
} else {
// Add an "s" as the unit of measurement used in Moment
if (!split[1].match(/s$/)) split[1] += 's'
if (!split[1].match(/s$/)) {
split[1] += 's'
}
v = moment.duration(parseInt(split[0], 10), split[1]).valueOf()
}
return v
},
validate: 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 Down
58 changes: 32 additions & 26 deletions packages/convict-format-with-moment/test/format-tests.js
@@ -1,17 +1,17 @@
'use strict';
'use strict'

require('must');
require('must')

const moment = require('moment');
const moment = require('moment')

describe('convict formats', function() {
const convict = require('convict');
let conf;
const convict = require('convict')
let conf

it('must add "duration" and "timestamp" format with convict-format-with-moment', function() {
convict.addFormat(require('../').duration);
convict.addFormat(require('../').timestamp);
});
convict.addFormat(require('../').duration)
convict.addFormat(require('../').timestamp)
})

it('must parse a config specification', function() {
conf = convict({
Expand Down Expand Up @@ -41,38 +41,44 @@ describe('convict formats', function() {
default: '12345'
}
}
});
})

});
})

it('validates default schema', function() {
(function() { conf.validate(); }).must.not.throw();
});
(function() {
conf.validate()
}).must.not.throw()
})

it('successfully fails to validate incorrect values', function() {
conf.set('foo.duration4', '-7 days');
(function() { conf.validate(); }).must.throw(Error, /must be a positive integer or human readable string/);
(function() {
conf.validate()
}).must.throw(Error, /must be a positive integer or human readable string/)

conf.set('foo.duration5', 'zz-7zzdays');
(function() { conf.validate(); }).must.throw(Error, /must be a positive integer or human readable string/);
});
(function() {
conf.validate()
}).must.throw(Error, /must be a positive integer or human readable string/)
})

describe('predefined formats', function() {
it('must handle timestamp', function() {
let val = conf.get('foo.date');
val.must.be(moment('2013-05-05').valueOf());
});
const val = conf.get('foo.date')
val.must.be(moment('2013-05-05').valueOf())
})

it('must handle duration in milliseconds', function() {
conf.get('foo.duration').must.be(604800000);
});
conf.get('foo.duration').must.be(604800000)
})

it('must handle duration in a human readable string', function() {
conf.get('foo.duration2').must.be(60 * 5 * 1000);
});
conf.get('foo.duration2').must.be(60 * 5 * 1000)
})

it('must handle duration in milliseconds as a string', function() {
conf.get('foo.duration3').must.be(12345);
});
});
});
conf.get('foo.duration3').must.be(12345)
})
})
})

0 comments on commit 5205849

Please sign in to comment.