Skip to content

Commit a67b61a

Browse files
authored
Added string.includes (#971)
1 parent 39c7420 commit a67b61a

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export enum LuaLibFeature {
6969
StringCharCodeAt = "StringCharCodeAt",
7070
StringConcat = "StringConcat",
7171
StringEndsWith = "StringEndsWith",
72+
StringIncludes = "StringIncludes",
7273
StringPadEnd = "StringPadEnd",
7374
StringPadStart = "StringPadStart",
7475
StringReplace = "StringReplace",

src/lualib/StringIncludes.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function __TS__StringIncludes(this: string, searchString: string, position?: number): boolean {
2+
// http://lua-users.org/wiki/StringLibraryTutorial
3+
if (!position) {
4+
position = 1;
5+
} else {
6+
position += 1;
7+
}
8+
return string.find(this, searchString, position, true) !== undefined;
9+
}

src/lualib/declarations/string.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ declare namespace string {
1414
function sub(s: string, i: number, j?: number): string;
1515
function format(formatstring: string, ...args: any[]): string;
1616
function match(string: string, pattern: string): string;
17+
function find(
18+
string: string,
19+
pattern: string,
20+
start?: number,
21+
plainflag?: boolean
22+
): MultiReturn<[number, number]> | undefined;
1723
}

src/transformation/builtins/string.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ export function transformStringPrototypeCall(
129129
return transformLuaLibFunction(context, LuaLibFeature.StringStartsWith, node, caller, ...params);
130130
case "endsWith":
131131
return transformLuaLibFunction(context, LuaLibFeature.StringEndsWith, node, caller, ...params);
132+
case "includes":
133+
return transformLuaLibFunction(context, LuaLibFeature.StringIncludes, node, caller, ...params);
132134
case "repeat":
133135
const math = lua.createIdentifier("math");
134136
const floor = lua.createStringLiteral("floor");

test/unit/builtins/string.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ test.each([
240240
test.each<{ inp: string; args: Parameters<string["startsWith"]> }>([
241241
{ inp: "hello test", args: [""] },
242242
{ inp: "hello test", args: ["hello"] },
243+
{ inp: "HELLO test", args: ["hello"] },
243244
{ inp: "hello test", args: ["test"] },
244245
{ inp: "hello test", args: ["test", 6] },
245246
])("string.startsWith (%p)", ({ inp, args }) => {
@@ -249,12 +250,25 @@ test.each<{ inp: string; args: Parameters<string["startsWith"]> }>([
249250
test.each<{ inp: string; args: Parameters<string["endsWith"]> }>([
250251
{ inp: "hello test", args: [""] },
251252
{ inp: "hello test", args: ["test"] },
253+
{ inp: "hello TEST", args: ["test"] },
252254
{ inp: "hello test", args: ["hello"] },
253255
{ inp: "hello test", args: ["hello", 5] },
254256
])("string.endsWith (%p)", ({ inp, args }) => {
255257
util.testExpression`"${inp}".endsWith(${util.formatCode(...args)})`.expectToMatchJsResult();
256258
});
257259

260+
test.each<{ inp: string; args: Parameters<string["includes"]> }>([
261+
{ inp: "hello test", args: [""] },
262+
{ inp: "hello test", args: ["test"] },
263+
{ inp: "HELLO TEST", args: ["test"] },
264+
{ inp: "hello test", args: ["hello"] },
265+
{ inp: "HELLO TEST", args: ["hello"] },
266+
{ inp: "hello test", args: ["hello", 5] },
267+
{ inp: "hello test", args: ["test", 6] },
268+
])("string.includes (%p)", ({ inp, args }) => {
269+
util.testExpression`"${inp}".includes(${util.formatCode(...args)})`.expectToMatchJsResult();
270+
});
271+
258272
test.each([
259273
{ inp: "hello test", count: 0 },
260274
{ inp: "hello test", count: 1 },

0 commit comments

Comments
 (0)