\$\begingroup\$
\$\endgroup\$
I sent a PR to a repo the other day and want to make sure I have the right idea for URI validation.
function validateURI(files) {
//...
// files can be either a string or an array of paths
// validates the relative path exists or is an http(s) URI
// returns a filtered list of valid paths
// (e.g.) ["../dne.js", "//cdn.jsdelivr.net/jquery/2.1.0/jquery.min.js"] => ["//cdn.jsdelivr.net/jquery/2.1.0/jquery.min.js"]
if(typeof files !== "object") files = [files]; //wrap string
var local = grunt.file.expand(opt, files),
remote = _.map(files, path.normalize).filter(function(path) { //for loading from cdn
return /^((http|https):)?(\\|\/\/)/.test(path); //is http, https, or //
});
return _.uniq(local.concat(remote));
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Mar 4, 2014 at 14:15
1 Answer 1
\$\begingroup\$
\$\endgroup\$
1
Very interesting,
I only have 1 snarky comment, if files
is an array of 'paths', maybe you should call it paths
;)
Also, as a user of this function, I might want to override your regex so you might want to provide support for that.
answered Mar 6, 2014 at 14:08
-
\$\begingroup\$ Good point on the optional user validator, that could definitely be useful. \$\endgroup\$megawac– megawac2014年03月06日 15:57:37 +00:00Commented Mar 6, 2014 at 15:57
default