-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(pouchdb-mapreduce): (#9104) - enable _stats reducer to digest arrays of numbers #9108
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
Merged
+168
−22
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,103 @@ | ||
| import { BuiltInError } from 'pouchdb-mapreduce-utils'; | ||
| import sum from './sum'; | ||
|
|
||
| function getMixedTypeError() { | ||
| return new BuiltInError( | ||
| 'builtin _stats function requires map values to be' + | ||
| ' numbers or number arrays, not a mix of both.' | ||
| ); | ||
| } | ||
|
|
||
| function getMixedLengthError() { | ||
| return new BuiltInError( | ||
| 'builtin _stats function: if the map function outputs' + | ||
| ' arrays, they need to have consistent length.' | ||
| ); | ||
| } | ||
|
|
||
| function sumsqr(values) { | ||
| let _sumsqr = 0; | ||
| for (let i = 0, len = values.length; i < len; i++) { | ||
| const num = values[i]; | ||
| _sumsqr += (num * num); | ||
| } | ||
| return _sumsqr; | ||
| } | ||
|
|
||
| function stats(keys, values) { | ||
|
|
||
| // Handle the edge case of an empty values array. | ||
| if (!values || values.length === 0) { | ||
| return { | ||
| sum: 0, | ||
| min: null, | ||
| max: null, | ||
| count: 0, | ||
| sumsqr: 0 | ||
| }; | ||
| } | ||
|
|
||
| const isArrayOfArrays = Array.isArray(values[0]); | ||
|
|
||
| if (isArrayOfArrays) { | ||
| // --- PATH 1: Handle Array of Arrays --- | ||
| // e.g., [[1, 100], [2, 200], [3, 300]] | ||
|
|
||
| const firstArrayLength = values[0].length; | ||
| const regroupedValues = []; | ||
|
|
||
| // Initialize the structure for regrouped ("transposed") values. | ||
| for (let i = 0; i < firstArrayLength; i++) { | ||
| regroupedValues.push([]); | ||
| } | ||
|
|
||
| // Validate and regroup the values. | ||
| for (const valueArray of values) { | ||
| // ERROR CHECK 1: Mixed member types. | ||
| if (!Array.isArray(valueArray)) { | ||
| throw getMixedTypeError(); | ||
| } | ||
| // ERROR CHECK 2: Inconsistent array lengths. | ||
| if (valueArray.length !== firstArrayLength) { | ||
| throw getMixedLengthError(); | ||
| } | ||
| // Add each number to its corresponding stats group. | ||
| for (let i = 0; i < firstArrayLength; i++) { | ||
| regroupedValues[i].push(valueArray[i]); | ||
| } | ||
| } | ||
|
|
||
| // Calculate stats for each regrouped array. | ||
| return regroupedValues.map(function (statValues) { | ||
| return { | ||
| sum: sum(statValues), | ||
| min: Math.min.apply(null, statValues), | ||
| max: Math.max.apply(null, statValues), | ||
| count: statValues.length, | ||
| sumsqr: sumsqr(statValues) | ||
| }; | ||
| }); | ||
|
|
||
| } else { | ||
| // --- PATH 2: Handle Array of Numbers --- | ||
| // e.g., [1, 2, 3] | ||
|
|
||
| // ERROR CHECK: Ensure no arrays are mixed in. | ||
| for (const value of values) { | ||
| if (typeof value !== 'number') { | ||
| throw getMixedTypeError(); | ||
| } | ||
| } | ||
|
|
||
| // Apply the original logic. | ||
| return { | ||
| sum: sum(values), | ||
| min: Math.min.apply(null, values), | ||
| max: Math.max.apply(null, values), | ||
| count: values.length, | ||
| sumsqr: sumsqr(values) | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export default stats; |
|
SourceR85 marked this conversation as resolved.
SourceR85 marked this conversation as resolved.
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.