Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit de818bc

Browse filesBrowse files
authored
Added support for array.find (#730)
* Added support for array.find * Changed lualib function signature to match TS lib
1 parent b26ec48 commit de818bc
Copy full SHA for de818bc

4 files changed

+37Lines changed: 37 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/LuaLib.ts‎

Copy file name to clipboardExpand all lines: src/LuaLib.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export enum LuaLibFeature {
66
ArrayEvery = "ArrayEvery",
77
ArrayFilter = "ArrayFilter",
88
ArrayForEach = "ArrayForEach",
9+
ArrayFind = "ArrayFind",
910
ArrayFindIndex = "ArrayFindIndex",
1011
ArrayIndexOf = "ArrayIndexOf",
1112
ArrayMap = "ArrayMap",
Collapse file

‎src/LuaTransformer.ts‎

Copy file name to clipboardExpand all lines: src/LuaTransformer.ts
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5113,6 +5113,8 @@ export class LuaTransformer {
51135113
);
51145114
case "forEach":
51155115
return this.transformLuaLibFunction(LuaLibFeature.ArrayForEach, node, caller, ...params);
5116+
case "find":
5117+
return this.transformLuaLibFunction(LuaLibFeature.ArrayFind, node, caller, ...params);
51165118
case "findIndex":
51175119
return this.transformLuaLibFunction(LuaLibFeature.ArrayFindIndex, node, caller, ...params);
51185120
case "indexOf":
Collapse file

‎src/lualib/ArrayFind.ts‎

Copy file name to clipboard
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://www.ecma-international.org/ecma-262/10.0/index.html#sec-array.prototype.find
2+
function __TS__ArrayFind<T>(
3+
this: void,
4+
arr: T[],
5+
predicate: (value: T, index: number, obj: T[]) => unknown
6+
): T | undefined {
7+
const len = arr.length;
8+
let k = 0;
9+
while (k < len) {
10+
const elem = arr[k];
11+
if (predicate(elem, k, arr)) {
12+
return elem;
13+
}
14+
k = k + 1;
15+
}
16+
17+
return undefined;
18+
}
Collapse file

‎test/unit/builtins/array.spec.ts‎

Copy file name to clipboardExpand all lines: test/unit/builtins/array.spec.ts
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,25 @@ test("array.forEach (%p)", () => {
246246
`.expectToMatchJsResult();
247247
});
248248

249+
test.each([
250+
{ array: [], predicate: "elem > 3" },
251+
{ array: [0, 2, 4, 8], predicate: "elem > 10" },
252+
{ array: [0, 2, 4, 8], predicate: "elem > 7" },
253+
{ array: [0, 2, 4, 8], predicate: "elem == 0" },
254+
{ array: [0, 2, 4, 8], predicate: "elem > 7" },
255+
{ array: [0, 2, 4, 8], predicate: "true" },
256+
{ array: [0, 2, 4, 8], predicate: "false" },
257+
])("array.find (%p)", ({ array, predicate }) => {
258+
util.testFunction`
259+
const array = ${util.valueToString(array)};
260+
return array.find((elem, index, arr) => ${predicate} && arr[index] === elem);
261+
`.expectToMatchJsResult();
262+
});
263+
249264
test.each([
250265
{ array: [], searchElement: 3 },
251266
{ array: [0, 2, 4, 8], searchElement: 10 },
267+
{ array: [0, 2, 4, 8], searchElement: 0 },
252268
{ array: [0, 2, 4, 8], searchElement: 8 },
253269
])("array.findIndex (%p)", ({ array, searchElement }) => {
254270
util.testFunction`

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.