Skip to content

Commit be667c4

Browse files
authored
Merge pull request #41 from Perryvw/fix-table-concat
Fixed concat if seperator is omitted
2 parents da29936 + 880ce9d commit be667c4

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Transpiler.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,12 @@ export class LuaTranspiler {
757757
case "splice":
758758
return `TS_splice(${caller}, ${params})`;
759759
case "join":
760-
return `table.concat(${caller}, ${params})`;
760+
if (node.arguments.length === 0) {
761+
// if seperator is omitted default seperator is ","
762+
return `table.concat(${caller}, ",")`;
763+
} else {
764+
return `table.concat(${caller}, ${params})`;
765+
}
761766
default:
762767
throw new TranspileError("Unsupported array function: " + expression.name.escapedText, node);
763768
}

test/integration/lua/lualib.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,35 @@ export class LuaLibArrayTests {
138138
Expect(result).toBe(JSON.stringify(inp.splice(start)));
139139
}
140140
}
141+
142+
@TestCase([], "")
143+
@TestCase(["test1"], "test1")
144+
@TestCase(["test1", "test2"], "test1,test2")
145+
@TestCase(["test1", "test2"], "test1;test2", ";")
146+
@TestCase(["test1", "test2"], "test1test2", "")
147+
@Test("array.join")
148+
public join<T>(inp: T[], expected: string, seperator?: string) {
149+
let seperatorLua;
150+
if (seperator === "") {
151+
seperatorLua = "\"\"";
152+
} else if (seperator) {
153+
seperatorLua = "\"" + seperator + "\"";
154+
} else {
155+
seperatorLua = "";
156+
}
157+
// Transpile
158+
let lua = util.transpileString(
159+
`let joinTestTable = ${JSON.stringify(inp)};
160+
return joinTestTable.join(${seperatorLua});`,
161+
util.dummyTypes.Array
162+
);
163+
164+
// Execute
165+
let result = util.executeLua(lua);
166+
167+
// Assert
168+
let joinedInp = inp.join(seperator);
169+
Expect(result).toBe(joinedInp);
170+
}
171+
141172
}

0 commit comments

Comments
 (0)