Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

bath-es5

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bath-es5

A simple path template engine

latest
Source
npmnpm
Version
3.0.3
Version published
Weekly downloads
107K
9.58%
Maintainers
1
Weekly downloads
 
Created
Source

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 `names()` only
import { params } from 'bath/params'; // import `params()` only
import { path } from 'bath/path';     // import `path()` only

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

// '/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
) => { names: ParameterName[]; path: PathFn; params: ParametersFn; };
  • bouzuya/spa-town ... A simple router based on bath.

Badges

npm version Travis CI Coveralls

License

MIT

Author

bouzuya <m@bouzuya.net> (http://bouzuya.net)

FAQs

Package last updated on 24 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts