Cscott has uploaded a new change for review. https://gerrit.wikimedia.org/r/180876
Change subject: Add `JSUtils.rejoin` method to make complicated regexps more readable. ...................................................................... Add `JSUtils.rejoin` method to make complicated regexps more readable. Change-Id: I494640eceeef62cc9f7e1459427216f369419f31 --- M lib/jsutils.js 1 file changed, 29 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid refs/changes/76/180876/1 diff --git a/lib/jsutils.js b/lib/jsutils.js index 7fc85cf..2952814 100644 --- a/lib/jsutils.js +++ b/lib/jsutils.js @@ -59,6 +59,35 @@ .replace( /\+/g, "-" ); }, + // Join pieces of regular expressions together. This helps avoid + // having to switch between string and regexp quoting rules, and + // can also give you a poor-man's version of the "x" flag, ie: + // var re = rejoin( "(", + // /foo|bar/, "|", + // someRegExpFromAVariable + // ")", { flags: "i" } ); + // Note that this is basically string concatenation, except that + // regular expressions are converted to strings using their `.source` + // property, and then the final resulting string is converted to a + // regular expression. + // If the final argument is a regular expression, its flags will be + // used for the result. Alternatively, you can make the final argument + // an object, with a `flags` property (as shown in the example above). + rejoin: function() { + var regexps = Array.prototype.slice.call(arguments); + var last = regexps[regexps.length-1], flags; + if (typeof(last)==='object') { + if (last instanceof RegExp) { + flags = /\/([gimy]*)$/.exec(last.toString())[1]; + } else { + flags = regexps.pop().flags; + } + } + return new RegExp(regexps.map(function(r) { + return (r instanceof RegExp) ? r.source : (r+''); + }).join(''), flags === undefined ? '' : flags); + }, + // Append an array to an accumulator using the most efficient method // available. Makes sure that accumulation is O(n). pushArray : function push (accum, arr) { -- To view, visit https://gerrit.wikimedia.org/r/180876 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I494640eceeef62cc9f7e1459427216f369419f31 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/parsoid Gerrit-Branch: master Gerrit-Owner: Cscott <canan...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits