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/rowmap

v1.0.1

Published

A lightweight library to map tabular data to objects effortlessly in JavaScript or TypeScript.

Downloads

99

Readme

RowMap

Effortlessly map tabular data (like CSV rows) to objects using lightweight dynamic mappers. RowMap is designed to be minimal, elegant, and developer-friendly, making it a powerful tool for working with structured data in JavaScript or TypeScript.

🌟 Features

  • Flexible Mapper Behavior: Use it as a class or a function—the choice is yours.
  • Header-Based Mapping: Map rows to meaningful property names using headers or configuration objects.
  • Dynamic Properties: Access and modify array values through descriptive property names.
  • Array Access: Direct access to the underlying array for advanced operations.
  • Iterable Support: Fully compatible with JavaScript's iterable protocol (for...of, spread syntax).
  • Serialization Ready: Automatically serializable with JSON.stringify or a toJSON() method.
  • Minimal API: Simple, efficient, and developer-friendly.

🚀 Installation

Install using npm:

npm install @eaterable/rowmap

✨ Usage

The rowmap function provides a universal mapper that can function as both a class and a direct function, depending on how you use it. It works seamlessly to map rows to objects with descriptive properties.

Universal Mapper Example

import rowmap from '@eaterable/rowmap';

// Example data
const [headers, ...rows] = [
  ['id', 'name', 'email'], // Header row
  [1, 'Alice', '[email protected]'],
  [2, 'Bob', '[email protected]'],
];

// Create a universal mapper using headers
const User = rowmap(['id', 'name', 'email']);

// Map rows directly to objects
const objects = rows.map(User);

console.log(objects);
// Output:
// [
//   { id: 1, name: 'Alice', email: '[email protected]' },
//   { id: 2, name: 'Bob', email: '[email protected]' },
// ]

// Or use it as a class
const obj = new Row([3, 'Charlie', '[email protected]']);
console.log(obj);
// Output: { id: 3, name: 'Charlie', email: '[email protected]' }

Configuration Options

const Row = rowmap({
  headers: ['id', 'name', 'email'],  // Required: column names
  index: false,                      // Optional: disable row index (default: true)
  array: false,                      // Optional: disable array access (default: true)
  className: 'User',                 // Optional: custom class name
  preventCollisions: true            // Optional: prevent header collisions (default: false)
});

Property Access and Modification

const row = new Row([1, 'Alice', '[email protected]']);

// Read values
console.log(row[0]);           // 1
console.log(row.name);         // 'Alice'
console.log(row.array);        // [1, 'Alice', '[email protected]']
console.log(row.array.length); // 3

// Modify values
row.name = 'Alicia';
console.log([...row]);  // [1, 'Alicia', '[email protected]']

// Advanced array operations
row.array.reverse();
console.log(row.name);  // Still works with reversed array!

// Note: 'array' can be used as a header name if needed
const CustomRow = rowmap(['id', 'name', 'array']);

Iteration and Conversion

// Spread syntax
const values = [...row];

// Convert to string
console.log(String(row));  // '1,Alicia,[email protected]'

// Convert to object
console.log(row.toJSON());

// JSON serialization
console.log(JSON.stringify(row));

💡 Why Use RowMap?

  • Universal Design: Whether you prefer functional programming or object-oriented patterns, RowMap fits your style.
  • Readable: Transform tabular data into meaningful structures with minimal effort.
  • Lightweight: Designed to do one job and do it well.
  • Developer-Friendly: Works seamlessly with native JavaScript features like iteration, JSON serialization, and dynamic properties.

🤝 Contributing

We welcome contributions! Feel free to fork the repository, submit pull requests, or open issues.

🛡 License

This project is licensed under the MIT License.


Ready to map your tabular data effortlessly? Try RowMap today! 🎉