Skip to content

Commit d497824

Browse files
zapp-brannigan-dotaPerryvw
authored andcommitted
Add some string functions (#59)
* Add .idea folder to .gitignore * Add string.toLowerCase(), string.toUpperCase(), string.split() and string.charAt()
1 parent b94b7fb commit d497824

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ coverage/
1010

1111
# OSX
1212
.DS_Store
13-
*.lcov
13+
*.lcov
14+
15+
# IDEA IDEs
16+
.idea/

dist/lualib/typescript.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,36 @@ function TS_indexOf(list, object )
8585
return -1
8686
end
8787

88+
function TS_split(str, separator)
89+
local out = {}
90+
91+
if separator == "" then
92+
string.gsub(str,".", function(c)
93+
table.insert(out, c)
94+
end)
95+
return out
96+
end
97+
98+
if not string.find(str, separator) then
99+
return { str }
100+
end
101+
102+
local fstr = str .. separator
103+
local fpat = "(.-)" .. separator
104+
local last_end = 1
105+
local s, e, cap = string.find(fstr, fpat, 1)
106+
while s do
107+
table.insert(out, cap)
108+
last_end = e+1
109+
s, e, cap = string.find(fstr, fpat, last_end)
110+
end
111+
if last_end <= #str then
112+
cap = string.sub(fstr, last_end)
113+
table.insert(out, cap)
114+
end
115+
return out
116+
end
117+
88118
-- Set data structure implementation
89119
Set = Set or {}
90120
Set.__index = Set

src/Transpiler.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,8 +793,8 @@ export class LuaTranspiler {
793793
let funcHolder = tsEx.findMemberHolder(expType, funcName, this.checker);
794794

795795
// ===== EXPERIMENTAL https://github.com/Perryvw/TypescriptToLua/issues/56
796-
if (ts.isParenthesizedExpression(node.expression.expression)
797-
&& (ts.isAsExpression(node.expression.expression.expression)
796+
if (ts.isParenthesizedExpression(node.expression.expression)
797+
&& (ts.isAsExpression(node.expression.expression.expression)
798798
|| ts.isTypeAssertion(node.expression.expression.expression))
799799
&& ts.isTypeReferenceNode(node.expression.expression.expression.type)) {
800800
const castTypeNode = node.expression.expression.expression.type;
@@ -851,6 +851,14 @@ export class LuaTranspiler {
851851
const arg2 = this.transpileExpression(node.arguments[1]);
852852
return `string.sub(${caller},${arg1}+1,${arg2})`;
853853
}
854+
case "toLowerCase":
855+
return `string.lower(${caller})`;
856+
case "toUpperCase":
857+
return `string.upper(${caller})`;
858+
case "split":
859+
return `TS_split(${caller},${params})`;
860+
case "charAt":
861+
return `string.sub(${caller},${params}+1,${params}+1)`;
854862
default:
855863
throw new TranspileError("Unsupported string function: " + expression.name.escapedText, node);
856864
}

test/integration/lua/string.spec.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export class StringTests {
8484
@TestCase("hello test", "")
8585
@TestCase("hello test", "t")
8686
@TestCase("hello test", "h")
87+
@TestCase("hello test", "invalid")
8788
@Test("string.indexOf")
8889
public indexOf(inp: string, searchValue: string) {
8990
// Transpile
@@ -136,4 +137,78 @@ export class StringTests {
136137
// Assert
137138
Expect(result).toBe(inp.length);
138139
}
140+
141+
@TestCase("hello TEST")
142+
@Test("string.toLowerCase")
143+
public toLowerCase(inp: string) {
144+
// Transpile
145+
let lua = util.transpileString(
146+
`return "${inp}".toLowerCase()`,
147+
util.dummyTypes.String
148+
);
149+
150+
// Execute
151+
let result = util.executeLua(lua);
152+
153+
// Assert
154+
Expect(result).toBe(inp.toLowerCase());
155+
}
156+
157+
@TestCase("hello test")
158+
@Test("string.toUpperCase")
159+
public toUpperCase(inp: string) {
160+
// Transpile
161+
let lua = util.transpileString(
162+
`return "${inp}".toUpperCase()`,
163+
util.dummyTypes.String
164+
);
165+
166+
// Execute
167+
let result = util.executeLua(lua);
168+
169+
// Assert
170+
Expect(result).toBe(inp.toUpperCase());
171+
}
172+
173+
@TestCase("hello test", "")
174+
@TestCase("hello test", " ")
175+
@TestCase("hello test", "h")
176+
@TestCase("hello test", "t")
177+
@TestCase("hello test", "l")
178+
@TestCase("hello test", "invalid")
179+
@TestCase("hello test", "hello test")
180+
@Test("string.split")
181+
public split(inp: string, separator: string) {
182+
// Transpile
183+
let lua = util.transpileString(
184+
`return JSONStringify("${inp}".split("${separator}"))`,
185+
util.dummyTypes.String
186+
);
187+
188+
// Execute
189+
let result = util.executeLua(lua);
190+
191+
// Assert
192+
Expect(result).toBe(JSON.stringify(inp.split(separator)));
193+
}
194+
195+
@TestCase("hello test", 1)
196+
@TestCase("hello test", 2)
197+
@TestCase("hello test", 3)
198+
@TestCase("hello test", 99)
199+
@Test("string.charAt")
200+
public charAt(inp: string, index: number) {
201+
// Transpile
202+
let lua = util.transpileString(
203+
`return "${inp}".charAt(${index})`,
204+
util.dummyTypes.String
205+
);
206+
207+
// Execute
208+
let result = util.executeLua(lua);
209+
210+
// Assert
211+
Expect(result).toBe(inp.charAt(index));
212+
}
213+
139214
}

0 commit comments

Comments
 (0)