From 58b3dac925a2bf628f0cdad26e7aa246df9753c0 Mon Sep 17 00:00:00 2001 From: Simon Mika Date: Wed, 25 Feb 2026 09:25:51 +0100 Subject: [PATCH] Workaround for if globalThis is not defined. --- Array/Class.ts | 3 ++- Intersection/Class.ts | 9 ++++---- Number/Class.ts | 3 ++- Number/Restriction.ts | 3 ++- Object/Class.ts | 9 ++++---- Object/Properties.ts | 7 ++++--- Record/index.spec.ts | 3 ++- Tuple/Class.ts | 3 ++- system.ts | 49 +++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 system.ts diff --git a/Array/Class.ts b/Array/Class.ts index 1ec4255..6fd039f 100644 --- a/Array/Class.ts +++ b/Array/Class.ts @@ -2,6 +2,7 @@ import { Base } from "../Base" import { Flaw } from "../Flaw" import type { isly } from "../index" import { Name } from "../Name" +import { system } from "../system" import { Restriction } from "./Restriction" export class Class extends Base { @@ -18,7 +19,7 @@ export class Class extends Base { this._name = name } override is(value: V[] | any): value is V[] { - return globalThis.Array.isArray(value) && value.every(this.base.is.bind(this.base)) + return system.Array.isArray(value) && value.every(this.base.is.bind(this.base)) } override flawed(value: V[] | any): Flaw | false { const result: Flaw | false = super.flawed(value) diff --git a/Intersection/Class.ts b/Intersection/Class.ts index 9e46456..d937c4a 100644 --- a/Intersection/Class.ts +++ b/Intersection/Class.ts @@ -2,6 +2,7 @@ import { Base } from "../Base" import { Flaw } from "../Flaw" import type { isly } from "../index" import { Name } from "../Name" +import { system } from "../system" export class Class = Base> extends Base { readonly class = "intersection" @@ -67,11 +68,11 @@ function merge(target: T, source: S): T & S { return Array.isArray(target) && Array.isArray(source) ? (target.map((item, index) => merge(item, source[index])) as S & T) : target && typeof target == "object" && source && typeof source == "object" - ? globalThis.Object.fromEntries([ - ...globalThis.Object.entries(target), - ...globalThis.Object.entries(source).map(([key, value]) => [ + ? system.Object.fromEntries([ + ...system.Object.entries(target), + ...system.Object.entries(source).map(([key, value]) => [ key, - globalThis.Object.getOwnPropertyDescriptor(target, key) && + system.Object.getOwnPropertyDescriptor(target, key) && typeof target[key as keyof typeof target] == "object" && typeof value == "object" ? merge(target[key as keyof typeof target], value) diff --git a/Number/Class.ts b/Number/Class.ts index 695af0e..6043b91 100644 --- a/Number/Class.ts +++ b/Number/Class.ts @@ -1,5 +1,6 @@ import { Base } from "../Base" import type { isly } from "../index" +import { system } from "../system" import { Restriction } from "./Restriction" export class Class extends Base { @@ -12,7 +13,7 @@ export class Class extends Base { super() } is(value: V | any): value is V { - return typeof value == "number" && globalThis.Number.isFinite(value) + return typeof value == "number" && system.Number.isFinite(value) } override restrict(...restriction: Restriction | Base.Restriction) { return super.restrict(...(Base.Restriction.is(restriction) ? restriction : Restriction.convert(restriction))) diff --git a/Number/Restriction.ts b/Number/Restriction.ts index b6e93d8..1252b67 100644 --- a/Number/Restriction.ts +++ b/Number/Restriction.ts @@ -1,5 +1,6 @@ import { Base } from "../Base" import { Name } from "../Name" +import { system } from "../system" import { Verifier } from "../Verifier" export type Restriction = @@ -27,7 +28,7 @@ export namespace Restriction { } export function getVerifier(...[category, ...restriction]: Restriction): Verifier { const verifiers: Record boolean> = { - integer: globalThis.Number.isInteger, + integer: system.Number.isInteger, positive: value => value > 0, negative: value => value < 0, minimum: value => value >= (restriction[0] as number), diff --git a/Object/Class.ts b/Object/Class.ts index 365c294..49cbbc4 100644 --- a/Object/Class.ts +++ b/Object/Class.ts @@ -2,6 +2,7 @@ import { Base } from "../Base" import { BindResult } from "../BindResult" import { Flaw } from "../Flaw" import type { isly } from "../index" +import { system } from "../system" import type { Type } from "../Type" import { Properties } from "./Properties" @@ -13,7 +14,7 @@ export class Class> extends Base { } override get definition(): isly.Definition { return Object.assign(super.definition, { - properties: globalThis.Object.fromEntries(Properties.entries(this.properties).map(([p, t]) => [p, t.definition])), + properties: system.Object.fromEntries(Properties.entries(this.properties).map(([p, t]) => [p, t.definition])), }) } private constructor(readonly properties: Properties, name?: string) { @@ -25,7 +26,7 @@ export class Class> extends Base { !!value && typeof value == "object" && !Array.isArray(value) && - globalThis.Object.entries(this.properties).every(([property, type]) => type.is(value[property])) + system.Object.entries(this.properties).every(([property, type]) => type.is(value[property])) ) } override prune(value: V | any): V | undefined { @@ -95,10 +96,10 @@ export class Class> extends Base { } export namespace Class { export function omit(object: T, omits: readonly K[]): Omit { - const keys = globalThis.Object.keys(object).filter(k => omits.every(omit => omit != k)) as Exclude[] + const keys = system.Object.keys(object).filter(k => omits.every(omit => omit != k)) as Exclude[] return pick(object, keys) } export function pick(object: T, picks: readonly K[]): Pick { - return globalThis.Object.fromEntries(picks.map(key => [key, object[key]])) as Pick + return system.Object.fromEntries(picks.map(key => [key, object[key]])) as Pick } } diff --git a/Object/Properties.ts b/Object/Properties.ts index bc3edbe..66927e4 100644 --- a/Object/Properties.ts +++ b/Object/Properties.ts @@ -1,12 +1,13 @@ import { Base } from "../Base" import { Name } from "../Name" +import { system } from "../system" export type Properties = { [P in keyof T]: Base } export namespace Properties { export function entries(properties: Properties): [keyof T, Base][] { - return globalThis.Object.entries(properties) as [keyof T, Base][] + return system.Object.entries(properties) as [keyof T, Base][] } export function getName(properties: Properties): Name { return `{ ${Properties.entries(properties) @@ -14,14 +15,14 @@ export namespace Properties { .join(", ")} }` } export function prune(properties: Properties, value: T): T { - return globalThis.Object.fromEntries( + return system.Object.fromEntries( Properties.entries(properties) .map(([p, t]) => [p, t.prune(value[p])]) .filter(([p, v]) => v !== undefined) ) as T } export function optional(properties: Properties): Properties> { - return globalThis.Object.fromEntries( + return system.Object.fromEntries( Properties.entries(properties).map(([p, t]) => [p, t.optional()]) ) as unknown as Properties> } diff --git a/Record/index.spec.ts b/Record/index.spec.ts index 48443df..349f254 100644 --- a/Record/index.spec.ts +++ b/Record/index.spec.ts @@ -1,4 +1,5 @@ import { isly } from "../index" +import { system } from "../system" describe("isly.record()", () => { // compile error if not working @@ -106,7 +107,7 @@ describe("isly.record()", () => { it("record.prune", () => { type User = { email: string } const userRecordType = isly.record(isly.string(), isly.object({ email: isly.string() })) - const usersRecords: Record = globalThis.Object.fromEntries( + const usersRecords: Record = system.Object.fromEntries( [...Array(5).keys()].map(id => [`${id}`, { email: `${id}@example.com`, password: "shouldBeSecret" }]) ) expect(userRecordType.is(usersRecords)).toBe(true) diff --git a/Tuple/Class.ts b/Tuple/Class.ts index ab9769b..4b1685d 100644 --- a/Tuple/Class.ts +++ b/Tuple/Class.ts @@ -2,6 +2,7 @@ import { Base } from "../Base" import { Flaw } from "../Flaw" import type { isly } from "../index" import { Name } from "../Name" +import { system } from "../system" export class Class extends Base { readonly class = "tuple" @@ -18,7 +19,7 @@ export class Class extends Base { } override is(value: V | any): value is V { return ( - globalThis.Array.isArray(value) && + system.Array.isArray(value) && value.length == this.base.length && this.base.every((type, index) => type.is(value[index])) ) diff --git a/system.ts b/system.ts new file mode 100644 index 0000000..34bf8cb --- /dev/null +++ b/system.ts @@ -0,0 +1,49 @@ +export const system = globalThis +for (const name of [ + "Atomics", + "AggregateError", + "Array", + "ArrayBuffer", + "BigInt", + "BigInt64Array", + "BigUint64Array", + "Boolean", + "Date", + "DataView", + "Error", + "EvalError", + "FinalizationRegistry", + "Float32Array", + "Float64Array", + "Function", + "Int8Array", + "Int16Array", + "Int32Array", + "Intl", + "JSON", + "Map", + "Math", + "Number", + "Object", + "Promise", + "Proxy", + "RangeError", + "Reflect", + "RegExp", + "ReferenceError", + "Set", + "SharedArrayBuffer", + "String", + "Symbol", + "SyntaxError", + "TypeError", + "Uint8Array", + "Uint8ClampedArray", + "Uint16Array", + "Uint32Array", + "URIError", + "WeakMap", + "WeakRef", + "WeakSet", +] as const) + (system as any)[name] ??= globalThis[name]