├── LICENSE ├── README.md ├── index.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2019, Andrey Raut 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # urls-tool 2 | A little script that will help you work with the url `path` / `params` / `hash` without reloading the page. 3 | 4 | ## Installing 5 | Using npm: 6 | ``` 7 | $ npm install urls-tool 8 | ``` 9 | 10 | Using yarn: 11 | ``` 12 | $ yarn add urls-tool 13 | ``` 14 | 15 | ## Example 16 | #### Easily get or change url pathname. 17 | ``` 18 | import Url from "urls-tool"; 19 | 20 | const { pathname } = Url; 21 | // Variable `pathname` will be have the pathname value in string. 22 | 23 | Url.pathname = "your path name"; 24 | // Pathname will be changed. 25 | 26 | ``` 27 | #### Effortlessly get or set (just one or all at once) params 28 | ``` 29 | import Url from "urls-tool"; 30 | 31 | const { params } = Url; 32 | // Variable `params` will be have the object with all params in shape `{ "paramsName": "paramsValue" }`. 33 | 34 | const { 35 | array, // Array of all params. [{ name: "paramsName", value: "paramsValue" }] 36 | object, // The same object as in the previous method 37 | string // String of params without "?". 38 | } = Url.getParams(); 39 | 40 | Url.params = ["paramsName", "paramsValue"]; 41 | // Use to set only one params item. Also it will be removed in the absence or FALSE value of "paramsValue". 42 | 43 | Url.params = { "paramsName": "paramsValue" }; 44 | // Use to set all params at once. All params will be cleared in case of an empty object. 45 | ``` 46 | #### No problem get or set the hash 47 | ``` 48 | import Url from "urls-tool"; 49 | 50 | const { hash } = Url; 51 | // Variable `hash` will be have the hash value in string without "#". 52 | 53 | Url.pathname = "your path name"; 54 | // Hash will be changed. And you don't need to add "#" 55 | ``` 56 | ## License 57 | ISC -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | class Url { 2 | static getParams() { 3 | try { 4 | if (!window.location.search) { 5 | return { array: [], object: {}, string: '' }; 6 | } 7 | const paramsString = window.location.search.substring(1); 8 | const paramsArray = paramsString.split('&'); 9 | const paramsObject = {}; 10 | 11 | for (let i = 0; i < paramsArray.length; i++) { 12 | const name = paramsArray[i].split('=')[0]; 13 | const value = paramsArray[i].split('=')[1]; 14 | 15 | paramsArray[i] = { name, value }; 16 | paramsObject[name] = value; 17 | } 18 | 19 | return { array: paramsArray, object: paramsObject, string: paramsString }; 20 | } catch (err) { 21 | console.log('Error in Url.getParams method:', err); 22 | return { array: [], object: {}, string: '' }; 23 | } 24 | } 25 | 26 | static get params() { 27 | return this.getParams().object; 28 | } 29 | 30 | static set params(arg = []) { 31 | try { 32 | let obj = {}; 33 | 34 | if (Array.isArray(arg)) { 35 | const [name, value] = arg; 36 | if (!name) { return; } 37 | 38 | obj = this.getParams().object; 39 | 40 | obj[name] = value; 41 | 42 | if (!value || !obj[name]) { 43 | delete obj[name]; 44 | } 45 | } else if (typeof arg === 'object') { 46 | obj = arg; 47 | } 48 | 49 | const arr = Object.keys(obj).map(key => ( 50 | `${key}=${obj[key]}` 51 | )); 52 | 53 | window.history.pushState(null, null, `${window.location.pathname}${arr.length ? '?' : ''}${arr.join('&')}${window.location.hash}`); 54 | } catch (err) { 55 | console.log('Error in Url.params(set) method:', err); 56 | } 57 | } 58 | 59 | static get hash() { 60 | try { 61 | const hash = window.location.hash.substring(1); 62 | return hash; 63 | } catch (err) { 64 | console.log('Error in Url.hash(get) method:', err); 65 | return undefined; 66 | } 67 | } 68 | 69 | static set hash(value) { 70 | try { 71 | window.location.hash = value; 72 | return true; 73 | } catch (err) { 74 | console.log('Error in Url.hash(set) method:', err); 75 | return false; 76 | } 77 | } 78 | 79 | static get pathname() { 80 | try { 81 | const pathname = window.location.pathname; 82 | return pathname; 83 | } catch (err) { 84 | console.log('Error in Url.pathname(get) method:', err); 85 | return ''; 86 | } 87 | } 88 | 89 | static set pathname(value) { 90 | try { 91 | window.history.pushState(null, null, `${value}${window.location.search}${window.location.hash}`); 92 | return true; 93 | } catch (err) { 94 | console.log('Error in Url.pathname(set) method:', err); 95 | return false; 96 | } 97 | } 98 | } 99 | 100 | module.exports = Url; 101 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "urls-tool", 3 | "version": "1.0.1", 4 | "description": "Tool for simple work with url, params, hash", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "-" 8 | }, 9 | "bugs": { 10 | "url": "https://github.com/raut-andrey/urls-tool/issues" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/raut-andrey/urls-tool.git" 15 | }, 16 | "homepage": "https://github.com/raut-andrey/urls-tool", 17 | "keywords": [ 18 | "url", 19 | "params", 20 | "hash", 21 | "query", 22 | "href", 23 | "path" 24 | ], 25 | "author": "andrey-raut", 26 | "readmeFilename": "README.md", 27 | "license": "ISC" 28 | } --------------------------------------------------------------------------------