Skip to content

Commit

Permalink
Back to map
Browse files Browse the repository at this point in the history
  • Loading branch information
mesmo committed Dec 8, 2024
1 parent e5b4d7a commit 21a78ea
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/dist/pivot.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ const criteria = (key) => (criterion) => (value) => value[key] === criterion;
exports.criteria = criteria;
// the implementation of pivot
function pivot(source, ...[dimension, ...dimensions]) {
const matrix = [];
for (const criteria of dimension) {
const matrix = dimension.map((criteria) => {
const slice = [];
for (const value of source) {
if (criteria(value)) {
slice.push(value);
}
}
matrix.push(slice);
}
return slice;
});
// recurse if there are other dimensions, otherwise just return the matrix
return dimensions.length ? matrix.map(slice => pivot(slice, ...dimensions)) : matrix;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/web/pivot.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ export function pivot<TValue>(source: Array<TValue>, ...dimensions: Array<Dimens

// the implementation of pivot
export function pivot<TValue>(source: Array<TValue>, ...[dimension, ...dimensions]: Array<Dimension<TValue>>) {
const matrix: Matrix<TValue> = [];

for (const criteria of dimension) {
const matrix: Matrix<TValue> = dimension.map((criteria: Criteria<TValue>) => {
const slice: Array<TValue> = [];

for (const value of source) {
Expand All @@ -144,8 +142,8 @@ export function pivot<TValue>(source: Array<TValue>, ...[dimension, ...dimension
}
}

matrix.push(slice);
}
return slice;
});

// recurse if there are other dimensions, otherwise just return the matrix
return dimensions.length ? matrix.map(slice => pivot(slice, ...dimensions)) : matrix;
Expand Down

0 comments on commit 21a78ea

Please sign in to comment.