Skip to content

Commit

Permalink
getComparableValue using minimumFractionDigits
Browse files Browse the repository at this point in the history
test: filter comparison uses minimumFractionDigits
  • Loading branch information
programmer4web committed Jun 25, 2018
1 parent eab158c commit ad1cbce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
29 changes: 29 additions & 0 deletions cosmoz-omnitable-column-number.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@ <h3 style="margin: 0;">[[ title ]]</h3>
return new Intl.NumberFormat(locale || undefined, options);
},

/**
* Get the comparable value of an item.
*
* @param {Object} item Item to be processed
* @param {String} valuePath Property path
* @returns {Number|void} Valid value or void
*/
getComparableValue(item, valuePath) {
if (item == null) {
return;
}
let value = item;
if (valuePath != null) {
value = this.get(valuePath, item);
}
value = this.toValue(value);
if (value == null) {
return;
}

const decimals = this.minimumFractionDigits;
if (decimals !== null) {
const d = Math.pow(10, decimals),
temp = parseInt(value * d, 10) / d;
return temp.toFixed(decimals);
}
return value;
},

renderValue(value, formatter = this.formatter) {
const number = this.toNumber(value);
if (number == null) {
Expand Down
21 changes: 19 additions & 2 deletions test/range.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<test-fixture id="range">
<template>
<cosmoz-omnitable id="omnitable" class="flex">
<cosmoz-omnitable-column-number title="Age" name="age" value-path="age">
<cosmoz-omnitable-column-number title="Age" name="age" value-path="age" minimum-fraction-digits=2>
</cosmoz-omnitable-column-number>
<cosmoz-omnitable-column-amount title="Amount" name="amount" value-path="amount">
</cosmoz-omnitable-column-amount>
Expand Down Expand Up @@ -76,6 +76,7 @@
amount: { amount: 347, currency: 'USD' }
},
{
age: 46.768, // needed in minimumFractionDigits test
amount: { amount: 2581, currency: 'EUR' }
}
];
Expand Down Expand Up @@ -124,7 +125,7 @@
test('computes range from values', function (done) {
assert.isObject(column._range);
assert.equal(column._range.min, -11);
assert.equal(column._range.max, 17);
assert.equal(column._range.max, 46.768);
done();
});

Expand Down Expand Up @@ -191,6 +192,22 @@
});
column._filterInput = {min: -11, max: 17};
});

test('filter comparision uses minimumFractionDigits', done => {
assert.isNull(column._filterInput.min);
assert.isNull(column._filterInput.max);

const handler = () => {
const items = omnitable.sortedFilteredGroupedItems;
assert.equal(items.length, 4, 'Expected item coresponding to the age: 46.768 to be in the filtered items.');
assert.equal(items[3].amount.amount, 2581); // item coresponding to the age: 46.768 is present on the filtered items.

omnitable.removeEventListener('sorted-filtered-grouped-items-changed', handler);
done();
};
omnitable.addEventListener('sorted-filtered-grouped-items-changed', handler);
column._filterInput = {min: 46.76, max: 46.76};
});
});

suite('amount', function () {
Expand Down

0 comments on commit ad1cbce

Please sign in to comment.