8000 Merge pull request #265 from EpicH0liday/foreign-currency-sort · leetcode/reactable@ce4f988 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce4f988

Browse files
committed
Merge pull request glittershark#265 from EpicH0liday/foreign-currency-sort
Add support for foreign currency sorting
2 parents 49b20ab + 97e49ce commit ce4f988

File tree

6 files changed

+61
-30
lines changed

6 files changed

+61
-30
lines changed

build/reactable.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,8 @@ window.ReactDOM["default"] = window.ReactDOM;
344344

345345
Currency: function Currency(a, b) {
346346
// Parse out dollar signs, then do a regular numeric sort
347-
// TODO: handle non-American currency
348-
349-
if (a[0] === '$') {
350-
a = a.substring(1);
351-
}
352-
if (b[0] === '$') {
353-
b = b.substring(1);
354-
}
347+
a = a.replace(/[^0-9\.\-\,]+/g, '');
348+
b = b.replace(/[^0-9\.\-\,]+/g, '');
355349

356350
return exports.Sort.Numeric(a, b);
357351
},
@@ -1075,6 +1069,8 @@ window.ReactDOM["default"] = window.ReactDOM;
10751069
}
10761070

10771071
switch (child.type) {
1072+
case _thead.Thead:
1073+
break;
10781074
case _tfoot.Tfoot:
10791075
if (typeof tfoot !== 'undefined') {
10801076
console.warn('You can only have one <Tfoot>, but more than one was specified.' + 'Ignoring all but the last one');
@@ -1119,7 +1115,7 @@ window.ReactDOM["default"] = window.ReactDOM;
11191115
break;
11201116

11211117
default:
1122-
console.warn('The only possible children of <Table>, are <Thead>, <Tr>, ' + 'or one <Tfoot>.');
1118+
console.warn('The only possible children of <Table> are <Thead>, <Tr>, ' + 'or one <Tfoot>.');
11231119
}
11241120
}).bind(this));
11251121
}

build/tests/reactable_test.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/reactable/sort.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,8 @@ var Sort = {
4141

4242
Currency: function Currency(a, b) {
4343
// Parse out dollar signs, then do a regular numeric sort
44-
// TODO: handle non-American currency
45-
46-
if (a[0] === '$') {
47-
a = a.substring(1);
48-
}
49-
if (b[0] === '$') {
50-
b = b.substring(1);
51-
}
44+
a = a.replace(/[^0-9\.\-\,]+/g, '');
45+
b = b.replace(/[^0-9\.\-\,]+/g, '');
5246

5347
return exports.Sort.Numeric(a, b);
5448
},

lib/reactable/table.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ var Table = (function (_React$Component) {
101101
}
102102

103103
switch (child.type) {
104+
case _thead.Thead:
105+
break;
104106
case _tfoot.Tfoot:
105107
if (typeof tfoot !== 'undefined') {
106108
console.warn('You can only have one <Tfoot>, but more than one was specified.' + 'Ignoring all but the last one');
@@ -145,7 +147,7 @@ var Table = (function (_React$Component) {
145147
break;
146148

147149
default:
148-
console.warn('The only possible children of <Table>, are <Thead>, <Tr>, ' + 'or one <Tfoot>.');
150+
console.warn('The only possible children of <Table> are <Thead>, <Tr>, ' + 'or one <Tfoot>.');
149151
}
150152
}).bind(this));
151153
}

src/reactable/sort.jsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,8 @@ export var Sort = {
3636

3737
Currency(a, b) {
3838
// Parse out dollar signs, then do a regular numeric sort
39-
// TODO: handle non-American currency
40-
41-
if (a[0] === '$') {
42-
a = a.substring(1);
43-
}
44-
if (b[0] === '$') {
45-
b = b.substring(1);
46-
}
39+
a = a.replace(/[^0-9\.\-\,]+/g, '');
40+
b = b.replace(/[^0-9\.\-\,]+/g, '');
4741

4842
return exports.Sort.Numeric(a, b);
4943
},

tests/reactable_test.jsx

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ describe('Reactable', function() {
494494
});
495495
});
496496
});
497-
497+
498498
describe('adding <Td> with style to the <Table>', function() {
499499
before(function () {
500500
var tdStyle = {width:"100px"};
@@ -503,7 +503,7 @@ describe('Reactable', function() {
503503
<Reactable.Tr>
504504
<Reactable.Td column="Name" className="name-1" style={tdStyle}>Griffin Smith</Reactable.Td>
505505
<Reactable.Td column="Age">18</Reactable.Td>
506-
</Reactable.Tr>
506+
</Reactable.Tr>
507507
</Reactable.Table>,
508508
ReactableTestUtils.testNode()
509509
);
@@ -513,7 +513,7 @@ describe('Reactable', function() {
513513

514514
it('renders the first column with the width', function() {
515515
expect($('td.name-1')).to.have.attr('style').match(/width/);
516-
});
516+
});
517517
});
518518

519519
describe('specifying an array of columns', function() {
@@ -1608,6 +1608,51 @@ describe('Reactable', function() {
16081608
});
16091609
});
16101610

1611+
describe('yen sort', function(){
1612+
before(function() {
1613+
ReactDOM.render(
1614+
<Reactable.Table className="table" id="table" data={[
1615+
{ Price: '1.25'},
1616+
{ Price: '¥1.01'},
1617+
{ Price: '1'},
1618+
{ Price: '¥10,000'},
1619+
{ Price: '¥10,500'},
1620+
{ Price: '¥10'},
1621+
{ Price: 'a'},
1622+
{ Price: 'z'},
1623+
{ Price: '¥2'},
1624+
{ Price: '¥.5'},
1625+
{ Price: '¥0.60'},
1626+
{ Price: '.1'},
1627+
]}
1628+
columns={[{ key: 'Price', sortable: Reactable.Sort.Currency }]} />,
1629+
ReactableTestUtils.testNode()
1630+
);
1631+
});
1632+
1633+
after(function() {
1634+
ReactableTestUtils.resetTestEnvironment();
1635+
});
1636+
1637+
it('sorts columns numerically. parsing out currency symbols', function(){
1638+
var sortHeader = $('#table thead tr.reactable-column-header th')[0];
1639+
ReactTestUtils.Simulate.click(sortHeader);
1640+
1641+
ReactableTestUtils.expectRowText(0, ['.1']);
1642+
ReactableTestUtils.expectRowText(1, ['¥.5']);
1643+
ReactableTestUtils.expectRowText(2, ['¥0.60']);
1644+
ReactableTestUtils.expectRowText(3, ['1']);
1645+
ReactableTestUtils.expectRowText(4, ['¥1.01']);
1646+
ReactableTestUtils.expectRowText(5, ['1.25']);
1647+
ReactableTestUtils.expectRowText(6, ['¥2']);
1648+
ReactableTestUtils.expectRowText(7, ['¥10']);
1649+
ReactableTestUtils.expectRowText(8, ['¥10,000']);
1650+
ReactableTestUtils.expectRowText(9, ['¥10,500']);
1651+
ReactableTestUtils.expectRowText(10, ['a']);
1652+
ReactableTestUtils.expectRowText(11, ['z']);
1653+
});
1654+
});
1655+
16111656
describe('date sort', function(){
16121657
before(function() {
16131658
ReactDOM.render(

0 commit comments

Comments
 (0)
0