2022-02-13 18:34:03 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022 Johannes Loher
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2023-07-10 22:23:13 +02:00
|
|
|
import en from '../lang/en.json';
|
2022-02-13 18:34:03 +01:00
|
|
|
|
2022-02-13 18:35:15 +01:00
|
|
|
function setupPrimitives() {
|
2023-07-10 22:23:13 +02:00
|
|
|
Object.defineProperties(Number, {
|
|
|
|
isNumeric: {
|
|
|
|
value: function (n: unknown) {
|
|
|
|
if (n instanceof Array) return false;
|
|
|
|
else if (([null, ''] as unknown[]).includes(n)) return false;
|
|
|
|
// @ts-expect-error Abusing JavaScript a bit here, but it's the implementation from foundry
|
|
|
|
return +n === +n;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fromString: {
|
|
|
|
value: function (str: unknown) {
|
|
|
|
if (typeof str !== 'string' || !str.length) return NaN;
|
|
|
|
// Remove whitespace.
|
|
|
|
str = str.replace(/\s+/g, '');
|
|
|
|
return Number(str);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-02-13 18:35:15 +01:00
|
|
|
|
2023-07-10 22:23:13 +02:00
|
|
|
Object.defineProperties(Math, {
|
|
|
|
clamped: {
|
|
|
|
value: function (num: number, min: number, max: number) {
|
|
|
|
return Math.min(max, Math.max(num, min));
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-02-13 18:35:15 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 18:34:03 +01:00
|
|
|
function setupStubs() {
|
2023-07-10 22:23:13 +02:00
|
|
|
class StubGame {
|
|
|
|
constructor() {
|
|
|
|
this.i18n = {
|
|
|
|
localize: (key: string) => (key in en ? en[key as keyof typeof en] : key),
|
|
|
|
};
|
2022-02-13 18:34:03 +01:00
|
|
|
}
|
2023-07-10 22:23:13 +02:00
|
|
|
i18n: {
|
|
|
|
localize: (key: string) => string;
|
|
|
|
};
|
|
|
|
}
|
2022-02-13 18:34:03 +01:00
|
|
|
|
2023-07-10 22:23:13 +02:00
|
|
|
const game = new StubGame();
|
2022-02-13 18:34:03 +01:00
|
|
|
|
2023-07-10 22:23:13 +02:00
|
|
|
Object.defineProperties(globalThis, {
|
|
|
|
game: { value: game },
|
|
|
|
Game: { value: StubGame },
|
|
|
|
});
|
2022-02-13 18:34:03 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 18:35:15 +01:00
|
|
|
setupPrimitives();
|
2022-02-13 18:34:03 +01:00
|
|
|
setupStubs();
|