Extract/interpolate strings.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test
.gitignore
.travis.yml
Gruntfile.js
LICENSE
README.md
bower.json
extractjs.js
extractjs.min.js
package.json

README.md

extractjs

Build Status npm version Bower version license

Extract/interpolate strings.

##Usage

###Node.js and Browserify

Install from npm

npm install extractjs

var extractjs = require('extractjs'),
    extractor = extractjs();

var captured = extractor("This is a {name} library",
    "This is a extractjs library");
// > { name: 'extractjs'}

Template Settings

Default Settings
    var defaults = {
        startExtract: '{',
        endExtract: '}',
        extractors: { /* capture functions */ }
        initValue: void 0
    }

Templates can be redefined as below:

var extractjs = require('extractjs'),
    settings = { startExtract: '[[', endExtract: ']]'}
    extractor = extractjs(settings);

var captured = extractor("This is [[name]], [[age]] years old", 
    "This is John, 26 years old");
// > { name: 'John', age: 26 } <-- age is no longer string

// Settings can be overridden at the time of extraction
var date = extractor("Date is |date|/|month|/|year|", "Date is 26/04/2015",
    { startExtract: '|', endExtract: '|'})
// > { date: 26, month: 4, year: 2015 }

Its possible to build a pattern before extract/interpolate data.

var extractjs = require('extractjs'),
    extractor = extractjs();

var loginPattern = extractor("You are logged in as {name}.");

var name = loginPattern.extract('You are logged in as John. Last login: Today')
    .name;
// > John

var output = loginPattern.interpolate({name: 'John'});
//> You are loggin in as John.

extractors are used to override or manipulate captured values.

var extractjs = require('extractjs'),
    settings = {
        extractors: {
            name: function(value) {
                var names = value.split(' ');
                return {
                    firstName: names[0],
                    lastName: value.substring(names[0].length).trim(),
                    fullName: value
                };
            }
        }
    },
    extractor = extractjs(settings);

var captured = extractor("This is {name}, {age} years old",
    "This is John Wesley, 26 years old");
// > { name: {
//      firstName: 'John',
//      lastName: 'Wesley',
//      fullName: 'John Wesley'
//     }, age: 26 }

###Web page Access from window/global as extractjs.

License

This plugin is licensed under the MIT license.

Copyright (c) 2015 Prince John Wesley