UNPKG

@ace-util/core

Version:
120 lines 3.74 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setQueryValues = exports.getQueryValues = exports.getQueryValue = exports.absoluteGo = exports.isAbsoluteUrl = exports.trailingSlash = void 0; /** * add slash to the end of the path * @param path path * @returns path with slash */ function trailingSlash(path) { return path.endsWith('/') ? path : "".concat(path, "/"); } exports.trailingSlash = trailingSlash; /** * check if the path is an external link * @param {string} url url * @returns {boolean} true/false */ function isAbsoluteUrl(url) { return /^(https?:\/\/|\/\/)[\w.]+\/?/gi.test(url); } exports.isAbsoluteUrl = isAbsoluteUrl; /** * jump to the specified absolute url * @param {string} url target url * @param {boolean} replace use replace or not */ function absoluteGo(url, replace) { if (replace === void 0) { replace = false; } try { window.location[replace ? 'replace' : 'assign'](url); } catch (e) { window.location.href = url; } } exports.absoluteGo = absoluteGo; /** * Get value from search by specified key. * @param {string} key query key * @param {string} search search, default: location.search */ function getQueryValue(key, search) { var _search = search ? '&' + search : location.search.replace(/(^\?+)|(#\S*$)/g, ''); var query = _search.match(new RegExp('(^|&)' + key + '=([^&]*)(&|$)', 'i')); return !query ? '' : decodeURIComponent(query[2]); } exports.getQueryValue = getQueryValue; /** * Get values from search by specified keys. * @param keys key query keys * @param search search, default: location.search */ function getQueryValues(keys, search) { return keys.reduce(function (prev, curr) { prev[curr] = getQueryValue(curr, search); return prev; }, {}); } exports.getQueryValues = getQueryValues; /** * Set values to uri search * @param values key/value * @param url url string */ function setQueryValues(values, url) { var baseWithSearch = url.split('#')[0]; var hash = url.split('#')[1]; var arrayValues = []; if (Array.isArray(values)) { arrayValues = values; } else { arrayValues = Object.entries(values).map(function (_a) { var _b = __read(_a, 2), key = _b[0], value = _b[1]; return ({ key: key, value: value }); }); } for (var i = 0; i < values.length; i++) { if (arrayValues[i].value !== undefined) { var newParam = arrayValues[i].key + '=' + arrayValues[i].value; if (baseWithSearch.indexOf('?') > 0) { var oldParamReg = new RegExp(arrayValues[i].key + '=[-\\w]{0,40}', 'g'); if (oldParamReg.test(baseWithSearch)) { baseWithSearch = baseWithSearch.replace(oldParamReg, newParam); } else { baseWithSearch += '&' + newParam; } } else { baseWithSearch += '?' + newParam; } } } if (hash) { url = baseWithSearch + '#' + hash; } else { url = baseWithSearch; } return url; } exports.setQueryValues = setQueryValues; //# sourceMappingURL=path.js.map