# url-compiler

> This library allows you to compile composite URLs from templates.

Latest version **1.0.2** (published 2021-12-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install url-compiler
pnpm add url-compiler
yarn add url-compiler
bun add url-compiler
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-12-19 |
| First published | 2020-03-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Mikhail Volynov |
| Maintainers | stealthtech |
| Keywords | typescript, url, util, helper, utility |

## Links

- npm: https://www.npmjs.com/package/url-compiler
- Repository: https://github.com/StealthTech/url-compiler
- Homepage: https://github.com/StealthTech/url-compiler#readme
- Issues: https://github.com/StealthTech/url-compiler/issues
- npm.io page: https://npm.io/package/url-compiler

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2021-12-19
- 0.3.0-beta.0 (beta) — 2021-12-18
- 1.0.1 — 2021-12-19
- 1.0.0 — 2021-12-19
- 0.2.0 — 2020-03-01
- 0.1.0 — 2020-03-01

## README

# URL Compiler

This library allows you to compile composite URLs from templates.

* No runtime dependencies
* Has typings for TypeScript

## Installation

Install and add to your project dependencies.

`npm install --save url-compiler`

## Usage

This packages contains four functions:
* `compileUrl` — compile url from template with params (also query, hash, trailing slash)
* `renderUrl` — render url from template with context (used in `compileUrl`)
* `handleTrailingSlash` — add or remove trailing slash (used in `compileUrl`)
* `queryString` — generate query-like string from object (used in `compileUrl`)

### Example: compileUrl
```js
import {compileUrl} from 'url-compiler';

const template = 'https://NPMJS.com/package/{package}';

// All params are optional
const params = {
    query: {
        foo: 'bar',
    },
    context: {
        package: 'url-compiler',
    },
    hash: 'usage',
    lowerCase: true,
    trailingSlash: true,
}
const url = compileUrl(template, params);

// https://npmjs.com/packages/url-compiler/#usage?foo=bar
console.log(url);
```

### Example: renderUrl
```js
import {renderUrl} from 'url-compiler';

const template = 'https://npmjs.com/package/{package}/';
const context = {
    package: 'url-compiler',
};

const url = renderUrl(template, context);

// https://npmjs.com/packages/url-compiler/
console.log(url);
```

### Example: handleTrailingSlash
```js
import {handleTrailingSlash} from 'url-compiler';

// --- If we want to remove trailing slash ---
const urlWithSlash = 'https://npmjs.com/';
const resultUrlWithSlash = handleTrailingSlash(urlWithSlash, false);

// https://npmjs.com (note that trailing slash is not present)
console.log(resultUrlWithSlash);

// --- If we want to add trailing slash ---
const urlWithoutSlash = 'https://npmjs.com';
const resultUrlWithoutSlash = handleTrailingSlash(urlWithSlash, true);

// https://npmjs.com/ (note that trailing slash is present)
console.log(resultUrlWithSlash);
```

### Example: queryString
```js
import {queryString} from 'url-compiler';

const query = {
    a: 'b',
    foo: 'bar',
}
const result = queryString(query);

// ?a=b&foo=bar
console.log(result);
```

---
_Source: https://npm.io/package/url-compiler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
