Skip to content

Commit

Permalink
Add option for case insensitive comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
tsekityam committed Oct 13, 2021
1 parent 20e6f60 commit 85ab1fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ console.log(["item20", "item19", "item1", "item10", "item2"].sort(compareFn));

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

### `compareFn(a: string, b: string, opts?: { sign?: boolean }): number`
### `compareFn(a: string, b: 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.

#### Options

- `sign?: boolean`: Allows `+` and `-` characters before numbers. _(Default: `false`)_
- `insensitive?: boolean`

Compares items case insensitively. _(Default: `false`)_

- `sign?: boolean`

Allows `+` and `-` characters before numbers. _(Default: `false`)_

## License

[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgit.luolix.top%2Ftsekityam%2Falphanum-compare.svg?type=large)](https://app.fossa.com/projects/git%2Bgit.luolix.top%2Ftsekityam%2Falphanum-compare?ref=badge_large)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgit.luolix.top%2Ftsekityam%2Falphanum-compare.svg?type=large)](https://app.fossa.com/projects/git%2Bgit.luolix.top%2Ftsekityam%2Falphanum-compare?ref=badge_large)
8 changes: 1 addition & 7 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,7 @@ describe("sorting", function () {
it(test.message, () => {
assert.deepEqual(
test.fixture.sort((a, b) =>
test.options?.insensitive ?? false
? compareFn(
a.toString().toLowerCase(),
b.toString().toLowerCase(),
test.options
)
: compareFn(a.toString(), b.toString(), test.options)
compareFn(a.toString(), b.toString(), test.options)
),
test.expected
);
Expand Down
34 changes: 21 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ function isSign(code: number) {
return code === minus || code === plus;
}

function compare(a: string, b: string, opts?: { sign?: boolean }): number {
function compare(
a: string,
b: 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;
let ia = 0;
let ib = 0;
const ma = a.length;
const mb = b.length;
const ma = av.length;
const mb = bv.length;
let ca: number, cb: number; // character code
let za: number, zb: number; // leading zero count
let na: number, nb: number; // number length
Expand All @@ -28,8 +36,8 @@ function compare(a: string, b: string, opts?: { sign?: boolean }): number {
let bias: number;

while (ia < ma && ib < mb) {
ca = a.charCodeAt(ia);
cb = b.charCodeAt(ib);
ca = av.charCodeAt(ia);
cb = bv.charCodeAt(ib);
za = zb = 0;
na = nb = 0;
sa = sb = true;
Expand All @@ -38,24 +46,24 @@ function compare(a: string, b: string, opts?: { sign?: boolean }): number {
// skip over leading spaces
while (isWhitespace(ca)) {
ia += 1;
ca = a.charCodeAt(ia);
ca = av.charCodeAt(ia);
}
while (isWhitespace(cb)) {
ib += 1;
cb = b.charCodeAt(ib);
cb = bv.charCodeAt(ib);
}

// skip and save sign
if (checkSign) {
ta = a.charCodeAt(ia + 1);
ta = av.charCodeAt(ia + 1);
if (isSign(ca) && isDigit(ta)) {
if (ca === minus) {
sa = false;
}
ia += 1;
ca = ta;
}
tb = b.charCodeAt(ib + 1);
tb = bv.charCodeAt(ib + 1);
if (isSign(cb) && isDigit(tb)) {
if (cb === minus) {
sb = false;
Expand Down Expand Up @@ -85,12 +93,12 @@ function compare(a: string, b: string, opts?: { sign?: boolean }): number {
while (ca === zero) {
za += 1;
ia += 1;
ca = a.charCodeAt(ia);
ca = av.charCodeAt(ia);
}
while (cb === zero) {
zb += 1;
ib += 1;
cb = b.charCodeAt(ib);
cb = bv.charCodeAt(ib);
}

// count numbers
Expand All @@ -113,12 +121,12 @@ function compare(a: string, b: string, opts?: { sign?: boolean }): number {
if (isDigit(ca)) {
ia += 1;
na += 1;
ca = a.charCodeAt(ia);
ca = av.charCodeAt(ia);
}
if (isDigit(cb)) {
ib += 1;
nb += 1;
cb = b.charCodeAt(ib);
cb = bv.charCodeAt(ib);
}
}

Expand Down

0 comments on commit 85ab1fa

Please sign in to comment.