Skip to content

Commit 58d69cd

Browse files
author
Andy Hanson
committed
Fix localeCompare differences between node versions
node 6.2.0: "a".localeCompare("A") is -1. node 0.10.45: "a".localeCompare("A") is 32.
1 parent 12914ea commit 58d69cd

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/services/navigationBar.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ namespace ts.NavigationBar {
134134
function sortNodes(nodes: Node[]): Node[] {
135135
return nodes.slice(0).sort((n1: Declaration, n2: Declaration) => {
136136
if (n1.name && n2.name) {
137-
return getPropertyNameForPropertyNameNode(n1.name).localeCompare(getPropertyNameForPropertyNameNode(n2.name));
137+
return localeCompareFix(getPropertyNameForPropertyNameNode(n1.name), getPropertyNameForPropertyNameNode(n2.name));
138138
}
139139
else if (n1.name) {
140140
return 1;
@@ -146,6 +146,16 @@ namespace ts.NavigationBar {
146146
return n1.kind - n2.kind;
147147
}
148148
});
149+
150+
// node 0.10 treats "a" as greater than "B".
151+
// For consistency, sort alphabetically, falling back to which is lower-case.
152+
function localeCompareFix(a: string, b: string) {
153+
const cmp = a.toLowerCase().localeCompare(b.toLowerCase());
154+
if (cmp !== 0)
155+
return cmp;
156+
// Return the *opposite* of the `<` operator, which works the same in node 0.10 and 6.0.
157+
return a < b ? 1 : a > b ? -1 : 0;
158+
}
149159
}
150160

151161
function addTopLevelNodes(nodes: Node[], topLevelNodes: Node[]): void {

0 commit comments

Comments
 (0)