Skip to content

Commit

Permalink
feat: add findLast/findLastIndex polyfill for Uint8Array (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 28, 2024
1 parent b758799 commit b4f133c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
Binary file modified lib/pkg/dnt_wasm_bg.wasm
Binary file not shown.
81 changes: 69 additions & 12 deletions rs-lib/src/polyfills/scripts/esnext.array-findLast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,84 @@ declare global {
thisArg?: any,
): number;
}
interface Uint8Array {
/**
* Returns the value of the last element in the array where predicate is true, and undefined
* otherwise.
* @param predicate findLast calls predicate once for each element of the array, in descending
* order, until it finds one where predicate returns true. If such an element is found, findLast
* immediately returns that element value. Otherwise, findLast returns undefined.
* @param thisArg If provided, it will be used as the this value for each invocation of
* predicate. If it is not provided, undefined is used instead.
*/
findLast<S extends number>(
predicate: (
value: number,
index: number,
array: Uint8Array,
) => value is S,
thisArg?: any,
): S | undefined;
findLast(
predicate: (value: number, index: number, array: Uint8Array) => unknown,
thisArg?: any,
): number | undefined;

/**
* Returns the index of the last element in the array where predicate is true, and -1
* otherwise.
* @param predicate findLastIndex calls predicate once for each element of the array, in descending
* order, until it finds one where predicate returns true. If such an element is found,
* findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1.
* @param thisArg If provided, it will be used as the this value for each invocation of
* predicate. If it is not provided, undefined is used instead.
*/
findLastIndex(
predicate: (value: number, index: number, array: Uint8Array) => unknown,
thisArg?: any,
): number;
}
}

function findLastIndex(self: any, callbackfn: any, that: any) {
const boundFunc = that === undefined ? callbackfn : callbackfn.bind(that);
let index = self.length - 1;
while (index >= 0) {
const result = boundFunc(self[index], index, self);
if (result) {
return index;
}
index--;
}
return -1;
}

function findLast(self: any, callbackfn: any, that: any) {
const index = self.findLastIndex(callbackfn, that);
return index === -1 ? undefined : self[index];
}

if (!Array.prototype.findLastIndex) {
Array.prototype.findLastIndex = function (callbackfn: any, that: any) {
const boundFunc = that === undefined ? callbackfn : callbackfn.bind(that);
let index = this.length - 1;
while (index >= 0) {
const result = boundFunc(this[index], index, this);
if (result) {
return index;
}
index--;
}
return -1;
return findLastIndex(this, callbackfn, that);
};
}

if (!Array.prototype.findLast) {
Array.prototype.findLast = function (callbackfn: any, that: any) {
const index = this.findLastIndex(callbackfn, that);
return index === -1 ? undefined : this[index];
return findLast(this, callbackfn, that);
};
}

if (!Uint8Array.prototype.findLastIndex) {
Uint8Array.prototype.findLastIndex = function (callbackfn: any, that: any) {
return findLastIndex(this, callbackfn, that);
};
}

if (!Uint8Array.prototype.findLast) {
Uint8Array.prototype.findLast = function (callbackfn: any, that: any) {
return findLast(this, callbackfn, that);
};
}

Expand Down
7 changes: 7 additions & 0 deletions tests/polyfill_array_find_last_project/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ Deno.test("should find last in array", () => {
undefined,
);
});

Deno.test("should find last for Uint8Array", () => {
assertEquals(new Uint8Array([1, 2, 3]).findLast(() => true), 3);
assertEquals(new Uint8Array([1, 2, 3]).findLastIndex(() => true), 2);
assertEquals(new Uint8Array([1, 2, 3]).findLast((v) => v == 2), 2);
assertEquals(new Uint8Array([1, 2, 3]).findLastIndex((v) => v == 2), 1);
});

0 comments on commit b4f133c

Please sign in to comment.