Skip to content

Commit 9526729

Browse files
ark120202Perryvw
authored andcommitted
Add string.{starts,ends}With (#551)
1 parent cb2c479 commit 9526729

File tree

6 files changed

+46
-1
lines changed

6 files changed

+46
-1
lines changed

src/LuaLib.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ export enum LuaLibFeature {
3939
WeakMap = "WeakMap",
4040
WeakSet = "WeakSet",
4141
SourceMapTraceBack = "SourceMapTraceBack",
42+
StringConcat = "StringConcat",
43+
StringEndsWith = "StringEndsWith",
4244
StringReplace = "StringReplace",
4345
StringSplit = "StringSplit",
44-
StringConcat = "StringConcat",
46+
StringStartsWith = "StringStartsWith",
4547
Symbol = "Symbol",
4648
SymbolRegistry = "SymbolRegistry",
4749
}

src/LuaTransformer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4001,6 +4001,10 @@ export class LuaTransformer {
40014001
const firstParamPlusOne = this.expressionPlusOne(params[0]);
40024002
return this.createStringCall("byte", node, caller, firstParamPlusOne);
40034003
}
4004+
case "startsWith":
4005+
return this.transformLuaLibFunction(LuaLibFeature.StringStartsWith, node, caller, ...params);
4006+
case "endsWith":
4007+
return this.transformLuaLibFunction(LuaLibFeature.StringEndsWith, node, caller, ...params);
40044008
case "byte":
40054009
case "char":
40064010
case "dump":

src/lualib/StringEndsWith.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function __TS__StringEndsWith(this: string, searchString: string, endPosition?: number): boolean {
2+
if (endPosition === undefined || endPosition > this.length) {
3+
endPosition = this.length;
4+
}
5+
6+
return string.sub(this, endPosition - searchString.length + 1, endPosition) === searchString;
7+
}

src/lualib/StringStartsWith.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function __TS__StringStartsWith(this: string, searchString: string, position?: number): boolean {
2+
if (position === undefined || position < 0) {
3+
position = 0;
4+
}
5+
6+
return string.sub(this, position + 1, searchString.length + position) === searchString;
7+
}

src/lualib/declarations/string.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ declare namespace string {
1212
): [string, number];
1313

1414
function gmatch(haystack: string, pattern: string): GMatchResult;
15+
function sub(s: string, i: number, j?: number): string;
1516
}

test/unit/string.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,30 @@ test.each([
284284
expect(result).toBe(inp.charAt(index));
285285
});
286286

287+
test.each<{ inp: string; args: Parameters<string["startsWith"]> }>([
288+
{ inp: "hello test", args: [""] },
289+
{ inp: "hello test", args: ["hello"] },
290+
{ inp: "hello test", args: ["test"] },
291+
{ inp: "hello test", args: ["test", 6] },
292+
])("string.startsWith (%p)", ({ inp, args }) => {
293+
const argsString = args.map(arg => JSON.stringify(arg)).join(", ");
294+
const result = util.transpileAndExecute(`return "${inp}".startsWith(${argsString})`);
295+
296+
expect(result).toBe(inp.startsWith(...args));
297+
});
298+
299+
test.each<{ inp: string; args: Parameters<string["endsWith"]> }>([
300+
{ inp: "hello test", args: [""] },
301+
{ inp: "hello test", args: ["test"] },
302+
{ inp: "hello test", args: ["hello"] },
303+
{ inp: "hello test", args: ["hello", 5] },
304+
])("string.endsWith (%p)", ({ inp, args }) => {
305+
const argsString = args.map(arg => JSON.stringify(arg)).join(", ");
306+
const result = util.transpileAndExecute(`return "${inp}".endsWith(${argsString})`);
307+
308+
expect(result).toBe(inp.endsWith(...args));
309+
});
310+
287311
test.each([
288312
{ input: "abcd", index: 3 },
289313
{ input: "abcde", index: 3 },

0 commit comments

Comments
 (0)