🚀 A powerful, flexible data transformation library that makes type casting, object hydration, and collections management a breeze! Built with TypeScript and designed with developer experience in mind.
- ✨ Seamless Type Casting: Convert values between types automatically
- 🌳 Model Hydration: Transform plain objects into rich class instances
- 🔄 Smart Collections: Filter, sort, group, and aggregate with a fluent API
- 🏗️ Extensible Architecture: Create custom casters and mixins
- 🌍 Framework Agnostic: Use with any JavaScript project
- 🪶 Modular: Use components together or independently
npm install @encola/hydratorimport { CastingManager, ClassBuilder } from '@encola/hydrator';
// Setup core components
const castingManager = new CastingManager();
const builder = new ClassBuilder(castingManager);
// Create a model class
const User = builder.newModelClass({
id: 'number',
name: 'string',
email: 'string',
lastLogin: 'date'
});
// Create a collection class
const UserCollection = builder.newCollectionClass(User);
// Register for easy hydration
castingManager.registerModel('user', User, UserCollection);
// Use in your application
async function fetchUsers() {
const response = await fetch('/api/users');
const data = await response.json();
// Automatically converts to User instances in a UserCollection
return castingManager.cast(data, 'userCollection');
}Intelligent type conversion with built-in and custom casters:
castingManager.cast('123', 'number'); // 123
castingManager.cast('2023-01-15', 'date'); // Date object
castingManager.cast(['1', '2'], 'array:number'); // [1, 2]Build structured data models with automatic serialization:
const product = new Product({
name: 'Laptop',
price: '1299.99', // Automatically converted to number
inStock: 'true' // Automatically converted to boolean
});
console.log(product.toJSON()); // Serialized for server APISupercharged arrays with powerful data operations:
const electronics = products.filterBy('category', 'electronics');
const expensive = products.filterBy(p => p.price > 1000);
const byPrice = products.sortBy('price', 'desc');
const total = products.sum('price');
const grouped = products.groupBy('category');We'd love your help improving @encolajs/hydrator! Check out our Contributing Guide to get started.
Found a bug? Open an issue
Have a great idea? Suggest a feature
MIT © EncolaJS
Built with ❤️ by the EncolaJS team