From 1c0a4da2e5660c72c7e4ba9725426c351dc10b02 Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Tue, 1 Apr 2014 21:07:11 +0200 Subject: [PATCH] added definitions for XRegExp --- README.md | 1 + xregexp/xregexp-tests.ts | 134 +++++++++++++++++++++++++++++++++++++++ xregexp/xregexp.d.ts | 62 ++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 xregexp/xregexp-tests.ts create mode 100644 xregexp/xregexp.d.ts diff --git a/README.md b/README.md index 30c81dd14..214494c3e 100755 --- a/README.md +++ b/README.md @@ -287,6 +287,7 @@ List of Definitions * [WinJS](http://msdn.microsoft.com/en-us/library/windows/apps/br229773.aspx) (from TypeScript samples) * [WinRT](http://msdn.microsoft.com/en-us/library/windows/apps/br211377.aspx) (from TypeScript samples) * [ws](http://einaros.github.io/ws/) (by [Paul Loyd](https://github.com/loyd)) +* [XRegExp](http://xregexp.com/) (by [Bart van der Schoor](https://github.com/Bartvds)) * [YouTube](https://developers.google.com/youtube/) (by [Daz Wilkin](https://github.com/DazWilkin/)) * [YouTube Analytics API](https://developers.google.com/youtube/analytics/) (by [Frank M](https://github.com/sgtfrankieboy)) * [YouTube Data API](https://developers.google.com/youtube/v3/) (by [Frank M](https://github.com/sgtfrankieboy/)) diff --git a/xregexp/xregexp-tests.ts b/xregexp/xregexp-tests.ts new file mode 100644 index 000000000..62d37439e --- /dev/null +++ b/xregexp/xregexp-tests.ts @@ -0,0 +1,134 @@ +/// + +import X = require('xregexp'); +import XRegExp = X.XRegExp; +import TokenOpts = X.TokenOpts; + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +var exp: RegExp; +var expArr: RegExp[]; +var chain: RegExp[]; +var regex: RegExp; +var value: any; +var str: string; +var scope: string; +var search: string; +var searchEx: RegExp; +var bool: boolean; +var strArr: string[]; +var pattern: string; +var flags: string; +var right: string; +var left: string; +var sticky: boolean; +var pos: number; +var num: number; +var limit: number; +var obj: Object; +var matchArr: RegExpExecArray; +var options: TokenOpts; +var replacer: Function; + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +regex = XRegExp(str); +regex = XRegExp(str, flags); +regex = XRegExp(regex); + +str = XRegExp.version; + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +XRegExp.addToken(regex, (arr, scope) => { + matchArr = arr; + str = scope; + return str; +}); + +XRegExp.addToken(regex, (arr, scope) => { + matchArr = arr; + str = scope; + return str; +}, options); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +regex = XRegExp.build(pattern, strArr, flags); +regex = XRegExp.build(pattern, strArr); +regex = XRegExp.cache(pattern); +regex = XRegExp.cache(pattern, flags); + +str = XRegExp.escape(str); + +matchArr = XRegExp.exec(str, regex, pos, sticky); +matchArr = XRegExp.exec(str, regex, pos); +matchArr = XRegExp.exec(str, regex); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +matchArr = XRegExp.forEach(str, regex, (match, index, input, regexp) => { + exp = regexp; + str = input; + num = index; + matchArr = match; +}, obj); + +matchArr = XRegExp.forEach(str, regex, (match, index, input, regexp) => { + exp = regexp; + str = input; + num = index; + matchArr = match; +}); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +regex = XRegExp.globalize(regex); + +XRegExp.install(str); +XRegExp.install(obj); + +bool = XRegExp.isInstalled(str); +bool = XRegExp.isRegExp(value); +strArr = XRegExp.matchChain(str, chain); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +strArr = XRegExp.matchRecursive(str, left, right, flags, options); +strArr = XRegExp.matchRecursive(str, left, right, flags); +strArr = XRegExp.matchRecursive(str, left, right); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- +str = XRegExp.replace(str, search, str, scope); +str = XRegExp.replace(str, search, str); +str = XRegExp.replace(str, search, replacer, scope); +str = XRegExp.replace(str, search, replacer); + +str = XRegExp.replace(str, searchEx, str, scope); +str = XRegExp.replace(str, searchEx, str); +str = XRegExp.replace(str, searchEx, replacer, scope); +str = XRegExp.replace(str, searchEx, replacer); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +strArr = XRegExp.split(str, search, limit); +strArr = XRegExp.split(str, search); +strArr = XRegExp.split(str, searchEx, limit); +strArr = XRegExp.split(str, searchEx); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +bool = XRegExp.test(str, regex, pos, bool); +bool = XRegExp.test(str, regex, pos); +bool = XRegExp.test(str, regex); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +XRegExp.uninstall(obj); +XRegExp.uninstall(str); + +regex = XRegExp.union(strArr, flags); +regex = XRegExp.union(strArr); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + diff --git a/xregexp/xregexp.d.ts b/xregexp/xregexp.d.ts new file mode 100644 index 000000000..9e30a2d07 --- /dev/null +++ b/xregexp/xregexp.d.ts @@ -0,0 +1,62 @@ +// Type definitions for XRegExp 2.0.0 +// Project: http://xregexp.com +// Definitions by: Bart van der Schoor +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module 'xregexp' { + // scopes: 'default', 'class', or 'all' + /* + Native flags: + g - global + i - ignore case + m - multiline anchors + y - sticky (Firefox 3+) + Additional XRegExp flags: + n - explicit capture + s - dot matches all (aka singleline) + x - free-spacing and line comments (aka extended) + */ + export interface TokenOpts { + scope?: string; + trigger?: () => boolean; + customFlags?: string; + } + + export function XRegExp(pattern: string, flags?: string): RegExp; + export function XRegExp(pattern: RegExp): RegExp; + + export module XRegExp { + function addToken(regex: RegExp, handler: (matchArr: RegExpExecArray, scope: string) => string, options?: TokenOpts): void; + + function build(pattern: string, subs: string[], flags?: string): RegExp; + function cache(pattern: string, flags?: string): RegExp; + function escape(str: string): string; + function exec(str: string, regex: RegExp, pos?: number, sticky?: boolean): RegExpExecArray; + function forEach(str: string, regex: RegExp, callback: (matchArr: RegExpExecArray, index: number, input: string, regexp: RegExp) => void, context?: Object): any; + function globalize(regex: RegExp): RegExp; + + function install(options: string): void; + function install(options: Object): void; + + function isInstalled(feature: string): boolean; + function isRegExp(value: any): boolean; + function matchChain(str: string, chain: RegExp[]): string[]; + function matchRecursive(str: string, left: string, right: string, flags?: string, options?: Object): string[]; + + function replace(str: string, search: string, replacement: string, scope?: string): string; + function replace(str: string, search: string, replacement: Function, scope?: string): string; + function replace(str: string, search: RegExp, replacement: string, scope?: string): string; + function replace(str: string, search: RegExp, replacement: Function, scope?: string): string; + + function split(str: string, separator: string, limit?: number): string[]; + function split(str: string, separator: RegExp, limit?: number): string[]; + + function test(str: string, regex: RegExp, pos?: number, sticky?: boolean): boolean; + + function uninstall(options: Object): void; + function uninstall(options: string): void; + + function union(patterns: string[], flags?: string): RegExp; + var version: string; + } +}