Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support numeric separators #231

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rotten-falcons-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'natural-orderby': minor
---

feat: support numeric separators
9 changes: 9 additions & 0 deletions src/utils/__tests__/baseCompare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ describe('baseCompare()', () => {
);
});
});
describe('numbers with separators', () => {
const origArray = ['100', '20', '10_000', '5_000', '2_000_000'];
const sortArray = ['20', '100', '5_000', '10_000', '2_000_000'];
it(`${origArray.toString()} should be returned as ${sortArray.toString()}`, () => {
expect(origArray.sort(baseCompare({ order: 'asc' }))).toEqual(
sortArray,
);
});
});
describe('alphanumerics containing very large numbers', () => {
const origArray = [
'MySnmp 1234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567',
Expand Down
5 changes: 5 additions & 0 deletions src/utils/__tests__/parseNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ describe('parseNumber()', () => {
const expected = undefined;
expect(parseNumber(value)).toEqual(expected);
});
it('should return 1000000 for number with numeric separators', () => {
const value = '1_000_000';
const expected = 1000000;
expect(parseNumber(value)).toEqual(expected);
});
});
2 changes: 1 addition & 1 deletion src/utils/parseNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ParsedNumber } from '../types';

export const parseNumber = (value: string): ParsedNumber | void => {
if (value.length !== 0) {
const parsedNumber = Number(value);
const parsedNumber = Number(value.replace(/_/g, ''));
if (!Number.isNaN(parsedNumber)) {
return parsedNumber;
}
Expand Down