alphanum-compare
Advanced tools
Comparing version 0.1.0 to 1.0.0
@@ -1,4 +0,5 @@ | ||
declare function compare(a: string, b: string, opts?: { | ||
declare function compare(a: number | string, b: number | string, opts?: { | ||
insensitive?: boolean; | ||
sign?: boolean; | ||
}): number; | ||
export default compare; |
@@ -16,8 +16,11 @@ "use strict"; | ||
function compare(a, b, opts) { | ||
var _a; | ||
const checkSign = (_a = opts === null || opts === void 0 ? void 0 : opts.sign) !== null && _a !== void 0 ? _a : false; | ||
var _a, _b; | ||
const checkCase = (_a = opts === null || opts === void 0 ? void 0 : opts.insensitive) !== null && _a !== void 0 ? _a : false; | ||
const checkSign = (_b = opts === null || opts === void 0 ? void 0 : opts.sign) !== null && _b !== void 0 ? _b : 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, cb; | ||
@@ -30,4 +33,4 @@ let za, zb; | ||
while (ia < ma && ib < mb) { | ||
ca = a.charCodeAt(ia); | ||
cb = b.charCodeAt(ib); | ||
ca = av.charCodeAt(ia); | ||
cb = bv.charCodeAt(ib); | ||
za = zb = 0; | ||
@@ -39,10 +42,10 @@ na = nb = 0; | ||
ia += 1; | ||
ca = a.charCodeAt(ia); | ||
ca = av.charCodeAt(ia); | ||
} | ||
while (isWhitespace(cb)) { | ||
ib += 1; | ||
cb = b.charCodeAt(ib); | ||
cb = bv.charCodeAt(ib); | ||
} | ||
if (checkSign) { | ||
ta = a.charCodeAt(ia + 1); | ||
ta = av.charCodeAt(ia + 1); | ||
if (isSign(ca) && isDigit(ta)) { | ||
@@ -55,3 +58,3 @@ if (ca === minus) { | ||
} | ||
tb = b.charCodeAt(ib + 1); | ||
tb = bv.charCodeAt(ib + 1); | ||
if (isSign(cb) && isDigit(tb)) { | ||
@@ -80,3 +83,3 @@ if (cb === minus) { | ||
ia += 1; | ||
ca = a.charCodeAt(ia); | ||
ca = av.charCodeAt(ia); | ||
} | ||
@@ -86,3 +89,3 @@ while (cb === zero) { | ||
ib += 1; | ||
cb = b.charCodeAt(ib); | ||
cb = bv.charCodeAt(ib); | ||
} | ||
@@ -111,3 +114,3 @@ while (isDigit(ca) || isDigit(cb)) { | ||
na += 1; | ||
ca = a.charCodeAt(ia); | ||
ca = av.charCodeAt(ia); | ||
} | ||
@@ -117,3 +120,3 @@ if (isDigit(cb)) { | ||
nb += 1; | ||
cb = b.charCodeAt(ib); | ||
cb = bv.charCodeAt(ib); | ||
} | ||
@@ -120,0 +123,0 @@ } |
{ | ||
"name": "alphanum-compare", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Compare alphanumeric strings", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
# alphanum-compare | ||
[![npm version](https://badge.fury.io/js/alphanum-compare.svg)](https://badge.fury.io/js/alphanum-compare) | ||
[![Node.js CI](https://github.com/tsekityam/alphanum-compare/actions/workflows/test.yml/badge.svg)](https://github.com/tsekityam/alphanum-compare/actions/workflows/test.yml) | ||
@@ -19,8 +20,16 @@ [![codecov](https://codecov.io/gh/tsekityam/alphanum-compare/branch/main/graph/badge.svg?token=DHFqZcVnZR)](https://codecov.io/gh/tsekityam/alphanum-compare) | ||
const result = ["item20", "item19", "item1", "item10", "item2"].sort(compareFn); | ||
console.log("5".localeCompare("10")); // 1, NO GOOD | ||
console.log(compareFn("5", "10")); // -1, GOOD | ||
console.log(compareFn("-5", "5")); // 1, NO GOOD | ||
console.log(compareFn("-5", "5", { sign: true })); // -1, GOOD | ||
console.log(["item20", "item19", "item1", "item10", "item2"].sort(compareFn)); | ||
// ['item1', 'item2', 'item10', 'item19', 'item20'] | ||
``` | ||
### `compareFn(a: string, b: string, opts?: { sign?: boolean }): number` | ||
[CodeSandbox](https://codesandbox.io/s/alphanum-compare-demo-bfhln) | ||
### `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. | ||
@@ -30,6 +39,12 @@ | ||
- `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) | ||
[![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) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8815
173
1
49