From 1fd030972464b7aa9eb0812ca88d54a159261d0f Mon Sep 17 00:00:00 2001 From: Bojun Chai Date: Tue, 21 Apr 2026 00:23:05 +0800 Subject: [PATCH] fix: validate non-numeric level options --- source/index.js | 2 +- test/instance.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/source/index.js b/source/index.js index 8bc993d..bbb0751 100644 --- a/source/index.js +++ b/source/index.js @@ -22,7 +22,7 @@ const levelMapping = [ const styles = Object.create(null); 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'); } diff --git a/test/instance.js b/test/instance.js index c3cc70b..7a953f9 100644 --- a/test/instance.js +++ b/test/instance.js @@ -20,5 +20,13 @@ test('the `level` option should be a number from 0 to 3', t => { t.throws(() => { new Chalk({level: -1}); }, {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 */ });