opusdev/vector-similarity-api
1
1'use strict';2 3import utils from '../utils.js';4import AxiosURLSearchParams from '../helpers/AxiosURLSearchParams.js';5 6/**7 * It replaces all instances of the characters `:`, `$`, `,`, `+`, `[`, and `]` with their8 * URI encoded counterparts9 *10 * @param {string} val The value to be encoded.11 *12 * @returns {string} The encoded value.13 */14function encode(val) {15 return encodeURIComponent(val).16 replace(/%3A/gi, ':').17 replace(/%24/g, '$').18 replace(/%2C/gi, ',').19 replace(/%20/g, '+');20}21 22/**23 * Build a URL by appending params to the end24 *25 * @param {string} url The base of the url (e.g., http://www.google.com)26 * @param {object} [params] The params to be appended27 * @param {?(object|Function)} options28 *29 * @returns {string} The formatted url30 */31export default function buildURL(url, params, options) {32 if (!params) {33 return url;34 }35 36 const _encode = options && options.encode || encode;37 38 const _options = utils.isFunction(options) ? {39 serialize: options40 } : options;41 42 const serializeFn = _options && _options.serialize;43 44 let serializedParams;45 46 if (serializeFn) {47 serializedParams = serializeFn(params, _options);48 } else {49 serializedParams = utils.isURLSearchParams(params) ?50 params.toString() :51 new AxiosURLSearchParams(params, _options).toString(_encode);52 }53 54 if (serializedParams) {55 const hashmarkIndex = url.indexOf("#");56 57 if (hashmarkIndex !== -1) {58 url = url.slice(0, hashmarkIndex);59 }60 url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;61 }62 63 return url;64}65 