Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reduce codeclimate cognitive complexity
- Loading branch information
1 parent
04a18a7
commit b722b9c
Showing
3 changed files
with
162 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* DRY function for checking if passed value is of expected type. | ||
* | ||
* @param {string} name Name of setting to check | ||
* @param {object} value Value to check | ||
* @param {string} type Type to match | ||
* @param {string} errorPrefix String to prepend thrown error with | ||
* @returns {bool} | ||
* @throws {TypeError} | ||
*/ | ||
function checkType(name, value, type, errorPrefix) { | ||
if (type === 'array' && !Array.isArray(value)) { | ||
throw new TypeError(`${errorPrefix} configuration setting '${name}' not of type '${type}'`); | ||
} | ||
|
||
// eslint-disable-next-line valid-typeof | ||
if (type !== 'array' && typeof value !== type) { | ||
throw new TypeError(`${errorPrefix} configuration setting '${name}' not of type '${type}'`); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Checks if optional value is of expected type. | ||
* | ||
* @param {string} name Object to check | ||
* @param {object} value Object to check | ||
* @param {string} type Property type | ||
* @returns {bool} | ||
* @throws {TypeError} | ||
*/ | ||
const checkTypeOptional = function(name, value, type) { | ||
if (value === null) { | ||
return true; | ||
} | ||
|
||
return checkType(name, value, type, 'Optional'); | ||
}; | ||
|
||
/** | ||
* Checks if required value is of expected type. | ||
* | ||
* @param {string} name Object to check | ||
* @param {object} value Object to check | ||
* @param {string} type Property type | ||
* @returns {bool} | ||
* @throws {TypeError} | ||
*/ | ||
const checkTypeRequired = function(name, value, type) { | ||
if (value === null) { | ||
throw new TypeError(`Required configuration setting '${name}' is missing`); | ||
} | ||
|
||
return checkType(name, value, type, 'Required'); | ||
}; | ||
|
||
module.exports = { | ||
checkTypeOptional, | ||
checkTypeRequired, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* Please note that `valiate()` is being tested through the two methods. | ||
*/ | ||
const { checkTypeOptional, checkTypeRequired } = require('../../lib/utils/type-checks'); | ||
|
||
describe('input-validation', function() { | ||
// -------------------------------------------------------------------------- | ||
// test checkTypeOptional() | ||
// -------------------------------------------------------------------------- | ||
describe('checkTypeOptional', function() { | ||
it(`returns true for null values`, function() { | ||
expect(checkTypeOptional('name', null, 'string')).toBe(true); | ||
}); | ||
|
||
it(`returns true when passed value matches type string`, function() { | ||
expect(checkTypeOptional('name', 'some-string', 'string')).toBe(true); | ||
}); | ||
|
||
it(`returns true when passed value matches type booelan`, function() { | ||
expect(checkTypeOptional('name', true, 'boolean')).toBe(true); | ||
}); | ||
|
||
it(`returns true when passed value matches type array`, function() { | ||
expect(checkTypeOptional('name', [1, 2, 3], 'array')).toBe(true); | ||
}); | ||
|
||
it('throws an error for mismatching string type', function() { | ||
expect(() => { | ||
checkTypeOptional('name', 123, 'string'); | ||
}).toThrow("Optional configuration setting 'name' not of type 'string'"); | ||
}); | ||
|
||
it('throws an error for mismatching boolean type', function() { | ||
expect(() => { | ||
checkTypeOptional('name', 123, 'boolean'); | ||
}).toThrow("Optional configuration setting 'name' not of type 'boolean'"); | ||
}); | ||
|
||
it('throws an error for mismatching array type', function() { | ||
expect(() => { | ||
checkTypeOptional('name', 123, 'array'); | ||
}).toThrow("Optional configuration setting 'name' not of type 'array'"); | ||
}); | ||
}); | ||
|
||
// -------------------------------------------------------------------------- | ||
// test checkTypeOptional() | ||
// -------------------------------------------------------------------------- | ||
describe('checkTypeRequired', function() { | ||
it('throws an error if value is missing', function() { | ||
expect(() => { | ||
checkTypeRequired('name', null, 'string'); | ||
}).toThrow("Required configuration setting 'name' is missing"); | ||
}); | ||
|
||
it(`returns true when passed value matches type string`, function() { | ||
expect(checkTypeRequired('name', 'some-string', 'string')).toBe(true); | ||
}); | ||
|
||
it(`returns true when passed value matches type booelan`, function() { | ||
expect(checkTypeRequired('name', true, 'boolean')).toBe(true); | ||
}); | ||
|
||
it(`returns true when passed value matches type array`, function() { | ||
expect(checkTypeRequired('name', [1, 2, 3], 'array')).toBe(true); | ||
}); | ||
|
||
it('throws an error for mismatching string type', function() { | ||
expect(() => { | ||
checkTypeRequired('name', 123, 'string'); | ||
}).toThrow("Required configuration setting 'name' not of type 'string'"); | ||
}); | ||
|
||
it('throws an error for mismatching boolean type', function() { | ||
expect(() => { | ||
checkTypeRequired('name', 123, 'boolean'); | ||
}).toThrow("Required configuration setting 'name' not of type 'boolean'"); | ||
}); | ||
|
||
it('throws an error for mismatching array type', function() { | ||
expect(() => { | ||
checkTypeRequired('name', 123, 'array'); | ||
}).toThrow("Required configuration setting 'name' not of type 'array'"); | ||
}); | ||
}); | ||
}); |