Merge pull request #7789 from glen-84/micromatch

Add type definitions for micromatch (and parse-glob)
This commit is contained in:
Masahiro Wakame
2016-01-27 23:31:12 +09:00
4 changed files with 344 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
/// <reference path="./micromatch.d.ts" />
import mm = require('micromatch');
var strArrResult: string[];
var boolResult: boolean;
var strMatchFuncResult: mm.MatchFunction<string>;
var anyMatchFuncResult: mm.MatchFunction<any>;
var globDataResult: mm.GlobData;
var regExpResult: RegExp;
// Usage.
strArrResult = mm(['a.js', 'b.md', 'c.txt'], '*.{js,txt}');
// Multiple patterns.
strArrResult = mm(['a.md', 'b.js', 'c.txt', 'd.json'], ['*.md', '*.txt']);
// "isMatch" method.
boolResult = mm.isMatch('.verb.md', '*.md');
boolResult = mm.isMatch('.verb.md', '*.md', {dot: true});
boolResult = mm.isMatch('*.md', {dot: true})('.verb.md');
// "contains" method.
boolResult = mm.contains('a/b/c', 'a/b');
boolResult = mm.contains('a/b/c', 'a/b', {dot: true});
// "matcher" method.
strMatchFuncResult = mm.matcher('*.md');
strMatchFuncResult = mm.matcher(/\.md$/);
strMatchFuncResult = mm.matcher((filePath: string) => true);
// "filter" method.
anyMatchFuncResult = mm.filter('*.md');
anyMatchFuncResult = mm.filter(/\.md$/);
anyMatchFuncResult = mm.filter((filePath: string) => true);
anyMatchFuncResult = mm.filter('*.md', {dot: true});
['a.js', 'b.txt', 'c.md'].filter(anyMatchFuncResult);
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
anyMatchFuncResult = mm.filter(['{1..10}', '![7-9]', '!{3..4}']);
arr.filter(anyMatchFuncResult);
// "any" method.
boolResult = mm.any('abc', ['!*z']);
boolResult = mm.any('abc', 'a*');
boolResult = mm.any('abc', 'a*', {dot: true});
// "expand" method.
globDataResult = mm.expand('*.js');
globDataResult = mm.expand('*.js', {dot: true});
// "makeRe" method.
regExpResult = mm.makeRe('*.js');
regExpResult = mm.makeRe('*.js', {dot: true});
+174
View File
@@ -0,0 +1,174 @@
// Type definitions for micromatch 2.3.7
// Project: https://github.com/jonschlinkert/micromatch
// Definitions by: glen-84 <https://github.com/glen-84>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../parse-glob/parse-glob.d.ts" />
declare module 'micromatch' {
import parseGlob = require('parse-glob');
namespace micromatch {
type MatchFunction<T> = ((value: T) => boolean);
type Pattern = (string | RegExp | MatchFunction<string>);
interface Options {
/**
* Normalize slashes in file paths and glob patterns to forward slashes.
*/
unixify?: boolean;
/**
* Match dotfiles. Same behavior as minimatch.
*/
dot?: boolean;
/**
* Unescape slashes in glob patterns. Use cautiously, especially on windows.
*/
unescape?: boolean;
/**
* Remove duplicate elements from the result array.
*/
nodupes?: boolean;
/**
* Allow glob patterns without slashes to match a file path based on its basename. Same behavior as
* minimatch.
*/
matchBase?: boolean;
/**
* Don't expand braces in glob patterns. Same behavior as minimatch nobrace.
*/
nobraces?: boolean;
/**
* Don't expand POSIX bracket expressions.
*/
nobrackets?: boolean;
/**
* Don't expand extended globs.
*/
noextglob?: boolean;
/**
* Use a case-insensitive regex for matching files. Same behavior as minimatch.
*/
nocase?: boolean;
/**
* If true, when no matches are found the actual (array-ified) glob pattern is returned instead of an empty
* array. Same behavior as minimatch.
*/
nonull?: boolean;
/**
* Cache the platform (e.g. win32) to prevent this from being looked up for every file path.
*/
cache?: boolean;
}
interface Glob {
options: micromatch.Options;
pattern: string;
history: {msg: any, pattern: string}[];
tokens: parseGlob.Result;
orig: string;
negated: boolean;
/**
* Initialize defaults.
*/
init(pattern: string): void;
/**
* Push a change into `glob.history`. Useful for debugging.
*/
track(msg: any): void;
/**
* Return true if `glob.pattern` was negated with `!`, also remove the `!` from the pattern.
*/
isNegated(): boolean;
/**
* Expand braces in the given glob pattern.
*/
braces(): void;
/**
* Expand bracket expressions in `glob.pattern`.
*/
brackets(): void;
/**
* Expand extended globs in `glob.pattern`.
*/
extglob(): void;
/**
* Parse the given pattern.
*/
parse(pattern: string): parseGlob.Result;
/**
* Escape special characters in the given string.
*/
escape(pattern: string): string;
/**
* Unescape special characters in the given string.
*/
unescape(pattern: string): string;
}
interface GlobData {
pattern: string;
tokens: parseGlob.Result;
options: micromatch.Options;
}
}
interface Micromatch {
(files: string | string[], patterns: micromatch.Pattern | micromatch.Pattern[]): string[];
isMatch: {
/**
* Returns true if a file path matches the given pattern.
*/
(filePath: string, pattern: micromatch.Pattern, opts?: micromatch.Options): boolean;
/**
* Returns a function for matching.
*/
(filePath: string, opts?: micromatch.Options): micromatch.MatchFunction<string>;
};
/**
* Returns true if any part of a file path matches the given pattern. Think of this as "has path" versus
* "is path".
*/
contains(filePath: string, pattern: micromatch.Pattern, opts?: micromatch.Options): boolean;
/**
* Returns a function for matching using the supplied pattern. e.g. create your own "matcher". The advantage of
* this method is that the pattern can be compiled outside of a loop.
*/
matcher(pattern: micromatch.Pattern): micromatch.MatchFunction<string>;
/**
* Returns a function that can be passed to Array#filter().
*/
filter(patterns: micromatch.Pattern | micromatch.Pattern[], opts?: micromatch.Options): micromatch.MatchFunction<any>;
/**
* Returns true if a file path matches any of the given patterns.
*/
any(filePath: string, patterns: micromatch.Pattern | micromatch.Pattern[], opts?: micromatch.Options): boolean;
/**
* Returns an object with a regex-compatible string and tokens.
*/
expand(pattern: string, opts?: micromatch.Options): micromatch.Glob | micromatch.GlobData;
/**
* Create a regular expression for matching file paths based on the given pattern.
*/
makeRe(pattern: string, opts?: micromatch.Options): RegExp;
}
const micromatch: Micromatch;
export = micromatch;
}
+24
View File
@@ -0,0 +1,24 @@
/// <reference path="./parse-glob.d.ts" />
import parseGlob = require('parse-glob');
var result: parseGlob.Result = parseGlob('a/b/c/**/*.{yml,json}');
var stringValue: string;
var boolValue: boolean;
stringValue = result.base;
stringValue = result.glob;
boolValue = result.is.braces;
boolValue = result.is.brackets;
boolValue = result.is.dotdir;
boolValue = result.is.dotfile;
boolValue = result.is.extglob;
boolValue = result.is.glob;
boolValue = result.is.globstar;
boolValue = result.is.negated;
stringValue = result.orig;
stringValue = result.path.basename;
stringValue = result.path.dirname;
stringValue = result.path.ext;
stringValue = result.path.extname;
stringValue = result.path.filename;
+92
View File
@@ -0,0 +1,92 @@
// Type definitions for parse-glob 3.0.4
// Project: https://github.com/jonschlinkert/parse-glob
// Definitions by: glen-84 <https://github.com/glen-84>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module 'parse-glob' {
namespace parseGlob {
interface Result {
/**
* A copy of the original, unmodified glob pattern.
*/
orig: string;
/**
* An object with boolean information about the glob.
*/
is: {
/**
* True if the pattern actually is a glob pattern.
*/
glob: boolean;
/**
* True if it's a negation pattern (!/foo.js).
*/
negated: boolean;
/**
* True if it has extglobs (@(foo|bar)).
*/
extglob: boolean;
/**
* True if it has braces ({1..2} or .{txt,md}).
*/
braces: boolean;
/**
* True if it has POSIX brackets ([[:alpha:]]).
*/
brackets: boolean;
/**
* True if the pattern has a globstar (double star, **).
*/
globstar: boolean;
/**
* True if the pattern should match dotfiles.
*/
dotfile: boolean;
/**
* True if the pattern should match dot-directories (like .git).
*/
dotdir: boolean;
};
/**
* The glob pattern part of the string, if any.
*/
glob: string;
/**
* The non-glob part of the string, if any.
*/
base: string;
/**
* File path segments.
*/
path: {
/**
* Directory.
*/
dirname: string;
/**
* File name with extension.
*/
basename: string;
/**
* File name without extension.
*/
filename: string;
/**
* File extension with dot.
*/
extname: string;
/**
* File extension without dot.
*/
ext: string;
};
}
}
interface ParseGlob {
(glob: string): parseGlob.Result;
}
const parseGlob: ParseGlob;
export = parseGlob;
}