Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aminnairi/url

Utility functions to help you work with urls.
https://github.com/aminnairi/url

Last synced: about 5 hours ago
JSON representation

Utility functions to help you work with urls.

Awesome Lists containing this project

README

        

# @aminnairi/url

Utility functions to help you work with urls.

[![Continuous Integration](https://github.com/aminnairi/url/actions/workflows/continuous_integration.yaml/badge.svg)](https://github.com/aminnairi/url/actions/workflows/continuous_integration.yaml)

## Usage

### Node

#### ECMAScript Module

```bash
npm install @aminnairi/url
```

```javascript
import {isMatchingPath, getMatchingPathParameters} from "@aminnairi/url";

console.log(isMatchingPath("/users/:user", "/users/123"));

console.log(getMatchingPathParameters("/users/:user", "/users/123"));
// {user: "123"}
```

#### CommonJS

```bash
npm install @aminnairi/url
```

```javascript
const {isMatchingPath, getMatchingPathParameters} = require("@aminnairi/url");

console.log(isMatchingPath("/users/:user", "/users/123"));
// true

console.log(getMatchingPathParameters("/users/:user", "/users/123"));
// {user: "123"}
```

### Browser

#### ECMAScript Module

```html






@aminnairi/url



import {isMatchingPath, getMatchingPathParameters} from "https://unpkg.com/@aminnairi/url?module";

console.log(isMatchingPath("/users/:user", "/users/123"));

console.log(getMatchingPathParameters("/users/:user", "/users/123"));

```

#### Script

```html






@aminnairi/url




const {isMatchingPath, getMatchingPathParameters} = window["@aminnairi/url"];

console.log(isMatchingPath("/users/:user", "/users/123"));

console.log(getMatchingPathParameters("/users/:user", "/users/123"));

```

## Documentation

### isMatchingPath

#### Signature

```typescript
const isMatchingPath = (path: string, url: string) => boolean;
```

#### Examples

```javascript
isMatchingPath(null, "/");
// false

isMatchingPath("/", null);
// false

isMatchingPath("/", "/");
// true

isMatchingPath("", "/");
// true

isMatchingPath("/", "");
// true

isMatchingPath("/", "/users");
// false

isMatchingPath("/users/:user", "/users/123");
// true

isMatchingPath("/users/:user", "/posts/123");
// false

isMatchingPath("/users/:user", "/users/123/posts");
// false

isMatchingPath("/users/:user/posts/:post", "/users/123/posts/456");
// true
```

### getMatchingPathParameters

#### Signature

```typescript
const getMatchingPathParameters = (path: string, url: string) => Record;
```

#### Examples

```javascript
getMatchingPathParameters(null, "/");
// {}

getMatchingPathParameters("/", null);
// {}

getMatchingPathParameters("/", "/");
// {}

getMatchingPathParameters("", "/");
// {}

getMatchingPathParameters("/", "");
// {}

getMatchingPathParameters("/", "/users");
// {}

getMatchingPathParameters("/users/:user", "/users/123");
// {user: "123"}

getMatchingPathParameters("/users/:user", "/posts/123");
// {}

getMatchingPathParameters("/users/:user", "/users/123/posts");
// {}

getMatchingPathParameters("/users/:user/posts/:post", "/users/123/posts/456");
// {user: "123", post: "456"}
```

## Changelog

See [`CHANGELOG.md`](https://github.com/aminnairi/url/blob/development/CHANGELOG.md).

## Contributing

See [`CONTRIBUTING.md`](https://github.com/aminnairi/url/blob/development/CONTRIBUTING.md).

## License

See [`LICENSE`](https://github.com/aminnairi/url/blob/development/LICENSE).