Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

spark-backend

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

spark-backend

The back end for the Spark collaborative text editor

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

Table of Contents

  • PieceTable.js

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"

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

ParamTypeDescription
fileTextstringthe initial text of the piece table

pieceTable.insert(str, offset)

Inserts a string into the piece table

Kind: instance method of PieceTable

ParamTypeDescription
strstringthe string to insert
offsetnumberthe offset at which to insert the string

pieceTable.delete(offset, length)

Deletes a string from the piece table

Kind: instance method of PieceTable

ParamTypeDescription
offsetnumberthe offset at which to begin deletion
lengthnumberthe 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

ParamTypeDescription
offsetnumberthe offset from which to get the string
lengthnumberthe number of characters to return from the offset. If negative, looks backwards

Keywords

realtime

FAQs

Package last updated on 08 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts