/*!1* encodeurl2* Copyright(c) 2016 Douglas Christopher Wilson3* MIT Licensed4*/56'use strict'78/**9* Module exports.10* @public11*/1213module.exports = encodeUrl1415/**16* RegExp to match non-URL code points, *after* encoding (i.e. not including "%")17* and including invalid escape sequences.18* @private19*/2021var ENCODE_CHARS_REGEXP = /(?:[^\x21\x25\x26-\x3B\x3D\x3F-\x5B\x5D\x5F\x61-\x7A\x7E]|%(?:[^0-9A-Fa-f]|[0-9A-Fa-f][^0-9A-Fa-f]))+/g2223/**24* RegExp to match unmatched surrogate pair.25* @private26*/2728var UNMATCHED_SURROGATE_PAIR_REGEXP = /(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g2930/**31* String to replace unmatched surrogate pair with.32* @private33*/3435var UNMATCHED_SURROGATE_PAIR_REPLACE = '$1\uFFFD$2'3637/**38* Encode a URL to a percent-encoded form, excluding already-encoded sequences.39*40* This function will take an already-encoded URL and encode all the non-URL41* code points. This function will not encode the "%" character unless it is42* not part of a valid sequence (`%20` will be left as-is, but `%foo` will43* be encoded as `%25foo`).44*45* This encode is meant to be "safe" and does not throw errors. It will try as46* hard as it can to properly encode the given URL, including replacing any raw,47* unpaired surrogate pairs with the Unicode replacement character prior to48* encoding.49*50* @param {string} url51* @return {string}52* @public53*/5455function encodeUrl (url) {56return String(url)57.replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE)58.replace(ENCODE_CHARS_REGEXP, encodeURI)59}606162