Add unist-util-is

This commit is contained in:
Junyoung Choi
2018-12-05 18:06:30 +09:00
parent e1a9b84984
commit f3ba892a83
4 changed files with 94 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
// Type definitions for unist-util-is 2.1
// Project: https://github.com/syntax-tree/unist-util-is#readme
// Definitions by: Junyoung Choi <https://github.com/rokt33r>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
/// <reference types='node' />
import * as Unist from 'unist';
declare namespace unistUtilIs {
/**
* @param node Node to test
* @param index Position of node in parent
* @param parent Parent of node
*/
type TestFunction<C = {}> = (this: C, node: unknown, index: number, parent: Unist.Parent) => boolean | void;
type Test<C = {}> = TestFunction<C> | Partial<Unist.Node> | string | null;
}
/**
* Unist utility to check if a node passes a test.
*
* @param test When not given, checks if `node` is a `Node`.
* When `string`, works like passing `function (node) {return node.type === test}`.
* When `array`, checks any one of the subtests pass.
* When `object`, checks that all keys in test are in node, and that they have (strictly) equal values
* @param node Node to check. `false` is returned
* @param index Position of `node` in `parent`
* @param parent Parent of `node`
* @param context Context object to invoke `test` with
* @returns Whether test passed and `node` is a `Node` (object with `type` set to non-empty `string`).
*/
declare function unistUtilIs<C = {}>(test: unistUtilIs.Test<C> | Array<unistUtilIs.Test<C>>, node: unknown, index?: number, parent?: Unist.Parent, context?: any): boolean;
export = unistUtilIs;
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"unist-util-is-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
@@ -0,0 +1,34 @@
import is = require('unist-util-is');
const node = {type: 'strong'};
const parent = {type: 'paragraph', children: [node]};
function test(node: unknown, n: number) {
return n === 5;
}
is(null, {children: []});
is(null, node);
is('strong', node);
is('emphasis', node);
is(node, node);
is({type: 'paragraph'}, parent);
is({type: 'strong'}, parent);
is(test, node);
is(test, node, 4, parent);
is(test, node, 5, parent);
interface TestContext {
some: string;
}
function testWithContext(this: TestContext, node: unknown) {}
is<TestContext>(testWithContext, node);
const wrongTestFunc: is.TestFunction = (node: unknown, n: string) => {}; // $ExpectError
interface OtherTestContext {
some: number;
}
is<OtherTestContext>(testWithContext, node); // $ExpectError