JSPM

  • Created
  • Published
  • Downloads 56311
  • Score
    100M100P100Q181179F
  • License MIT

URL Connection String Parser.

Package Exports

  • connection-string

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (connection-string) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

connection-string

URL Connection String Parser, with fully optional syntax.

Build Status Coverage Status

Takes a URL connection string (with every element being optional):

protocol://user:password@hostname:12345/seg1/seg2?p1=val1&p2=val2

and converts it into an object that contains only what's specified:

{
    protocol: 'protocol',
    user: 'user',
    password: 'password',
    host: 'hostname:12345',
    hostname: 'hostname',
    port: 12345,
    segments: ['seg1', 'seg2'],
    params: {
        p1: 'val1',
        p2: 'val2'
    }
}

Short-syntax examples:

  • localhost => {host: 'localhost', hostname: 'localhost'}
  • localhost:12345 => {host: 'localhost:12345', hostname: 'localhost', port: 12345}
  • abc:///seg1?p1=val => {protocol: 'abc', segments: ['seg1'], params: {p1: 'val'}}
  • :12345 => {host: ':12345', port: 12345}
  • username@ => {user: 'username'}
  • :pass123@ => {password: 'pass123'}
  • /seg1/seg2 => {segments: ['seg1', 'seg2']}
  • ?param1=1&param2=2 => {params: {param1: '1', param2: '2'}}

For a complete list of short-syntax examples see the Optional Format.

All browsers and Node.js versions are supported.

Installing

$ npm install connection-string

Usage

  • Node.js
var parse = require('connection-string');
var obj = parse('my-server:12345');

or as a class:

var ConnectionString = require('connection-string');
var obj = new ConnectionString('my-server:12345');
  • Browsers
<script src="./connection-string/src"></script>

<script>
    var obj = new ConnectionString('my-server:12345');
</script>
  • TypeScript
import {ConnectionString} from 'connection-string'

var a = new ConnectionString('my-server:12345');

For details and examples see the WiKi Pages.

API

After parsing you always get a class instance with all the properties, plus method build([defaults]), which can construct a new connection string from the current properties, using an optional set of defaults.

Example:

var a = new ConnectionString('abc://localhost');

a.build(); //=> 'abc://localhost'

// using defaults:
a.build({
    hostname: '127.0.0.1',
    port: 12345,
    user: 'tester'
}); //=> 'abc://tester@localhost:12345'