UNPKG

tiny-uri

Version:

Lightweight Javascript library for handling URLs

42 lines 6.97 kB
{ "name": "tiny-uri", "version": "9.1.5", "description": "Lightweight Javascript library for handling URLs", "keywords": [ "Lightweight", "Javascript", "library", "es2015", "URLs" ], "repository": "https://github.com/robhicks/tiny-uri.git", "browser": "dist/TinyUri.js", "main": "index.js", "module": "dist/tiny-uri.js", "author": "rob@hixfamily.org", "license": "MIT", "devDependencies": { "@rollup/plugin-commonjs": "^21.0.3", "@rollup/plugin-node-resolve": "^13.1.3", "cypress": "^9.5.3", "esdoc": "^1.1.0", "esdoc-standard-plugin": "^1.0.0", "eslint": "^8.13.0", "eslint-config-standard": "^16.0.3", "eslint-plugin-import": "^2.26.0", "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^6.0.0", "rollup": "^2.70.1", "rollup-plugin-terser": "^7.0.2" }, "dependencies": {}, "scripts": { "build": "npx rollup -c", "cypress:open": "npx cypress open", "cypress:run": "cypress run", "docs": "npx esdoc -c .esdoc.json", "start": "rollup -cw", "test": "npx cypress run" }, "readme": "# tiny-uri\n\ntiny-uri is yet another Javascript library for working with URLs. It offers a [fluent interface](<https://en.wikipedia.org/wiki/Fluent_interface>), method chaining, and sensible means of manipulating Url parts and a file size of less than 5K, gzipped.\n\n# Installation\n\n```shell\nnpm install https://github.com/robhicks/tiny-uri.git\n\n# or\n\nyarn add https://github.com/robhicks/tiny-uri.git\n```\n\n# Use\n\nYou can use the library in the browser or on NodeJs.\n\nIn the browser you can load dist/tiny-uri.iife.js in a script tag, or if you are using an es6 aware bundler like RollupJs, you can import it into your entry file.\n\nIn NodeJs, require it as usual.\n\n# Examples\n\n## Getting URL parts\n\n```javascript\nimport TinyUri from 'tiny-uri';\n\nconst uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');\n\nconsole.log('scheme', uri.scheme()); // 'http'\nconsole.log('host', uri.host()); // 'example.org'\nconsole.log('path', uri.path.toString()); // /path/to/foo/index.html\nconsole.log('query', uri.query.toString()); // hello=world\n```\n\n## Manipulating URL parts\n\n```javascript\nimport TinyUri from 'tiny-uri';\n\nconst uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');\n\nuri\n .scheme('https')\n .host('example.com')\n .authority('user:password@example.com')\n .path.set('path/to/bar/index.html')\n .query.merge({q, 'foo'});\n\nconsole.log('scheme', uri.scheme()); // 'https'\nconsole.log('host', uri.host()); // 'example.com'\nconsole.log('authority', uri.authority()) // 'user:password@example.com'\nconsole.log('path', uri.path.toString()); // /path/to/bar/index.html\nconsole.log('query', uri.query.toString()); // hello=world\n```\n\n## Manipulating paths\n\n```JavaScript\n// remove the last segment of a path\n\nconst url = 'https://user:pass@big.example.com/path/to/file.xml';\nconst uri = new TinyUri(url);\n\nexpect(uri.path.delete().path.toString()).to.be.equal('to/file.xml');\n```\n\n```JavaScript\n// remove a specific segment of the the path\n\nconst url = 'https://user:pass@big.example.com/path/to/file.xml';\nconst uri = new TinyUri(url);\n\nexpect(uri.path.delete(0).path.toString()).to.be.equal('to/file.xml');\n```\n\n```JavaScript\n// should remove several segments of the the path\n// delete accepts an array of segment locations. segment locations must be\n// in ascending order and within the range of path segments\n\nconst url = 'https://user:pass@big.example.com/really/long/path/to/file.xml';\nconst uri = new TinyUri(url);\n\nexpect(uri.path.delete([0, 1, 2, 3]).path.toString()).to.be.equal('file.xml');\n```\n\n## Manipulating query strings\n\n```javascript\nimport TinyUri from 'tiny-uri';\n\nconst uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');\n\nconsole.log('query', uri.query.toString()); // hello=world\n```\n\n### Setting the query string\n\n```javascript\nimport TinyUri from 'tiny-uri';\n\nconst uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');\n\nuri.query.set({goodby: 'world'});\n\nconsole.log('query', uri.query.toString()); // goodby=world\n```\n\n### Clearing the query string\n\n```javascript\nimport TinyUri from 'tiny-uri';\n\nconst uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');\n\nuri.query.clear();\n\nconsole.log('query', uri.query.toString()); //\n```\n\n### Adding to the query string\n\n```javascript\nimport TinyUri from 'tiny-uri';\n\nconst uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');\n\nuri.query.add({goodby: 'world'});\n\nconsole.log('query', uri.query.toString()); // hello=world&goodby=world\n```\n\n### Merging the query string\n\n```javascript\nimport TinyUri from 'tiny-uri';\n\nconst uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');\n\nuri.\n query.add({goodby: 'world'})\n query.merge({hello: 'universe'})\n\nconsole.log('query', uri.query.toString()); // hello=universe&goodby=world\n```\n\n### Getting a specific query string parameter\n\nLet's say you want to get the value of a context query string parameter\n\n```JavaScript\nconst url = 'https://user:pass@big.example.com/path/to/file.xml?context=foo&credentials=bar';\nconst uri = new TinyUri(url);\nconst credentials = uri.query.get('credentials');\nconst context = uri.query.get('context');\nconst hot = uri.query.get('hot');\n\nconsole.log(credentials === 'bar') // true\nconsole.log(context === 'foo') // true\nconsole.log(hot === null) // true\n```\n\n## Chaining Methods\n\nYou can chain Uri, Path and Query methods.\n\n```javascript\nexpect(uri.scheme().toString()).toBe('https');\nexpect(uri.host().toString()).toBe('big.example.com');\nexpect(uri.port().toString()).toBe('');\nexpect(Array.isArray(uri.path.get())).toEqual(true);\nexpect(uri.path.toString()).toEqual('path/to/file.xml');\nexpect(uri.query).toEqual(jasmine.any(Object));\nexpect(uri.query.add({foo: 'bar'}).query.toString()).toEqual('context=foo&credentials=bar&foo=bar');\nexpect(uri.query.add({foo: 'bar'}).query.merge({foo: 'bars'}).query.toString()).toEqual('context=foo&credentials=bar&foo=bars');\nexpect(uri.query.clear().query.add({foo: 'bar'}).query.merge({foo: 'bars'}).query.toString()).toEqual('foo=bars');\nexpect(uri.query.clear().query.add({foo: 'bar'}).query.merge({foo: 'bars'}).query.toString(true)).toEqual('https://user:pass@big.example.com/path/to/file.xml?foo=bars');\n```\n\n# Changes\n\n- 9.0.0 - the library hasn't changed but the build system has. This library only supports modern browsers and Node JS. Importing the library in the browser, pulls in an minified esm build. If you want to import the esm source directly, use ```import TinyUri from 'tiny-uri/src/TinyUri.js'```. In node, you can use the default cjs or if you are using esm ```import TinyUri from 'tiny-uri/index.mjs'```.\n" }