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

piece-table

v1.1.0

Published

An implementation of the piece table data structure

Downloads

5

Readme

Table of Contents

PieceTable.js

A JavaScript implementation of the piece table data structure

A piece table is an efficient data structure for tracking edits to a text document. A detailed explanation can be found here.

Overview

A piece table consists of two buffers, the file buffer and the add buffer, and a table of "pieces", or windows into those buffers (the piece table). Each piece consists of a length, an offset, and a descriptor of which buffer the piece points to. The actual text of the document is represented by the series of pieces in the piece table. Initially, the file buffer consists of the full text of the document and the add buffer is empty. Insertions involve appending the new text to the add buffer and adding pieces to the piece table to reflect the addition. Deletion is performed simply by removing and editing pieces from the piece table. This has the added benefit of never losing data, allowing easy undo operations. Piece table operations are very efficient because they only ever append to the buffers, so no mid-array insertions or deletions are performed.

Installation

npm install --save piece-table

Usage

const PieceTable = require("piece-table");

const document = new PieceTable("This is a document with some text.");

document.insert("This is some more text to insert at offset 33.", 33);

// Delete the previously inserted sentence
document.delete(79, 46);

var sequence = document.getSequence();
// sequence == "This is a document with some text."

var subString = document.stringAt(9, 8);
// subString == "document"

// PieceTable is an iterable:
for (let character of document) {
    console.log(character);
    // 'T', 'h', 'i', 's', ...
}

API Documentation

PieceTable

Kind: global class

new PieceTable(fileText)

A piece table is an efficient data structure to track changes to a text. See https://www.cs.unm.edu/~crowley/papers/sds/node15.html for details

| Param | Type | Description | | --- | --- | --- | | fileText | string | the initial text of the piece table |

pieceTable.insert(str, offset)

Inserts a string into the piece table

Kind: instance method of PieceTable

| Param | Type | Description | | --- | --- | --- | | str | string | the string to insert | | offset | number | the offset at which to insert the string |

pieceTable.delete(offset, length)

Deletes a string from the piece table

Kind: instance method of PieceTable

| Param | Type | Description | | --- | --- | --- | | offset | number | the offset at which to begin deletion | | length | number | the number of characters to delete. If negative, deletes backwards |

pieceTable.getSequence() ⇒ string

Gets the sequence as a string

Kind: instance method of PieceTable
Returns: string - The sequence

pieceTable.stringAt(offset, length)

Gets a string of a particular length from the piece table at a particular offset

Kind: instance method of PieceTable

| Param | Type | Description | | --- | --- | --- | | offset | number | the offset from which to get the string | | length | number | the number of characters to return from the offset. If negative, looks backwards |