-
Notifications
You must be signed in to change notification settings - Fork 0
/
swiftToJava.js
84 lines (72 loc) · 2.05 KB
/
swiftToJava.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function convertSwiftToJava(swiftCode) {
var swiftLines = swiftCode.split("\n");
var javaLines = [];
for (i = 0; i < swiftLines.length; i++) {
var swiftLine = swiftLines[i].trim();
var javaLine;
if (swiftLine.startsWith("static let ")) {
javaLine = convertSwiftConstantDeclarationToJava(swiftLine);
}
else if (swiftLine.startsWith("//")) {
javaLine = swiftLine;
}
else if (swiftLine.length == 0) {
javaLine = swiftLine;
}
else {
javaLine = "//TODO: ".concat(swiftLine);
}
javaLines.push(javaLine);
}
return javaCode = javaLines.join("\n");
}
function convertSwiftConstantDeclarationToJava(swiftStatement) {
var success = true;
var javaStatement;
if (swiftStatement.startsWith("static let ")) {
var remainingSwiftStatement = swiftStatement;
var javaDeclarationStart;
javaDeclarationStart = "private static final";
remainingSwiftStatement = remainingSwiftStatement.substring("static let ".length);
var variableNameAndValue = remainingSwiftStatement.split("=");
var variableNameAndType = variableNameAndValue[0].split(":");
var variableName = variableNameAndType[0].trim();
var variableValue = variableNameAndValue[1].trim();
var variableSwiftType = "";
if (variableNameAndType.length > 1) {
variableSwiftType = variableNameAndType[1].trim();
}
else {
if (variableValue.startsWith("\"")) {
variableSwiftType = "String";
}
else {
success = false;
}
}
javaStatement = javaDeclarationStart
.concat(" ")
.concat(variableSwiftType)
.concat(" ")
.concat(variableName)
.concat(" = ")
.concat(variableValue)
.concat(";");
}
else {
success = false;
}
if (success) {
return javaStatement;
}
else {
return "//TODO: ".concat(swiftStatement);
}
}
function addSemicollonIfMissing(text) {
var updatedStatement = text;
if (updatedStatement.slice(-1) != ';') {
updatedStatement = updatedStatement.concat(";");
}
return updatedStatement;
}