-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new .build addon (thanks to @LeaVerou)
- Loading branch information
Showing
10 changed files
with
187 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/*! | ||
* XRegExp.build v0.1.0-beta | ||
* Copyright 2012 Steven Levithan <http://xregexp.com/> | ||
* Available under the MIT License | ||
* Based on RegExp.create by Lea Verou <http://lea.verou.me/2011/03/create-complex-regexps-more-easily/> | ||
*/ | ||
|
||
(function (XRegExp) { | ||
"use strict"; | ||
|
||
var data = null; | ||
|
||
XRegExp.install("extensibility"); | ||
|
||
/** | ||
* Builds complex regular expressions using named subpatterns for readability and code reuse. | ||
* @memberOf XRegExp | ||
* @param {String} pattern XRegExp pattern using `{{..}}` for embedded subpatterns. | ||
* @param {Object} subs Named subpatterns as strings or regexes. Leading ^ and trailing $ are | ||
* stripped from subpatterns provided as regex objects. | ||
* @param {String} [flags] Any combination of XRegExp flags. | ||
* @returns {RegExp} Extended regular expression object. | ||
* @example | ||
* | ||
* var color = XRegExp.build("{{keyword}}|{{func}}|{{hex}}", { | ||
* keyword: /^(?:red|tan|[a-z]{4,20})$/, | ||
* func: XRegExp.build("^(?:rgb|hsl)a?\\((?:\\s*{{number}}%?\\s*,?\\s*){3,4}\\)$", { | ||
* number: /^-?\d+(?:\.\d+)?$/ | ||
* }), | ||
* hex: /^#(?:[0-9a-f]{1,2}){3}$/ | ||
* }); | ||
*/ | ||
XRegExp.build = function (pattern, subs, flags) { | ||
var p, regex; | ||
data = {}; | ||
for (p in subs) { | ||
data[p] = XRegExp.isRegExp(subs[p]) ? subs[p].source.replace(/^\^|\$$/g, "") : subs[p]; | ||
} | ||
try { | ||
regex = XRegExp(pattern, flags); | ||
} catch (err) { | ||
throw err; | ||
} finally { | ||
data = null; // Reset | ||
} | ||
return regex; | ||
}; | ||
|
||
XRegExp.addToken( | ||
/{{([\w$]+)}}/, | ||
function (match) { | ||
if (data[match[1]] === undefined) { | ||
throw new ReferenceError("unknown property: " + match[1]); | ||
} | ||
return "(?:" + data[match[1]] + ")"; | ||
}, | ||
{ | ||
trigger: function () { | ||
return !!data; | ||
}, | ||
scope: "all" | ||
} | ||
); | ||
|
||
}(XRegExp)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.