Skip to content

Commit

Permalink
fix: Throw correct type of error
Browse files Browse the repository at this point in the history
* throw RangeError on range errors
  • Loading branch information
manferlo81 committed Jul 15, 2024
1 parent 0c373a2 commit 77fafee
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 15 deletions.
3 changes: 2 additions & 1 deletion __test__/format/find-option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ describe('format "find" option', () => {

});

test('Should throw on invalid find function result multiplier', () => {
test('Should throw on invalid find function resulting multiplier', () => {

const values = [
{ pre: '', mul: 0 },
Expand All @@ -370,6 +370,7 @@ describe('format "find" option', () => {
find: () => value,
});

expect(() => format(10)).toThrow(RangeError);
expect(() => format(10)).toThrow('is not a valid multiplier');

});
Expand Down
1 change: 1 addition & 0 deletions __test__/format/output-option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('format "output" option', () => {

invalidSpaces.forEach((space) => {
const create = () => createFormatter({ output: { space } });
expect(create).toThrow(RangeError);
expect(create).toThrow('Can\'t format output with');
});

Expand Down
2 changes: 2 additions & 0 deletions __test__/format/round-option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('format "round" option', () => {
const create = () => createFormatter({
round: invalid,
});
expect(create).toThrow(RangeError);
expect(create).toThrow('Can\'t create round function with');
});

Expand Down Expand Up @@ -109,6 +110,7 @@ describe('format "round" option', () => {
dec: invalid,
},
});
expect(create).toThrow(RangeError);
expect(create).toThrow('Can\'t create round function with');
});

Expand Down
9 changes: 5 additions & 4 deletions __test__/parse/find-option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('parse "find" option', () => {
items: invalid as never,
},
});
expect(create).toThrow();
expect(create).toThrow('Invalid "find" option');
});

});
Expand Down Expand Up @@ -374,12 +374,11 @@ describe('parse "find" option', () => {
];

invalidMultipliers.forEach((invalid) => {

const parse = createParser({
find: () => invalid as never,
});
expect(() => parse('10 k')).toThrow(RangeError);
expect(() => parse('10 k')).toThrow('is not a valid multiplier');

});

});
Expand Down Expand Up @@ -417,7 +416,9 @@ describe('parse "find" option', () => {
];

invalidFindOptions.forEach((options) => {
expect(() => createParser({ find: options })).toThrow('is not a valid multiplier');
const create = () => createParser({ find: options });
expect(create).toThrow(RangeError);
expect(create).toThrow('is not a valid multiplier');
});

});
Expand Down
4 changes: 4 additions & 0 deletions src/common/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export function error(message: string) {
return new TypeError(message);
}

export function rangeError(message: string) {
return new RangeError(message);
}

export function errorInvalidOption(option: string) {
return error(`Invalid "${option}" option`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/transform-items.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isFiniteNumber } from '../tools/is';
import { error } from './error';
import { rangeError } from './error';
import type { ExponentFindItems, MultiplierFindItems } from './types';

export function transformFindItems(items: ExponentFindItems, base: number): MultiplierFindItems {
return items.map(({ pre, exp }) => {
const mul = base ** exp;
if (!isFiniteNumber(mul) || mul <= 0) {
throw error(`${base} to the power of ${exp} is not a valid multiplier`);
throw rangeError(`${base} to the power of ${exp} is not a valid multiplier`);
}
return { pre, mul };
});
Expand Down
4 changes: 2 additions & 2 deletions src/format/find-unit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { error } from '../common/error';
import { error, rangeError } from '../common/error';
import { createFindTable } from '../common/find-table';
import { MultiplierFindItem } from '../common/types';
import { isFiniteNumber, isFunction, isNumber, isObject } from '../tools/is';
Expand Down Expand Up @@ -31,7 +31,7 @@ export function createUnitFinder(find: FormatFindUnitOption): FormatFindUnitFunc
const { pre, mul } = deprecated_handleResult(result);

if (!isNumber(mul) || !isFiniteNumber(mul) || mul <= 0) {
throw error(`${mul} is not a valid multiplier`);
throw rangeError(`${mul} is not a valid multiplier`);
}

return { pre, mul };
Expand Down
4 changes: 2 additions & 2 deletions src/format/output.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { error, errorInvalidOption } from '../common/error';
import { errorInvalidOption, rangeError } from '../common/error';
import { isFiniteNumber, isFunction, isNumber, isObject } from '../tools/is';
import type { FormatOutputFunction, FormatOutputOption } from './types';

Expand Down Expand Up @@ -34,7 +34,7 @@ export function createFormatOutput(output: FormatOutputOption): FormatOutputFunc

if (isNumber(space)) {
if (!isFiniteNumber(space) || space < 0) {
throw error(`Can't format output with ${space} spaces`);
throw rangeError(`Can't format output with ${space} spaces`);
}
return createOutputFormatter(oneSpace.repeat(space));
}
Expand Down
4 changes: 2 additions & 2 deletions src/format/round.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { error, errorInvalidOption } from '../common/error';
import { errorInvalidOption, rangeError } from '../common/error';
import type { AllowNullish } from '../tools/helper-types';
import { isFiniteNumber, isFunction, isNumber, isObject } from '../tools/is';
import type { FormatRoundOption, RoundFunction } from './types';

function validateNumberOfDecimals(dec: number): number {
if (!isFiniteNumber(dec) || dec < 0) {
throw error(`Can't create round function with ${dec} decimal.`);
throw rangeError(`Can't create round function with ${dec} decimal.`);
}
return dec;
}
Expand Down
4 changes: 2 additions & 2 deletions src/parse/find-multiplier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { error } from '../common/error';
import { error, rangeError } from '../common/error';
import { createFindTable } from '../common/find-table';
import { isFiniteNumber, isFunction, isNumber, isObject } from '../tools/is';
import { defaultBase1000ParseExpItems } from './default-items';
Expand Down Expand Up @@ -31,7 +31,7 @@ export function createMulFinder(find: ParseFindMultiplierOption): ParseFindMulti
}

if (!isNumber(result) || !isFiniteNumber(result) || result <= 0) {
throw error(`${result} is not a valid multiplier`);
throw rangeError(`${result} is not a valid multiplier`);
}

return result;
Expand Down

0 comments on commit 77fafee

Please sign in to comment.