Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

extractjs

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extractjs

Extract/interpolate string

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
236
increased by69.78%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 23 Apr 2016

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc