fix: validate non-numeric level options
This commit is contained in:
parent
aa06bb5ac3
commit
1fd0309724
2 changed files with 9 additions and 1 deletions
|
|
@ -22,7 +22,7 @@ const levelMapping = [
|
||||||
const styles = Object.create(null);
|
const styles = Object.create(null);
|
||||||
|
|
||||||
const applyOptions = (object, options = {}) => {
|
const applyOptions = (object, options = {}) => {
|
||||||
if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
|
if (options.level !== undefined && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
|
||||||
throw new Error('The `level` option should be an integer from 0 to 3');
|
throw new Error('The `level` option should be an integer from 0 to 3');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,5 +20,13 @@ test('the `level` option should be a number from 0 to 3', t => {
|
||||||
t.throws(() => {
|
t.throws(() => {
|
||||||
new Chalk({level: -1});
|
new Chalk({level: -1});
|
||||||
}, {message: /should be an integer from 0 to 3/});
|
}, {message: /should be an integer from 0 to 3/});
|
||||||
|
|
||||||
|
t.throws(() => {
|
||||||
|
new Chalk({level: null});
|
||||||
|
}, {message: /should be an integer from 0 to 3/});
|
||||||
|
|
||||||
|
t.throws(() => {
|
||||||
|
new Chalk({level: Number.NaN});
|
||||||
|
}, {message: /should be an integer from 0 to 3/});
|
||||||
/* eslint-enable no-new */
|
/* eslint-enable no-new */
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue