1.0.0 • Published 2 years ago
linkity v1.0.0
linkity v1.0.0
A simple and convenient package for working with a website address. Exported as Node.js modules.
Installation
Using npm:
$ npm i --save linkity
In Node.js:
import linkity from 'linkity';
/* In the browser environment, the default is window.location.href. */
const https = 'https://example.com:8080/path/page?p1=v1&p2=v2#hash';
const ws = 'ws://locahost:5000';
/* Hash */
linkity.url.getHash(https); // #hash
linkity.url.setHash(https, 'new-hash'); // ...path/page?p1=v1&p2=v2#new-hash
linkity.url.addHash(https, '-new-hash'); // ...path/page?p1=v1&p2=v2#hash-new-hash
linkity.url.removeHash(https); // ...path/page?p1=v1&p2=v2
linkity.url.isHashAccess('https://example.com:8080#hash', 'https://example.com:8080'); // true
linkity.url.isHashAccess('https://example.com:8080/path#hash', 'https://example.com:8080'); // false
/* Params */
linkity.url.getParam(https); // ?p1=v1&p2=v2
linkity.url.getParams(https); // ['p1=v1', 'p2=v2']
linkity.url.setParams(https, 'p3=v3'); // ...path/page?p3=v3
linkity.url.removeParams(https); // ...path/page
/* Param */
linkity.url.getParam(https, 'p1'); // v1
linkity.url.setParam(https, 'p1', 'v12345'); // ...path/page?p1=v12345&p2=v2
linkity.url.updateParam(https, 'p1', 'v256'); // ...path/page?p1=v1v256&p2=v2
linkity.url.removeParam(https, 'p1'); // ...path/page?p2=v2
linkity.url.addParam(https, 'p3', 'v3'); // ...path/page?p1=v1&p2=v2&p3=v3
linkity.url.createParam('p1', 'v1'); // p1=v1
linkity.url.createParam('p2', 'v2', true); // ?p2=v2
linkity.url.createParam('p3', 'v3', true, true); // ?p3=v3&
/* Path */
linkity.url.getPath(https); // /path/page
linkity.url.editPath(https, '/hello/world'); // ...example.com:8080/hello/world...
linkity.url.joinPath(https, 'hello', 'world'); // ...example.com:8080/path/page/hello/world...
/* Origin */
linkity.url.getOrigin(https); // https://example.com:8080
/* Host */
linkity.url.getHost(https); // example.com:8080
linkity.url.getHostname(https); // example.com
linkity.url.getPort(https); // 8080
/* Protocol (Not limited to https and http) */
linkity.url.getProtocol(https); // https
linkity.url.getProtocol(ws); // ws
/* Other */
linkity.url.get(https, /path/); // path (Regex result)
linkity.url.getClean(https);// https://example.com:8080/path/page
linkity.url.isValid(https); // true
linkity.url.isValid(ws); // true
const urlObject = linkity.url.toJSON(https);
const url = linkity.url.toSTRING(urlObject);
/* Browser */
const saveHistory = true;
linkity.url.redirect(https, saveHistory);
linkity.url.setHref(https);
linkity.url.getHref();
See the package source for more details.
Note:
- linkity does not work only in the browser and has no dependencies.
- linkity does not control URL encoding.
- linkity does not support
<login>:<pass>@
because it is deprecated, use # (hash) instead of@
.
1.0.0
2 years ago