sharkey/src/web/app/common/scripts/parse-search-query.ts

48 lines
1.0 KiB
TypeScript
Raw Normal View History

2017-12-21 04:01:44 +09:00
export default function(qs: string) {
const q = {
text: ''
};
qs.split(' ').forEach(x => {
if (/^([a-z_]+?):(.+?)$/.test(x)) {
const [key, value] = x.split(':');
switch (key) {
case 'user':
q['username'] = value;
break;
2017-12-21 06:31:56 +09:00
case 'follow':
q['following'] = value == 'null' ? null : value == 'true';
break;
2017-12-21 04:01:44 +09:00
case 'reply':
2017-12-21 07:35:16 +09:00
q['reply'] = value == 'null' ? null : value == 'true';
break;
case 'repost':
q['repost'] = value == 'null' ? null : value == 'true';
2017-12-21 04:01:44 +09:00
break;
case 'media':
2017-12-21 07:35:16 +09:00
q['media'] = value == 'null' ? null : value == 'true';
2017-12-21 04:01:44 +09:00
break;
case 'until':
case 'since':
// YYYY-MM-DD
if (/^[0-9]+\-[0-9]+\-[0-9]+$/) {
const [yyyy, mm, dd] = value.split('-');
q[`${key}_date`] = (new Date(parseInt(yyyy, 10), parseInt(mm, 10) - 1, parseInt(dd, 10))).getTime();
}
break;
default:
q[key] = value;
break;
}
} else {
q.text += x + ' ';
}
});
if (q.text) {
q.text = q.text.trim();
}
return q;
}