Skip to content

Commit

Permalink
feat: add separate class bindings, rename set to setObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Jan 31, 2024
1 parent 5a53e74 commit e64d791
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
42 changes: 40 additions & 2 deletions src/binder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,60 @@
* ```ts
* type bindings = { test: Function };
*
* set<bindings>("wallet", bindings);
* setObject<bindings>("wallet", bindings);
* ```
*
* @export
* @template T
* @param {string} name
* @param {T} bindings
*/
export function set<T = object>(name: string, bindings: T) {
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());
* ```
*
* @export
* @template T
* @param {string} name
* @param {T} bindings
*/
export function setClass<T = object>(name: string, bindings: T) {
const target = globalThis as typeof globalThis & {
[key in typeof name]: object;
};

const newBindings: { [key: string]: Function } = {};
for (let key of Object.getOwnPropertyNames(Object.getPrototypeOf(bindings))) {
if (typeof key !== "string") {
continue;
}

newBindings[key] = (bindings as { [key: string]: Function })[key].bind(
bindings
);
}

target[name] = newBindings;
}

/**
* Returns bindings assigned to globalThis
*
Expand Down
38 changes: 36 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,30 @@ const testBindings = {
test3() {},
};

test("test binding to 'window' or 'global' variables", () => {
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);
bindings.setObject(bindingName, testBindings);

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

Expand All @@ -29,3 +44,22 @@ test("test binding to 'window' or 'global' variables", () => {
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.setClass(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);
});

0 comments on commit e64d791

Please sign in to comment.