* Make url safe by encoding url components * @param {string} url * @returns {string}
(url)
| 120 | * @returns {string} |
| 121 | */ |
| 122 | safe(url) { |
| 123 | let { url: uri, query } = this.parse(url); |
| 124 | url = uri; |
| 125 | const protocol = (this.PROTOCOL_PATTERN.exec(url) || [])[0] || ""; |
| 126 | if (protocol) url = url.replace(new RegExp("^" + protocol), ""); |
| 127 | const parts = url.split("/").map((part, i) => { |
| 128 | if (i === 0) return part; |
| 129 | return fixedEncodeURIComponent(part); |
| 130 | }); |
| 131 | return protocol + parts.join("/") + query; |
| 132 | |
| 133 | function fixedEncodeURIComponent(str) { |
| 134 | return encodeURIComponent(str).replace(/[!'()*]/g, function (c) { |
| 135 | return "%" + c.charCodeAt(0).toString(16); |
| 136 | }); |
| 137 | } |
| 138 | }, |
| 139 | /** |
| 140 | * Gets pathname from url eg. gets '/foo/bar' from 'ftp://myhost.com/foo/bar' |
| 141 | * @param {string} url |
nothing calls this directly
no test coverage detected