bath (ES5, transpiled for the browser)
A simple path template engine. Forked from bouzuya/bath
Twitter hashtag is #bathjs.
This branch is for 2.x users. 1.x users: See 1.x branch.
Installation
$ npm install bath-es5
Usage
import assert from 'assert';
import bath from 'bath';
const { names, params, path } = bath('/users/{id}');
assert.deepEqual(names, ['id']);
assert.deepEqual(params('/users/123'), { id: '123' });
assert.deepEqual(path({ id: '123' }), '/users/123');
import assert from 'assert';
import { names, params, path } from 'bath';
const template = '/users/{id}';
assert.deepEqual(names(template), ['id']);
assert.deepEqual(params(template)('/users/123'), { id: '123' });
assert.deepEqual(path(template)({ id: '123' }), '/users/123');
import assert from 'assert';
import { names } from 'bath/names';
import { params } from 'bath/params';
import { path } from 'bath/path';
const template = '/users/{id}';
assert.deepEqual(names(template), ['id']);
assert.deepEqual(params(template)('/users/123'), { id: '123' });
assert.deepEqual(path(template)({ id: '123' }), '/users/123');
Types
export type PathTemplate = string;
export type ParameterName = string;
export type ParameterPatterns = { [parameterName: string]: RegExp; };
export type Parameters = { [parameterName: string]: string; };
export type Path = string;
export type ParametersFn = (path: Path) => Parameters | null;
export type PathFn = (params: Parameters) => Path | null;
export type Bath = (
template: PathTemplate,
patterns?: ParameterPatterns
) => { names: ParameterName[]; path: PathFn; params: ParametersFn; };
Related Project
Badges
License
MIT
Author
bouzuya <m@bouzuya.net> (http://bouzuya.net)