Skip to content

Commit 97f653f

Browse files
authored
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.
1 parent 3eb6a68 commit 97f653f

File tree

6 files changed

+112
-1
lines changed

6 files changed

+112
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "add findLastIndex and inRange functions to fast-web-utilities",
4+
"packageName": "@microsoft/fast-web-utilities",
5+
"email": "john.kreitlow@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect } from "chai";
2+
import { findLastIndex } from "./array";
3+
4+
describe("findLastIndex", (): void => {
5+
it("should return -1 when array is empty", (): void => {
6+
expect(findLastIndex([], () => true)).to.equal(-1);
7+
});
8+
9+
it("should return the last valid item that matches the predicate", (): void => {
10+
const array = [
11+
{ value: true },
12+
{ value: false },
13+
{ value: true },
14+
{ value: false },
15+
];
16+
17+
expect(findLastIndex(array, v => v.value)).to.equal(2);
18+
});
19+
20+
it("should return -1 when no items match the predicate", (): void => {
21+
const array = [
22+
{ value: false },
23+
{ value: false },
24+
{ value: false },
25+
{ value: false },
26+
];
27+
28+
expect(findLastIndex(array, v => v.value)).to.equal(-1);
29+
});
30+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Returns the index of the last element in the array where predicate is true, and -1 otherwise.
3+
*
4+
* @param array - the array to test
5+
* @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.
6+
*/
7+
export function findLastIndex<T>(
8+
array: Array<T>,
9+
predicate: (value: T, index: number, obj: T[]) => unknown
10+
): number {
11+
let k = array.length;
12+
while (k--) {
13+
if (predicate(array[k], k, array)) {
14+
return k;
15+
}
16+
}
17+
18+
return -1;
19+
}

packages/utilities/fast-web-utilities/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./aria";
2+
export * from "./array";
23
export * from "./class-names";
34
export * from "./dom";
45
export * from "./events";

packages/utilities/fast-web-utilities/src/numbers.spec.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from "chai";
2-
import { limit, wrapInBounds } from "./numbers";
2+
import { inRange, limit, wrapInBounds } from "./numbers";
33

44
describe("wrapInBounds", () => {
55
it("should not throw if any parameters are null", () => {
@@ -76,3 +76,45 @@ describe("limit", () => {
7676
expect(limit(0, 10, 5)).to.equal(5);
7777
});
7878
});
79+
80+
describe("inRange", () => {
81+
it("should not throw if any parameters are null", () => {
82+
expect(() => {
83+
inRange(null, null, null);
84+
}).not.to.throw();
85+
expect(() => {
86+
inRange(0, null, null);
87+
}).not.to.throw();
88+
expect(() => {
89+
inRange(0, null, 1);
90+
}).not.to.throw();
91+
expect(() => {
92+
inRange(0, 10, null);
93+
}).not.to.throw();
94+
});
95+
96+
it("should return `true` if `value` is within range of `min` and `max`", () => {
97+
expect(inRange(10, 0, 20)).to.be.true;
98+
expect(inRange(10, 20)).to.be.true;
99+
});
100+
101+
it("should return `false` when `value` is less than `min` and `max`", () => {
102+
expect(inRange(10, 20, 30)).to.be.false;
103+
});
104+
105+
it("should return `false` when `value` is greater than `min` and `max`", () => {
106+
expect(inRange(10, 0, 5)).to.be.false;
107+
});
108+
109+
it("should return `false` when `value` is equal to `max`", () => {
110+
expect(inRange(10, 0, 10)).to.be.false;
111+
});
112+
113+
it("should return `true` when `value` is less than `min` and `max` is omitted", () => {
114+
expect(inRange(10, 20)).to.be.true;
115+
});
116+
117+
it("should return `false` when `value` is less than 0 and `max` is omitted", () => {
118+
expect(inRange(-10, 20)).to.be.false;
119+
});
120+
});

packages/utilities/fast-web-utilities/src/numbers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ export function wrapInBounds(min: number, max: number, value: number): number {
2020
export function limit(min: number, max: number, value: number): number {
2121
return Math.min(Math.max(value, min), max);
2222
}
23+
24+
/**
25+
* Determines if a number value is within a specified range.
26+
*
27+
* @param value - the value to check
28+
* @param min - the range start
29+
* @param max - the range end
30+
*/
31+
export function inRange(value: number, min: number, max: number = 0): boolean {
32+
[min, max] = [min, max].sort((a, b) => a - b);
33+
return min <= value && value < max;
34+
}

0 commit comments

Comments
 (0)