diff --git a/types/is-hotkey/index.d.ts b/types/is-hotkey/index.d.ts new file mode 100644 index 0000000000..a41521fcc1 --- /dev/null +++ b/types/is-hotkey/index.d.ts @@ -0,0 +1,58 @@ +// Type definitions for is-hotkey 0.1 +// Project: https://github.com/ianstormtaylor/is-hotkey#readme +// Definitions by: Pierre-Marc Airoldi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export interface HotKeyOptions { + byKey: boolean; +} + +export interface HotKey { + which?: number; + key?: string; + altKey: boolean; + ctrlKey: boolean; + metaKey: boolean; + shiftKey: boolean; +} + +/** + * Is hotkey? + */ +export function isHotkey( + hotkey: string, + options?: HotKeyOptions +): (event: KeyboardEvent) => boolean; + +export function isHotkey( + hotkey: string, + options?: HotKeyOptions | KeyboardEvent, + event?: KeyboardEvent +): boolean; + +export function isCodeHotkey(hotkey: string): (event: KeyboardEvent) => boolean; +export function isCodeHotkey(hotkey: string, event: KeyboardEvent): boolean; + +export function isKeyHotkey(hotkey: string): (event: KeyboardEvent) => boolean; +export function isKeyHotkey(hotkey: string, event: KeyboardEvent): boolean; + +/** + * Parse. + */ +export function parseHotkey(hotkey: string, options?: HotKeyOptions): HotKey; + +/** + * Compare. + */ +export function compareHotkey(object: HotKey, event: KeyboardEvent): boolean; + +/** + * Utils. + */ +export function toKeyCode(name: string): number; +export function toKeyName(name: string): string; + +/** + * Export. + */ +export default isHotkey; diff --git a/types/is-hotkey/is-hotkey-tests.ts b/types/is-hotkey/is-hotkey-tests.ts new file mode 100644 index 0000000000..6973d87de6 --- /dev/null +++ b/types/is-hotkey/is-hotkey-tests.ts @@ -0,0 +1,32 @@ +import { + isHotkey, + isCodeHotkey, + isKeyHotkey, + toKeyName, + toKeyCode, + parseHotkey, + compareHotkey +} from "is-hotkey"; + +const event = new KeyboardEvent(""); + +isHotkey("mod+s")(event); // $ExpectType boolean +isHotkey("mod+s", { byKey: true })(event); // $ExpectType boolean + +isHotkey("mod+s", event); // $ExpectType boolean +isHotkey("mod+s", { byKey: true }, event); // $ExpectType boolean + +isCodeHotkey("mod+s")(event); // $ExpectType boolean +isKeyHotkey("mod+s")(event); // $ExpectType boolean + +isCodeHotkey("mod+s", event); // $ExpectType boolean +isKeyHotkey("mod+s", event); // $ExpectType boolean + +toKeyName("cmd"); // $ExpectType string + +toKeyCode("shift"); // $ExpectType number + +parseHotkey("cmd+s"); // $ExpectType HotKey +parseHotkey("cmd+s", { byKey: true }); // $ExpectType HotKey + +compareHotkey(parseHotkey("cmd+s"), event); // $ExpectType boolean diff --git a/types/is-hotkey/tsconfig.json b/types/is-hotkey/tsconfig.json new file mode 100644 index 0000000000..df8e256f29 --- /dev/null +++ b/types/is-hotkey/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true + }, + "files": [ + "index.d.ts", + "is-hotkey-tests.ts" + ] +} diff --git a/types/is-hotkey/tslint.json b/types/is-hotkey/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/is-hotkey/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }