Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use consts and arrow functions where possible. #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"node": true
},
"rules": {
"prefer-arrow-callback": ["error", {
"allowNamedFunctions": true
}],
"prefer-const": ["error"],
"indent": ["error", 2],
"no-eval": 2,
"no-console": 2,
Expand Down
18 changes: 8 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
'use strict';

var minimist = require('minimist');
var processConfig = require('./lib/processConfig');
var defaultParsers = require('./lib/defaultParsers');
const minimist = require('minimist');
const processConfig = require('./lib/processConfig');
const defaultParsers = require('./lib/defaultParsers');

module.exports = function konfiga(schema, options) {
var opts = options || {};
var argv = opts.argv || minimist(process.argv.slice(2));
var env = opts.env || process.env;
var parsers = new Map(defaultParsers);
const opts = options || {};
const argv = opts.argv || minimist(process.argv.slice(2));
const env = opts.env || process.env;
const parsers = new Map(defaultParsers);

(opts.parsers || []).forEach(function(parserSpec) {
parsers.set(parserSpec.type, parserSpec.parser);
});
(opts.parsers || []).forEach(({type, parser}) => parsers.set(type, parser));

return processConfig(schema, argv, env, parsers);
};
2 changes: 1 addition & 1 deletion lib/castValue.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports = function castValue(value, type, parsers) {
var parser = type ? parsers.get(type) : parsers.get('default');
const parser = type ? parsers.get(type) : parsers.get('default');

if (!parser) {
throw new Error('Unsupported config value type: ' + type.name);
Expand Down
2 changes: 1 addition & 1 deletion lib/defaultParsers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var defaultParsers = new Map();
const defaultParsers = new Map();

defaultParsers.set('default', function parseDefault(value) {
return value.toString();
Expand Down
24 changes: 12 additions & 12 deletions lib/processConfig.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use strict';

var castValue = require('./castValue');
const castValue = require('./castValue');

module.exports = function processConfig(schema, argv, env, parsers) {
var config = {};
var argvNames = Object.getOwnPropertyNames(argv);
var envNames = Object.getOwnPropertyNames(env);
var missingRequiredProperties = [];
const config = {};
const argvNames = Object.getOwnPropertyNames(argv);
const envNames = Object.getOwnPropertyNames(env);
const missingRequiredProperties = [];

Object.keys(schema).forEach(function processOption(optionName) {
var optionProperties = schema[optionName];
var type = optionProperties.type;
var cmdArgName = optionProperties.cmdLineArgName;
var envVarname = optionProperties.envVariableName;
var hasArgvName = argvNames.indexOf(cmdArgName) !== -1;
var hasEnvName = envNames.indexOf(envVarname) !== -1;
var hasDefault = Object.hasOwnProperty.call(optionProperties, 'defaultValue');
const optionProperties = schema[optionName];
const type = optionProperties.type;
const cmdArgName = optionProperties.cmdLineArgName;
const envVarname = optionProperties.envVariableName;
const hasArgvName = argvNames.indexOf(cmdArgName) !== -1;
const hasEnvName = envNames.indexOf(envVarname) !== -1;
const hasDefault = Object.hasOwnProperty.call(optionProperties, 'defaultValue');

if (hasArgvName) {
config[optionName] = castValue(argv[cmdArgName], type, parsers);
Expand Down
38 changes: 19 additions & 19 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ var assert = require('assert');
var sinon = require('sinon');
var SandboxedModule = require('sandboxed-module');

describe('konfiga', function() {
describe('konfiga', () => {
var konfiga;
var minimistStub;
var processConfigStub;
var fakeArgv;
var fakeEnv;

before(function() {
before(() => {
minimistStub = sinon.stub();
processConfigStub = sinon.stub();

Expand All @@ -33,85 +33,85 @@ describe('konfiga', function() {
});
});

beforeEach(function() {
beforeEach(() => {
minimistStub.returns('fakeMinimistOutput');
processConfigStub.returns('fakeProcessedConfig');
});

afterEach(function() {
afterEach(() => {
minimistStub.reset();
processConfigStub.reset();
});

it('is a function', function() {
it('is a function', () => {
assert.strictEqual(typeof konfiga, 'function');
});

it('has an arity of 2', function() {
it('has an arity of 2', () => {
assert.strictEqual(konfiga.length, 2);
});

it('returns a processed config object', function() {
it('returns a processed config object', () => {
assert.strictEqual(konfiga('fakeSchema'), 'fakeProcessedConfig');
});

it('passes the first argument (schema) to processConfig', function() {
it('passes the first argument (schema) to processConfig', () => {
konfiga('fakeSchema');

assert.strictEqual(processConfigStub.args[0][0], 'fakeSchema');
});

it('passes the argv option to processConfig if present', function() {
it('passes the argv option to processConfig if present', () => {
var customArgvObject = {some: 'rubbish'};

konfiga('fakeSchema', {argv: customArgvObject});

assert.strictEqual(processConfigStub.args[0][1], customArgvObject);
});

it('passes the env option to processConfig if present', function() {
it('passes the env option to processConfig if present', () => {
var customEnvObject = {some: 'rubbish'};

konfiga('fakeSchema', {env: customEnvObject});

assert.strictEqual(processConfigStub.args[0][2], customEnvObject);
});

it('passes the parsers option merged with default parsers to processConfig if present', function() {
it('passes the parsers option merged with default parsers to processConfig if present', () => {
konfiga('fakeSchema', {parsers: [{type: 'more', parser: 'custom-parsers'}]});

assert.equal(processConfigStub.args[0][3].size, 2);
assert.equal(processConfigStub.args[0][3].get('some'), 'parsers');
assert.equal(processConfigStub.args[0][3].get('more'), 'custom-parsers');
});

describe('if no argv option is passed', function() {
it('passes every arg after the second of process.argv to minimist', function() {
describe('if no argv option is passed', () => {
it('passes every arg after the second of process.argv to minimist', () => {
konfiga('fakeSchema');

assert.strictEqual(minimistStub.callCount, 1);
assert.strictEqual(minimistStub.calledWith(['--an-arg', 'argValue']), true);
});

describe('the value returned by minimist', function() {
it('is passed to processConfig', function() {
describe('the value returned by minimist', () => {
it('is passed to processConfig', () => {
konfiga('fakeSchema');

assert.strictEqual(processConfigStub.args[0][1], 'fakeMinimistOutput');
});
});
});

describe('if no env option is passed', function() {
it('passes proccess.env to processConfig', function() {
describe('if no env option is passed', () => {
it('passes proccess.env to processConfig', () => {
konfiga('fakeSchema');

assert.strictEqual(processConfigStub.args[0][2], fakeEnv);
});
});

describe('if no parsers option is passed', function() {
it('passes default parsers into the processConfig', function() {
describe('if no parsers option is passed', () => {
it('passes default parsers into the processConfig', () => {
konfiga('fakeSchema');

assert.equal(processConfigStub.args[0][3].size, 1);
Expand Down
26 changes: 13 additions & 13 deletions test/lib/castValue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ var assert = require('assert');
var sinon = require('sinon');
var castValue = require('../../lib/castValue');

describe('castValue', function() {
describe('castValue', () => {
var parsers;

beforeEach(function() {
beforeEach(() => {
parsers = new Map([
['test-type', sinon.stub().returns('returned-by-parser-two')],
['default', sinon.stub().returns('returned-by-default-parser')]
]);
});

it('is a function', function() {
it('is a function', () => {
assert.strictEqual(typeof castValue, 'function');
});

it('has an arity of 3', function() {
it('has an arity of 3', () => {
assert.strictEqual(castValue.length, 3);
});

it('throws if the type passed is not supported', function() {
it('throws if the type passed is not supported', () => {
function UnsupportedType() {}

function test() {
Expand All @@ -32,36 +32,36 @@ describe('castValue', function() {
assert.throws(test, 'Unsupported config value type: UnsupportedType');
});

describe('when passed a value with no type', function() {
describe('when passed a value with no type', () => {
var value;
var parsed;

beforeEach(function() {
beforeEach(() => {
value = {toString: sinon.stub().returns('gunk')};
parsed = castValue(value, undefined, parsers);
});

it('calls the default parser with the value', function() {
it('calls the default parser with the value', () => {
assert.deepStrictEqual(parsers.get('default').args, [[value]]);
});

it('returns the result of default parser', function() {
it('returns the result of default parser', () => {
assert.strictEqual(parsed, 'returned-by-default-parser');
});
});

describe('when passed a valid type', function() {
describe('when passed a valid type', () => {
var parsed;

beforeEach(function() {
beforeEach(() => {
parsed = castValue('blah', 'test-type', parsers);
});

it('calls the appropriate parser with the given value', function() {
it('calls the appropriate parser with the given value', () => {
assert.ok(parsers.get('test-type').callCount, 1);
});

it('returns the retured value of the parser', function() {
it('returns the retured value of the parser', () => {
assert.equal(parsed, 'returned-by-parser-two');
});
});
Expand Down
38 changes: 19 additions & 19 deletions test/lib/defaultParsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
var assert = require('assert');
var defaultParsers = require('../../lib/defaultParsers');

describe('defaultParsers', function() {
describe('default', function() {
it('calls toString on the given value, and returns the result', function() {
describe('defaultParsers', () => {
describe('default', () => {
it('calls toString on the given value, and returns the result', () => {
var value = {
toString: function() {
return 'to-string-result';
Expand All @@ -16,8 +16,8 @@ describe('defaultParsers', function() {
});
});

describe('String', function() {
it('calls toString on the given value, and returns the result', function() {
describe('String', () => {
it('calls toString on the given value, and returns the result', () => {
var value = {
toString: function() {
return 'to-string-result';
Expand All @@ -28,54 +28,54 @@ describe('defaultParsers', function() {
});
});

describe('Boolean', function() {
it('returns true if the value is "true"', function() {
describe('Boolean', () => {
it('returns true if the value is "true"', () => {
assert.strictEqual(defaultParsers.get(Boolean)('true'), true);
});

it('returns true if the value is true', function() {
it('returns true if the value is true', () => {
assert.strictEqual(defaultParsers.get(Boolean)(true), true);
});

it('returns false if the value is falsey', function() {
it('returns false if the value is falsey', () => {
assert.strictEqual(defaultParsers.get(Boolean)(undefined), false);
});
});

describe('Number', function() {
it('returns a number', function() {
describe('Number', () => {
it('returns a number', () => {
assert.strictEqual(defaultParsers.get(Number)(12.34), 12.34);
});

it('destringifies an integer', function() {
it('destringifies an integer', () => {
assert.strictEqual(defaultParsers.get(Number)('12'), 12);
});

it('destringifies a decimal', function() {
it('destringifies a decimal', () => {
assert.strictEqual(defaultParsers.get(Number)('12.34'), 12.34);
});

it('destringifies other things to NaN', function() {
it('destringifies other things to NaN', () => {
assert.ok(Number.isNaN(defaultParsers.get(Number)('blah')));
});
});

describe('Array', function() {
it('returns an empty array if the value is falsey', function() {
describe('Array', () => {
it('returns an empty array if the value is falsey', () => {
var returnedValue = defaultParsers.get(Array)(undefined);

assert.strictEqual(Array.isArray(returnedValue), true);
assert.strictEqual(returnedValue.length, 0);
});

it('returns the passed value if it is already an array', function() {
it('returns the passed value if it is already an array', () => {
var passedValue = [1, 2, 3];
var returnedValue = defaultParsers.get(Array)(passedValue);

assert.strictEqual(returnedValue, passedValue);
});

it('returns the passed comma delimited string as an array', function() {
it('returns the passed comma delimited string as an array', () => {
var returnedValue = defaultParsers.get(Array)('foo,bar,baz');

assert.strictEqual(Array.isArray(returnedValue), true);
Expand All @@ -85,7 +85,7 @@ describe('defaultParsers', function() {
assert.strictEqual(returnedValue[2], 'baz');
});

it('casts a number to a string before returning it wrapped in an array', function() {
it('casts a number to a string before returning it wrapped in an array', () => {
var returnedValue = defaultParsers.get(Array)(123);

assert.strictEqual(Array.isArray(returnedValue), true);
Expand Down
Loading