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

retoken

v0.5.2

Published

Awesome string tokenizer for JavaScript

Downloads

25

Readme

Build Status npm version Bower version

Retoken is a string tokenization library for JavaScript. It provides a Tokenizer class that is based on your standard JavaScript array. This facilitates features like:

Retoken is also cross-platform compatible and has a light footprint.

Installation

You can install retoken with either NPM or bower:

Install with NPM on Node

npm install retoken

Install with Bower

bower install retoken

Features

In-place Extraction & Storage

Because the retoken Tokenizer class is subclassed directly from an array, tokenization occurs within the context of an array. Extracted tokens are essentially elements of the array, and can be manipulated as such.

Here we demonstrate how to extract words from a sentence:

var retoken = require('retoken');

// Construct a tokenizer that splits on the space character
var tokenizer = retoken(' ');

// Insert our test string into the tokenizer
tokenizer.push('The quick brown fox jumped over the lazy dog')

// Nicely inspect the tokenizer
console.log(tokenizer.toArray())
// [ 'The quick brown fox jumped over the lazy dog' ]

// Extract our first token
tokenizer.extract()

// Nicely inspect the tokenizer
console.log(tokenizer.toArray())
// [ 'The', 'quick brown fox jumped over the lazy dog' ]

Retraction

Tokens can be re-incorporated back into the untokenized string via a process we call retraction. Retraction can be used to undo extraction.

Continuing from our previous example, we can undo the extractions and reconstruct the original sentence.


// Nicely inspect the tokenizer
console.log(tokenizer.toArray())
// [ 'The', 'quick brown fox jumped over the lazy dog' ]

// Retract a token
tokenizer.retract()

// Nicely inspect the tokenizer
console.log(tokenizer.toArray())
// [ 'The quick brown fox jumped over the lazy dog' ]

Retraction works, because we keep track of delimiters as we extract tokens.

Cross-platform

This library should work under various environments including the following:

  • Browser script tags
  • AMD loaders
  • Node.js modules

Light footprint

Retoken is relatively light. It is:

  1. Single file,
  2. < 500 lines unminified,
  3. Free from runtime dependencies

Access

Retoken exposes a few properties to facilitate access to tokens within the tokenizer:

.head

Use this property to get/set the element at index 0 within the tokenizer.

// Nicely inspect the tokenizer
console.log(tokenizer.toArray())    
// [ 'The', 'quick brown fox jumped over the lazy dog' ]

// Output the element at index 0
console.log(tokenizer[0])           
// The

// Output the element at index 0 with the .head property
console.log(tokenizer.head)         
// The

.tail

Use this property to get/set the element at the last index within the tokenizer.

// Nicely inspect the tokenizer
console.log(tokenizer.toArray())
// [ 'The', 'quick brown fox jumped over the lazy dog' ]

// Output the element at the last index
console.log(tokenizer[tokenizer.length - 1])
// quick brown fox jumped over the lazy dog

// Output the element at the last index w/ the .tail property
console.log(tokenizer.tail)
// quick brown fox jumped over the lazy dog

.origin

Use this property to get/set the untokenized string.

// Nicely inspect the tokenizer
console.log(tokenizer.toArray())
// [ 'The', 'quick brown fox jumped over the lazy dog' ]

// Output the position of the untokenized string
console.log(tokenizer.origin)
// quick brown fox jumped over the lazy dog

.originIndex

Use this property to get the position of untokenized string.

// Nicely inspect the tokenizer
console.log(tokenizer.toArray())
// [ 'The', 'quick brown fox jumped over the lazy dog' ]

// Output the position of the untokenized string
console.log(tokenizer.originIndex)
// 1

.tap

Use this property to get/set the token closest to the origin.

// Nicely inspect the tokenizer
console.log(tokenizer.toArray())
// [ 'The', 'quick brown fox jumped over the lazy dog' ]

// Output the token closest to the origin
console.log(tokenizer.tap)
// The

.tapIndex

Use this property to get position of the token closest to the origin.

// Nicely inspect the tokenizer
console.log(tokenizer.toArray())
// [ 'The', 'quick brown fox jumped over the lazy dog' ]

// Output the token closest to the origin
console.log(tokenizer.tapIndex)
// 0

Usage

The following examples illustrates how retoken can be used:

Word Splitting

This example shows how to split words from a sentence:

var retoken = require('retoken');

// Construct a tokenizer that splits on the space character
var tokenizer = retoken(' ');

// Insert our test string into the tokenizer
tokenizer.push('The quick brown fox jumped over the lazy dog')

// Nicely inspect the tokenizer
console.log(tokenizer.toArray()) // [ 'The quick brown fox jumped over the lazy dog' ]

// Extract our first token
tokenizer.extract()

// Nicely inspect the tokenizer
console.log(tokenizer.toArray()) // [ 'The', 'quick brown fox jumped over the lazy dog' ]

API

Import Library on Node

var retoken = require('retoken');

License

The MIT License (MIT)

Copyright (c) 2015 Dickson Tam

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.