-
Notifications
You must be signed in to change notification settings - Fork 77
feat: add support for dates to max and min functions. #428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,19 +211,19 @@ export function avg<T>( | |
* Creates a min aggregate function that computes the minimum value in a group | ||
* @param valueExtractor Function to extract a numeric value from each data entry | ||
*/ | ||
export function min<T>( | ||
valueExtractor: (value: T) => number = (v) => v as unknown as number | ||
): AggregateFunction<T, number, number> { | ||
export function min<T, V>( | ||
valueExtractor: (value: T) => V = (v) => v as unknown as V | ||
): AggregateFunction<T, never, V> { | ||
return { | ||
preMap: (data: T) => valueExtractor(data), | ||
reduce: (values: Array<[number, number]>) => { | ||
let minValue = Number.POSITIVE_INFINITY | ||
reduce: (values) => { | ||
let minValue = Number.POSITIVE_INFINITY as V | ||
for (const [value, _multiplicity] of values) { | ||
if (value < minValue) { | ||
if (Number(value) < Number(minValue)) { | ||
minValue = value | ||
} | ||
} | ||
return minValue === Number.POSITIVE_INFINITY ? 0 : minValue | ||
return minValue === Number.POSITIVE_INFINITY ? (0 as V) : minValue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue I have here is that I need the returned type to be the type passed in. So if they pass in a Date, I want the return type to be Date as well. That's why I ended up casting as I couldn't figure out what's the best way to handle this |
||
}, | ||
} | ||
} | ||
|
@@ -232,19 +232,19 @@ export function min<T>( | |
* Creates a max aggregate function that computes the maximum value in a group | ||
* @param valueExtractor Function to extract a numeric value from each data entry | ||
*/ | ||
export function max<T>( | ||
valueExtractor: (value: T) => number = (v) => v as unknown as number | ||
): AggregateFunction<T, number, number> { | ||
export function max<T, V>( | ||
valueExtractor: (value: T) => V = (v) => v as unknown as V | ||
): AggregateFunction<T, never, V> { | ||
return { | ||
preMap: (data: T) => valueExtractor(data), | ||
reduce: (values: Array<[number, number]>) => { | ||
let maxValue = Number.NEGATIVE_INFINITY | ||
reduce: (values) => { | ||
let maxValue = Number.NEGATIVE_INFINITY as V | ||
for (const [value, _multiplicity] of values) { | ||
if (value > maxValue) { | ||
if (Number(value) > Number(maxValue)) { | ||
maxValue = value | ||
} | ||
} | ||
return maxValue === Number.NEGATIVE_INFINITY ? 0 : maxValue | ||
return maxValue === Number.NEGATIVE_INFINITY ? (0 as V) : maxValue | ||
}, | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.