Add unique function (#2644)

This commit is contained in:
Aya Morisawa 2018-09-07 00:10:03 +09:00 committed by syuilo
parent e34b264af2
commit ff0a05a2d6
2 changed files with 10 additions and 6 deletions

View file

@ -17,3 +17,7 @@ export function intersperse<T>(sep: T, xs: T[]): T[] {
export function erase<T>(x: T, xs: T[]): T[] { export function erase<T>(x: T, xs: T[]): T[] {
return xs.filter(y => x !== y); return xs.filter(y => x !== y);
} }
export function unique<T>(xs: T[]): T[] {
return [...new Set(xs)];
}

View file

@ -24,7 +24,7 @@ import isQuote from '../../misc/is-quote';
import { TextElementMention } from '../../mfm/parse/elements/mention'; import { TextElementMention } from '../../mfm/parse/elements/mention';
import { TextElementHashtag } from '../../mfm/parse/elements/hashtag'; import { TextElementHashtag } from '../../mfm/parse/elements/hashtag';
import { updateNoteStats } from '../update-chart'; import { updateNoteStats } from '../update-chart';
import { erase } from '../../prelude/array'; import { erase, unique } from '../../prelude/array';
type NotificationType = 'reply' | 'renote' | 'quote' | 'mention'; type NotificationType = 'reply' | 'renote' | 'quote' | 'mention';
@ -385,7 +385,7 @@ function extractHashtags(tokens: ReturnType<typeof parse>): string[] {
.map(t => (t as TextElementHashtag).hashtag) .map(t => (t as TextElementHashtag).hashtag)
.filter(tag => tag.length <= 100); .filter(tag => tag.length <= 100);
return [...new Set(hashtags)]; return unique(hashtags);
} }
function index(note: INote) { function index(note: INote) {
@ -542,12 +542,12 @@ function incNotesCount(user: IUser) {
async function extractMentionedUsers(tokens: ReturnType<typeof parse>): Promise<IUser[]> { async function extractMentionedUsers(tokens: ReturnType<typeof parse>): Promise<IUser[]> {
if (tokens == null) return []; if (tokens == null) return [];
const mentionTokens = [...new Set( const mentionTokens = unique(
tokens tokens
.filter(t => t.type == 'mention') as TextElementMention[] .filter(t => t.type == 'mention') as TextElementMention[]
)]; );
const mentionedUsers = [...new Set( const mentionedUsers = unique(
erase(null, await Promise.all(mentionTokens.map(async m => { erase(null, await Promise.all(mentionTokens.map(async m => {
try { try {
return await resolveUser(m.username, m.host); return await resolveUser(m.username, m.host);
@ -555,7 +555,7 @@ async function extractMentionedUsers(tokens: ReturnType<typeof parse>): Promise<
return null; return null;
} }
}))) })))
)]; );
return mentionedUsers; return mentionedUsers;
} }