This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
74 lines (57 loc) · 1.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var XRegExp = require('xregexp').XRegExp;
var hashtagExp = XRegExp('#[\\p{L}\\d]+', 'i');
function FindHashtags() {
this._nextMatchPosition = 0;
this._matches = [];
this._currentMatch = null;
this._currentMatchText = null;
this._content = null;
}
FindHashtags.prototype.setContent = function (content) {
this._content = content;
};
FindHashtags.prototype.getHashtags = function () {
this._loopMatches();
return this._getMatches();
};
FindHashtags.prototype._loopMatches = function () {
this._findNextMatch();
while (this._foundMatch()) {
this._setCurrentMatchText();
if (this._currentMatchIsUnique()) {
this._addCurrentResultToMatches();
}
this._incrementNextMatchPosition();
this._findNextMatch();
}
};
FindHashtags.prototype._findNextMatch = function () {
this._currentMatch = XRegExp.exec(this._content, hashtagExp, this._nextMatchPosition);
};
FindHashtags.prototype._foundMatch = function () {
return this._currentMatch === null ? false : true;
};
FindHashtags.prototype._setCurrentMatchText = function () {
this._currentMatchText = this._formatMatchText(this._currentMatch[0]);
};
FindHashtags.prototype._formatMatchText = function (match) {
return match.substring(1).toLowerCase();
};
FindHashtags.prototype._currentMatchIsUnique = function () {
return this._matches.indexOf(this._currentMatchText) === -1;
};
FindHashtags.prototype._addCurrentResultToMatches = function () {
this._matches.push(this._currentMatchText);
};
FindHashtags.prototype._incrementNextMatchPosition = function () {
this._nextMatchPosition = this._currentMatch.index + this._currentMatch[0].length;
};
FindHashtags.prototype._getMatches = function () {
return this._matches.length === 0 ? [] : this._matches;
};
function findHashtags(content) {
var tags = new FindHashtags();
tags.setContent(content);
return tags.getHashtags();
}
module.exports = findHashtags;