yumechi-no-kuni/src/client/app/common/scripts/parse-search-query.ts

54 lines
1.2 KiB
TypeScript
Raw Normal View History

2017-12-20 13:01:44 -06: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':
2018-03-29 00:48:47 -05:00
q['includeUserUsernames'] = value.split(',');
2017-12-22 16:21:52 -06:00
break;
case 'exclude_user':
2018-03-29 00:48:47 -05:00
q['excludeUserUsernames'] = value.split(',');
2017-12-20 13:01:44 -06:00
break;
2017-12-20 15:31:56 -06:00
case 'follow':
q['following'] = value == 'null' ? null : value == 'true';
break;
2017-12-20 13:01:44 -06:00
case 'reply':
2017-12-20 16:35:16 -06:00
q['reply'] = value == 'null' ? null : value == 'true';
break;
2018-04-07 12:30:37 -05:00
case 'renote':
q['renote'] = value == 'null' ? null : value == 'true';
2017-12-20 13:01:44 -06:00
break;
case 'media':
2017-12-20 16:35:16 -06:00
q['media'] = value == 'null' ? null : value == 'true';
2017-12-20 13:01:44 -06:00
break;
2017-12-20 16:57:31 -06:00
case 'poll':
q['poll'] = value == 'null' ? null : value == 'true';
break;
2017-12-20 13:01:44 -06:00
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;
}