feat(globrex): new type definition (#43826)

- type definition
- tests

https://github.com/terkelg/globrex#api

Thanks!
This commit is contained in:
Piotr Błażejewicz (Peter Blazejewicz) 2020-04-14 01:20:44 +02:00 committed by GitHub
parent d3164d63b2
commit 621363e730
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,15 @@
import globrex = require('globrex');
import { Options, Results } from 'globrex';
const result = globrex('p*uck'); // $ExpectType Results
// => { regex: /^p.*uck$/, string: '^p.*uck$', segments: [ /^p.*uck$/ ] }
const options: Options = {
extended: true,
filepath: true,
globstar: true,
strict: true,
};
const resultWithOptions: Results = globrex('p*uck', options); // $ExpectType Results
result.regex.test('pluck'); // $ExpectType boolean
resultWithOptions.regex.test('pluck'); // $ExpectType boolean

78
types/globrex/index.d.ts vendored Normal file
View File

@ -0,0 +1,78 @@
// Type definitions for globrex 0.1
// Project: https://github.com/terkelg/globrex#readme
// Definitions by: Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/**
* Transform globs into regular expressions.
* {@link: https://github.com/terkelg/globrex#api}
* @param glob - Glob string to transform.
* @param options - transform options
*/
declare function globrex(glob: string, options?: globrex.Options): globrex.Results;
declare namespace globrex {
interface Results {
/** This property only exists if the option `filepath` is true. */
path?: Path;
/** JavaScript RegExp instance. */
regex: RegExp;
}
interface Path {
/**
* Array of RegExp instances separated by /.
* This can be usable when working with file paths or urls.
* ```js
* [ /^foo$/, /^bar$/, /^([^\/]*)$/, '^baz\\.(md|js|txt)$' ]
* ```
*/
segments: RegExp[];
/**
* String representation of the RegExp
*/
string: string;
/**
* JavaScript RegExp instance build for testing against paths.
* The regex have different path separators depending on host OS.
*/
regex: RegExp;
}
interface Options {
/**
* Enable all advanced features from extglob.
* Matching so called "extended" globs pattern like single character matching,
* matching ranges of characters, group matching, etc.
* Note: Interprets [a-d] as [abcd].
* To match a literal -, include it as first or last character.
* @default false
*/
extended?: boolean;
/**
* When `globstar` is false globs like '/foo/*' are transformed to the following '^\/foo\/.*$'
* which will match any string beginning with '/foo/'
* When the globstar option is true, the same '/foo/*'
* glob is transformed to '^\/foo\/[^/]*$' which will match any string beginning with '/foo/'
* that does not have a '/' to the right of it. '/foo/*' will match: '/foo/bar', '/foo/bar.txt' but not '/foo/bar/baz' or '/foo/bar/baz.txt'.
* Note: When globstar is true, '/foo/**' is equivalent to '/foo/*' when globstar is false
* @default false
*/
globstar?: boolean;
/**
* Be forgiving about multiple slashes, like /// and make everything after the first / optional
* This is how bash glob works.
* @default false
*/
strict?: boolean;
/**
* Parse input strings as it was a file path for special path related features.
* This feature only makes sense if the input is a POSIX path like /foo/bar/hello.js or URLs.
* When true the returned object will have an additional path object.
* @default false
*/
filepath?: boolean;
}
}
export = globrex;

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"globrex-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }