Skip to content

Commit

Permalink
Refactor StringUtilities.matchFirstOrNull() => matchFirst(), format c…
Browse files Browse the repository at this point in the history
…odes
  • Loading branch information
imWildCat committed Jul 27, 2016
1 parent cc395c4 commit c81f25a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/components/common/TopicList/TopicListRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TopicListRow extends Component {
let nodeView = null;
if (!isNode) {
nodeView = <View style={styles.nodeView}>
<PointSeparator style={styles.pointSeparator}/>
<PointSeparator style={styles.pointSeparator} />
<View style={styles.nodeNameWrapper}>
<Text style={styles.nodeName}>{nodeName}</Text>
</View>
Expand All @@ -23,19 +23,19 @@ class TopicListRow extends Component {
<TouchableHighlight underlayColor="#E6E6E6" onPress={() => onRowPress(id)} style={{ overflow: 'hidden' }}>
<View style={styles.rowContainer}>
<View style={styles.rowInnerContainer}>
<Image style={styles.avatarImage} source={{ uri: `https:${authorAvatarURI}` }}/>
<Image style={styles.avatarImage} source={{ uri: `https:${authorAvatarURI}` }} />
<View style={styles.infoContainer}>
<Text style={styles.titleText}>{title}</Text>
<View style={styles.metaContainer}>
<Text style={styles.authorName}>{this.renderAuthorName(authorName)}</Text>
{nodeView}
<View style={styles.replyCountLayout}>
<ReplyCount count={replyCount}/>
<ReplyCount count={replyCount} />
</View>
</View>
</View>
</View>
<View style={styles.rowSeparator}/>
<View style={styles.rowSeparator} />
</View>
</TouchableHighlight>
);
Expand Down
10 changes: 5 additions & 5 deletions src/components/common/TopicList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TopicListPage extends Component {
var element = topicElements[index];
const e = $(element);

const id = Number(StringUtilities.matchFirstOrNull(e.find('.item_title a').attr('href'), /t\/(\d+)/));
const id = Number(StringUtilities.matchFirst(e.find('.item_title a').attr('href'), /t\/(\d+)/));
const title = e.find('.item_title a').text();
const nodeName = e.find('a.node').text();
const nodeURI = e.find('a.node').attr('href');
Expand Down Expand Up @@ -108,10 +108,10 @@ class TopicListPage extends Component {
return 0;
} else {
const cleanTime = time.replace(/ /g, '');
days = Number(StringUtilities.matchFirstOrNull(cleanTime, /(\d+)天/));
hours = Number(StringUtilities.matchFirstOrNull(cleanTime, /(\d+)小时/));
minutes = Number(StringUtilities.matchFirstOrNull(cleanTime, /(\d+)分钟/));
seconds = Number(StringUtilities.matchFirstOrNull(cleanTime, /(\d+)秒/));
days = Number(StringUtilities.matchFirst(cleanTime, /(\d+)天/));
hours = Number(StringUtilities.matchFirst(cleanTime, /(\d+)小时/));
minutes = Number(StringUtilities.matchFirst(cleanTime, /(\d+)分钟/));
seconds = Number(StringUtilities.matchFirst(cleanTime, /(\d+)秒/));
}

const m = moment().subtract({ days, hours, minutes, seconds });
Expand Down
6 changes: 3 additions & 3 deletions src/components/pages/Topic/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ class Post extends Component {
_onLinkPress(link) {
const { onUserPress } = this.props;
if (link.startsWith('/member/')) {
const username = StringUtilities.matchFirstOrNull(link, /\/member\/(\w+)/);
const username = StringUtilities.matchFirst(link, /\/member\/(\w+)/);
onUserPress(username);
} else if (link.startsWith('/t/')) {
const topicID = StringUtilities.matchFirstOrNull(link, /\/t\/(\d+)/);
const topicID = StringUtilities.matchFirst(link, /\/t\/(\d+)/);
console.log({ topicID, });
} else if (link.startsWith('/go/')) {
const nodeSlug = StringUtilities.matchFirstOrNull(link, /\/go\/(\w+)/);
const nodeSlug = StringUtilities.matchFirst(link, /\/go\/(\w+)/);
console.log({ nodeSlug });
} else if (link.startsWith('https://') || link.startsWith('http://')) {
const outerLink = link;
Expand Down
16 changes: 8 additions & 8 deletions src/components/pages/Topic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ class TopicPage extends Component {
// Parse topic
const _topicMetaElement = $($('#Main div.box .header'));
const title = _topicMetaElement.find('h1').text();
const topicID = Number(StringUtilities.matchFirstOrNull(_topicMetaElement.find('div.votes').attr('id'), /topic_(\d+)_votes/));
const replyCount = Number(StringUtilities.matchFirstOrNull($('#Main div.box .cell span.gray').text(), /(\d+) 回复/));
const topicID = Number(StringUtilities.matchFirst(_topicMetaElement.find('div.votes').attr('id'), /topic_(\d+)_votes/));
const replyCount = Number(StringUtilities.matchFirst($('#Main div.box .cell span.gray').text(), /(\d+) 回复/));
const topicContent = $('#Main .box .topic_content').html();
// const postscript = $('#Main .box .subtle').html();
// TODO: Find a better regex expression for Chinese
const topicTime = StringUtilities.betterV2TimeString(StringUtilities.matchFirstOrNull(_topicMetaElement.find('small.gray').text(), / · ([a-zA-Z0-9 \u4E00-\u9FA5\uF900-\uFA2D]+) ·/));
const topicTime = StringUtilities.betterV2TimeString(StringUtilities.matchFirst(_topicMetaElement.find('small.gray').text(), / · ([a-zA-Z0-9 \u4E00-\u9FA5\uF900-\uFA2D]+) ·/));

// TODO: Implement appreciation count and favourite count for topic:
// const _topicOtherInfoElementText = $('#Main .box .topic_buttons .fr').text();
// const appreciationCount = Number(StringUtilities.matchFirstOrNull(_topicOtherInfoElementText, /(\d+) 人感谢/));
// const favoriteCount = Number(StringUtilities.matchFirstOrNull(_topicOtherInfoElementText, /(\d+) 人收藏/));
// const appreciationCount = Number(StringUtilities.matchFirst(_topicOtherInfoElementText, /(\d+) 人感谢/));
// const favoriteCount = Number(StringUtilities.matchFirst(_topicOtherInfoElementText, /(\d+) 人收藏/));

// Parse author of topic
const authorName = _topicMetaElement.find('small.gray > a').text();
Expand All @@ -91,7 +91,7 @@ class TopicPage extends Component {
// Parse node
const _nodeElement = $('#Main .box .header > a:nth-child(4)');
const nodeName = _nodeElement.text();
const nodeSlug = StringUtilities.matchFirstOrNull(_nodeElement.attr('href'), /\/go\/(\w{1,31})/);
const nodeSlug = StringUtilities.matchFirst(_nodeElement.attr('href'), /\/go\/(\w{1,31})/);

const topic = {
id: topicID,
Expand All @@ -117,7 +117,7 @@ class TopicPage extends Component {
const _element = $(_replyElements[index]);

// console.log(_element.attr('class'), _element.hasClass('normalUser'));
const replyID = Number(StringUtilities.matchFirstOrNull(_element.attr('id'), /r_(\d{1,15})/));
const replyID = Number(StringUtilities.matchFirst(_element.attr('id'), /r_(\d{1,15})/));
if (!replyID) {
// If there is no replyID, it might be a pagination element
continue;
Expand All @@ -128,7 +128,7 @@ class TopicPage extends Component {

const time = StringUtilities.betterV2TimeString(_element.find('span[class="fade small"]').text());
const floor = Number(_element.find('span.no').text());
const replyAppreciationCount = Number(StringUtilities.matchFirstOrNull(_element.find('span[class="small fade"]').text(), /♥ (\d+)/));
const replyAppreciationCount = Number(StringUtilities.matchFirst(_element.find('span[class="small fade"]').text(), /♥ (\d+)/));

posts.push({
id: replyID, content: replyContent, authorAvatarURI: replyAuthorAvatarURI,
Expand Down
6 changes: 3 additions & 3 deletions src/components/pages/User/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ class UserPage extends Component {
}
const avatarURI = $('#Main .box .cell table tr td img.avatar').attr('src');
const _numberAndDateElementText = $('#Main .fa-time').parent().text();
const num = Number(StringUtilities.matchFirstOrNull(_numberAndDateElementText, /第 (\d+) 号会员/));
let regDate = StringUtilities.matchFirstOrNull(_numberAndDateElementText, /(\d{4}-\d{2}-\d{2})/);
const num = Number(StringUtilities.matchFirst(_numberAndDateElementText, /第 (\d+) 号会员/));
let regDate = StringUtilities.matchFirst(_numberAndDateElementText, /(\d{4}-\d{2}-\d{2})/);
if (regDate) {
regDate = regDate.replace(/(\-0)|(\-)/g, '.');
}
const liveness = Number(StringUtilities.matchFirstOrNull(_numberAndDateElementText, /今日活跃度排名 (\d+)/));
const liveness = Number(StringUtilities.matchFirst(_numberAndDateElementText, /今日活跃度排名 (\d+)/));

// Social
const getSocialElement = (socialMediaName) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/pages/UserTopic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ class UserTopicPage extends Component {
for (var i = 0; i < _topicElements.length; i++) {
const _element = $(_topicElements[i]);
const _titleElement = _element.find('.item_title a');
const topicID = Number(StringUtilities.matchFirstOrNull(
const topicID = Number(StringUtilities.matchFirst(
_titleElement.attr('href'),
/\/t\/(\d+)/
));
const title = _titleElement.text();
const nodeName = _element.find('a.node').text();
const time = StringUtilities.matchFirstOrNull(
const time = StringUtilities.matchFirst(
_element.find('.small.fade').text(),
/&nbsp;•&nbsp; ([0-9 \u4e00-\u9fa5]+)前 &nbsp;/
);
Expand Down

0 comments on commit c81f25a

Please sign in to comment.