Skip to content

Commit

Permalink
refactor(api): refactor error documentation into RangeError type
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Scott-Lassiter committed Apr 30, 2022
1 parent e7d0ac6 commit 17f4124
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ console.log(encoder.dictionary) // 'ABCD'
encoder.dictionary = 'ABCDA' // Throws error because the letter 'A' is repeated
```

- Throws **[Error][13]** if setting dictionary to `null`, `undefined` or empty string (i.e. `''`)
- Throws **[Error][13]** if `newDictionary` contains a non-alphanumeric character
- Throws **[Error][13]** if `newDictionary` has a repeating character
- 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

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

Expand Down Expand Up @@ -99,7 +99,7 @@ console.log(encoder.encode(null)) // undefined
console.log(encoder.encode(undefined)) // undefined
```

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

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

Expand Down Expand Up @@ -137,7 +137,7 @@ console.log(encoder.decode('ADBAC')) // 551
console.log(encoder.decode('ANE')) // undefined
```

- Throws **[Error][13]** if the decoded integer exceeds the maximum safe integer for Javascript (`2^53 - 1 = 9007199254740991`).
- Throws **[RangeError][13]** 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`.

Expand All @@ -153,5 +153,5 @@ Returns **[number][14]** Positive integer representation. If one of the characte
[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/Error
[13]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RangeError
[14]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class AlphanumericEncoder {
* Returns or sets the current dictionary.
*
* @param {string} newDictionary (If setting) String of unique letters and numbers, in order, for the new dictionary
* @throws {Error} if setting dictionary to `null`, `undefined` or empty string (i.e. `''`)
* @throws {Error} if `newDictionary` contains a non-alphanumeric character
* @throws {Error} if `newDictionary` has a repeating character
* @throws {RangeError} if setting dictionary to `null`, `undefined` or empty string (i.e. `''`)
* @throws {RangeError} if `newDictionary` contains a non-alphanumeric character
* @throws {RangeError} if `newDictionary` has a repeating character
* @returns {string} (If used as getter) The current dictionary in use
* @default `ABCDEFGHIJKLMNOPQRSTUVWXYZ`
*
Expand All @@ -39,13 +39,13 @@ class AlphanumericEncoder {
set dictionary(newDictionary) {
// Check for empty dictionaries
if (newDictionary === null || newDictionary === undefined || newDictionary.length === 0) {
throw new Error('The dictionary cannot be null, undefined, or an empty string.')
throw new RangeError('The dictionary cannot be null, undefined, or an empty string.')
}

// Check for invalid characters. Using a regular expression, make sure only letters and numbers are allowed.
const regExPattern = /^[a-z0-9]+$/i
if (!regExPattern.test(newDictionary)) {
throw new Error('All characters in the dictionary must be alphanumeric.')
throw new RangeError('All characters in the dictionary must be alphanumeric.')
}

// Convert to upper case only. Verify each character is only used one time within the dictionary.
Expand All @@ -56,7 +56,7 @@ class AlphanumericEncoder {
uppercaseDictionary.indexOf(uppercaseDictionary[i]) !==
uppercaseDictionary.lastIndexOf(uppercaseDictionary[i])
) {
throw new Error(
throw new RangeError(
`The dictionary in use has at least one repeating symbol: ${uppercaseDictionary[i]}`
)
}
Expand All @@ -75,7 +75,7 @@ class AlphanumericEncoder {
*
* @param {number} integerToEncode Base 10 integer. If passed a non-integer number, decimal values are truncated.
* Passing zero, negative numbers, or non-numbers will return `undefined`.
* @throws {Error} if `integerToEncode` exceeds the maximum safe integer for Javascript (`2^53 - 1 = 9007199254740991`).
* @throws {RangeError} if `integerToEncode` exceeds the maximum safe integer for Javascript (`2^53 - 1 = 9007199254740991`).
* @returns {string} Dictionary encoded value
*
* @example
Expand Down Expand Up @@ -113,7 +113,7 @@ class AlphanumericEncoder {
return undefined
}
if (integerToEncode > Number.MAX_SAFE_INTEGER) {
throw new Error(
throw new RangeError(
'The encoding value is greater than the maximum safe integer for Javascript.'
)
}
Expand Down Expand Up @@ -154,7 +154,7 @@ class AlphanumericEncoder {
*
* @param {string} stringToDecode If passed a non-integer number, decimal values are truncated.
* Passing an empty string, `null`, or `undefined` will return `undefined`.
* @throws {Error} if the decoded integer exceeds the maximum safe integer for Javascript (`2^53 - 1 = 9007199254740991`).
* @throws {RangeError} if the decoded integer exceeds the maximum safe integer for Javascript (`2^53 - 1 = 9007199254740991`).
* @returns {number} Positive integer representation. If one of the characters is not present in the dictionary, it will return `undefined`.
* @example
* const encoder = new AlphanumericEncoder()
Expand Down Expand Up @@ -197,7 +197,7 @@ class AlphanumericEncoder {
}

if (result > Number.MAX_SAFE_INTEGER) {
throw new Error(
throw new RangeError(
'The decoded value is greater than the maximum safe integer for Javascript.'
)
}
Expand Down

0 comments on commit 17f4124

Please sign in to comment.