Skip to content

Commit

Permalink
Add functions min() and max() to DataArray (#2372)
Browse files Browse the repository at this point in the history
* fix: DataArray.avg() return type

* feat: Add functions min() and max() to DataArray
  • Loading branch information
carlesalbasboix authored Jun 18, 2024
1 parent 75c38b9 commit 6bb3cc1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
12 changes: 9 additions & 3 deletions docs/docs/api/data-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@ export interface DataArray<T> {
/** Run a lambda on each element in the array. */
forEach(f: ArrayFunc<T, void>): void;

/** Calculate the sum of the elements of the array. */
/** Calculate the sum of the elements in the array. */
sum(): number;

/** Calculate the average of the elements of the array. */
avg(): number | undefined;
/** Calculate the average of the elements in the array. */
avg(): number;

/** Calculate the minimum of the elements in the array. */
min(): number;

/** Calculate the maximum of the elements in the array. */
max(): number;

/** Convert this to a plain javascript array. */
array(): T[];
Expand Down
24 changes: 20 additions & 4 deletions src/api/data-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,17 @@ export interface DataArray<T> {
/** Run a lambda on each element in the array. */
forEach(f: ArrayFunc<T, void>): void;

/** Calculate the sum of the elements of the array. */
/** Calculate the sum of the elements in the array. */
sum(): number;

/** Calculate the average of the elements of the array. */
avg(): number | undefined;
/** Calculate the average of the elements in the array. */
avg(): number;

/** Calculate the minimum of the elements in the array. */
min(): number;

/** Calculate the maximum of the elements in the array. */
max(): number;

/** Convert this to a plain javascript array. */
array(): T[];
Expand Down Expand Up @@ -171,7 +177,9 @@ class DataArrayImpl<T> implements DataArray<T> {
"toString",
"settings",
"sum",
"avg"
"avg",
"min",
"max",
]);

private static ARRAY_PROXY: ProxyHandler<DataArrayImpl<any>> = {
Expand Down Expand Up @@ -462,6 +470,14 @@ class DataArrayImpl<T> implements DataArray<T> {
return this.sum() / this.values.length;
}

public min() {
return Math.min(...this.values);
}

public max() {
return Math.max(...this.values);
}

public array(): T[] {
return ([] as any[]).concat(this.values);
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/api/data-array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ describe("avg", () => {
test("numbers", () => expect(da([5, 10, 15]).avg()).toEqual(10));
});

describe("min", () => {
test("empty", () => expect(da([]).min()).toEqual(Infinity));
test("numbers", () => expect(da([14, 10, 15]).min()).toEqual(10));
});

describe("max", () => {
test("empty", () => expect(da([]).max()).toEqual(-Infinity));
test("numbers", () => expect(da([14, 10, 15]).max()).toEqual(15));
});

/** Utility function for quickly creating a data array. */
function da<T>(val: T[]): DataArray<T> {
return DataArray.wrap(val, DEFAULT_QUERY_SETTINGS);
Expand Down

0 comments on commit 6bb3cc1

Please sign in to comment.