From f4a2bdfb50b402d282c99880314a46f6f6abad5d Mon Sep 17 00:00:00 2001 From: Tse Kit Yam Date: Wed, 13 Oct 2021 09:31:49 +0800 Subject: [PATCH] Add option for case insensitive comparison --- README.md | 12 +++++++++--- src/index.spec.ts | 8 +------- src/index.ts | 34 +++++++++++++++++++++------------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 52ca885..28d841d 100644 --- a/README.md +++ b/README.md @@ -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%2Bgithub.com%2Ftsekityam%2Falphanum-compare.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Ftsekityam%2Falphanum-compare?ref=badge_large) \ No newline at end of file +[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Ftsekityam%2Falphanum-compare.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Ftsekityam%2Falphanum-compare?ref=badge_large) diff --git a/src/index.spec.ts b/src/index.spec.ts index eebc1f4..9b56b2d 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -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 ); diff --git a/src/index.ts b/src/index.ts index d517933..002b3cc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -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; @@ -38,16 +46,16 @@ 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; @@ -55,7 +63,7 @@ function compare(a: string, b: string, opts?: { sign?: boolean }): number { ia += 1; ca = ta; } - tb = b.charCodeAt(ib + 1); + tb = bv.charCodeAt(ib + 1); if (isSign(cb) && isDigit(tb)) { if (cb === minus) { sb = false; @@ -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 @@ -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); } }