-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dynamic window pathing, adjust tests, add prettier, cleanup…
… tsconfig (#12) * feat: add dynamic window pathing, adjust tests, add prettier, cleanup tsconfig * fix: adjust eslint rule for single var * feat: allow binding any object to globalThis variable * feat: add separate class bindings, rename set to setObject * style: fix formatting * feat: single entry point for class or object window assignment * remove prettier from package.json
- Loading branch information
Showing
5 changed files
with
166 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Safely binds an `object` or `class` to the window instance under a name variable | ||
* | ||
* Avoid using `lambda` functions for classes for this to work properly. | ||
* | ||
* @example | ||
* ```ts | ||
* type bindings = { test: Function }; | ||
* | ||
* set("wallet", bindings); | ||
* ``` | ||
* | ||
* ```ts | ||
* class Test { | ||
* constructor(){} | ||
* | ||
* test1() {} | ||
* } | ||
* | ||
* set("wallet", new Test()); | ||
* ``` | ||
* | ||
* @export | ||
* @param {string} name | ||
* @param {object} bindings | ||
* @return {*} | ||
*/ | ||
export function set(name: string, bindings: object) { | ||
const isObject = (Object.keys(bindings) as Array<keyof object>).find((key) => typeof bindings[key] === "function"); | ||
|
||
if (isObject) { | ||
( | ||
globalThis as typeof globalThis & { | ||
[key in typeof name]: object; | ||
} | ||
)[name] = bindings; | ||
return; | ||
} | ||
|
||
const target = globalThis as typeof globalThis & { | ||
[key in typeof name]: object; | ||
}; | ||
|
||
const newBindings: { [key: string]: () => void } = {}; | ||
for (const key of Object.getOwnPropertyNames(Object.getPrototypeOf(bindings))) { | ||
if (typeof key !== "string") { | ||
continue; | ||
} | ||
|
||
newBindings[key] = (bindings as { [key: string]: () => void })[key].bind(bindings); | ||
} | ||
|
||
target[name] = newBindings; | ||
} | ||
|
||
/** | ||
* Returns bindings assigned to globalThis | ||
* | ||
* @example | ||
* ``` | ||
* type bindings = { test: Function }; | ||
* | ||
* const wallet = get<bindings>("wallet"); | ||
* ``` | ||
* | ||
* @export | ||
* @template T | ||
* @param {string} name | ||
* @return {T} | ||
*/ | ||
export function get<T = object>(name: string): T { | ||
return ( | ||
globalThis as typeof globalThis & { | ||
[key in typeof name]: T; | ||
} | ||
)[name]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as bindings from "./binder"; | ||
import { expect, test } from "vitest"; | ||
|
||
declare global { | ||
// eslint-disable-next-line no-var | ||
var wallet: typeof testBindings; | ||
} | ||
|
||
const testBindings = { | ||
test1() {}, | ||
test2() {}, | ||
test3() {}, | ||
}; | ||
|
||
let index = 0; | ||
|
||
class Test { | ||
private id = 0; | ||
|
||
constructor() { | ||
this.id = index; | ||
index += 1; | ||
} | ||
|
||
getId() { | ||
return this.id; | ||
} | ||
} | ||
|
||
test("test binding to 'window' or 'global' variables with object", () => { | ||
const bindingName = "wallet"; | ||
|
||
const target = typeof window !== "undefined" ? window : global; | ||
|
||
expect(typeof target).not.toBe("undefined"); | ||
expect(Object.hasOwn(target, bindingName)).toBe(false); | ||
|
||
bindings.set(bindingName, testBindings); | ||
|
||
expect(typeof target[bindingName]).not.toBe("undefined"); | ||
|
||
const result = bindings.get<typeof testBindings>(bindingName); | ||
expect(typeof result.test1).toBe("function"); | ||
expect(typeof result.test2).toBe("function"); | ||
expect(typeof result.test3).toBe("function"); | ||
}); | ||
|
||
test("test binding to 'window' or 'global' variables with class", () => { | ||
const bindingName = "wallet"; | ||
|
||
// Bindings testing | ||
const testClass = new Test(); | ||
expect(testClass.getId() === 0).toBe(true); | ||
const testClass2 = new Test(); | ||
expect(testClass2.getId() === 1).toBe(true); | ||
|
||
bindings.set(bindingName, testClass); | ||
|
||
const target = typeof window !== "undefined" ? window : global; | ||
expect(typeof target[bindingName]).not.toBe("undefined"); | ||
|
||
const result = bindings.get<Test>(bindingName); | ||
expect(typeof result.getId).toBe("function"); | ||
expect(result.getId() === 0).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters