Skip to content

propagate domain sort in basic transforms #1315

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
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/transforms/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {ascendingDefined, descendingDefined} from "../defined.js";
import {arrayify, isDomainSort, isOptions, maybeValue, valueof} from "../options.js";

/** @jsdoc transform */
export function basic(options = {}, transform) {
let {filter: f1, sort: s1, reverse: r1, transform: t1, initializer: i1, ...remainingOptions} = options;
export function basic({filter: f1, sort: s1, reverse: r1, transform: t1, initializer: i1, ...options} = {}, transform) {
// If both t1 and t2 are defined, returns a composite transform that first
// applies t1 and then applies t2.
if (t1 === undefined) {
Expand All @@ -15,15 +14,14 @@ export function basic(options = {}, transform) {
}
if (transform != null && i1 != null) throw new Error("transforms cannot be applied after initializers");
return {
...remainingOptions,
...options,
...((s1 === null || isDomainSort(s1)) && {sort: s1}),
transform: composeTransform(t1, transform)
};
}

/** @jsdoc initializer */
export function initializer(options = {}, initializer) {
let {filter: f1, sort: s1, reverse: r1, initializer: i1, ...remainingOptions} = options;
export function initializer({filter: f1, sort: s1, reverse: r1, initializer: i1, ...options} = {}, initializer) {
// If both i1 and i2 are defined, returns a composite initializer that first
// applies i1 and then applies i2.
if (i1 === undefined) {
Expand All @@ -33,7 +31,7 @@ export function initializer(options = {}, initializer) {
if (r1) i1 = composeInitializer(i1, reverseTransform);
}
return {
...remainingOptions,
...options,
...((s1 === null || isDomainSort(s1)) && {sort: s1}),
initializer: composeInitializer(i1, initializer)
};
Expand Down Expand Up @@ -76,25 +74,30 @@ function filterTransform(value) {
}

/** @jsdoc reverse */
export function reverse(options) {
return {...apply(options, reverseTransform), sort: null};
export function reverse({sort, ...options} = {}) {
return {
...apply(options, reverseTransform),
sort: isDomainSort(sort) ? sort : null
};
}

function reverseTransform(data, facets) {
return {data, facets: facets.map((I) => I.slice().reverse())};
}

/** @jsdoc shuffle */
export function shuffle(options = {}) {
const {seed, ...remainingOptions} = options;
return {...apply(remainingOptions, sortValue(seed == null ? Math.random : randomLcg(seed))), sort: null};
export function shuffle({seed, sort, ...options} = {}) {
return {
...apply(options, sortValue(seed == null ? Math.random : randomLcg(seed))),
sort: isDomainSort(sort) ? sort : null
};
}

/** @jsdoc sort */
export function sort(order, options) {
export function sort(order, {sort, ...options} = {}) {
return {
...(isOptions(order) && order.channel !== undefined ? initializer : apply)(options, sortTransform(order)),
sort: null
sort: isDomainSort(sort) ? sort : null
};
}

Expand Down
Loading