Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #201 from FormidableLabs/feature/data-sorting-prop
Browse files Browse the repository at this point in the history
Feature/data sorting prop
  • Loading branch information
DerekMaffett authored Feb 18, 2017
2 parents 95f95e7 + e9a16c4 commit ad267f6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/victory-util/data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assign, uniq, range, last, isFunction, property } from "lodash";
import { assign, uniq, range, last, isFunction, property, sortBy } from "lodash";
import Helpers from "./helpers";
import Collection from "./collection";
import Log from "./log";
Expand Down Expand Up @@ -90,7 +90,25 @@ export default {
typeof y === "string" ? { _y: stringMap.y[y], yName: y } : {}
);
});
return this.cleanData(data, props);

const sortedData = this.sortData(data, props.sortKey);

return this.cleanData(sortedData, props);
},

/**
* Returns sorted data. If no sort keys are provided, data is returned unaltered.
* Sort key should correspond to the `iteratees` argument in lodash `sortBy` function.
* @param {Array} dataset: the original domain
* @param {Function} sortKey: the sort key
* @returns {Array} the sorted data
*/
sortData(dataset, sortKey) {
if (!sortKey) {
return dataset;
}

return sortBy(dataset, sortKey);
},

/**
Expand Down
27 changes: 27 additions & 0 deletions test/client/spec/victory-util/data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,33 @@ describe("helpers/data", () => {
expect(returnData).to.eql(expectedReturnWithEventKeys);
});

it("does not sort data when function not passed", () => {
const data = [{x: 2, y: 2}, {x: 1, y: 3}, {x: 3, y: 1}];
const props = {data};

const returnData = Data.getData(props);

expect(returnData).to.eql([
{_x: 2, x: 2, _y: 2, y: 2, eventKey: 0},
{_x: 1, x: 1, _y: 3, y: 3, eventKey: 1},
{_x: 3, x: 3, _y: 1, y: 1, eventKey: 2}
]);
});

it("sorts data according to passed function", () => {
const data = [{x: 2, y: 2}, {x: 1, y: 3}, {x: 3, y: 1}];
const sortKey = ["_x"];
const props = {data, sortKey};

const returnData = Data.getData(props);

expect(returnData).to.eql([
{_x: 1, x: 1, _y: 3, y: 3, eventKey: 0},
{_x: 2, x: 2, _y: 2, y: 2, eventKey: 1},
{_x: 3, x: 3, _y: 1, y: 1, eventKey: 2}
]);
});

it("generates a dataset from domain", () => {
const generatedReturn = [{x: 0, y: 0}, {x: 10, y: 10}];
const expectedReturn = [{_x: 0, x: 0, _y: 0, y: 0}, {_x: 10, x: 10, _y: 10, y: 10}];
Expand Down

0 comments on commit ad267f6

Please sign in to comment.