Gathering detailed insights and metrics for ectomapper
Gathering detailed insights and metrics for ectomapper
Gathering detailed insights and metrics for ectomapper
Gathering detailed insights and metrics for ectomapper
npm install ectomapper
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
250%
7
Compared to previous week
Last Month
-42.9%
12
Compared to previous month
Last Year
0%
383
Compared to previous year
A lightweight, flexible, and type-safe AutoMapper utility for TypeScript, allowing you to easily map between different object types.
To install the package:
1npm i ectomapper
Here’s how to create and use an AutoMapper in your TypeScript project:
1import { createAutoMapper } from 'ectomapper' 2 3// Define your source and destination types 4interface SourceType { 5 id: number 6 name: string 7} 8 9interface DestinationType { 10 id: number 11 fullName: string 12} 13 14// Create an instance of AutoMapper 15const autoMapper = createAutoMapper() 16 17// Define a map between SourceType and DestinationType 18autoMapper.createMap<SourceType, DestinationType>( 19 'SourceType', 20 'DestinationType', 21 (source) => ({ 22 id: source.id, 23 fullName: source.name, 24 }), 25) 26 27// Use the map to transform an object 28const source: SourceType = { id: 1, name: 'John Doe' } 29const destination = autoMapper.map<SourceType, DestinationType>( 30 source, 31 'SourceType', 32 'DestinationType', 33) 34 35console.log(destination) // Output: { id: 1, fullName: 'John Doe' }
You can define multiple mappings for different types:
1interface UserDTO { 2 userId: number 3 username: string 4} 5 6interface User { 7 id: number 8 name: string 9} 10 11// Create mappings 12autoMapper.createMap<UserDTO, User>('UserDTO', 'User', (source) => ({ 13 id: source.userId, 14 name: source.username, 15})) 16 17autoMapper.createMap<User, UserDTO>('User', 'UserDTO', (source) => ({ 18 userId: source.id, 19 username: source.name, 20})) 21 22// Map between UserDTO and User 23const userDTO: UserDTO = { userId: 42, username: 'johndoe' } 24const user = autoMapper.map<UserDTO, User>(userDTO, 'UserDTO', 'User') 25 26console.log(user) // Output: { id: 42, name: 'johndoe' }
If you attempt to map an object without a corresponding mapping, an error will be thrown:
1try { 2 const unknownMapping = autoMapper.map<SourceType, DestinationType>( 3 source, 4 'UnknownSource', 5 'UnknownDestination', 6 ) 7} catch (error) { 8 console.error(error.message) // Output: No mapping found for key: UnknownSource->UnknownDestination 9}
createMap
1createMap<TSource, TDestination>( 2 sourceKey: string, 3 destinationKey: string, 4 mappingFunction: MapperFunction<TSource, TDestination> 5): void
Defines a mapping between two types using the provided string keys.
map
1map<TSource, TDestination>( 2 source: TSource, 3 sourceKey: string, 4 destinationKey: string 5): TDestination
Maps an object from the source type to the destination type using the defined mapping.
Contributions are welcome! Please submit issues and pull requests to help improve the package.
This project is licensed under the terms of the MIT License.
No vulnerabilities found.
No security vulnerabilities found.