3
\$\begingroup\$

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
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Good point on the optional user validator, that could definitely be useful. \$\endgroup\$ Commented Mar 6, 2014 at 15:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.