compiler/utils/util.ts
Florian Bouillon ddc8916d08
Added Support for legal file (#13)
* Added Support for legal file

Signed-off-by: Avior <florian.bouillon@delta-wings.net>

* Fixed bug

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2021-06-18 23:35:14 +02:00

60 lines
1.5 KiB
TypeScript

import { Card, Set } from 'db/interfaces'
import glob from 'glob'
import fetch from 'node-fetch'
import * as legals from '../db/legals'
export function urlize(str: string): string {
return str.replace('?', '%3F').normalize('NFC').replace(/["'\u0300-\u036f]/g, "")
}
interface fileCacheInterface {
[key: string]: any
}
const fileCache: fileCacheInterface = {}
export async function fetchRemoteFile<T = any>(url: string): Promise<T> {
if (!fileCache[url]) {
const resp = await fetch(url, {
timeout: 60 * 1000
})
fileCache[url] = resp.json()
}
return fileCache[url]
}
const globCache: Record<string, Array<string>> = {}
export async function smartGlob(query: string) {
if (!globCache[query]) {
globCache[query] = await new Promise((res) => glob(query, (err, matches) => res(matches)))
}
return globCache[query]
}
export function cardIsLegal(type: 'standard' | 'expanded', card: Card, localId: string) {
const legal = legals[type]
if (
legal.includes.series.includes(card.set.serie.id) ||
legal.includes.sets.includes(card.set.id) ||
(card.regulationMark && legal.includes.regulationMark.includes(card.regulationMark))
) {
return !(
legal.excludes.sets.includes(card.set.id) ||
legal.excludes.cards.includes(`${card.set.id}-${localId}`)
)
}
return false
}
export function setIsLegal(type: 'standard' | 'expanded', set: Set) {
const legal = legals[type]
if (
legal.includes.series.includes(set.serie.id) ||
legal.includes.sets.includes(set.id)
) {
return !legal.excludes.sets.includes(set.id)
}
return false
}