npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tokenizer2

v2.0.1

Published

tokenize any text stream given some basic regex rules to match tokens

Downloads

14,691

Readme

tokenizer2

build status

tokenize any text stream given some basic regex rules to match tokens

NOTE This library works well, but I don't use it anymore. I just use while loops in a state machine pattern to tokenize. No library needed (or wanted). Here are some examples: one, two Yes it's stateful and verbose, but in my experience this is easier to write and maintain (using TDD of course). Just setup a test-runner and start small then grow it to tokenize everything you want. Once you get the hang of it, it's really easy to figure out how to tokenize something since you have full control of the state machine.

Example

var tokenizer2 = require('tokenizer2');

//create a readable/writeable stream
var token_stream = tokenizer2();

//make some rules
token_stream.addRule(/^[\s]+$/               , 'whitespace');
token_stream.addRule(/^"([^"]|\\")*"$/       , 'string');
token_stream.addRule(/^[-+]?[0-9]+\.?[0-9]*$/, 'number');
token_stream.addRule(/^[^"0-9\s][^\s]*$/     , 'symbol');

//write some info to the console
token_stream.on('data', function(token){
  console.log('token:', token);
});
token_stream.on('end', function(){
  console.log('DONE');
});

//pipe in some data
fs.createReadStream('./demo.txt').pipe(token_stream);

demo.txt

print "some multi-
lined string"

123.25 times -10

The output

token: {type: 'symbol'    , src: 'print',  line: 1, col:  1 }
token: {type: 'whitespace', src: ' ',      line: 1, col:  6 }
token: {type: 'string'    , src: '"some multi-\nlined string"', line: 1, col: 7 }
token: {type: 'whitespace', src: '\n\n',   line: 2, col: 14 }
token: {type: 'number'    , src: '123.25', line: 4, col:  1 }
token: {type: 'whitespace', src: ' ',      line: 4, col:  7 }
token: {type: 'symbol'    , src: 'times',  line: 4, col:  8 }
token: {type: 'whitespace', src: ' ',      line: 4, col: 13 }
token: {type: 'number'    , src: '-10',    line: 4, col: 14 }
token: {type: 'whitespace', src: '\n',     line: 4, col: 17 }
DONE

What if more than one rule matches a token?

token_stream.addRule adds rules in an order sensitive way. The first matching rule will be used.

Why tokenizer2

The key difference between this and tokenizer is the way it matches rules. tokenizer uses disect to do bisection on a chunk of text. This is a fast approach, however doesn't work well if your regex rule expects some specific characters at the end of the token. To solve this tokenizer2 simply starts at the beginning of the chunk, and finds the longest matching rule.

Other differences

  • tokenizer2 wraps through2.obj so all the node stream APIs should work nicely
  • tokenizer2 uses the standard 'data' event to emit the tokens
  • tokenizer2 emits line and col numbers

Non-streaming, synchronous API

If, for whatever reason, you don't want to use the streaming api. There is a lighter weight, synchronous api.

var core = require('tokenizer2/core');

var t = core(function(token){
  //called synchronously on every token found
});


//add rules just like the streaming api
t.addRule(/^[\s]+$/, 'whitespace');

//Give it strings to tokenize
t.onText("some text to tokenize");
t.onText("some more text");

//Call this when it's done
t.end();//this may throw an error

License

MIT