index.js 694 B

123456789101112131415161718192021222324252627
  1. "use strict";
  2. /**
  3. * protocols
  4. * Returns the protocols of an input url.
  5. *
  6. * @name protocols
  7. * @function
  8. * @param {String} input The input url.
  9. * @param {Boolean|Number} first If `true`, the first protocol will be returned. If number, it will represent the zero-based index of the protocols array.
  10. * @return {Array|String} The array of protocols or the specified protocol.
  11. */
  12. module.exports = function protocols(input, first) {
  13. if (first === true) {
  14. first = 0;
  15. }
  16. var index = input.indexOf("://"),
  17. splits = input.substring(0, index).split("+").filter(Boolean);
  18. if (typeof first === "number") {
  19. return splits[first];
  20. }
  21. return splits;
  22. };