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

@eaterable/tsv-parser

v1.0.0

Published

⚡️ Memory-efficient TSV string parser using native JavaScript iterators

Downloads

90

Readme

TSV Parser

⚡️ Memory-efficient TSV string parser using native JavaScript iterators

🌟 Features

  • Memory Efficient: Uses JavaScript iterators for streaming-like parsing
  • Zero Dependencies: Pure JavaScript implementation
  • Flexible: Configurable value and line separators
  • TypeScript Ready: Full TypeScript support with type definitions
  • Modern API: Uses iterator pattern for efficient parsing
  • Iterable: Works with for...of loops and spread operator
  • Minimal: Focused on doing one thing well

Here's how our TSV parser performs compared to naive approaches:

| Method | Duration (ms) | Heap Used (MB) | RSS* (MB) | External (MB) | |------------------------|---------------|----------------|-----------|---------------| | TSV.parse | 377.25 | 13.14 | 1.30 | 0.04 | | String.split (Naive) | 822.36 | 394.59 | 393.60 | 0.00 |

* RSS (Resident Set Size) represents the total physical memory (RAM) used by the process

🚀 Installation

npm install @eaterable/tsv-parser

✨ Usage

Basic Usage

import TSV from '@eaterable/tsv-parser';

// Example TSV data
const data = 'name\tage\tcity\nAlice\t30\tNew York\nBob\t25\tLondon';

// Parse all rows at once (includes headers)
const allRows = [...TSV.parse(data)];
console.log(allRows);
// Output:
// [
//   ['name', 'age', 'city'],
//   ['Alice', '30', 'New York'],
//   ['Bob', '25', 'London']
// ]

// Get just the headers
const headers = TSV.headers(data);
console.log(headers);
// Output: ['name', 'age', 'city']

// Iterate over data rows (excluding headers)
for (const row of TSV.rows(data)) {
  console.log(row);
}
// Output:
// ['Alice', '30', 'New York']
// ['Bob', '25', 'London']

Custom Separators

const customData = 'name|age|city%Alice|30|New York%Bob|25|London';
const options = {
  valueSeparator: '|',  // Default: '\t'
  lineSeparator: '%'    // Default: '\n'
};

const rows = [...TSV.parse(customData, options)];

Low-Level Iterator Usage

// Using the iterator directly
const iterator = new TSV.Iterator(data);
for (const row of iterator) {
  console.log(row);
}

🔍 API Reference

TSV.parse(raw: string, options?: TSVParserOptions): Iterable<string[]>

Creates an iterable that yields all rows (including headers) from the TSV string.

  • raw: The raw TSV string to parse
  • options: Optional configuration object
    • valueSeparator: Character used to separate values (default: '\t')
    • lineSeparator: Character used to separate lines (default: '\n')

TSV.headers(raw: string, options?: TSVParserOptions): string[]

Returns just the headers (first row) from the TSV string.

TSV.rows(raw: string, options?: TSVParserOptions): Iterable<string[]>

Creates an iterable that yields data rows (excluding headers) from the TSV string.

TSV.Iterator

The underlying iterator class used for parsing. Useful for advanced use cases or when you need more control over the iteration process.

💡 Why Use This Parser?

  • Memory Efficiency: The iterator-based design means it only processes one row at a time, making it ideal for large datasets
  • Flexibility: Works with both standard TSV and custom-separated values
  • Simplicity: Clean API that works well with modern JavaScript features
  • Type Safety: Full TypeScript support for better development experience

🔋 Browser Support

This package works in all modern browsers and Node.js environments that support:

  • ES2015+ features
  • Iterators and Iterables
  • Symbol.iterator

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'feat: add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Inspired by the need for memory-efficient parsing of large TSV files
  • Built with modern JavaScript features for optimal performance
  • Follows the iterator pattern for streaming-like data processing