Skip to content

Commit 55f2891

Browse files
committed
Allow comparing between number and string
1 parent 7df0191 commit 55f2891

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ console.log(["item20", "item19", "item1", "item10", "item2"].sort(compareFn));
2929

3030
[CodeSandbox](https://codesandbox.io/s/alphanum-compare-demo-bfhln)
3131

32-
### `compareFn(a: string, b: string, opts?: Options): number`
32+
### `compareFn(a: number | string, b: number | string, opts?: Options): number`
3333

3434
It returns a negative value if first argument is less than second argument, zero if they're equal and a positive value otherwise.
3535

src/index.spec.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,7 @@ describe("sorting", function () {
363363
tests.forEach((test) => {
364364
it(test.message, () => {
365365
assert.deepEqual(
366-
test.fixture.sort((a, b) =>
367-
compareFn(a.toString(), b.toString(), test.options)
368-
),
366+
test.fixture.sort((a, b) => compareFn(a, b, test.options)),
369367
test.expected
370368
);
371369
});

src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ function isSign(code: number) {
1515
}
1616

1717
function compare(
18-
a: string,
19-
b: string,
18+
a: number | string,
19+
b: number | string,
2020
opts?: { insensitive?: boolean; sign?: boolean }
2121
): number {
2222
const checkCase = opts?.insensitive ?? false;
2323
const checkSign = opts?.sign ?? false;
2424

25-
const av = checkCase ? a.toLowerCase() : a;
26-
const bv = checkCase ? b.toLowerCase() : b;
25+
const av = checkCase ? `${a}`.toLowerCase() : `${a}`;
26+
const bv = checkCase ? `${b}`.toLowerCase() : `${b}`;
2727
let ia = 0;
2828
let ib = 0;
2929
const ma = av.length;

0 commit comments

Comments
 (0)