diff --git a/hooker/hooker-tests.ts b/hooker/hooker-tests.ts new file mode 100644 index 000000000..d9aea606c --- /dev/null +++ b/hooker/hooker-tests.ts @@ -0,0 +1,72 @@ +/// + +import hooker = require('hooker'); + +// Type definitions for JavaScript Hooker v0.2.3 +// Project: https://github.com/cowboy/javascript-hooker +// Definitions by: Michael Zabka +// Definitions: https://github.com/borisyankov/DefinitelyTyped +function tests() { + var objectToHook: any = { + hello: 'world' + }; + hooker.hook(objectToHook, 'hello', () => { }); + hooker.hook(objectToHook, 'hello', () => { + return null; + }); + hooker.hook(objectToHook, ['hello', 'foo'], () => { }); + hooker.hook(objectToHook, ['hello', 'bar'], () => { + return null; + }); + hooker.hook(objectToHook, 'bar', () => { + return hooker.filter(this, ['foo', 'bar']); + }); + hooker.hook(objectToHook, 'bar', () => { + return hooker.override('good'); + }); + hooker.hook(objectToHook, 'bar', () => { + return hooker.preempt('good'); + }); + hooker.orig(objectToHook, 'hello'); + hooker.orig(objectToHook, ['hello', 'foo']); + hooker.hook(objectToHook, 'foo', { + pre: () => { } + }); + hooker.hook(objectToHook, 'foo', { + pre: () => { + return hooker.preempt(1); + } + }); + hooker.hook(objectToHook, 'foo', { + pre: () => { + return hooker.override(1); + } + }); + hooker.hook(objectToHook, 'foo', { + pre: () => { + return hooker.filter(1, ['abc']); + } + }); + hooker.hook(objectToHook, 'foo', { + post: () => { } + }); + hooker.hook(objectToHook, 'foo', { + post: () => { + return hooker.filter(1, ['abc']); + } + }); + hooker.hook(objectToHook, 'foo', { + once: false + }); + hooker.hook(objectToHook, 'foo', { + passName: true + }); + hooker.hook(objectToHook, 'foo', { + pre: () => { }, + post: () => { + return hooker.filter(this, []); + }, + once: true, + passName: false + }); +} diff --git a/hooker/hooker.d.ts b/hooker/hooker.d.ts new file mode 100644 index 000000000..570805d39 --- /dev/null +++ b/hooker/hooker.d.ts @@ -0,0 +1,42 @@ +// Type definitions for JavaScript Hooker v0.2.3 +// Project: https://github.com/cowboy/javascript-hooker +// Definitions by: Michael Zabka +// Definitions: https://github.com/borisyankov/DefinitelyTyped + + +declare type HookerPostHookFunction = (result: any, ...args: any[]) => IHookerPostHookResult|void; +declare type HookerPreHookFunction = (...args: any[]) => IHookerPreHookResult|void; + +declare module "hooker" { + function hook(object: any, props: string|string[], options: IHookerOptions): void; + function hook(object: any, props: string|string[], prehookFunction: HookerPreHookFunction): void; + function unhook(object: any, props?: string|string[]): string[]; + function orig(object: any, props: string|string[]): Function; + function override(value: any): HookerOverride; + function preempt(value: any): HookerPreempt; + function filter(context: any, args: any[]): HookerFilter; +} + +declare class HookerOverride implements IHookerPostHookResult, IHookerPreHookResult { + value: any; +} + +declare class HookerPreempt implements IHookerPreHookResult { + value: any; +} + +declare class HookerFilter implements IHookerPreHookResult { + context: any; + args: any[]; +} + +interface IHookerPostHookResult {} + +interface IHookerPreHookResult {} + +interface IHookerOptions { + pre?: HookerPreHookFunction; + post?: HookerPostHookFunction; + once?: boolean; + passName?: boolean; +}