Skip to content

Commit

Permalink
fix: improve dictionary type checking
Browse files Browse the repository at this point in the history
Passing a boolean value or NaN as a dictionary did not cause an error, and it should have. This
updates behavior and adds applicable tests.

Resolves: #39
  • Loading branch information
M-Scott-Lassiter committed May 14, 2022
1 parent 79e2652 commit 3f37616
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,15 @@ class AlphanumericEncoder {
*/
set dictionary(newDictionary) {
// Check for empty dictionaries
if (newDictionary === null || newDictionary === undefined || newDictionary.length === 0) {
throw new RangeError('The dictionary cannot be null, undefined, or an empty string.')
if (
typeof newDictionary !== 'string' ||
newDictionary.length === 0 ||
// eslint-disable-next-line no-self-compare
newDictionary !== newDictionary // This verifies it wasn't passed NaN
) {
throw new RangeError(
'The dictionary cannot be null, undefined, boolean, NaN, or an empty string.'
)
}

// Check for invalid characters. Using a regular expression, make sure only letters and numbers are allowed.
Expand Down
12 changes: 12 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ describe('Dictionary Validation', () => {
}).toThrow(/undefined/)
})

test.each([true, false])('Dictionary cannot be boolean %p', (input) => {
expect(() => {
encoder.dictionary = input
}).toThrow(/boolean/)
})

test('Dictionary cannot be NaN', () => {
expect(() => {
encoder.dictionary = NaN
}).toThrow(/NaN/)
})

describe('Valid Dictionaries (no lower case)', () => {
setupNewEncoderForTesting()

Expand Down

0 comments on commit 3f37616

Please sign in to comment.