Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ coverage/

# OSX
.DS_Store
*.lcov
*.lcov

# IDEA IDEs
.idea/
30 changes: 30 additions & 0 deletions dist/lualib/typescript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ function TS_indexOf(list, object )
return -1
end

function TS_split(str, separator)
local out = {}

if separator == "" then
string.gsub(str,".", function(c)
table.insert(out, c)
end)
return out
end

if not string.find(str, separator) then
return { str }
end

local fstr = str .. separator
local fpat = "(.-)" .. separator
local last_end = 1
local s, e, cap = string.find(fstr, fpat, 1)
while s do
table.insert(out, cap)
last_end = e+1
s, e, cap = string.find(fstr, fpat, last_end)
end
if last_end <= #str then
cap = string.sub(fstr, last_end)
table.insert(out, cap)
end
return out
end

-- Set data structure implementation
Set = Set or {}
Set.__index = Set
Expand Down
12 changes: 10 additions & 2 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,8 +793,8 @@ export class LuaTranspiler {
let funcHolder = tsEx.findMemberHolder(expType, funcName, this.checker);

// ===== EXPERIMENTAL https://github.com/Perryvw/TypescriptToLua/issues/56
if (ts.isParenthesizedExpression(node.expression.expression)
&& (ts.isAsExpression(node.expression.expression.expression)
if (ts.isParenthesizedExpression(node.expression.expression)
&& (ts.isAsExpression(node.expression.expression.expression)
|| ts.isTypeAssertion(node.expression.expression.expression))
&& ts.isTypeReferenceNode(node.expression.expression.expression.type)) {
const castTypeNode = node.expression.expression.expression.type;
Expand Down Expand Up @@ -851,6 +851,14 @@ export class LuaTranspiler {
const arg2 = this.transpileExpression(node.arguments[1]);
return `string.sub(${caller},${arg1}+1,${arg2})`;
}
case "toLowerCase":
return `string.lower(${caller})`;
case "toUpperCase":
return `string.upper(${caller})`;
case "split":
return `TS_split(${caller},${params})`;
case "charAt":
return `string.sub(${caller},${params}+1,${params}+1)`;
default:
throw new TranspileError("Unsupported string function: " + expression.name.escapedText, node);
}
Expand Down
75 changes: 75 additions & 0 deletions test/integration/lua/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class StringTests {
@TestCase("hello test", "")
@TestCase("hello test", "t")
@TestCase("hello test", "h")
@TestCase("hello test", "invalid")
@Test("string.indexOf")
public indexOf(inp: string, searchValue: string) {
// Transpile
Expand Down Expand Up @@ -136,4 +137,78 @@ export class StringTests {
// Assert
Expect(result).toBe(inp.length);
}

@TestCase("hello TEST")
@Test("string.toLowerCase")
public toLowerCase(inp: string) {
// Transpile
let lua = util.transpileString(
`return "${inp}".toLowerCase()`,
util.dummyTypes.String
);

// Execute
let result = util.executeLua(lua);

// Assert
Expect(result).toBe(inp.toLowerCase());
}

@TestCase("hello test")
@Test("string.toUpperCase")
public toUpperCase(inp: string) {
// Transpile
let lua = util.transpileString(
`return "${inp}".toUpperCase()`,
util.dummyTypes.String
);

// Execute
let result = util.executeLua(lua);

// Assert
Expect(result).toBe(inp.toUpperCase());
}

@TestCase("hello test", "")
@TestCase("hello test", " ")
@TestCase("hello test", "h")
@TestCase("hello test", "t")
@TestCase("hello test", "l")
@TestCase("hello test", "invalid")
@TestCase("hello test", "hello test")
@Test("string.split")
public split(inp: string, separator: string) {
// Transpile
let lua = util.transpileString(
`return JSONStringify("${inp}".split("${separator}"))`,
util.dummyTypes.String
);

// Execute
let result = util.executeLua(lua);

// Assert
Expect(result).toBe(JSON.stringify(inp.split(separator)));
}

@TestCase("hello test", 1)
@TestCase("hello test", 2)
@TestCase("hello test", 3)
@TestCase("hello test", 99)
@Test("string.charAt")
public charAt(inp: string, index: number) {
// Transpile
let lua = util.transpileString(
`return "${inp}".charAt(${index})`,
util.dummyTypes.String
);

// Execute
let result = util.executeLua(lua);

// Assert
Expect(result).toBe(inp.charAt(index));
}

}