Skip to content

Commit cc5b103

Browse files
author
Andy Hanson
committed
Fix localeCompare differences in node versions
1 parent 27a1e91 commit cc5b103

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
@@ -140,7 +140,7 @@ namespace ts.NavigationBar {
140140
function sortNodes(nodes: Node[]): Node[] {
141141
return nodes.slice(0).sort((n1: Declaration, n2: Declaration) => {
142142
if (n1.name && n2.name) {
143-
return getPropertyNameForPropertyNameNode(n1.name).localeCompare(getPropertyNameForPropertyNameNode(n2.name));
143+
return localeCompareFix(getPropertyNameForPropertyNameNode(n1.name), getPropertyNameForPropertyNameNode(n2.name));
144144
}
145145
else if (n1.name) {
146146
return 1;
@@ -152,6 +152,16 @@ namespace ts.NavigationBar {
152152
return n1.kind - n2.kind;
153153
}
154154
});
155+
156+
// node 0.10 treats "a" as greater than "B".
157+
// For consistency, sort alphabetically, falling back to which is lower-case.
158+
function localeCompareFix(a: string, b: string) {
159+
const cmp = a.toLowerCase().localeCompare(b.toLowerCase());
160+
if (cmp !== 0)
161+
return cmp;
162+
// Return the *opposite* of the `<` operator, which works the same in node 0.10 and 6.0.
163+
return a < b ? 1 : a > b ? -1 : 0;
164+
}
155165
}
156166

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

0 commit comments

Comments
 (0)