[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
This commit is contained in:
woutervh-
2018-10-22 08:56:10 -07:00
committed by Sheetal Nandi
parent fa01268068
commit 9efeea3ac3
5 changed files with 148 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
// Type definitions for nwmatcher 1.4
// Project: https://github.com/dperini/nwmatcher
// Definitions by: Wouter van Heeswijk <https://github.com/woutervh->
// 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;
@@ -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');
@@ -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');
+25
View File
@@ -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"
]
}
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}