<commit_before># bath

A simple path template engine.

Twitter hashtag is [#bathjs](https://twitter.com/hashtag/bathjs).

This branch is for 2.x users. 1.x users: See [1.x](./tree/1.x) branch.

## Installation

```bash
$ npm install bath
```

## Usage

```ts
import assert from 'assert';
import bath from 'bath';

const { path, params } = bath('/users/{id}');

assert.deepEqual(path({ id: '123' }), '/users/123');
assert.deepEqual(params('/users/123'), { id: '123' });
```

```ts
import assert from 'assert';
import { path, params } from 'bath';

const template = '/users/{id}';
assert.deepEqual(path(template)({ id: '123' }), '/users/123');
assert.deepEqual(params(template)('/users/123'), { id: '123' });
```

```ts
import assert from 'assert';
import { path } from 'bath/path';     // import `path()` only
import { params } from 'bath/params'; // import `params()` only

const template = '/users/{id}';
assert.deepEqual(path(template)({ id: '123' }), '/users/123');
assert.deepEqual(params(template)('/users/123'), { id: '123' });
```

## Types

```ts
// '/users/{userId}/messages/{messageId}'
export type PathTemplate = string;

// 'userId'
export type ParameterName = string;

// { 'userId': /^\w+$/, 'messageId': /^\d+$/ }
export type ParameterPatterns = { [parameterName: string]: RegExp; };

// { 'userId': 'john', 'messageId': '123' }
export type Parameters = { [parameterName: string]: string; };

// '/users/john/messages/123'
export type Path = string;

// assert.deepEqual(
//   params('/users/john/messages/123'),
//   { 'userId': 'john', 'messageId': '123' }
// );
export type ParametersFn = (path: Path) => Parameters | null;

// assert.deepEqual(
//   path({ 'userId': 'john', 'messageId': '123' }),
//   '/users/john/messages/123'
// );
export type PathFn = (params: Parameters) => Path | null;

// const { params, path } = bath('/users/{userId}/messages/{messageId}')
export type Bath = (
  template: PathTemplate,
  patterns?: ParameterPatterns
) => { path: PathFn, params: ParametersFn };
```

## Badges

[![npm version][npm-badge-url]][npm-url]
[![Travis CI][travisci-badge-url]][travisci-url]

[npm-badge-url]: https://badge.fury.io/js/bath.svg
[npm-url]: https://www.npmjs.com/package/bath
[travisci-badge-url]: https://travis-ci.org/bouzuya/bath.svg?branch=master
[travisci-url]: https://travis-ci.org/bouzuya/bath

## License

[MIT](LICENSE)

## Author

[bouzuya][user] &lt;[<EMAIL>][email]&gt; ([http://bouzuya.net][url])

[user]: https://github.com/bouzuya
[email]: mailto:<EMAIL>
[url]: http://bouzuya.net
<commit_msg>fix README<commit_after># bath

A simple path template engine.

Twitter hashtag is [#bathjs](https://twitter.com/hashtag/bathjs).

This branch is for 2.x users. 1.x users: See [1.x](/../../tree/1.x) branch.

## Installation

```bash
$ npm install bath
```

## Usage

```ts
import assert from 'assert';
import bath from 'bath';

const { path, params } = bath('/users/{id}');

assert.deepEqual(path({ id: '123' }), '/users/123');
assert.deepEqual(params('/users/123'), { id: '123' });
```

```ts
import assert from 'assert';
import { path, params } from 'bath';

const template = '/users/{id}';
assert.deepEqual(path(template)({ id: '123' }), '/users/123');
assert.deepEqual(params(template)('/users/123'), { id: '123' });
```

```ts
import assert from 'assert';
import { path } from 'bath/path';     // import `path()` only
import { params } from 'bath/params'; // import `params()` only

const template = '/users/{id}';
assert.deepEqual(path(template)({ id: '123' }), '/users/123');
assert.deepEqual(params(template)('/users/123'), { id: '123' });
```

## Types

```ts
// '/users/{userId}/messages/{messageId}'
export type PathTemplate = string;

// 'userId'
export type ParameterName = string;

// { 'userId': /^\w+$/, 'messageId': /^\d+$/ }
export type ParameterPatterns = { [parameterName: string]: RegExp; };

// { 'userId': 'john', 'messageId': '123' }
export type Parameters = { [parameterName: string]: string; };

// '/users/john/messages/123'
export type Path = string;

// assert.deepEqual(
//   params('/users/john/messages/123'),
//   { 'userId': 'john', 'messageId': '123' }
// );
export type ParametersFn = (path: Path) => Parameters | null;

// assert.deepEqual(
//   path({ 'userId': 'john', 'messageId': '123' }),
//   '/users/john/messages/123'
// );
export type PathFn = (params: Parameters) => Path | null;

// const { params, path } = bath('/users/{userId}/messages/{messageId}')
export type Bath = (
  template: PathTemplate,
  patterns?: ParameterPatterns
) => { path: PathFn, params: ParametersFn };
```

## Badges

[![npm version][npm-badge-url]][npm-url]
[![Travis CI][travisci-badge-url]][travisci-url]

[npm-badge-url]: https://badge.fury.io/js/bath.svg
[npm-url]: https://www.npmjs.com/package/bath
[travisci-badge-url]: https://travis-ci.org/bouzuya/bath.svg?branch=master
[travisci-url]: https://travis-ci.org/bouzuya/bath

## License

[MIT](LICENSE)

## Author

[bouzuya][user] &lt;[<EMAIL>][email]&gt; ([http://bouzuya.net][url])

[user]: https://github.com/bouzuya
[email]: mailto:<EMAIL>
[url]: http://bouzuya.net

{'fraction_non_alphanumeric': 0.22664835164835165, 'fraction_numerical': 0.022762951334379906, 'mean_word_length': 4.292834890965732, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 12, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '432135', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1843, 'n_words': 403}