4 const parse = require("url").parse;
7 * Get unit from value node
9 * Returns `null` if the unit is not found.
11 module.exports = function(urlString /*: string*/) /*: ?string*/ {
12 const url = parse(urlString);
13 const protocol = url.protocol;
14 if (protocol === null || typeof protocol === "undefined") {
18 const scheme = protocol.slice(0, -1); // strip trailing `:`
20 // The URL spec does not require a scheme to be followed by `//`, but checking
21 // for it allows this rule to differentiate <scheme>:<hostname> urls from
22 // <hostname>:<port> urls. `data:` scheme urls are an exception to this rule.
23 const slashIndex = protocol.length;
24 const expectedSlashes = urlString.slice(slashIndex, slashIndex + 2);
25 const isSchemeLessUrl = expectedSlashes !== "//" && scheme !== "data";
27 if (isSchemeLessUrl) {