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
4 changes: 3 additions & 1 deletion src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ export enum LuaLibFeature {
WeakMap = "WeakMap",
WeakSet = "WeakSet",
SourceMapTraceBack = "SourceMapTraceBack",
StringConcat = "StringConcat",
StringEndsWith = "StringEndsWith",
StringReplace = "StringReplace",
StringSplit = "StringSplit",
StringConcat = "StringConcat",
StringStartsWith = "StringStartsWith",
Symbol = "Symbol",
SymbolRegistry = "SymbolRegistry",
}
Expand Down
4 changes: 4 additions & 0 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4003,6 +4003,10 @@ export class LuaTransformer {
const firstParamPlusOne = this.expressionPlusOne(params[0]);
return this.createStringCall("byte", node, caller, firstParamPlusOne);
}
case "startsWith":
return this.transformLuaLibFunction(LuaLibFeature.StringStartsWith, node, caller, ...params);
case "endsWith":
return this.transformLuaLibFunction(LuaLibFeature.StringEndsWith, node, caller, ...params);
case "byte":
case "char":
case "dump":
Expand Down
7 changes: 7 additions & 0 deletions src/lualib/StringEndsWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function __TS__StringEndsWith(this: string, searchString: string, endPosition?: number): boolean {
if (endPosition === undefined || endPosition > this.length) {
endPosition = this.length;
}

return string.sub(this, endPosition - searchString.length + 1, endPosition) === searchString;
}
7 changes: 7 additions & 0 deletions src/lualib/StringStartsWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function __TS__StringStartsWith(this: string, searchString: string, position?: number): boolean {
if (position === undefined || position < 0) {
position = 0;
}

return string.sub(this, position + 1, searchString.length + position) === searchString;
}
1 change: 1 addition & 0 deletions src/lualib/declarations/string.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ declare namespace string {
function gsub(source: string, searchValue: string, replaceValue: (...groups: string[]) => string): [string, number];

function gmatch(haystack: string, pattern: string): GMatchResult;
function sub(s: string, i: number, j?: number): string;
}
24 changes: 24 additions & 0 deletions test/unit/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,30 @@ test.each([
expect(result).toBe(inp.charAt(index));
});

test.each<{ inp: string; args: Parameters<string["startsWith"]> }>([
{ inp: "hello test", args: [""] },
{ inp: "hello test", args: ["hello"] },
{ inp: "hello test", args: ["test"] },
{ inp: "hello test", args: ["test", 6] },
])("string.startsWith (%p)", ({ inp, args }) => {
const argsString = args.map(arg => JSON.stringify(arg)).join(", ");
const result = util.transpileAndExecute(`return "${inp}".startsWith(${argsString})`);

expect(result).toBe(inp.startsWith(...args));
});

test.each<{ inp: string; args: Parameters<string["endsWith"]> }>([
{ inp: "hello test", args: [""] },
{ inp: "hello test", args: ["test"] },
{ inp: "hello test", args: ["hello"] },
{ inp: "hello test", args: ["hello", 5] },
])("string.endsWith (%p)", ({ inp, args }) => {
const argsString = args.map(arg => JSON.stringify(arg)).join(", ");
const result = util.transpileAndExecute(`return "${inp}".endsWith(${argsString})`);

expect(result).toBe(inp.endsWith(...args));
});

test.each([
{ input: "abcd", index: 3 },
{ input: "abcde", index: 3 },
Expand Down