Skip to content

Commit

Permalink
Allow comparing between number and string
Browse files Browse the repository at this point in the history
  • Loading branch information
tsekityam committed Oct 13, 2021
1 parent 7df0191 commit 55f2891
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ console.log(["item20", "item19", "item1", "item10", "item2"].sort(compareFn));

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

### `compareFn(a: string, b: string, opts?: Options): number`
### `compareFn(a: number | string, b: number | string, opts?: Options): number`

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

Expand Down
4 changes: 1 addition & 3 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,7 @@ describe("sorting", function () {
tests.forEach((test) => {
it(test.message, () => {
assert.deepEqual(
test.fixture.sort((a, b) =>
compareFn(a.toString(), b.toString(), test.options)
),
test.fixture.sort((a, b) => compareFn(a, b, test.options)),
test.expected
);
});
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ function isSign(code: number) {
}

function compare(
a: string,
b: string,
a: number | string,
b: number | string,
opts?: { insensitive?: boolean; sign?: boolean }
): number {
const checkCase = opts?.insensitive ?? false;
const checkSign = opts?.sign ?? false;

const av = checkCase ? a.toLowerCase() : a;
const bv = checkCase ? b.toLowerCase() : b;
const av = checkCase ? `${a}`.toLowerCase() : `${a}`;
const bv = checkCase ? `${b}`.toLowerCase() : `${b}`;
let ia = 0;
let ib = 0;
const ma = av.length;
Expand Down

0 comments on commit 55f2891

Please sign in to comment.