Skip to content

Commit 0ce71d7

Browse files
committed
[FIX] alphanumeric compare
1 parent 85051a4 commit 0ce71d7

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
/**
22
* **`alphanumericCompare`**: Function which, given two strings, the type of comparison to be verified, and optional options, performs the comparison between the two strings and returns a boolean indicating whether the indicated comparison is respected or not. [See demo](https://react-tools.ndria.dev/#/utils/alphanumericCompare)
3-
* @param {string} string1 - first string to compare.
4-
* @param {string} string2 - second string to compare.
5-
* @param {"<" | ">" | "=" | ">=" | "<="} compareType - type of compare to verify.
6-
* @param {Intl.CollatorOptions} opts - options object to execute compare.
7-
* @returns {boolean} result - boolean that indicates whether the indicated comparison is respected or not.
3+
* @param {Object} param - object
4+
* @param {string} param.string1 - first string to compare.
5+
* @param {string} param.string2 - second string to compare.
6+
* @param {"<" | ">" | "=" | ">=" | "<="} [param.compareType] - type of compare to verify.
7+
* @param {Intl.LocalesArgument} [param.locales] - A string with a BCP 47 language tag or an Intl.Locale instance, or an array of such locale identifiers. The runtime's default locale is used when undefined is passed or when none of the specified locale identifiers is supported.
8+
* @param {Intl.CollatorOptions} [param.opts] - An object adjusting the output format. Corresponds to the options parameter of the Intl.Collator() constructor.
9+
* @returns {boolean|number} result - boolean or number that indicates whether the indicated comparison is respected or not.
810
*/
9-
export function alphanumericCompare(string1: string, string2: string, compareType?: "<" | ">" | "=" | ">=" | "<=", opts?: Intl.CollatorOptions) {
10-
const result = string1.localeCompare(string2, "it", opts);
11+
function alphanumericCompare({ string1, string2, compareType, locales, opts }: { string1: string, string2: string, compareType?: undefined, locales?: Intl.LocalesArgument, opts?: Intl.CollatorOptions }): number;
12+
function alphanumericCompare({ string1, string2, compareType, locales, opts }: { string1: string, string2: string, compareType?: "<" | ">" | "=" | ">=" | "<=", locales?: Intl.LocalesArgument, opts?: Intl.CollatorOptions }): boolean;
13+
function alphanumericCompare({ string1, string2, compareType, locales, opts }: {string1: string, string2: string, compareType?: "<" | ">" | "=" | ">=" | "<=", locales?: Intl.LocalesArgument, opts?: Intl.CollatorOptions}): boolean|number {
14+
const result = string1.localeCompare(string2, locales, opts);
1115
switch (compareType) {
1216
case "<":
1317
return result < 0;
@@ -22,4 +26,6 @@ export function alphanumericCompare(string1: string, string2: string, compareTyp
2226
default:
2327
return result;
2428
}
25-
}
29+
}
30+
31+
export { alphanumericCompare };

0 commit comments

Comments
 (0)