pat310 / quick-pivot

Quickly format data to create a pivot table
https://www.npmjs.com/package/quick-pivot
MIT License
63 stars 21 forks source link

Filtering on value dimensions with string filter values instead of ints #45

Closed turnerniles closed 7 years ago

turnerniles commented 7 years ago

When filtering on value fields, strings passed in as filters are not converted into ints. Should we do this in the pivot logic or on the front end? Numbers on the front-end are rendered as strings so when passed back as in a filter as a they do not match the respective int stored in the pivot.

const dataArray = [
 ['name', 'gender', 'house', 'age'],
 ['Jon', 'm', 'Stark', 14],
 ['Arya', 'f', 'Stark', 10],
 ['Cersei', 'f', 'Baratheon', 38],
 ['Tywin', 'm', 'Lannister', 67],
 ['Tyrion', 'm', 'Lannister', 34],
 ['Joffrey', 'm', 'Baratheon', 18],
 ['Bran', 'm', 'Stark', 8],
 ['Jaime', 'm', 'Lannister', 32],
 ['Sansa', 'f', 'Stark', 12]
];

const pivot = new pivot(dataArray, ['age'], [], 'age', 'sum');

// returns original pivoted data because it is excluding a string that does match the int 8 in the pivot
pivot.filter("age", ["8"], 'exclude')

// works
pivot.filter("age", [8], 'exclude')
pat310 commented 7 years ago

@turnerniles We could convert everything to strings and tell the user that everything will be converted to a string. But then the user would be limited with the fancy custom filtering when passing a callback function (I guess they could convert it back to whatever data type they want or we could just not change the data type if a callback is passed). It also feels weird to change the user's data type to a string for filtering but if they do some mathematical aggregations on that dimension (like averaging or maximum/minimum) then it should stay as a number? The more I think about it, the more complicated I think it actually is to manipulate the data type... Maybe we should just handle it on the front-end by using the callback feature for filtering?

turnerniles commented 7 years ago

I agree in principle if you are writing code and using quick-pivot without a front-end. But when rendering it on the frontend and passing it back, the value passed back will be a string. And if I convert all strings to ints, before calling the filtering function, if the number is stored as a string on the backend then it won't work. Also if we use an <input> tag to allow the user to pass a custom value, it will also be passed back as a string.

I will use the custom callback for the UI.: return filterValues.indexOf(value) == -1; instead of return filterValues.indexOf(value) === -1;

And we can close the issue. What do you think?

pat310 commented 7 years ago

Sounds good!