]> Untitled Git - lemmy.git/blob - ui/src/utils.ts
Merge branch 'moderation'
[lemmy.git] / ui / src / utils.ts
1 import { UserOperation, Comment } from './interfaces';
2 import * as markdown_it from 'markdown-it';
3
4 export let repoUrl = 'https://github.com/dessalines/lemmy';
5
6 export function msgOp(msg: any): UserOperation {
7   let opStr: string = msg.op;
8   return UserOperation[opStr];
9 }
10
11 var md = new markdown_it({
12   html: true,
13   linkify: true,
14   typographer: true
15 });
16
17 export function hotRank(comment: Comment): number {
18   // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
19
20   let date: Date = new Date(comment.published + 'Z'); // Add Z to convert from UTC date
21   let now: Date = new Date();
22   let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
23
24   let rank = (10000 * Math.sign(comment.score) * Math.log10(1 + Math.abs(comment.score))) / Math.pow(hoursElapsed + 2, 1.8);
25
26   // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
27
28   return rank;
29 }
30
31 export function mdToHtml(text: string) {
32   return {__html: md.render(text)};
33 }
34
35 export function getUnixTime(text: string): number { 
36   return text ? new Date(text).getTime()/1000 : undefined;
37 }
38
39 export function addTypeInfo<T>(arr: Array<T>, name: string): Array<{type_: string, data: T}> {  
40   return arr.map(e => {return {type_: name, data: e}});
41 }