|
| 1 | +/// <reference path="types.ts" /> |
| 2 | + |
| 3 | +/* @internal */ |
| 4 | +namespace ts { |
| 5 | + export function equateValues<T>(a: T, b: T) { |
| 6 | + return a === b; |
| 7 | + } |
| 8 | + |
| 9 | + /** |
| 10 | + * Compare the equality of two strings using a case-sensitive ordinal comparison. |
| 11 | + * |
| 12 | + * Case-sensitive comparisons compare both strings one code-point at a time using the integer |
| 13 | + * value of each code-point after applying `toUpperCase` to each string. We always map both |
| 14 | + * strings to their upper-case form as some unicode characters do not properly round-trip to |
| 15 | + * lowercase (such as `ẞ` (German sharp capital s)). |
| 16 | + */ |
| 17 | + export function equateStringsCaseInsensitive(a: string, b: string) { |
| 18 | + return a === b |
| 19 | + || a !== undefined |
| 20 | + && b !== undefined |
| 21 | + && a.toUpperCase() === b.toUpperCase(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Compare the equality of two strings using a case-sensitive ordinal comparison. |
| 26 | + * |
| 27 | + * Case-sensitive comparisons compare both strings one code-point at a time using the |
| 28 | + * integer value of each code-point. |
| 29 | + */ |
| 30 | + export function equateStringsCaseSensitive(a: string, b: string) { |
| 31 | + return equateValues(a, b); |
| 32 | + } |
| 33 | + |
| 34 | + function compareComparableValues(a: string, b: string): Comparison; |
| 35 | + function compareComparableValues(a: number, b: number): Comparison; |
| 36 | + function compareComparableValues(a: string | number, b: string | number) { |
| 37 | + return a === b ? Comparison.EqualTo : |
| 38 | + a === undefined ? Comparison.LessThan : |
| 39 | + b === undefined ? Comparison.GreaterThan : |
| 40 | + a < b ? Comparison.LessThan : |
| 41 | + Comparison.GreaterThan; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Compare two numeric values for their order relative to each other. |
| 46 | + * To compare strings, use any of the `compareStrings` functions. |
| 47 | + */ |
| 48 | + export function compareValues(a: number, b: number) { |
| 49 | + return compareComparableValues(a, b); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Compare two strings using a case-insensitive ordinal comparison. |
| 54 | + * |
| 55 | + * Ordinal comparisons are based on the difference between the unicode code points of both |
| 56 | + * strings. Characters with multiple unicode representations are considered unequal. Ordinal |
| 57 | + * comparisons provide predictable ordering, but place "a" after "B". |
| 58 | + * |
| 59 | + * Case-insensitive comparisons compare both strings one code-point at a time using the integer |
| 60 | + * value of each code-point after applying `toUpperCase` to each string. We always map both |
| 61 | + * strings to their upper-case form as some unicode characters do not properly round-trip to |
| 62 | + * lowercase (such as `ẞ` (German sharp capital s)). |
| 63 | + */ |
| 64 | + export function compareStringsCaseInsensitive(a: string, b: string) { |
| 65 | + if (a === b) return Comparison.EqualTo; |
| 66 | + if (a === undefined) return Comparison.LessThan; |
| 67 | + if (b === undefined) return Comparison.GreaterThan; |
| 68 | + a = a.toUpperCase(); |
| 69 | + b = b.toUpperCase(); |
| 70 | + return a < b ? Comparison.LessThan : a > b ? Comparison.GreaterThan : Comparison.EqualTo; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Compare two strings using a case-sensitive ordinal comparison. |
| 75 | + * |
| 76 | + * Ordinal comparisons are based on the difference between the unicode code points of both |
| 77 | + * strings. Characters with multiple unicode representations are considered unequal. Ordinal |
| 78 | + * comparisons provide predictable ordering, but place "a" after "B". |
| 79 | + * |
| 80 | + * Case-sensitive comparisons compare both strings one code-point at a time using the integer |
| 81 | + * value of each code-point. |
| 82 | + */ |
| 83 | + export function compareStringsCaseSensitive(a: string, b: string) { |
| 84 | + return compareComparableValues(a, b); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Creates a string comparer for use with string collation in the UI. |
| 89 | + */ |
| 90 | + const createUIStringComparer = (() => { |
| 91 | + let defaultComparer: Comparer<string> | undefined; |
| 92 | + let enUSComparer: Comparer<string> | undefined; |
| 93 | + |
| 94 | + const stringComparerFactory = getStringComparerFactory(); |
| 95 | + return createStringComparer; |
| 96 | + |
| 97 | + function compareWithCallback(a: string | undefined, b: string | undefined, comparer: (a: string, b: string) => number) { |
| 98 | + if (a === b) return Comparison.EqualTo; |
| 99 | + if (a === undefined) return Comparison.LessThan; |
| 100 | + if (b === undefined) return Comparison.GreaterThan; |
| 101 | + const value = comparer(a, b); |
| 102 | + return value < 0 ? Comparison.LessThan : value > 0 ? Comparison.GreaterThan : Comparison.EqualTo; |
| 103 | + } |
| 104 | + |
| 105 | + function createIntlCollatorStringComparer(locale: string | undefined): Comparer<string> { |
| 106 | + // Intl.Collator.prototype.compare is bound to the collator. See NOTE in |
| 107 | + // http://www.ecma-international.org/ecma-402/2.0/#sec-Intl.Collator.prototype.compare |
| 108 | + const comparer = new Intl.Collator(locale, { usage: "sort", sensitivity: "variant" }).compare; |
| 109 | + return (a, b) => compareWithCallback(a, b, comparer); |
| 110 | + } |
| 111 | + |
| 112 | + function createLocaleCompareStringComparer(locale: string | undefined): Comparer<string> { |
| 113 | + // if the locale is not the default locale (`undefined`), use the fallback comparer. |
| 114 | + if (locale !== undefined) return createFallbackStringComparer(); |
| 115 | + |
| 116 | + return (a, b) => compareWithCallback(a, b, compareStrings); |
| 117 | + |
| 118 | + function compareStrings(a: string, b: string) { |
| 119 | + return a.localeCompare(b); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + function createFallbackStringComparer(): Comparer<string> { |
| 124 | + // An ordinal comparison puts "A" after "b", but for the UI we want "A" before "b". |
| 125 | + // We first sort case insensitively. So "Aaa" will come before "baa". |
| 126 | + // Then we sort case sensitively, so "aaa" will come before "Aaa". |
| 127 | + // |
| 128 | + // For case insensitive comparisons we always map both strings to their |
| 129 | + // upper-case form as some unicode characters do not properly round-trip to |
| 130 | + // lowercase (such as `ẞ` (German sharp capital s)). |
| 131 | + return (a, b) => compareWithCallback(a, b, compareDictionaryOrder); |
| 132 | + |
| 133 | + function compareDictionaryOrder(a: string, b: string) { |
| 134 | + return compareStrings(a.toUpperCase(), b.toUpperCase()) || compareStrings(a, b); |
| 135 | + } |
| 136 | + |
| 137 | + function compareStrings(a: string, b: string) { |
| 138 | + return a < b ? Comparison.LessThan : a > b ? Comparison.GreaterThan : Comparison.EqualTo; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + function getStringComparerFactory() { |
| 143 | + // If the host supports Intl, we use it for comparisons using the default locale. |
| 144 | + if (typeof Intl === "object" && typeof Intl.Collator === "function") { |
| 145 | + return createIntlCollatorStringComparer; |
| 146 | + } |
| 147 | + |
| 148 | + // If the host does not support Intl, we fall back to localeCompare. |
| 149 | + // localeCompare in Node v0.10 is just an ordinal comparison, so don't use it. |
| 150 | + if (typeof String.prototype.localeCompare === "function" && |
| 151 | + typeof String.prototype.toLocaleUpperCase === "function" && |
| 152 | + "a".localeCompare("B") < 0) { |
| 153 | + return createLocaleCompareStringComparer; |
| 154 | + } |
| 155 | + |
| 156 | + // Otherwise, fall back to ordinal comparison: |
| 157 | + return createFallbackStringComparer; |
| 158 | + } |
| 159 | + |
| 160 | + function createStringComparer(locale: string | undefined) { |
| 161 | + // Hold onto common string comparers. This avoids constantly reallocating comparers during |
| 162 | + // tests. |
| 163 | + if (locale === undefined) { |
| 164 | + return defaultComparer || (defaultComparer = stringComparerFactory(locale)); |
| 165 | + } |
| 166 | + else if (locale === "en-US") { |
| 167 | + return enUSComparer || (enUSComparer = stringComparerFactory(locale)); |
| 168 | + } |
| 169 | + else { |
| 170 | + return stringComparerFactory(locale); |
| 171 | + } |
| 172 | + } |
| 173 | + })(); |
| 174 | + |
| 175 | + let uiComparerCaseSensitive: Comparer<string> | undefined; |
| 176 | + let uiLocale: string | undefined; |
| 177 | + |
| 178 | + export function getUILocale() { |
| 179 | + return uiLocale; |
| 180 | + } |
| 181 | + |
| 182 | + export function setUILocale(value: string) { |
| 183 | + if (uiLocale !== value) { |
| 184 | + uiLocale = value; |
| 185 | + uiComparerCaseSensitive = undefined; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Compare two strings in a using the case-sensitive sort behavior of the UI locale. |
| 191 | + * |
| 192 | + * Ordering is not predictable between different host locales, but is best for displaying |
| 193 | + * ordered data for UI presentation. Characters with multiple unicode representations may |
| 194 | + * be considered equal. |
| 195 | + * |
| 196 | + * Case-sensitive comparisons compare strings that differ in base characters, or |
| 197 | + * accents/diacritic marks, or case as unequal. |
| 198 | + */ |
| 199 | + export function compareStringsCaseSensitiveUI(a: string, b: string) { |
| 200 | + const comparer = uiComparerCaseSensitive || (uiComparerCaseSensitive = createUIStringComparer(uiLocale)); |
| 201 | + return comparer(a, b); |
| 202 | + } |
| 203 | + |
| 204 | + export function compareProperties<T, K extends keyof T>(a: T, b: T, key: K, comparer: Comparer<T[K]>) { |
| 205 | + return a === b ? Comparison.EqualTo : |
| 206 | + a === undefined ? Comparison.LessThan : |
| 207 | + b === undefined ? Comparison.GreaterThan : |
| 208 | + comparer(a[key], b[key]); |
| 209 | + } |
| 210 | +} |
0 commit comments