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

sirrobert-tokenize

v1.0.2

Published

A utility module for tokenizing a string.

Downloads

5

Readme

Rationale

A module for string tokenization after a particular pattern.

Namespace rationale

This module exists in the sirrobert- namespace so as not to clutter npm and to keep my related packages together. If there's enough interest, I can move this into the general namespace.

Installation

Local installation

npm install --save sirrobert-tokenize

Global installation

npm install --global sirrobert-tokenize

Token Definitions

This module contains only one function, called "tokenize". It takes a string and returns an array of tokens.

Tokens are defined by the module as one of:

  • Quoted Strings. Quoted strings include single- or double-quoted strings. Double quoted strings are defined as

    /("[^"\\]*(?:\\.[^"\\]*)*")/

    and single-quoted strings are defined similarly as

    /('[^'\\]*(?:\\.[^'\\]*)*')/
        

    Note that this means that escaped quotes inside a quoted string are permitted, so these would be valid tokens: "\"", '\''

  • Consecutive Non-Whitespace. Any whitespace (or string boundary) sequence. That means the following are examples of valid tokens of this kind: pickle, potato-pants, 249nf9W$GH(WGOSWJEUR. The definition of this kind of token is:

    /\S+/
        
  • Consecutive Whitespace. Any consecutive whitespace. The specific definition of this kind of token is:

    /\s+/

Usage

This module provides one function that takes a string and gives an array of tokens as defined above.

The usage pattern goes: tokenize(string, [options-hash])

let tokenize = require("sirrobert-tokenize");

let str = "I am the \"Egg Man\" and the 'the \'Walrus\''";

tokenize(str);

/* ['I',
 *  'am',
 *  'the',
 *  '"Egg Man"',
 *  'and',
 *  'the \'Walrus\''
 * ]
 */

Options

There are two options available for the options hash:

  • whitespace How to handle whitespace in the string. Available values are:

    • ignore Disregard all whitespace. This is the default.
    • append Keep all whitespace. Append it to the token it comes immediately after. Whitespace at the beginning of the string is its own token.
    • prepend Keep all whitespace. Prepend it to the token it comes immediately before. Whitespace at the end of the string is its own token.
    • tokenize Keep all whitespace. Each set of whitespace is its own token.
  • trimInput Whether to trim whitespace from the input string before processing. Defaults to true. Any values are evaluated as boolean values.

Here are examples of various combinations of options using the string above.

{ whitespace: 'ignore', trimInput: true }
[ 'I',
  'am',
  'the',
  '"Egg Man"',
  'and',
  'the',
  '\'the \'',
  'Walrus\'\'' ]

{ whitespace: 'ignore', trimInput: false }
[ 'I',
  'am',
  'the',
  '"Egg Man"',
  'and',
  'the',
  '\'the \'',
  'Walrus\'\'' ]

{ whitespace: 'append', trimInput: true }
[ 'I ',
  'am ',
  'the ',
  '"Egg Man" ',
  'and ',
  'the ',
  '\'the \'',
  'Walrus\'\'' ]

{ whitespace: 'append', trimInput: false }
[ ' ',
  'I ',
  'am ',
  'the ',
  '"Egg Man" ',
  'and ',
  'the ',
  '\'the \'',
  'Walrus\'\'  ' ]

{ whitespace: 'prepend', trimInput: true }
[ 'I',
  ' am',
  ' the',
  ' "Egg Man"',
  ' and',
  ' the',
  ' \'the \'',
  'Walrus\'\'' ]

{ whitespace: 'prepend', trimInput: false }
[ ' I',
  ' am',
  ' the',
  ' "Egg Man"',
  ' and',
  ' the',
  ' \'the \'',
  'Walrus\'\'',
  '  ' ]

{ whitespace: 'tokenize', trimInput: true }
[ 'I',
  ' ',
  'am',
  ' ',
  'the',
  ' ',
  '"Egg Man"',
  ' ',
  'and',
  ' ',
  'the',
  ' ',
  '\'the \'',
  'Walrus\'\'' ]

{ whitespace: 'tokenize', trimInput: false }
[ ' ',
  'I',
  ' ',
  'am',
  ' ',
  'the',
  ' ',
  '"Egg Man"',
  ' ',
  'and',
  ' ',
  'the',
  ' ',
  '\'the \'',
  'Walrus\'\'',
  '  ' ]

LICENSE

MIT