Skip to content

Commit

Permalink
feat: add the resetDefaultDictionary method
Browse files Browse the repository at this point in the history
This provides the end user with an easy ability to reset the dictionary in use back to the default.
It also refactors the constructor to have separate values for the default dictionary and the
dictionary in use.

Resolves: #29
  • Loading branch information
M-Scott-Lassiter committed Apr 30, 2022
1 parent 17f4124 commit 9166bd8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 35 deletions.
74 changes: 47 additions & 27 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
- [dictionary][3]
- [Parameters][4]
- [Examples][5]
- [encode][6]
- [Parameters][7]
- [Examples][8]
- [decode][9]
- [Parameters][10]
- [Examples][11]
- [resetDefaultDictionary][6]
- [Examples][7]
- [encode][8]
- [Parameters][9]
- [Examples][10]
- [decode][11]
- [Parameters][12]
- [Examples][13]

## AlphanumericEncoder

Expand All @@ -32,7 +34,7 @@ Returns or sets the current dictionary.

#### Parameters

- `newDictionary` **[string][12]** (If setting) String of unique letters and numbers, in order, for the new dictionary
- `newDictionary` **[string][14]** (If setting) String of unique letters and numbers, in order, for the new dictionary

#### Examples

Expand All @@ -47,19 +49,36 @@ console.log(encoder.dictionary) // 'ABCD'
encoder.dictionary = 'ABCDA' // Throws error because the letter 'A' is repeated
```

- Throws **[RangeError][13]** if setting dictionary to `null`, `undefined` or empty string (i.e. `''`)
- Throws **[RangeError][13]** if `newDictionary` contains a non-alphanumeric character
- Throws **[RangeError][13]** if `newDictionary` has a repeating character
- Throws **[RangeError][15]** if setting dictionary to `null`, `undefined` or empty string (i.e. `''`)
- Throws **[RangeError][15]** if `newDictionary` contains a non-alphanumeric character
- Throws **[RangeError][15]** if `newDictionary` has a repeating character

Returns **[string][12]** (If used as getter) The current dictionary in use
Returns **[string][14]** (If used as getter) The current dictionary in use

### resetDefaultDictionary

Reset the dictionary in use to the default.

#### Examples

```javascript
const encoder = new AlphanumericEncoder()
console.log(encoder.dictionary) // 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
encoder.dictionary = 'ABCD'
console.log(encoder.dictionary) // 'ABCD'
encoder.resetDefaultDictionary()
console.log(encoder.dictionary) // 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
```

Returns **void**

### encode

Takes any number and converts it into a base (dictionary length) letter combo.

#### Parameters

- `integerToEncode` **[number][14]** Base 10 integer. If passed a non-integer number, decimal values are truncated.
- `integerToEncode` **[number][16]** Base 10 integer. If passed a non-integer number, decimal values are truncated.
Passing zero, negative numbers, or non-numbers will return `undefined`.

#### Examples
Expand Down Expand Up @@ -93,23 +112,22 @@ console.log(encoder.encode(733)) // 'CBBA'
```

```javascript
const encoder = new AlphanumericEncoder()
console.log(encoder.encode('A')) // undefined
console.log(encoder.encode(null)) // undefined
console.log(encoder.encode(undefined)) // undefined
```

- Throws **[RangeError][13]** if `integerToEncode` exceeds the maximum safe integer for Javascript (`2^53 - 1 = 9007199254740991`).
- Throws **[RangeError][15]** if `integerToEncode` exceeds the maximum safe integer for Javascript (`2^53 - 1 = 9007199254740991`).

Returns **[string][12]** Dictionary encoded value
Returns **[string][14]** Dictionary encoded value

### decode

Takes any string and converts it into a base 10 integer based on the defined dictionary.

#### Parameters

- `stringToDecode` **[string][12]** If passed a non-integer number, decimal values are truncated.
- `stringToDecode` **[string][14]** If passed a non-integer number, decimal values are truncated.
Passing an empty string, `null`, or `undefined` will return `undefined`.

#### Examples
Expand Down Expand Up @@ -137,21 +155,23 @@ console.log(encoder.decode('ADBAC')) // 551
console.log(encoder.decode('ANE')) // undefined
```

- Throws **[RangeError][13]** if the decoded integer exceeds the maximum safe integer for Javascript (`2^53 - 1 = 9007199254740991`).
- Throws **[RangeError][15]** if the decoded integer exceeds the maximum safe integer for Javascript (`2^53 - 1 = 9007199254740991`).

Returns **[number][14]** Positive integer representation. If one of the characters is not present in the dictionary, it will return `undefined`.
Returns **[number][16]** Positive integer representation. If one of the characters is not present in the dictionary, it will return `undefined`.

[1]: #alphanumericencoder
[2]: #examples
[3]: #dictionary
[4]: #parameters
[5]: #examples-1
[6]: #encode
[7]: #parameters-1
[8]: #examples-2
[9]: #decode
[10]: #parameters-2
[11]: #examples-3
[12]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[13]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RangeError
[14]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[6]: #resetdefaultdictionary
[7]: #examples-2
[8]: #encode
[9]: #parameters-1
[10]: #examples-3
[11]: #decode
[12]: #parameters-2
[13]: #examples-4
[14]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[15]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RangeError
[16]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
32 changes: 24 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
*/
class AlphanumericEncoder {
constructor() {
/**
* @private
* @type {string} Internal property holding the dictionary in use
*/
this._dictionary = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' // Default dictionary is the English alphabet, all capitalized, in order
/** @private */
this._defaultDictionary = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' // Default dictionary is the English alphabet, all capitalized, in order
/** @private */
this._dictionaryInUse = ''
/** @private */
this.resetDefaultDictionary()
}

/**
Expand Down Expand Up @@ -63,11 +64,27 @@ class AlphanumericEncoder {
}

// Validation is complete. Update the internal property.
this._dictionary = uppercaseDictionary
this._dictionaryInUse = uppercaseDictionary
}

get dictionary() {
return this._dictionary
return this._dictionaryInUse
}

/**
* Reset the dictionary in use to the default.
* @default `ABCDEFGHIJKLMNOPQRSTUVWXYZ`
* @returns {void}
* @example
* const encoder = new AlphanumericEncoder()
* console.log(encoder.dictionary) // 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
* encoder.dictionary = 'ABCD'
* console.log(encoder.dictionary) // 'ABCD'
* encoder.resetDefaultDictionary()
* console.log(encoder.dictionary) // 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
*/
resetDefaultDictionary() {
this._dictionaryInUse = this._defaultDictionary
}

/**
Expand Down Expand Up @@ -103,7 +120,6 @@ class AlphanumericEncoder {
* console.log(encoder.encode(733)) // 'CBBA'
*
* @example
* const encoder = new AlphanumericEncoder()
* console.log(encoder.encode('A')) // undefined
* console.log(encoder.encode(null)) // undefined
* console.log(encoder.encode(undefined)) // undefined
Expand Down

0 comments on commit 9166bd8

Please sign in to comment.