UNPKG

url-params-helper

Version:

A helper for URL parameters/query strings.

160 lines (137 loc) 4.65 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define("url-params", [], factory); else if(typeof exports === 'object') exports["url-params"] = factory(); else root["url-params"] = factory(); })(this, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getParams = getParams; exports.getParam = getParam; exports.buildQueryString = buildQueryString; exports.buildQueryStringURL = buildQueryStringURL; exports.replaceParam = replaceParam; exports.removeParam = removeParam; function getParams(pageURL) { var url = pageURL || window.location.href; var queryString = url.split('?')[1]; var rawParams = typeof queryString === 'string' && queryString.length > 0 ? queryString.split('&') : ''; var params = {}; for (var i = 0; i < rawParams.length; i++) { var rawParam = rawParams[i]; var param = rawParam.split('='); var paramName = param[0]; var paramValue = param[1]; if (paramName.indexOf('[]') === -1) { // Handle literal params if (paramValue && paramValue !== '') { params[paramName] = decodeURIComponent(paramValue); } } else { // Handle array params paramName = paramName.replace(/[\[|\]]/g, ''); if (params[paramName] === undefined) { params[paramName] = []; } params[paramName].push(decodeURIComponent(paramValue)); } } return Object.keys(params).length > 0 ? params : null; } function getParam(param, pageURL) { var params = getParams(pageURL); return params ? params[param] : null; } function buildQueryString(params) { var queryString = []; for (var param in params) { if (params.hasOwnProperty(param)) { (function () { var paramName = encodeURIComponent(param); var paramValue = params[param]; if (paramValue !== undefined) { if (paramValue instanceof Array) { /* eslint-disable no-loop-func */ paramValue.map(function (v) { queryString.push(paramName + '[]=' + (v === undefined || v === null ? '' : encodeURIComponent(v))); }); } else { queryString.push(paramName + '=' + (paramValue === null ? '' : encodeURIComponent(paramValue))); } } })(); } } return queryString.join('&'); } function buildQueryStringURL(params, pageURL) { var url = (pageURL || window.location.href).split('?')[0]; var queryString = buildQueryString(params); return url + (queryString.length > 0 ? '?' + queryString : ''); } function replaceParam(param, value, pageURL) { var params = getParams(pageURL) || {}; params[param] = value; return buildQueryStringURL(params, pageURL); } function removeParam(param, pageURL) { var params = getParams(pageURL) || {}; delete params[param]; return buildQueryStringURL(params, pageURL); } /***/ } /******/ ]) }); ; //# sourceMappingURL=url-params.js.map