From 1b2d0806511613077e36a707f24cae215bfce03f Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Sat, 11 Feb 2017 02:32:00 +0900
Subject: [PATCH] Refactoring :sparkles:

---
 src/common/text/elements/bold.js    | 21 ++++++++-----------
 src/common/text/elements/code.js    | 23 +++++++++------------
 src/common/text/elements/hashtag.js | 32 +++++++++++++----------------
 src/common/text/elements/mention.js | 21 ++++++++-----------
 src/common/text/elements/url.js     | 19 ++++++++---------
 src/common/text/index.js            |  4 ++--
 6 files changed, 52 insertions(+), 68 deletions(-)

diff --git a/src/common/text/elements/bold.js b/src/common/text/elements/bold.js
index 41a01399dd..ce25764457 100644
--- a/src/common/text/elements/bold.js
+++ b/src/common/text/elements/bold.js
@@ -2,16 +2,13 @@
  * Bold
  */
 
-const regexp = /\*\*(.+?)\*\*/;
-
-module.exports = {
-	test: x => new RegExp('^' + regexp.source).test(x),
-	parse: text => {
-		const bold = text.match(new RegExp('^' + regexp.source))[0];
-		return {
-			type: 'bold',
-			content: bold,
-			bold: bold.substr(2, bold.length - 4)
-		};
-	}
+module.exports = text => {
+	const match = text.match(/^\*\*(.+?)\*\*/);
+	if (!match) return null;
+	const bold = match[0];
+	return {
+		type: 'bold',
+		content: bold,
+		bold: bold.substr(2, bold.length - 4)
+	};
 };
diff --git a/src/common/text/elements/code.js b/src/common/text/elements/code.js
index 0761a4c960..902504c9ec 100644
--- a/src/common/text/elements/code.js
+++ b/src/common/text/elements/code.js
@@ -2,19 +2,16 @@
  * Code
  */
 
-const regexp = /```([\s\S]+?)```/;
-
-module.exports = {
-	test: x => new RegExp('^' + regexp.source).test(x),
-	parse: text => {
-		const code = text.match(new RegExp('^' + regexp.source))[0];
-		return {
-			type: 'code',
-			content: code,
-			code: code.substr(3, code.length - 6).trim(),
-			codeHtml: genHtml(code.substr(3, code.length - 6).trim())
-		};
-	}
+module.exports = text => {
+	const match = text.match(/^```([\s\S]+?)```/);
+	if (!match) return null;
+	const code = match[0];
+	return {
+		type: 'code',
+		content: code,
+		code: code.substr(3, code.length - 6).trim(),
+		codeHtml: genHtml(code.substr(3, code.length - 6).trim())
+	};
 };
 
 function escape(text) {
diff --git a/src/common/text/elements/hashtag.js b/src/common/text/elements/hashtag.js
index f04b782007..048dbd8929 100644
--- a/src/common/text/elements/hashtag.js
+++ b/src/common/text/elements/hashtag.js
@@ -2,22 +2,18 @@
  * Hashtag
  */
 
-module.exports = {
-	test: (x, i) =>
-		/^\s#[^\s]+/.test(x) || (i == 0 && /^#[^\s]+/.test(x))
-	,
-	parse: text => {
-		const isHead = text[0] == '#';
-		const hashtag = text.match(/^\s?#[^\s]+/)[0];
-		const res = !isHead ? [{
-			type: 'text',
-			content: text[0]
-		}] : [];
-		res.push({
-			type: 'hashtag',
-			content: isHead ? hashtag : hashtag.substr(1),
-			hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2)
-		});
-		return res;
-	}
+module.exports = (text, i) => {
+	if (!(/^\s#[^\s]+/.test(text) || (i == 0 && /^#[^\s]+/.test(text)))) return null;
+	const isHead = text[0] == '#';
+	const hashtag = text.match(/^\s?#[^\s]+/)[0];
+	const res = !isHead ? [{
+		type: 'text',
+		content: text[0]
+	}] : [];
+	res.push({
+		type: 'hashtag',
+		content: isHead ? hashtag : hashtag.substr(1),
+		hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2)
+	});
+	return res;
 };
diff --git a/src/common/text/elements/mention.js b/src/common/text/elements/mention.js
index b58786fd1e..e0fac4dd76 100644
--- a/src/common/text/elements/mention.js
+++ b/src/common/text/elements/mention.js
@@ -2,16 +2,13 @@
  * Mention
  */
 
-const regexp = /@[a-zA-Z0-9\-]+/;
-
-module.exports = {
-	test: x => new RegExp('^' + regexp.source).test(x),
-	parse: text => {
-		const mention = text.match(new RegExp('^' + regexp.source))[0];
-		return {
-			type: 'mention',
-			content: mention,
-			username: mention.substr(1)
-		};
-	}
+module.exports = text => {
+	const match = text.match(/^@[a-zA-Z0-9\-]+/);
+	if (!match) return null;
+	const mention = match[0];
+	return {
+		type: 'mention',
+		content: mention,
+		username: mention.substr(1)
+	};
 };
diff --git a/src/common/text/elements/url.js b/src/common/text/elements/url.js
index d02aef0800..f350b707ac 100644
--- a/src/common/text/elements/url.js
+++ b/src/common/text/elements/url.js
@@ -2,15 +2,12 @@
  * URL
  */
 
-const regexp = /https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/;
-
-module.exports = {
-	test: x => new RegExp('^' + regexp.source).test(x),
-	parse: text => {
-		const link = text.match(new RegExp('^' + regexp.source))[0];
-		return {
-			type: 'link',
-			content: link
-		};
-	}
+module.exports = text => {
+	const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/); 
+	if (!match) return null;
+	const link = match[0];
+	return {
+		type: 'link',
+		content: link
+	};
 };
diff --git a/src/common/text/index.js b/src/common/text/index.js
index 636f0c4681..d0ef8674b0 100644
--- a/src/common/text/index.js
+++ b/src/common/text/index.js
@@ -30,8 +30,8 @@ function analyze(source) {
 	// パース
 	while (source != '') {
 		const parsed = elements.some(el => {
-			if (el.test(source, i)) {
-				let tokens = el.parse(source);
+			let tokens = el(source, i);
+			if (tokens) {
 				if (!Array.isArray(tokens)) {
 					tokens = [tokens];
 				}