diff --git a/__tests__/humanize.spec.js b/__tests__/humanize.spec.js index b49aee9..03d44c4 100644 --- a/__tests__/humanize.spec.js +++ b/__tests__/humanize.spec.js @@ -40,6 +40,12 @@ describe('compactInteger tests', () => { expect(Humanize.compactInteger(100)).toEqual('100'); expect(Humanize.compactInteger(123456789, 1)).toEqual('123.5M'); }); + it('should fail for non-integer', () => { + const nonIntegerInput = '2.3'; // Example non-integer input + expect(() => { + Humanize.compactInteger(nonIntegerInput); + }).toThrowError('Input is not an integer.'); + }); }); diff --git a/src/humanize.js b/src/humanize.js index afe4354..ac763cd 100644 --- a/src/humanize.js +++ b/src/humanize.js @@ -82,6 +82,11 @@ // Converts an integer into its most compact representation compactInteger(input, decimals = 0) { + + if (!Number.isInteger(input)) { + throw new Error('Input is not an integer.'); + } + decimals = Math.max(decimals, 0); const number = parseInt(input, 10); const signString = number < 0 ? '-' : '';