source: trip-planner-front/node_modules/stylus/lib/functions/match.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.0 KB
RevLine 
[6a3a178]1var utils = require('../utils')
2 , nodes = require('../nodes');
3
4var VALID_FLAGS = 'igm';
5
6/**
7 * retrieves the matches when matching a `val`(string)
8 * against a `pattern`(regular expression).
9 *
10 * Examples:
11 * $regex = '^(height|width)?([<>=]{1,})(.*)'
12 *
13 * match($regex,'height>=sm')
14 * // => ('height>=sm' 'height' '>=' 'sm')
15 * // => also truthy
16 *
17 * match($regex, 'lorem ipsum')
18 * // => null
19 *
20 * @param {String} pattern
21 * @param {String|Ident} val
22 * @param {String|Ident} [flags='']
23 * @return {String|Null}
24 * @api public
25 */
26
27function match(pattern, val, flags){
28 utils.assertType(pattern, 'string', 'pattern');
29 utils.assertString(val, 'val');
30 var re = new RegExp(pattern.val, validateFlags(flags) ? flags.string : '');
31 return val.string.match(re);
32}
33match.params = ['pattern', 'val', 'flags'];
34module.exports = match;
35
36function validateFlags(flags) {
37 flags = flags && flags.string;
38
39 if (flags) {
40 return flags.split('').every(function(flag) {
41 return ~VALID_FLAGS.indexOf(flag);
42 });
43 }
44 return false;
45}
Note: See TracBrowser for help on using the repository browser.