From 17f4124357a9cf66cc14bd1517f5ef46072d82c4 Mon Sep 17 00:00:00 2001 From: M-Scott-Lassiter Date: Sat, 30 Apr 2022 15:04:49 -0700 Subject: [PATCH] refactor(api): refactor error documentation into `RangeError` type --- API.md | 12 ++++++------ index.js | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/API.md b/API.md index 1d2eaa4..a0be9c5 100644 --- a/API.md +++ b/API.md @@ -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 @@ -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 @@ -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`. @@ -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 diff --git a/index.js b/index.js index 71dbf63..7673875 100644 --- a/index.js +++ b/index.js @@ -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` * @@ -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. @@ -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]}` ) } @@ -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 @@ -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.' ) } @@ -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() @@ -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.' ) }