diff --git a/index.js b/index.js index 6b1c1e5..9369547 100644 --- a/index.js +++ b/index.js @@ -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') @@ -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 } } @@ -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 || diff --git a/index.test.js b/index.test.js index 2c5b139..59f488d 100644 --- a/index.test.js +++ b/index.test.js @@ -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) => { @@ -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() @@ -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()