├── .babelrc ├── .gitignore ├── LICENSE ├── README.md ├── lib └── get-query.js └── package.json /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Node template 3 | # Logs 4 | logs 5 | *.log 6 | npm-debug.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | 13 | # Directory for instrumented libs generated by jscoverage/JSCover 14 | lib-cov 15 | 16 | # Coverage directory used by tools like istanbul 17 | coverage 18 | dist 19 | # nyc test coverage 20 | .nyc_output 21 | 22 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 23 | .grunt 24 | 25 | # node-waf configuration 26 | .lock-wscript 27 | 28 | # Compiled binary addons (http://nodejs.org/api/addons.html) 29 | build/Release 30 | 31 | # Dependency directories 32 | node_modules 33 | jspm_packages 34 | 35 | # Optional npm cache directory 36 | .npm 37 | 38 | # Optional REPL history 39 | .node_repl_history 40 | 41 | # IDEA 42 | .idea 43 | 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Sabbir Ahmed 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## When you want to access get query parameters from browser. 2 | 3 | ### Installation 4 | 5 | ```sh 6 | npm install get-query --save 7 | ``` 8 | 9 | ### Example 10 | 11 | Suppose we have link with query parameters: 12 | `http://localhost/home?name=Joe&subs=1,2,3` 13 | 14 | 15 | ```js 16 | 17 | import getQuery from 'get-query'; 18 | 19 | const urlGetParams = getQuery(); // { name: "Joe", subs: [1,2,3] } 20 | 21 | ``` 22 | 23 | 24 | ### License 25 | 26 | The MIT License (MIT) 27 | 28 | Copyright (c) 2016 Sabbir Ahmed 29 | -------------------------------------------------------------------------------- /lib/get-query.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Takes a raw param strings then converts it to int if integer 3 | * If separated with , then return an array 4 | * @param rawParams 5 | * @returns {Array} 6 | */ 7 | function paramsMake(rawParams) { 8 | const param = rawParams.split(',').map(item => { 9 | if (Number.isNaN(parseInt(item, 8))) return item; 10 | return parseInt(item, 8); 11 | }); 12 | return (param.length <= 1) ? param[0] : param; 13 | } 14 | 15 | /** 16 | * @param paramString 17 | * @returns {Object} 18 | */ 19 | function transform(paramString) { 20 | const params = {}; 21 | const paramsArray = paramString.split('&'); 22 | for (let i = 0; i < paramsArray.length; i++) { 23 | const tmp = paramsArray[i].split('='); 24 | params[tmp[0]] = paramsMake(tmp[1]); 25 | } 26 | return params; 27 | } 28 | 29 | /** 30 | * gets get parameters 31 | * @returns { Object } 32 | */ 33 | function getParameters() { 34 | const paramString = window.location.search.substr(1); 35 | if (paramString !== null && paramString !== '') { 36 | return transform(paramString); 37 | } 38 | return {}; 39 | } 40 | 41 | export default getParameters; 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "get-query", 3 | "version": "1.0.2", 4 | "description": "Get get request parameter from browser", 5 | "main": "dist/get-query.js", 6 | "scripts": { 7 | "prepublish": "npm run build", 8 | "build": "babel lib --out-dir dist", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "author": "Sabbir Ahmed (https://thesabbir.com)", 12 | "license": "MIT", 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/thesabbir/get-query.git" 16 | }, 17 | "bugs": { 18 | "url": "https://github.com/thesabbir/get-query/issues" 19 | }, 20 | "keywords": [ 21 | "get", 22 | "parameters", 23 | "browser", 24 | "query", 25 | "query-string" 26 | ], 27 | "devDependencies": { 28 | "babel-cli": "^6.5.2", 29 | "babel-preset-es2015": "^6.9.0" 30 | } 31 | } 32 | --------------------------------------------------------------------------------