Skip to content

Commit c826edd

Browse files
refactor(formatLocation): upgrade to ES6
1 parent 82ddd16 commit c826edd

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

lib/formatLocation.js

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,53 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
module.exports = function formatLocation(loc) {
6-
if(typeof loc === "string")
7-
return loc;
8-
if(typeof loc === "number")
9-
return loc + "";
10-
if(loc && typeof loc === "object") {
11-
if(loc.start && loc.end) {
12-
if(typeof loc.start.line === "number" && typeof loc.end.line === "number" && typeof loc.end.column === "number" && loc.start.line === loc.end.line)
13-
return formatPosition(loc.start) + "-" + loc.end.column;
14-
return formatPosition(loc.start) + "-" + formatPosition(loc.end);
15-
}
16-
if(loc.start)
17-
return formatPosition(loc.start);
18-
return formatPosition(loc);
19-
}
20-
return "";
215

22-
function formatPosition(pos) {
23-
if(typeof pos === "string")
6+
"use strict";
7+
8+
const formatPosition = (pos) => {
9+
if(pos === null)
10+
return "";
11+
const typeOfPos = typeof pos;
12+
switch(typeOfPos) {
13+
case "string":
2414
return pos;
25-
if(typeof pos === "number")
26-
return pos + "";
27-
if(pos && typeof pos === "object") {
15+
case "number":
16+
return `${pos}`;
17+
case "object":
2818
if(typeof pos.line === "number" && typeof pos.column === "number")
29-
return pos.line + ":" + pos.column;
30-
if(typeof pos.line === "number")
31-
return pos.line + ":?";
32-
if(typeof pos.index === "number")
33-
return "+" + pos.index;
34-
}
19+
return `${pos.line}:${pos.column}`;
20+
else if(typeof pos.line === "number")
21+
return `${pos.line}:?`;
22+
else if(typeof pos.index === "number")
23+
return `+${pos.index}`;
24+
else
25+
return "";
26+
default:
27+
return "";
28+
}
29+
};
30+
31+
const formatLocation = (loc) => {
32+
if(loc === null)
3533
return "";
34+
const typeOfLoc = typeof loc;
35+
switch(typeOfLoc) {
36+
case "string":
37+
return loc;
38+
case "number":
39+
return `${loc}`;
40+
case "object":
41+
if(loc.start && loc.end) {
42+
if(typeof loc.start.line === "number" && typeof loc.end.line === "number" && typeof loc.end.column === "number" && loc.start.line === loc.end.line)
43+
return `${formatPosition(loc.start)}-${loc.end.column}`;
44+
return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`;
45+
}
46+
if(loc.start)
47+
return formatPosition(loc.start);
48+
return formatPosition(loc);
49+
default:
50+
return "";
3651
}
3752
};
53+
54+
module.exports = formatLocation;

0 commit comments

Comments
 (0)