From 9efeea3ac3caf8925b7c0f8dc2ced0f15553fe22 Mon Sep 17 00:00:00 2001 From: woutervh- Date: Mon, 22 Oct 2018 17:56:10 +0200 Subject: [PATCH] [nwmatcher] Created types for nwmatcher (#29778) * Created types for nwmatcher * Rename class -> className, fix tests * Remove duplicate test case * Added global namespace for accessing global matcher * Moved tests to test folder, added separate global and module test files --- types/nwmatcher/index.d.ts | 33 ++++++++++++++ types/nwmatcher/test/nwmatcher-global.test.ts | 42 +++++++++++++++++ types/nwmatcher/test/nwmatcher-module.test.ts | 45 +++++++++++++++++++ types/nwmatcher/tsconfig.json | 25 +++++++++++ types/nwmatcher/tslint.json | 3 ++ 5 files changed, 148 insertions(+) create mode 100644 types/nwmatcher/index.d.ts create mode 100644 types/nwmatcher/test/nwmatcher-global.test.ts create mode 100644 types/nwmatcher/test/nwmatcher-module.test.ts create mode 100644 types/nwmatcher/tsconfig.json create mode 100644 types/nwmatcher/tslint.json diff --git a/types/nwmatcher/index.d.ts b/types/nwmatcher/index.d.ts new file mode 100644 index 0000000000..cd0f97360e --- /dev/null +++ b/types/nwmatcher/index.d.ts @@ -0,0 +1,33 @@ +// Type definitions for nwmatcher 1.4 +// Project: https://github.com/dperini/nwmatcher +// Definitions by: Wouter van Heeswijk +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare function nwmatcher(global: { document: Document }): nwmatcher.Matcher; + +declare namespace nwmatcher { + interface Matcher { + // DOM Selection + + first: (selector: string, context?: Element) => Element | null; + match: (element: Element, selector: string, context?: Element) => boolean; + select: (selector: string, context?: Element, callback?: (element: Element) => void) => Element[]; + + // DOM Helpers + + byId: (id: string, from?: Element) => Element | null; + byTag: (tag: string, from?: Element) => Element[]; + byClass: (className: string, from?: Element) => Element[]; + byName: (name: string, from?: Element) => Element[]; + getAttribute: (element: Element, attribute: string) => string | undefined; + hasAttribute: (element: Element, attribute: string) => boolean; + } +} + +declare global { + namespace NW { + const Dom: nwmatcher.Matcher; + } +} + +export = nwmatcher; diff --git a/types/nwmatcher/test/nwmatcher-global.test.ts b/types/nwmatcher/test/nwmatcher-global.test.ts new file mode 100644 index 0000000000..b58e3f2367 --- /dev/null +++ b/types/nwmatcher/test/nwmatcher-global.test.ts @@ -0,0 +1,42 @@ +const matcher = NW.Dom; + +/** + * Test matcher (select/first/...) functions. + */ + +const element: Element = {} as any; + +let resultElementOrNull: Element | null; +let resultStringOrUndefined: string | undefined; +let resultElementArray: Element[]; +let resultBoolean: boolean; + +resultElementOrNull = matcher.first('foo'); +resultElementOrNull = matcher.first('foo', element); +resultElementOrNull = matcher.first('foo'); +resultElementOrNull = matcher.first('foo', element); + +resultBoolean = matcher.match(element, 'foo'); +resultBoolean = matcher.match(element, 'foo', element); + +resultElementArray = matcher.select('foo'); +resultElementArray = matcher.select('foo', element); +resultElementArray = matcher.select('foo', element, (element: Element) => undefined); + +resultElementOrNull = matcher.byId('foo'); +resultElementOrNull = matcher.byId('foo', element); +resultElementOrNull = matcher.byId('foo'); +resultElementOrNull = matcher.byId('foo', element); + +resultElementArray = matcher.byTag('foo'); +resultElementArray = matcher.byTag('foo', element); + +resultElementArray = matcher.byClass('foo'); +resultElementArray = matcher.byClass('foo', element); + +resultElementArray = matcher.byName('foo'); +resultElementArray = matcher.byName('foo', element); + +resultStringOrUndefined = matcher.getAttribute(element, 'foo'); + +resultBoolean = matcher.hasAttribute(element, 'foo'); diff --git a/types/nwmatcher/test/nwmatcher-module.test.ts b/types/nwmatcher/test/nwmatcher-module.test.ts new file mode 100644 index 0000000000..9275502bfd --- /dev/null +++ b/types/nwmatcher/test/nwmatcher-module.test.ts @@ -0,0 +1,45 @@ +import nwmatcher = require('nwmatcher'); + +const global: { document: Document } = { document: {} as any }; +const matcher = nwmatcher(global); + +/** + * Test matcher (select/first/...) functions. + */ + +const element: Element = {} as any; + +let resultElementOrNull: Element | null; +let resultStringOrUndefined: string | undefined; +let resultElementArray: Element[]; +let resultBoolean: boolean; + +resultElementOrNull = matcher.first('foo'); +resultElementOrNull = matcher.first('foo', element); +resultElementOrNull = matcher.first('foo'); +resultElementOrNull = matcher.first('foo', element); + +resultBoolean = matcher.match(element, 'foo'); +resultBoolean = matcher.match(element, 'foo', element); + +resultElementArray = matcher.select('foo'); +resultElementArray = matcher.select('foo', element); +resultElementArray = matcher.select('foo', element, (element: Element) => undefined); + +resultElementOrNull = matcher.byId('foo'); +resultElementOrNull = matcher.byId('foo', element); +resultElementOrNull = matcher.byId('foo'); +resultElementOrNull = matcher.byId('foo', element); + +resultElementArray = matcher.byTag('foo'); +resultElementArray = matcher.byTag('foo', element); + +resultElementArray = matcher.byClass('foo'); +resultElementArray = matcher.byClass('foo', element); + +resultElementArray = matcher.byName('foo'); +resultElementArray = matcher.byName('foo', element); + +resultStringOrUndefined = matcher.getAttribute(element, 'foo'); + +resultBoolean = matcher.hasAttribute(element, 'foo'); diff --git a/types/nwmatcher/tsconfig.json b/types/nwmatcher/tsconfig.json new file mode 100644 index 0000000000..b70d5c6fc3 --- /dev/null +++ b/types/nwmatcher/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "test/nwmatcher-global.test.ts", + "test/nwmatcher-module.test.ts" + ] +} \ No newline at end of file diff --git a/types/nwmatcher/tslint.json b/types/nwmatcher/tslint.json new file mode 100644 index 0000000000..b4b47a0378 --- /dev/null +++ b/types/nwmatcher/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}