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

fast-sax

v1.0.2

Published

A minimal, dependency free, ES3 compatible, well tested, and lightning fast SAX-like XML parser for Node and the browser.

Downloads

5

Readme

fast-sax Build Status npm dependencies Coverage Status npm version Greenkeeper badge

A minimal, dependency free, ES3 compatible, well tested, and lightning fast SAX-like XML parser for Node and the browser. Under 1KB gzipped and ready for your well-formed XML needs.

Features

  • A SAX-like API.
  • Support for parsing regular elements, text nodes, CDATA, and comments.
  • Absolutely no support for anything else. No processing instructions, no auto-unescaping, no text trimming.
  • ECMAScript 3 support (IE6+).
  • Fully unit tested.
  • <1KB gzipped.

Installation

Node

$ npm install fast-sax

Browser

<script src="fast-sax.min.js"></script>

Usage

fast-sax exposes a SAX-like API. Given a string XML document, fast-sax will fire events associated with the elements within. Consumers attach callbacks for the events they require and then may handle the parsed string data at will. It operates synchronously, with attributes and text nodes being lazily parsed -- the consumer may choose to extract them as part of the event handler.

General Parsing

var FastSax = require("fast-sax"); //In the browser: window.FastSax
var parser = new FastSax();

/*
 * This function will be called every time a new element is found. 
 * 
 * The first argument is the elementName, which is the raw string name of the element.
 * The second argument is a function for obtaining a map of attributes in the element. This function enables fast-sax to
 * avoid parsing the attributes until needed. It will be a map of attribute names to string values.
 */
parser.onElementStart = function(elementName, getAttributes) {
    const attributes = getAttributes();

    console.log(`Element: '${elementName}' name is ${attributes["to"]}`) //Element: 'message' to 'world'
};

/*
 * This function will be called every time an element has ended. This includes both self-closing elements and actual
 * close tags.
 */
parser.onElementEnd = function(elementName) {
    console.log(`Element end: '${elementName}'`); //Element end: 'message'
};

/*
* This function will be called every time a text node has been found. It is the user's responsibility to keep track
* of the parent element as needed.
*
* FastSax does NOT trim or otherwise change text values. Indentation and other whitespace will result in onText
* being called.
* 
* The only argument, getText, is a function which returns the raw text as a string.
*/
parser.onText = function(getText) {
    const text = getText();
    
    console.log(`Text: '${text}'`); //Text: 'hello'
};

parser.parse("<message to='world'>hello</message>");
console.log("Parse complete.");

/*
 * Prints:
 * > Element: 'message' to 'world'
 * > Text: 'hello'
 * > Element end: 'message' 
 * > Parse complete.
 */

Comments

fast-sax also supports parsing of comment blocks through the onComment callback.

var FastSax = require("fast-sax"); //In the browser: window.FastSax
var parser = new FastSax();

/*
 * This function will be called every time a comment block has been found.
 * 
 * The only argument, getText, is a function which returns the raw text as a string.
 */
parser.onComment = function(getText) {
    const text = getText();
    
    console.log(`Found comment: ' ${text} '`); //Comment: ' hello '
};

parser.parse("<!-- hello -->");
console.log("Parse complete.");

/*
 * Prints:
 * > Comment: ' hello '
 * > Parse complete.
 */

CDATA

fast-sax also supports parsing of CDATA blocks through the onCData callback.

var FastSax = require("fast-sax"); //In the browser: window.FastSax
var parser = new FastSax();

/*
 * This function will be called every time a CDATA block has been found.
 * 
 * The only argument, getText, is a function which returns the raw text as a string.
 */
parser.onCData = function(getText) {
    const text = getText();
    
    console.log(`Found CDATA: ' ${text} '`); //Found comment: ' hello cdata '
};

parser.parse("<![CDATA[ hello cdata ]]>");
console.log("Parse complete.");

/*
 * Prints:
 * > Comment: ' hello cdata '
 * > Parse complete.
 */

API

class FastSax {

       /**
        * Fired when a text node is parsed.
        *
        * @param {() => string} getText A function returning the node text as a string.
        */
       onText: (getText: () => string) => void;
       
       /**
        * Fired when a a new element has been found.
        *
        * @param {string} elementName The name of the element.
        * @param {() => {[attribute: string]: string}} getAttributes A function returning a map of attribute names to values.
        */
       onElementStart: (elementName: string, getAttributes: () => {
           [attribute: string]: string;
       }) => void;
       
       /**
        * Fired when an element's end has been found.
        *
        * @param {string} elementName the name of the element.
        */
       onElementEnd: (elementName: string) => void;
       
       /**
        * Fired when a CDATA block has been found.
        *
        * @param {() => string} getText A function returning the CDATA text as a string.
        */
       onCData: (getText: () => string) => void;
       
       /**
        * Fired when a comment block has been found.
        *
        * @param {() => string} getText A function returning the comment text as a string.
        */
       onComment: (getText: () => string) => void;
       
       /**
        * Parse the given XML document (in string form), emitting events along the way.
        *
        * @param {string} xmlContents Valid, well formed XML.
        */
       parse(xmlContents: string): void;
}

Versioning

fast-sax uses SemVer for versioning. All releases will be available on both Github and npm.

License

MIT