patternjavascriptMinor
Validate that a relative path exists or is an external URI
Viewed 0 times
validatepathurithatexistsexternalrelative
Problem
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));
}Solution
Very interesting,
I only have 1 snarky comment, if
Also, as a user of this function, I might want to override your regex so you might want to provide support for that.
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.
Context
StackExchange Code Review Q#43405, answer score: 2
Revisions (0)
No revisions yet.