npm stats
  • Search
  • About
  • Repo
  • Sponsor
  • more
    • Search
    • About
    • Repo
    • Sponsor

Made by Antonio Ramirez

map-tokens

0.1.2

@jonschlinkert

npmHomeRepoSnykSocket
Downloads:0
$ npm install map-tokens
DailyWeeklyMonthlyYearly

map-tokens NPM version

Register patterns to find tokens in a string and map them to unique IDs, allowing them to be extracted, replaced or restored.

Install

Install with npm

npm i map-tokens --save

Run tests

npm test

Usage

var Tokens = require('map-tokens');
var tokens = new Tokens('foo "bar" baz quux');

// tokenize anything in double quotes
tokens.pattern(/\"([^\"]+?)\"/);

// replace tokens with temporary IDs
tokens.extract();
//=> 'foo "__TOKEN_IDFOBV3I__" baz quux'

// Run a replacement on the string while the IDs are in place.
// Even though our regex specifies `bar`, `bar` won't be replaced
// since it was replaced with an ID.
tokens.content = tokens.content.replace(/foo|bar/g, 'AAA');
//=> 'AAA "__TOKEN_IDFOBV3I__" baz quux'

// last, retore the tokens
tokens.restore();
console.log(tokens.result);
//=> 'AAA "bar" baz quux'

Or, you can save the result of .extract() and explicitly pass the result to the .restore() method later:

var tokens = new Tokens();

tokens.restore('abc __TOKEN_ID9H8AY1__ __TOKEN_IDLDO84N__ jkl', {
  tokens: {
    __TOKEN_ID9H8AY1__: 'def',
    __TOKEN_IDLDO84N__: 'ghi'
  }
});

console.log(tokens.result);
//=> 'abc def ghi jkl'

API

Tokens

Create an instance of Tokens with the given string.

  • string {String}
var Tokens = require('map-tokens');
var tokens = new Tokens(string);

._input

Register a regex pattern to use for matching tokens.

  • regex {RegExp}
tokens.pattern(/def/);

.pattern

Register a regex pattern to use for matching tokens.

  • regex {RegExp}
tokens.pattern(/def/);

.extract

Run the registered patterns on the input string, which inserts tokens in place of matching strings.

  • patterns {Array}: Optionally pass an array of regex patterns to use
tokens.extract();
// or
tokens.extract([/def/]);

.restore

Restore previously inserted tokens.

  • obj {Object}: Optionally pass an object with tokens and content properties to use.
tokens.restore();
// or
tokens.restore(obj);

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jon Schlinkert

  • github/jonschlinkert
  • twitter/jonschlinkert

License

Copyright (c) 2014 Jon Schlinkert
Released under the MIT license


This file was generated by verb on November 27, 2014.