Skip to content

Commit

Permalink
adds regex helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Jul 4, 2017
1 parent 1efe1ee commit 39c1fbe
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lib/regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

var util = require('handlebars-utils');
var utils = require('./utils');
var helpers = module.exports;

/**
* Convert the given string to a regular expression.
*
* ```handlebars
* {{toRegex "foo"}}
* //=> /foo/
* ```
* @param {String} `str`
* @return {RegExp}
* @api public
*/

helpers.toRegex = function(str, locals, options) {
var opts = util.options({}, locals, options);
return new RegExp(str, opts.flags);
};

/**
* Returns true if the given `str` matches the given regex. A regex can
* be passed on the context, or using the [toRegex](#toregex) helper as a
* subexpression.
*
* ```handlebars
* {{test "bar" (toRegex "foo")}}
* //=> false
* {{test "foobar" (toRegex "foo")}}
* //=> true
* {{test "foobar" (toRegex "^foo$")}}
* //=> false
* ```
* @param {String} `str`
* @return {RegExp}
* @api public
*/

helpers.test = function(str, regex) {
if (!util.isString(str)) {
return false;
}
if (!utils.typeOf(regex) === 'regexp') {
throw new TypeError('expected a regular expression');
}
return regex.test(str);
};

0 comments on commit 39c1fbe

Please sign in to comment.