aboutsummaryrefslogtreecommitdiff
path: root/src/lib/utils.ts
blob: c2e4b86698540195c612f692992c46e961ef6476 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
export const colorCodes: { [key: string]: string } = {
    '0': '#000000', // black
    '1': '#0000be', // blue
    '2': '#00be00', // green
    '3': '#00bebe', // cyan
    '4': '#be0000', // red
    '5': '#be00be', // magenta
    '6': '#ffaa00', // gold
    '7': '#bebebe', // light gray
    '8': '#3f3f3f', // dark gray
    '9': '#3f3ffe', // light blue
    'a': '#3ffe3f', // light green
    'b': '#3ffefe', // light cyan
    'c': '#fe3f3f', // light red
    'd': '#fe3ffe', // light magenta
    'e': '#fefe3f', // yellow
    'f': '#ffffff', // white
}

export const TIER_COLORS = {
    COMMON: 'f',
    UNCOMMON: 'a',
    RARE: '9',
    EPIC: '5',
    LEGENDARY: '6',
    MYTHIC: 'e',
    DIVINE: 'b',
    SPECIAL: 'c',
    VERY_SPECIAL: 'c',
}

const specialCodes: { [key: string]: string } = {
    'l': 'font-weight: bold'
}

export const colorCodeCharacter = '§'

export function formattingCodeToHtml(formatted: string): string {
    let htmlOutput = ''
    // we store the hex code, not the formatting code
    let currentColor: null | string = null
    // we store the css code, not the formatting code
    const activeSpecialCodes: string[] = []
    function reset() {
        if (currentColor) {
            htmlOutput += '</span>'
            currentColor = null
        }
        while (activeSpecialCodes.pop()) {
            htmlOutput += '</span>'
        }
    }
    while (formatted.length > 0) {
        const character = formatted[0]
        formatted = formatted.slice(1)
        // if it encounters § (or whatever colorCodeCharacter is), then read the next character
        if (character === colorCodeCharacter) {
            const colorCharacter = formatted[0]
            formatted = formatted.slice(1)
            if (colorCodes[colorCharacter]) {
                if (currentColor !== colorCodes[colorCharacter]) { // make sure the color is different than the active one
                    // if there's already a color, close that tag
                    if (currentColor) htmlOutput += '</span>'
                    currentColor = colorCodes[colorCharacter]
                    htmlOutput += `<span style="color:${currentColor}">`
                }
            } else if (specialCodes[colorCharacter]) {
                if (!activeSpecialCodes.includes(specialCodes[colorCharacter])) {
                    activeSpecialCodes.push(specialCodes[colorCharacter])
                    htmlOutput += `<span style="${specialCodes[colorCharacter]}">`
                }
            } else if (colorCharacter === 'r') {
                reset()
            }
        } else if (character === '\n') {
            htmlOutput += '<br>'
        } else {
            // no xss!
            htmlOutput += character.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
        }
    }
    reset()
    return htmlOutput
}

// we store the regex here so we don't have to build the object every time
const formattingCodeRegex = new RegExp(colorCodeCharacter + '.', 'g')
export function removeFormattingCode(formatted: string): string {
    return formatted.replace(formattingCodeRegex, '')
}

function moveToEndOfId(word: string, thing: string) {
    if (thing.startsWith(`${word}_`))
        thing = thing.slice(`${word}_`.length) + `_${word}`
    return thing
}

interface MillisecondsToTimeOpts {
    parts?: number
    /**
     * 0: All units
     * 1: Don't show milliseconds
     * 2: Don't show seconds
     * 3: Don't show minutes
     * 4: Don't show hours
     * 5: Don't show days
     */
    smallestUnit?: number
}

export function millisecondsToTime(totalMilliseconds: number, opts: MillisecondsToTimeOpts = {}) {
    if (totalMilliseconds < 0) {
        return 'Invalid time'
    }
    const totalSeconds = totalMilliseconds / 1000
    const totalMinutes = totalSeconds / 60
    const totalHours = totalMinutes / 60
    const totalDays = totalHours / 24
    const milliseconds = Math.floor(totalMilliseconds) % 1000

    const seconds = Math.floor(totalSeconds) % 60
    const minutes = Math.floor(totalMinutes) % 60
    const hours = Math.floor(totalHours) % 24
    const days = Math.floor(totalDays)

    const stringUnits: string[] = []

    const smallestUnit = opts.smallestUnit ?? 0

    if (totalDays > 1 && smallestUnit <= 4) stringUnits.push(days === 1 ? `${days} day` : `${days} days`)
    if (totalHours > 1 && smallestUnit <= 3) stringUnits.push(hours === 1 ? `${hours} hour` : `${hours} hours`)
    if (totalMinutes > 1 && smallestUnit <= 2) stringUnits.push(minutes === 1 ? `${minutes} minute` : `${minutes} minutes`)
    if (totalSeconds > 1 && smallestUnit <= 1) stringUnits.push(seconds === 1 ? `${seconds} second` : `${seconds} seconds`)
    if (totalMilliseconds > 0 && smallestUnit <= 0) stringUnits.push(`${milliseconds} ms`)

    // comma separated, "and" before last

    let partsCount = opts.parts ?? 2
    if (stringUnits.length < partsCount) partsCount = stringUnits.length
    if (partsCount === 1) return stringUnits[0]
    return stringUnits.slice(0, partsCount - 1).join(', ') + ' and ' + stringUnits[partsCount - 1]
}

export function cleanId(id: string) {
    for (const string of ['deaths', 'kills', 'collection', 'skill'])
        id = moveToEndOfId(string, id)
    if (id.startsWith('harp_') && id.endsWith('_completions'))
        id = id.slice('harp_'.length)

    return id
        .replace(/^./, id[0].toUpperCase())
        .replace(/_/g, ' ')
}

export function toTitleCase(s: string) {
    return s.replace(
        /\w\S*/g,
        w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()
    )
}

export function toRomanNumerals(number: number) {
    return ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX'][number]
}

export function shuffle<T>(a: T[]): T[] {
    for (let i = a.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1))
            ;[a[i], a[j]] = [a[j], a[i]]
    }
    return a
}

const emojiRegex = /(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/g


export function twemojiHtml(s: string) {
    const htmlEncoded = s.replace('<', '&lt;').replace('>', '&gt;').replace('&', '&amp;')
    // replace unicode emojis with <img src="/emoji/[hex].svg">
    const asTwemoji = htmlEncoded.replace(emojiRegex, (match) => {
        return `<img src="/emoji/${[...match].map(p => p.codePointAt(0)!.toString(16)).join('-')}.svg" class="emoji" alt="${match}">`
    })
    return asTwemoji
}

export function formatNumber(n: number, digits = 3) {
    // from https://stackoverflow.com/a/9462382 with some modifications
    const numberSymbolsLookup = [
        { value: 1, symbol: '' },
        { value: 1e3, symbol: 'k' },
        { value: 1e6, symbol: 'M' },
        { value: 1e9, symbol: 'G' },
        { value: 1e12, symbol: 'T' },
        { value: 1e15, symbol: 'P' },
        { value: 1e18, symbol: 'E' },
    ]
    const item = numberSymbolsLookup.slice().reverse().find(item => n >= item.value)
    return (n / (item?.value ?? 1)).toPrecision(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + (item?.symbol ?? '')
}

export function formatNumberFromUnit(n: number, unit: null | 'date' | 'time' | string) {
    switch (unit) {
        case null:
            return n.toLocaleString()
        case 'date':
            return (new Date(n)).toUTCString()
        case 'time':
            return millisecondsToTime(Math.abs(n))
        default:
            return `${n.toLocaleString()} ${unit}`
    }
}

/** Get the milliseconds since epoch for a given SkyBlock date. The year, month, and day are 1 indexed. */
export function skyblockTime(year: number, month = 1, day = 1) {
    const sbEpoch = 1560275700000
    let time = sbEpoch
    if (year) time += 446400000 * (year)
    if (month) time += 37200000 * (month - 1)
    if (day) time += 1200000 * (day - 1)
    return time
}

export interface PreviewedAuctionData {
    pageX: number
    pageY: number
    uuid: string
}