forked from microsoft/fast
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add findLastIndex and inRange utility functions to fast-web-utilities (…
…microsoft#5528) * add findLastIndex function to fast-web-utilities * add inRange function to fast-web-utilities * Change files * remove instance type check * restore non-optional parameters TS environments with strictNullChecks will throw errors if these parameters are optional.
- Loading branch information
Showing
6 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-web-utilities-e9facd80-2d8e-4553-ae3d-8d06ad809738.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "add findLastIndex and inRange functions to fast-web-utilities", | ||
"packageName": "@microsoft/fast-web-utilities", | ||
"email": "john.kreitlow@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { expect } from "chai"; | ||
import { findLastIndex } from "./array"; | ||
|
||
describe("findLastIndex", (): void => { | ||
it("should return -1 when array is empty", (): void => { | ||
expect(findLastIndex([], () => true)).to.equal(-1); | ||
}); | ||
|
||
it("should return the last valid item that matches the predicate", (): void => { | ||
const array = [ | ||
{ value: true }, | ||
{ value: false }, | ||
{ value: true }, | ||
{ value: false }, | ||
]; | ||
|
||
expect(findLastIndex(array, v => v.value)).to.equal(2); | ||
}); | ||
|
||
it("should return -1 when no items match the predicate", (): void => { | ||
const array = [ | ||
{ value: false }, | ||
{ value: false }, | ||
{ value: false }, | ||
{ value: false }, | ||
]; | ||
|
||
expect(findLastIndex(array, v => v.value)).to.equal(-1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Returns the index of the last element in the array where predicate is true, and -1 otherwise. | ||
* | ||
* @param array - the array to test | ||
* @param predicate - find 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, findIndex returns -1. | ||
*/ | ||
export function findLastIndex<T>( | ||
array: Array<T>, | ||
predicate: (value: T, index: number, obj: T[]) => unknown | ||
): number { | ||
let k = array.length; | ||
while (k--) { | ||
if (predicate(array[k], k, array)) { | ||
return k; | ||
} | ||
} | ||
|
||
return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters