url-lib
Version:
A simple, lightweight string utility for Node and browsers that supports serializing and parsing URLs and query strings.
87 lines (59 loc) • 5.29 kB
Markdown
# url-lib
[](http://npm.im/url-lib)
[](http://npm-stat.com/charts.html?package=url-lib&from=2016-03-27)

[](https://github.com/semantic-release/semantic-release)
[](http://spdx.org/licenses/MIT)
[](https://github.com/benmvp/url-lib/pulse)
[](https://travis-ci.org/benmvp/url-lib)
[](https://github.com/facebook/jest)
[](https://coveralls.io/github/benmvp/url-lib?branch=master)
[](https://david-dm.org/benmvp/url-lib#info=dependencies)
[](https://david-dm.org/benmvp/url-lib#info=devDependencies)
[](https://greenkeeper.io/)
[](http://makeapullrequest.com)
[](https://github.com/benmvp/url-lib/watchers)
[](https://github.com/benmvp/url-lib/stargazers)
[](https://twitter.com/intent/tweet?text=Check%20out%20url-lib%20by%20%40benmvp!%0A%0Ahttps%3A%2F%2Fgithub.com%2Fbenmvp%2Furl-lib)
A simple, lightweight string utility for Node and browsers that supports serializing and parsing URLs and query strings.
The primary use case is for building string URLs with query parameters for the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) that is polyfilled in the browser via [`fetch`](https://github.com/github/fetch) and in Node via [`node-fetch`](https://github.com/bitinn/node-fetch) libraries. [`isomorphic-fetch`](https://github.com/matthew-andrews/isomorphic-fetch) combines the two.
`url-lib` is derived from the [`Uize.Url`](https://github.com/UIZE/UIZE-JavaScript-Framework/blob/master/site-source/js/Uize/Url.js) module that is a part of the open-source [UIZE JavaScript Framework](https://github.com/UIZE/UIZE-JavaScript-Framework). It is stable, [dependency-free](https://david-dm.org/benmvp/url-lib#info=dependencies), [heavily-tested](https://coveralls.io/github/benmvp/url-lib?branch=master) and [well-documented](docs/).
## Installation
Install via [npm](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
```sh
npm install --save url-lib
```
Install via [Yarn](https://yarnpkg.com/lang/en/docs/managing-dependencies/):
```sh
yarn add url-lib
```
Use with [Node](https://nodejs.org/en/), [webpack](https://webpack.github.io/) or [browserify](http://browserify.org/):
```js
import * as urllib from 'url-lib'; // ES6+
var urllib = require('url-lib'); // ES5-
```
## Usage
```js
import {formatUrl} from 'url-lib'
const url = formatUrl('http://www.benmvp.com/search?sort=recent&results=20&pg=1', {
sort: 'popular', // overwrites existing `sort` param in URL
category: 'holiday',
type: 'all',
results: 100, // overwrites existing `results` param in URL
})
```
With the above code, `url` will be `'http://www.benmvp.com/search?sort=popular&results=100&pg=1&category=holiday&type=all'`
Check out the [docs](docs/) for more usage examples or [try out `url-lib` in your browser!](https://runkit.com/npm/url-lib)
## API Docs
- [`formatQuery`](docs/formatQuery.md) - Serializes the properties of a params object to produce a URL query string.
- [`formatUrl`](docs/formatUrl.md) - Serializes the specified URL path with properties of a params object to produce a URL.
- [`parseQuery`](docs/parseQuery.md) - Parses query parameters from a string, returning the query parameters as an object.
- [`getCacheDefeatStr`](docs/getCacheDefeatStr.md) - Returns a string value (generated using the time and a random number) that can be used as a query parameter value to cause a URL to be unique in order to defeat caching.
- [`parseUrl`](docs/parseUrl.md) - Parses the specified URL string into an object containing properties for the various logical segments.
## Contributing
Contributions are welcome! See [CONTRIBUTING](CONTRIBUTING.md) for more details.
## Project philosophy
We take the stability of this utility package **very** seriously. `url-lib` follows the [SemVer](http://semver.org/) standard for versioning.
All updates must pass the [CI build](https://travis-ci.org/benmvp/url-lib) while maintaining [100% code coverage](https://coveralls.io/github/benmvp/url-lib).
## License
[MIT](LICENSE). Copyright (c) 2016- Ben Ilegbodu.