Skip to content
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

Add comparator function to ordinal domain sort #152

Merged
merged 7 commits into from
Oct 14, 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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changelog
===

# 8.0.1

> 2023-10-14

Corrects the change in 8.0.0 for sorting a domain by adding a comparator function to better sort strings and dates. Also fixes an oversight where domains that were dates would not be calculated uniquely. Also some security upgrades. [Closes #151](https://github.com/mhkeller/layercake/issues/151)

* [PR#149](https://github.com/mhkeller/layercake/pull/149)
* [PR#151](https://github.com/mhkeller/layercake/pull/151)

# 8.0.0

> 2023-09-19
Expand Down
8 changes: 5 additions & 3 deletions src/lib/lib/calcUniques.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@param {{x?: Function, y?: Function, z?: Function, r?: Function}} fields An object containing `x`, `y`, `r` or `z` keys that equal an accessor function. If an accessor function returns an array of values, each value will also be evaluated..
@returns {{x?: [min: Number, max: Number]|[min: String, max: String], y?: [min: Number, max: Number]|[min: String, max: String], z?: [min: Number, max: Number]|[min: String, max: String], r?: [min: Number, max: Number]|[min: String, max: String]}} An object with the same structure as `fields` but instead of an accessor, each key contains an array of unique items.
*/
import { ascending, InternSet } from 'd3-array';

export default function calcUniques (data, fields, { sort = false } = {}) {
if (!Array.isArray(data)) {
throw new TypeError(`The first argument of calcUniques() must be an array. You passed in a ${typeof data}. If you got this error using the <LayerCake> component, consider passing a flat array to the \`flatData\` prop. More info: https://layercake.graphics/guide/#flatdata`);
Expand Down Expand Up @@ -38,7 +40,7 @@ export default function calcUniques (data, fields, { sort = false } = {}) {

const dl = data.length;
for (i = 0; i < kl; i += 1) {
set = new Set();
set = new InternSet();
s = keys[i];
acc = fields[s];
for (j = 0; j < dl; j += 1) {
Expand All @@ -48,13 +50,13 @@ export default function calcUniques (data, fields, { sort = false } = {}) {
for (k = 0; k < vl; k += 1) {
set.add(val[k]);
mhkeller marked this conversation as resolved.
Show resolved Hide resolved
}
} else{
} else {
set.add(val);
mhkeller marked this conversation as resolved.
Show resolved Hide resolved
}
}
const results = Array.from(set);
if (sort === true) {
results.sort();
results.sort(ascending);
}
uniques[s] = results;
}
Expand Down
Loading
Loading