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));
}
\$\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.

\$\endgroup\$
1
  • \$\begingroup\$ Good point on the optional user validator, that could definitely be useful. \$\endgroup\$
    – megawac
    Mar 6, 2014 at 15:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.