Skip to content

Commit

Permalink
fix: correct bug that allowed bad dictionary values to pass through t…
Browse files Browse the repository at this point in the history
…he constructor

Although directly setting bad dictionary values (e.g. `encoder.dictionary = null`) would throw an
error, using the constructor did not (e.g. `encoder = new AlphanumericEncoder( { dictionary: null }
)` ). This was due to the manner in which the constructor was checking to see if these optional
values were passed or not. This patch corrects that unintended behavior.

Resolves: #41
  • Loading branch information
M-Scott-Lassiter committed May 14, 2022
1 parent 8cca388 commit bc6e6c7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* A class for encoding and decoding base 10 integers to a custom alphanumeric base representation.
* @param {object} [configOptions] Optional object defining initial settings for the class
* @param {boolean} [configOptions.allowLowerCaseDictionary] Whether or not to allow lower case letters in the dictionary
* @param {string} [configOptions.dictionary] Starting dictionary to use
* @param {string} [configOptions.dictionary] Starting dictionary to use. Must contain only letters or numbers. Characters cannot be repeated.
* If `allowLowerCaseDictionary = true`, then lower case letters are not considered the same as upper case. (e.g. 'ABCabc' has 6 unique characters.)
* @example
* // Import into a project
* const AlphanumericEncoder = require('alphanumeric-encoder')
Expand Down Expand Up @@ -47,10 +48,10 @@ class AlphanumericEncoder {

// Process the options. If the user included any, then the if statements will evaluate truthy and try to
// set the appropriate values.
if (configOptions.allowLowerCaseDictionary) {
if ('allowLowerCaseDictionary' in configOptions) {
this.allowLowerCaseDictionary = configOptions.allowLowerCaseDictionary
}
if (configOptions.dictionary) {
if ('dictionary' in configOptions) {
this.dictionary = configOptions.dictionary
}
}
Expand All @@ -76,7 +77,7 @@ class AlphanumericEncoder {
* encoder.dictionary = 'ABCDA' // Throws error because the letter 'A' is repeated
*/
set dictionary(newDictionary) {
// Check for empty dictionaries
// Check for empty or wrong type dictionaries
if (
typeof newDictionary !== 'string' ||
newDictionary.length === 0 ||
Expand Down
34 changes: 34 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ describe('Allow Lower Case Dictionaries', () => {
}
)

test.each([true, 1, [123], { value: 1 }])(
'allowLowerCaseDictionary with truthy value in setup: new AlphanumericEncoder({ allowLowerCaseDictionary: %p })',
(truthyTestValue) => {
const setupEncoder = new AlphanumericEncoder({
allowLowerCaseDictionary: truthyTestValue
})
expect(setupEncoder.allowLowerCaseDictionary).toBeTruthy()
}
)

test.each([false, 0, null, undefined])(
'allowLowerCaseDictionary with falsy value %p',
(truthyTestValue) => {
Expand All @@ -82,6 +92,16 @@ describe('Allow Lower Case Dictionaries', () => {
}
)

test.each([false, 0, null, undefined])(
'allowLowerCaseDictionary with falsy value in setup: new AlphanumericEncoder({ allowLowerCaseDictionary: %p })',
(truthyTestValue) => {
const setupEncoder = new AlphanumericEncoder({
allowLowerCaseDictionary: truthyTestValue
})
expect(setupEncoder.allowLowerCaseDictionary).toBeFalsy()
}
)

test('Allow lower case dictionaries by using a config object', () => {
encoder = new AlphanumericEncoder({ allowLowerCaseDictionary: true })
expect(encoder.allowLowerCaseDictionary).toBeTruthy()
Expand Down Expand Up @@ -120,16 +140,30 @@ describe('Dictionary Validation', () => {

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

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

test.each([true, false, null, undefined, '', NaN, { dictionary: 'ABC' }, ['ABC'], 'ABCDA'])(
'Cannot pass bad dictionary arguments in constructor: new AlphanumericEncoder({dictionary: %p}',
(dictionaryInput) => {
expect(() => {
// eslint-disable-next-line no-unused-vars
const setupEncoder = new AlphanumericEncoder({
dictionary: dictionaryInput
})
}).toThrow()
}
)

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

Expand Down

0 comments on commit bc6e6c7

Please sign in to comment.