You've already forked tf2wikipricing
refactor: moved components to separate modules
`content.ts` is now half less than the size, exported functions can be unit tested
This commit is contained in:
9
src/content/utils/dom.ts
Normal file
9
src/content/utils/dom.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export function findFirstElement(selector: string): HTMLElement | null {
|
||||
const elements = document.querySelectorAll(selector);
|
||||
return elements.length > 0 ? elements[0] as HTMLElement : null;
|
||||
}
|
||||
|
||||
export function findFirstChildElement(selector: string, root: Element): HTMLElement | null {
|
||||
const elements = root.querySelectorAll(selector);
|
||||
return elements.length > 0 ? elements[0] as HTMLElement : null;
|
||||
}
|
||||
28
src/content/utils/formatting.ts
Normal file
28
src/content/utils/formatting.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { conversion_ref_usd } from '../config';
|
||||
import { $T } from './localization'
|
||||
|
||||
function toFixed(num: number, fixed: number) {
|
||||
var re = new RegExp('^-?\\d+(?:\.\\d{0,' + (fixed || -1) + '})?');
|
||||
return num.toString().match(re)[0];
|
||||
}
|
||||
|
||||
export function formatPrice(keys: number, metal: number, keyPrice: number, locale: string = 'en') {
|
||||
const pureMetal = (keys * keyPrice) + metal;
|
||||
const formattedKeys = +(keys + (metal / keyPrice)).toFixed(2)
|
||||
|
||||
var output: string = ''
|
||||
if(keys > 0) {
|
||||
output += (formattedKeys == 1.0 ? $T("%@ key") : $T("%@ keys")).replace('%@', formattedKeys.toLocaleString(locale))
|
||||
} else {
|
||||
output += `${(+toFixed(metal, 2)).toLocaleString(locale)} ref`
|
||||
}
|
||||
const currencyFormatter = new Intl.NumberFormat(locale, {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
});
|
||||
|
||||
// Round price up to nearest cent
|
||||
const price = Math.ceil(pureMetal * conversion_ref_usd * 100) / 100
|
||||
output += ` (US$${currencyFormatter.format(price)})`
|
||||
return output;
|
||||
}
|
||||
40
src/content/utils/localization.ts
Normal file
40
src/content/utils/localization.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
const localizations: {[lang: string]: any} = {
|
||||
'en': require('../../strings/en'), // English
|
||||
'es': require('../../strings/es'), // Spanish
|
||||
// 'ja': require('../../strings/ja'), // Japanese
|
||||
// 'it': require('../../strings/it'), // Italian
|
||||
// 'ar': require('../../strings/ar.json') as object, // Arabic
|
||||
// 'cs': require('../../strings/cs.json') as object, // Czech
|
||||
// 'da': require('../../strings/da.json') as object, // Danish
|
||||
// 'de': require('../../strings/de.json') as object, // German
|
||||
// 'fi': require('../../strings/fi.json') as object, // Finnish
|
||||
// 'fr': require('../../strings/fr.json') as object, // French
|
||||
// 'hu': require('../../strings/hu.json') as object, // Hungarian
|
||||
// 'ko': require('../../strings/ko.json') as object, // Korean
|
||||
// 'nl': require('../../strings/nl.json') as object, // Dutch
|
||||
// 'no': require('../../strings/no.json') as object, // Norwegian Bokmål
|
||||
// 'pl': require('../../strings/pl.json') as object, // Polish
|
||||
// 'pt': require('../../strings/pt.json') as object, // Portuguese
|
||||
// 'pt-BR': require('../../strings/pt-BR.json') as object, // Brazilian Portuguese
|
||||
// 'ro': require('../../strings/ro.json') as object, // Romanian
|
||||
// 'ru': require('../../strings/ru.json') as object, // Russian
|
||||
// 'sv': require('../../strings/sv.json') as object, // Swedish
|
||||
// 'tr': require('../../strings/tr.json') as object, // Turkish
|
||||
// 'zh-Hans': require('../../strings/zh-Hans.json') as object, // Simplified Chinese
|
||||
// 'zh-Hant': require('../../strings/zh-Hant.json') as object, // Traditional Chinese
|
||||
}
|
||||
|
||||
export function $T(s: string, locale?: Intl.LocalesArgument): string {
|
||||
const code = locale ? locale.toString() : extractLocaleFromURL(document.URL)
|
||||
return localizations.hasOwnProperty(code) ? (localizations[code as unknown as keyof object])[s] || s : s;
|
||||
}
|
||||
|
||||
export function extractLocaleFromURL(url: string): string {
|
||||
var split = url.substring(url.indexOf("/wiki/") + "/wiki/".length);
|
||||
if (split.indexOf('/') != -1) {
|
||||
// Remove language suffix e.g. `/es`
|
||||
return split.substring(split.indexOf('/') + 1);
|
||||
} else {
|
||||
return 'en';
|
||||
}
|
||||
}
|
||||
20
src/content/utils/log.ts
Normal file
20
src/content/utils/log.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
declare var __PRODUCTION: boolean;
|
||||
declare var __EXTENSION_NAME: string;
|
||||
const logHeader = `[${__EXTENSION_NAME}] `;
|
||||
|
||||
/** `console.debug` with header; automatically NO-OP on production build */
|
||||
function logDebug(message?: any, ...optionalParams: any[]): void {
|
||||
if(process.env.NODE_ENV !== 'production') console.debug(logHeader + message, optionalParams);
|
||||
}
|
||||
|
||||
/** `console.log` with header */
|
||||
function log(message?: any, ...optionalParams: any[]): void {
|
||||
console.log(logHeader + message, optionalParams)
|
||||
}
|
||||
|
||||
/** `console.error` with header */
|
||||
function logError(message?: any, ...optionalParams: any[]): void {
|
||||
console.error(logHeader + message, optionalParams)
|
||||
}
|
||||
|
||||
export { log, logDebug, logError }
|
||||
Reference in New Issue
Block a user