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';
const [headers, ...rows] = [
['id', 'name', 'email'],
[1, 'Alice', 'alice@example.com'],
[2, 'Bob', 'bob@example.com'],
];
const User = rowmap(['id', 'name', 'email']);
const objects = rows.map(User);
console.log(objects);
const obj = new Row([3, 'Charlie', 'charlie@example.com']);
console.log(obj);
Configuration Options
const Row = rowmap({
headers: ['id', 'name', 'email'],
index: false,
array: false,
className: 'User',
preventCollisions: true
});
Property Access and Modification
const row = new Row([1, 'Alice', 'alice@example.com']);
console.log(row[0]);
console.log(row.name);
console.log(row.array);
console.log(row.array.length);
row.name = 'Alicia';
console.log([...row]);
row.array.reverse();
console.log(row.name);
const CustomRow = rowmap(['id', 'name', 'array']);
Iteration and Conversion
const values = [...row];
console.log(String(row));
console.log(row.toJSON());
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! 🎉