{"name":"url-pattern","version":"0.2.0","description":"url-pattern matches urls with patterns and extracts named url segments","homepage":"http://github.com/snd/url-pattern","author":{"name":"Maximilian Krüger","email":"kruemaxi@gmail.com","url":"http://github.com/snd"},"bugs":{"url":"http://github.com/snd/url-pattern/issues","email":"kruemaxi@gmail.com"},"repository":{"type":"git","url":"git://github.com/snd/url-pattern.git"},"licenses":{"type":"MIT","url":"git://github.com/snd/url-pattern/LICENSE"},"engines":{"node":">=0.8.5"},"dependencies":{"coffee-script":"1.4.0"},"devDependencies":{"nodeunit":"0.7.4"},"_id":"url-pattern@0.2.0","dist":{"shasum":"519e7f3302c694c3af6c8adbac27a504e5ee9d8e","tarball":"http://registry-npm.ywwl.com/url-pattern/download/url-pattern-0.2.0.tgz","integrity":"sha512-7n2VHWRb9YvJFvcI+LOcnIe8fuMMHsafGeOtSHP1vZErI0gJieSxQwXth89j7bw3bq/UGnTOQVwMVE7dXkn8qw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICzUROxMDUzEoseM9Hm+GEYJvy+zzccy59qXeKgfuzfmAiBYN77ss9p7YlpKBdUoigjQCTXEJorA7BrISkOc2qLxLg=="}],"size":2848,"noattachment":false,"key":"/url-pattern/-/url-pattern-0.2.0.tgz"},"maintainers":[{"name":"snd","email":"kruemaxi@googlemail.com"}],"directories":{},"readme":"# url-pattern\n\n[![NPM Package](https://img.shields.io/npm/v/url-pattern.svg?style=flat)](https://www.npmjs.org/package/url-pattern)\n[![Build Status](https://travis-ci.org/snd/url-pattern.svg?branch=master)](https://travis-ci.org/snd/url-pattern/branches)\n[![Sauce Test Status](https://saucelabs.com/buildstatus/urlpattern)](https://saucelabs.com/u/urlpattern)\n[![codecov.io](http://codecov.io/github/snd/url-pattern/coverage.svg?branch=master)](http://codecov.io/github/snd/url-pattern?branch=master)\n[![Downloads per Month](https://img.shields.io/npm/dm/url-pattern.svg?style=flat)](https://www.npmjs.org/package/url-pattern)\n\n**easier than regex string matching patterns for urls and other strings.  \nturn strings into data or data into strings.**\n\n> This is a great little library -- thanks!  \n> [michael](https://github.com/snd/url-pattern/pull/7)\n\n[make pattern:](#make-pattern-from-string)\n``` javascript\nvar pattern = new UrlPattern('/api/users(/:id)');\n```\n\n[match pattern against string and extract values:](#match-pattern-against-string)\n``` javascript\npattern.match('/api/users/10'); // {id: '10'}\npattern.match('/api/users'); // {}\npattern.match('/api/products/5'); // null\n```\n\n[generate string from pattern and values:](#stringify-patterns)\n``` javascript\npattern.stringify() // '/api/users'\npattern.stringify({id: 20}) // '/api/users/20'\n```\n\n- continuously tested in Node.js (0.12, 4.2.3 and 5.3) and all relevant browsers:\n  [![Sauce Test Status](https://saucelabs.com/browser-matrix/urlpattern.svg)](https://saucelabs.com/u/urlpattern)\n- [tiny single file with just under 500 lines of simple, readable, maintainable code](src/url-pattern.coffee)\n- [huge test suite](test)\n  passing [![Build Status](https://travis-ci.org/snd/url-pattern.svg?branch=master)](https://travis-ci.org/snd/url-pattern/branches)\n  with [![codecov.io](http://codecov.io/github/snd/url-pattern/coverage.svg?branch=master)](http://codecov.io/github/snd/url-pattern?branch=master)\n  code coverage\n- widely used [![Downloads per Month](https://img.shields.io/npm/dm/url-pattern.svg?style=flat)](https://www.npmjs.org/package/url-pattern)\n- supports CommonJS, [AMD](http://requirejs.org/docs/whyamd.html) and browser globals\n  - `require('url-pattern')`\n  - use [lib/url-pattern.js](lib/url-pattern.js) in the browser\n  - sets the global variable `UrlPattern` when neither CommonJS nor [AMD](http://requirejs.org/docs/whyamd.html) are available.\n- very fast matching as each pattern is compiled into a regex exactly once\n- zero dependencies\n- [customizable](#customize-the-pattern-syntax)\n- [frequently asked questions](#frequently-asked-questions)\n- npm package: `npm install url-pattern`\n- bower package: `bower install url-pattern`\n- pattern parser implemented using simple, combosable, testable [parser combinators](https://en.wikipedia.org/wiki/Parser_combinator)\n- [typescript typings](index.d.ts)\n\n[check out **passage** if you are looking for simple composable routing that builds on top of url-pattern](https://github.com/snd/passage)\n\n```\nnpm install url-pattern\n```\n\n```\nbower install url-pattern\n```\n\n```javascript\n> var UrlPattern = require('url-pattern');\n```\n\n``` javascript\n> var pattern = new UrlPattern('/v:major(.:minor)/*');\n\n> pattern.match('/v1.2/');\n{major: '1', minor: '2', _: ''}\n\n> pattern.match('/v2/users');\n{major: '2', _: 'users'}\n\n> pattern.match('/v/');\nnull\n```\n``` javascript\n> var pattern = new UrlPattern('(http(s)\\\\://)(:subdomain.):domain.:tld(\\\\::port)(/*)')\n\n> pattern.match('google.de');\n{domain: 'google', tld: 'de'}\n\n> pattern.match('https://www.google.com');\n{subdomain: 'www', domain: 'google', tld: 'com'}\n\n> pattern.match('http://mail.google.com/mail');\n{subdomain: 'mail', domain: 'google', tld: 'com', _: 'mail'}\n\n> pattern.match('http://mail.google.com:80/mail');\n{subdomain: 'mail', domain: 'google', tld: 'com', port: '80', _: 'mail'}\n\n> pattern.match('google');\nnull\n```\n\n## make pattern from string\n\n```javascript\n> var pattern = new UrlPattern('/api/users/:id');\n```\n\na `pattern` is immutable after construction.  \nnone of its methods changes its state.  \nthat makes it easier to reason about.\n\n## match pattern against string\n\nmatch returns the extracted segments:\n\n```javascript\n> pattern.match('/api/users/10');\n{id: '10'}\n```\n\nor `null` if there was no match:\n\n``` javascript\n> pattern.match('/api/products/5');\nnull\n```\n\npatterns are compiled into regexes which makes `.match()` superfast.\n\n## named segments\n\n`:id` (in the example above) is a named segment:\n\na named segment starts with `:` followed by the **name**.  \nthe **name** must be at least one character in the regex character set `a-zA-Z0-9`.\n\nwhen matching, a named segment consumes all characters in the regex character set\n`a-zA-Z0-9-_~ %`.\na named segment match stops at `/`, `.`, ... but not at `_`, `-`, ` `, `%`...\n\n[you can change these character sets. click here to see how.](#customize-the-pattern-syntax)\n\nif a named segment **name** occurs more than once in the pattern string,\nthen the multiple results are stored in an array on the returned object:\n\n```javascript\n> var pattern = new UrlPattern('/api/users/:ids/posts/:ids');\n> pattern.match('/api/users/10/posts/5');\n{ids: ['10', '5']}\n```\n\n## optional segments, wildcards and escaping\n\nto make part of a pattern optional just wrap it in `(` and `)`:\n\n```javascript\n> var pattern = new UrlPattern(\n  '(http(s)\\\\://)(:subdomain.):domain.:tld(/*)'\n);\n```\n\nnote that `\\\\` escapes the `:` in `http(s)\\\\://`.\nyou can use `\\\\` to escape `(`, `)`, `:` and `*` which have special meaning within\nurl-pattern.\n\noptional named segments are stored in the corresponding property only if they are present in the source string:\n\n```javascript\n> pattern.match('google.de');\n{domain: 'google', tld: 'de'}\n```\n\n```javascript\n> pattern.match('https://www.google.com');\n{subdomain: 'www', domain: 'google', tld: 'com'}\n```\n\n`*` in patterns are wildcards and match anything.\nwildcard matches are collected in the `_` property:\n\n```javascript\n> pattern.match('http://mail.google.com/mail');\n{subdomain: 'mail', domain: 'google', tld: 'com', _: 'mail'}\n```\n\nif there is only one wildcard then `_` contains the matching string.\notherwise `_` contains an array of matching strings.\n\n[look at the tests for additional examples of `.match`](test/match-fixtures.coffee)\n\n## make pattern from regex\n\n```javascript\n> var pattern = new UrlPattern(/^\\/api\\/(.*)$/);\n```\n\nif the pattern was created from a regex an array of the captured groups is returned on a match:\n\n```javascript\n> pattern.match('/api/users');\n['users']\n\n> pattern.match('/apiii/test');\nnull\n```\n\nwhen making a pattern from a regex\nyou can pass an array of keys as the second argument.\nreturns objects on match with each key mapped to a captured value:\n\n```javascript\n> var pattern = new UrlPattern(\n  /^\\/api\\/([^\\/]+)(?:\\/(\\d+))?$/,\n  ['resource', 'id']\n);\n\n> pattern.match('/api/users');\n{resource: 'users'}\n\n> pattern.match('/api/users/5');\n{resource: 'users', id: '5'}\n\n> pattern.match('/api/users/foo');\nnull\n```\n\n## stringify patterns\n\n```javascript\n> var pattern = new UrlPattern('/api/users/:id');\n\n> pattern.stringify({id: 10})\n'/api/users/10'\n```\n\noptional segments are only included in the output if they contain named segments\nand/or wildcards and values for those are provided:\n\n```javascript\n> var pattern = new UrlPattern('/api/users(/:id)');\n\n> pattern.stringify()\n'/api/users'\n\n> pattern.stringify({id: 10})\n'/api/users/10'\n```\n\nwildcards (key = `_`), deeply nested optional groups and multiple value arrays should stringify as expected.\n\nan error is thrown if a value that is not in an optional group is not provided.\n\nan error is thrown if an optional segment contains multiple\nparams and not all of them are provided.\n*one provided value for an optional segment\nmakes all values in that optional segment required.*\n\n[look at the tests for additional examples of `.stringify`](test/stringify-fixtures.coffee)\n\n## customize the pattern syntax\n\nfinally we can completely change pattern-parsing and regex-compilation to suit our needs:\n\n```javascript\n> var options = {};\n```\n\nlet's change the char used for escaping (default `\\\\`):\n\n```javascript\n> options.escapeChar = '!';\n```\n\nlet's change the char used to start a named segment (default `:`):\n\n```javascript\n> options.segmentNameStartChar = '$';\n```\n\nlet's change the set of chars allowed in named segment names (default `a-zA-Z0-9`)\nto also include `_` and `-`:\n\n```javascript\n> options.segmentNameCharset = 'a-zA-Z0-9_-';\n```\n\nlet's change the set of chars allowed in named segment values\n(default `a-zA-Z0-9-_~ %`) to not allow non-alphanumeric chars:\n\n```javascript\n> options.segmentValueCharset = 'a-zA-Z0-9';\n```\n\nlet's change the chars used to surround an optional segment (default `(` and `)`):\n\n```javascript\n> options.optionalSegmentStartChar = '[';\n> options.optionalSegmentEndChar = ']';\n```\n\nlet's change the char used to denote a wildcard (default `*`):\n\n```javascript\n> options.wildcardChar = '?';\n```\n\npass options as the second argument to the constructor:\n\n```javascript\n> var pattern = new UrlPattern(\n  '[http[s]!://][$sub_domain.]$domain.$toplevel-domain[/?]',\n  options\n);\n```\n\nthen match:\n\n```javascript\n> pattern.match('http://mail.google.com/mail');\n{\n  sub_domain: 'mail',\n  domain: 'google',\n  'toplevel-domain': 'com',\n  _: 'mail'\n}\n```\n\n## frequently asked questions\n\n### how do i match the query part of an URL ?\n\nthe query part of an URL has very different semantics than the rest.\nurl-pattern is not well suited for parsing the query part.\n\nthere are good existing libraries for parsing the query part of an URL.\nhttps://github.com/hapijs/qs is an example.\nin the interest of keeping things simple and focused\ni see no reason to add special support\nfor parsing the query part to url-pattern.\n\ni recommend splitting the URL at `?`, using url-pattern\nto parse the first part (scheme, host, port, path)\nand using https://github.com/hapijs/qs to parse the last part (query).\n\n### how do i match an IP ?\n\nyou can't exactly match IPs with url-pattern so you have to\nfall back to regexes and pass in a regex object.\n\n[here's how you do it](https://github.com/snd/url-pattern/blob/c8e0a943bb62e6feeca2d2595da4e22782e617ed/test/match-fixtures.coffee#L237)\n\n## [contributing](contributing.md)\n\n## [license: MIT](LICENSE)\n","publish_time":1354723073914,"_cnpm_publish_time":1354723073914,"dist-tags":{"latest":"1.0.3"}}