diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-06-03 02:25:34 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-06-03 02:25:34 +0200 |
| commit | 46fbedf0ad7b167a28daf2030e06f34480576394 (patch) | |
| tree | 81877c97f777a8d705d47bb97ac96e5fa244bd2b /app/client/util/sort.js | |
| parent | 2149eb581c35a93d41dbad6e3409c498b4bed804 (diff) | |
add results page
Diffstat (limited to 'app/client/util/sort.js')
| -rw-r--r-- | app/client/util/sort.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/app/client/util/sort.js b/app/client/util/sort.js new file mode 100644 index 0000000..cc1b462 --- /dev/null +++ b/app/client/util/sort.js @@ -0,0 +1,32 @@ +export const numericSort = { + asc: (a,b) => a[0] - b[0], + desc: (a,b) => b[0] - a[0], +} +export const stringSort = { + asc: (a,b) => a[0].localeCompare(b[0]), + desc: (a,b) => b[0].localeCompare(a[0]), +} +export const orderByFn = (s='name asc') => { + const [field='name', direction='asc'] = s.split(' ') + let mapFn, sortFn + switch (field) { + case 'epoch': + mapFn = a => [parseInt(a.epoch || a.epochs) || 0, a] + sortFn = numericSort[direction] + break + case 'size': + mapFn = a => [a.size, a] + sortFn = numericSort[direction] + break + case 'date': + mapFn = a => [+new Date(a.date || a.created_at), a] + sortFn = numericSort[direction] + break + case 'name': + default: + mapFn = a => [a.id || a.name, a] + sortFn = stringSort[direction] + break + } + return { mapFn, sortFn } +} |
