1 import { UserOperation, Comment } from './interfaces';
2 import * as markdown_it from 'markdown-it';
4 export let repoUrl = 'https://github.com/dessalines/lemmy';
6 export function msgOp(msg: any): UserOperation {
7 let opStr: string = msg.op;
8 return UserOperation[opStr];
11 var md = new markdown_it({
17 export function hotRank(comment: Comment): number {
18 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
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;
24 let rank = (10000 * Math.sign(comment.score) * Math.log10(1 + Math.abs(comment.score))) / Math.pow(hoursElapsed + 2, 1.8);
26 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
31 export function mdToHtml(text: string) {
32 return {__html: md.render(text)};
35 export function getUnixTime(text: string): number {
36 return text ? new Date(text).getTime()/1000 : undefined;
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}});