mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-23 21:00:01 +08:00
Add library hooker
* on github is named javascript-hooker in case of already existance of hooker repo
This commit is contained in:
72
hooker/hooker-tests.ts
Normal file
72
hooker/hooker-tests.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
/// <reference path="hooker.d.ts" />
|
||||
|
||||
import hooker = require('hooker');
|
||||
|
||||
// Type definitions for JavaScript Hooker v0.2.3
|
||||
// Project: https://github.com/cowboy/javascript-hooker
|
||||
// Definitions by: Michael Zabka <https://github.com/misak113/>
|
||||
// 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
|
||||
});
|
||||
}
|
||||
42
hooker/hooker.d.ts
vendored
Normal file
42
hooker/hooker.d.ts
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
// Type definitions for JavaScript Hooker v0.2.3
|
||||
// Project: https://github.com/cowboy/javascript-hooker
|
||||
// Definitions by: Michael Zabka <https://github.com/misak113/>
|
||||
// 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;
|
||||
}
|
||||
Reference in New Issue
Block a user