simple-capture

0.2.0 • Public • Published

SimpleCapture

Simplified RegExp style string captures.

This module provides a single function aimed at making extremely simple capture operations for strings.

It can be thought of as an extremely dumbed down and easy to understand RegExp group capture operation.

The function:

simpleCapture(template, string);

where template is generally a string containing square bracketed named arguments (e.g. "My name is [name]") and the string is the input (e.g. "My name is Matt"). It will return a hash of all captured tokens (in the previous case: {name: "Matt"}) or null if the input string did not match the template.

Examples

A simple example taking two named captures and returning a hash

var $template = '[name] is [age] years old';
var $string = 'Matt is 29 years old'
var $result = simpleCapture($template, $string);

console.log($result); // Output: {name: 'Matt', age: '29'}

Another simple example, this time using simple URL templating:

var $template = '/[controller]/[function]/[arg]';
var $string = '/foo/bar/baz';
var $result = simpleCapture($template, $string);

console.log($result); // Output: {controller: 'foo', function: 'bar', arg: 'baz'}

You can also specify defaults that should always be returned even if the rest of the string doesnt match:

var $template = '/[controller]/[function]/[arg!hello world]';
var $string = 'FAIL FAIL FAIL';
var $result = simpleCapture($template, $string);

console.log($result); // Output: {arg: 'hello world'}

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.2.0
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.2.0
    0
  • 0.1.0
    0

Package Sidebar

Install

npm i simple-capture

Weekly Downloads

0

Version

0.2.0

License

MIT

Last publish

Collaborators

  • hash-bang