UNPKG

1.32 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5"use strict";
6
7/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
8
9// TODO webpack 5 remove string type from a and b
10/**
11 * Compare two locations
12 * @param {string|DependencyLocation} a A location node
13 * @param {string|DependencyLocation} b A location node
14 * @returns {-1|0|1} sorting comparator value
15 */
16module.exports = (a, b) => {
17 if (typeof a === "string") {
18 if (typeof b === "string") {
19 if (a < b) return -1;
20 if (a > b) return 1;
21 return 0;
22 } else if (typeof b === "object") {
23 return 1;
24 } else {
25 return 0;
26 }
27 } else if (typeof a === "object") {
28 if (typeof b === "string") {
29 return -1;
30 } else if (typeof b === "object") {
31 if ("start" in a && "start" in b) {
32 const ap = a.start;
33 const bp = b.start;
34 if (ap.line < bp.line) return -1;
35 if (ap.line > bp.line) return 1;
36 if (ap.column < bp.column) return -1;
37 if (ap.column > bp.column) return 1;
38 }
39 if ("name" in a && "name" in b) {
40 if (a.name < b.name) return -1;
41 if (a.name > b.name) return 1;
42 }
43 if ("index" in a && "index" in b) {
44 if (a.index < b.index) return -1;
45 if (a.index > b.index) return 1;
46 }
47 return 0;
48 } else {
49 return 0;
50 }
51 }
52};