From eaf0d5e637e0fd5be62b7ccf940ba1bcebeba786 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 21 Dec 2017 04:01:44 +0900 Subject: #1017 #155 --- src/web/app/common/scripts/parse-search-query.ts | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/web/app/common/scripts/parse-search-query.ts (limited to 'src/web/app/common/scripts') diff --git a/src/web/app/common/scripts/parse-search-query.ts b/src/web/app/common/scripts/parse-search-query.ts new file mode 100644 index 0000000000..adcbfbb8fe --- /dev/null +++ b/src/web/app/common/scripts/parse-search-query.ts @@ -0,0 +1,41 @@ +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; + case 'reply': + q['include_replies'] = value == 'true'; + break; + case 'media': + q['with_media'] = value == 'true'; + 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; +} -- cgit v1.2.3-freya