Skip to content

Commit

Permalink
replace sorter with d3.ascending; update comments; add getTime test
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Oct 14, 2023
1 parent c272982 commit e42614c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
14 changes: 8 additions & 6 deletions src/lib/lib/calcUniques.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@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 sorter from '../utils/sorter.js';
import { ascending } from 'd3-array';
import getTime from '../utils/getTime.js';

export default function calcUniques (data, fields, { sort = false } = {}) {
Expand Down Expand Up @@ -52,7 +52,8 @@ export default function calcUniques (data, fields, { sort = false } = {}) {
const vl = val.length;
for (k = 0; k < vl; k += 1) {
/**
* If val[k] is a date, return its time. Otherwise, return val[k].
* Date objects of the same date are not equivalent in a Set
* If val[k] is a date we turn it into a time value with getTime, otherwise getTime returns val[k].
* Push val[k] to results, though, so that we aren't converting the users data.
*/
if (!set.has(getTime(val[k]))) {
Expand All @@ -62,17 +63,18 @@ export default function calcUniques (data, fields, { sort = false } = {}) {
}
} else {
/**
* If val is a date, return its time. Otherwise, return val.
* Push val to results, though, so that we aren't converting the users data.
* Date objects of the same date are not equivalent in a Set
* If val[k] is a date we turn it into a time value with getTime, otherwise getTime returns val[k].
* Push val[k] to results, though, so that we aren't converting the users data.
*/
if (!set.has(getTime(val)) ){
if (!set.has(getTime(val))) {
set.add(getTime(val));
results.push(val);
}
}
}
if (sort === true) {
results.sort(sorter);
results.sort(ascending);
}
uniques[s] = results;
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/utils/getTime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/**
* If val is a date, return its time. Otherwise, return val.
* @param {Date|*} val
* @returns {number|*}
*/
export default function getTime (val) {
if (Object.prototype.toString.call(val) === '[object Date]') {
Expand Down
13 changes: 0 additions & 13 deletions src/lib/utils/sorter.js

This file was deleted.

27 changes: 27 additions & 0 deletions test/getTime.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* globals describe it */
import * as assert from 'assert';
import fn from '../src/lib/utils/getTime.js';

const name = 'getTime';

const tests = [
{ args: [0], expected: 0 },
{ args: [[0, 1, 2]], expected: [0, 1, 2] },
{ args: ['hello'], expected: 'hello' },
{ args: [Infinity], expected: Infinity },
{ args: [null], expected: null },
{ args: [undefined], expected: undefined },
{ args: [new Date(2020, 0, 1)], expected: 1577854800000 },
{ args: [new Date(Date.UTC(2020, 0, 1))], expected: 1577836800000 }
];

describe(name, () => {
tests.forEach((test) => {
describe(JSON.stringify(test.args), () => {
it(`should equal ${JSON.stringify(test.expected)}`, () => {
const actual = fn(...test.args);
assert.deepStrictEqual(actual, test.expected);
});
});
});
});

0 comments on commit e42614c

Please sign in to comment.