From 46fbedf0ad7b167a28daf2030e06f34480576394 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sun, 3 Jun 2018 02:25:34 +0200 Subject: add results page --- app/client/util/sort.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 app/client/util/sort.js (limited to 'app/client/util/sort.js') 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 } +} -- cgit v1.2.3-70-g09d2