Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that caused issues in constructor function #42

Merged
merged 2 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ A class for encoding and decoding base 10 integers to a custom alphanumeric base
- `configOptions` **[object][21]?** Optional object defining initial settings for the class (optional, default `{}`)

- `configOptions.allowLowerCaseDictionary` **[boolean][22]?** Whether or not to allow lower case letters in the dictionary
- `configOptions.dictionary` **[string][23]?** Starting dictionary to use
- `configOptions.dictionary` **[string][23]?** 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.)

### Examples

Expand Down
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
### [1.5.1](https://github.com/M-Scott-Lassiter/Alphanumeric-Encoder/compare/v1.5.0...v1.5.1) (2022-05-14)


### :lady_beetle: Bug Fixes

* improve dictionary type checking ([3f37616](https://github.com/M-Scott-Lassiter/Alphanumeric-Encoder/commit/3f37616376682edeaeed59dfe66a606a61797de4)), closes [#39](https://github.com/M-Scott-Lassiter/Alphanumeric-Encoder/issues/39)
- improve dictionary type checking ([3f37616](https://github.com/M-Scott-Lassiter/Alphanumeric-Encoder/commit/3f37616376682edeaeed59dfe66a606a61797de4)), closes [#39](https://github.com/M-Scott-Lassiter/Alphanumeric-Encoder/issues/39)

## [1.5.0](https://github.com/M-Scott-Lassiter/Alphanumeric-Encoder/compare/v1.4.0...v1.5.0) (2022-05-03)

Expand Down
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