Skip to content

Commit

Permalink
feat: single entry point for class or object window assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Feb 1, 2024
1 parent 367c199 commit 0a57d03
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,12 @@
"prettier": "^3.2.4",
"typescript": "^5.3.3",
"vitest": "^1.2.2"
},
"prettier": {
"tabWidth": 2,
"singleQuote": false,
"trailingComma": "all",
"printWidth": 120,
"bracketSpacing": true
}
}
42 changes: 19 additions & 23 deletions src/binder/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,42 @@
/**
* Safely binds functions to globalThis
* 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 };
*
* setObject<bindings>("wallet", bindings);
* set("wallet", bindings);
* ```
*
* @export
* @template T
* @param {string} name
* @param {T} bindings
*/
export function setObject<T = object>(name: string, bindings: T) {
(
globalThis as typeof globalThis & {
[key in typeof name]: T;
}
)[name] = bindings;
}

/**
* Safely binds class functions to globalThis
*
* @example
* ```ts
* class Test {
* constructor(){}
*
* test1() {}
* }
*
* setClass('wallet', new Test());
* set("wallet", new Test());
* ```
*
* @export
* @template T
* @param {string} name
* @param {T} bindings
* @param {object} bindings
* @return {*}
*/
export function setClass<T = object>(name: string, bindings: T) {
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;
};
Expand Down
4 changes: 2 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test("test binding to 'window' or 'global' variables with object", () => {
expect(typeof target).not.toBe("undefined");
expect(Object.hasOwn(target, bindingName)).toBe(false);

bindings.setObject(bindingName, testBindings);
bindings.set(bindingName, testBindings);

expect(typeof target[bindingName]).not.toBe("undefined");

Expand All @@ -54,7 +54,7 @@ test("test binding to 'window' or 'global' variables with class", () => {
const testClass2 = new Test();
expect(testClass2.getId() === 1).toBe(true);

bindings.setClass(bindingName, testClass);
bindings.set(bindingName, testClass);

const target = typeof window !== "undefined" ? window : global;
expect(typeof target[bindingName]).not.toBe("undefined");
Expand Down

0 comments on commit 0a57d03

Please sign in to comment.