diff --git a/packages/core/src/integer.ts b/packages/core/src/integer.ts index b04c6f866..458871e78 100644 --- a/packages/core/src/integer.ts +++ b/packages/core/src/integer.ts @@ -956,6 +956,20 @@ class Integer { } } +/** + * @private + * @param num + * @param radix + * @param minSize + * @returns {string} + */ +function _convertNumberToString (num: number, radix: number, minSize: number): string { + const theNumberString = num.toString(radix) + const paddingLength = Math.max(minSize - theNumberString.length, 0) + const padding = '0'.repeat(paddingLength) + return `${padding}${theNumberString}` +} + /** * * @private @@ -967,7 +981,7 @@ class Integer { function _isValidNumberFromString (theString: string, theNumber: number, radix: number): boolean { return !Number.isNaN(theString) && !Number.isNaN(theNumber) && - theNumber.toString(radix).toLocaleLowerCase() === theString.toLocaleLowerCase() + _convertNumberToString(theNumber, radix, theString.length) === theString.toLowerCase() } type Integerable = diff --git a/packages/core/test/integer.test.ts b/packages/core/test/integer.test.ts index f76677b98..c135e20a7 100644 --- a/packages/core/test/integer.test.ts +++ b/packages/core/test/integer.test.ts @@ -309,6 +309,12 @@ describe('Integer', () => { test('int("7891123456789876a") not toThrow invalid character', () => expect(() => int('7891123456789876a')).not.toThrow()) + test.each(malformedNumbers())('int("%s", { strictStringValidation: true }) toThrow invalid character', (theNumberString) => + expect(() => int(theNumberString, { strictStringValidation: true })).toThrow()) + + test.each(wellFormedNumbersAndRadix())('Integer.fromString("%s", %n, { strictStringValidation: true }) not toThrown', (theNumberString, radix) => + expect(() => Integer.fromString(theNumberString, radix, { strictStringValidation: true })).not.toThrow()) + forEachStaticToNumberScenarios(({ input, expectedOutput }) => test(`Integer.toNumber(${mayIntegerToString(input)}) toEqual ${expectedOutput}`, () => expect(Integer.toNumber(input)).toEqual(expectedOutput)) @@ -1109,6 +1115,79 @@ function forEachStaticInSafeRangeScenarios ( ].forEach(func) } +function malformedNumbers (): string[] { + return [ + '7a', + '7891123a', + '78911234a', + '789112345a', + '7891123456a', + '7891123456789876a', + '78911234567898765a', + '789112345678987654a', + '78911234567898765a2', + '7891123456789876a25', + '789112345678987a256', + '78911234567898a2567', + '7891123456789a25678', + '789112345678a256789', + '78911234567a2567898', + '7891123456a25678987', + '789112345a256789876', + '78911234a2567898765', + '7891123a25678987654', + '7891123ab2567898765', + '78911234ab256789876', + '789112345ab25678987', + '7891123456ab2567898', + '78911234567ab256789', + '78911234567abc25678', + '78911234567abcd2567', + '78911234567abcde256', + '78911234567abcdef25', + '78911234567abcdefg2', + '7891123456abcdefgh1', + '789112345abcdefgh12', + '78911234abcdefgh123', + '7891123abcdefgh1234', + '789112abcdefghij123', + '7kkkkabcdefghijklmn', + '7kkkkabcdefg12345mn', + '7kkkkabcdefg123456n', + '7kkkkab22efg123456n', + '7kkkkab22efg12345mn', + '7kkkkab223fg12345mn', + 'kkkkk11223fg12345mn', + 'kkkkk11223fg123456n', + 'kkkkk11223fg1234567', + 'kkkkk11223451234567', + 'kkk111gkk3451234567', + 'kkk111gkkkk51234567', + 'kkk111gkkkkk123kk67', + 'kkkk234', + 'kkkk2345', + 'kkkk23456', + 'kkkk234567', + 'kkkk2345679kk', + 'kkkk2345679kkkkkk', + 'kkk234567', + 'kkk2345679', + 'kk2345679', + 'kkkkkkkkkkkkkkkkkkk' + ] +} + +function wellFormedNumbersAndRadix (): Array<[string, number]> { + return [ + ['01', 2], + ['012', 3], + ['0123', 4], + ['0123456789', 10], + ['0123456789ab', 12], + ['0123456789abCde', 16] + ] +} + interface AssertionPair { input: I expectedOutput: O