Installations
npm install ectomapper
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
22.4.1
NPM Version
10.8.1
Score
65.8
Supply Chain
98.6
Quality
77.7
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Download Statistics
Total Downloads
249
Last Day
1
Last Week
3
Last Month
10
Last Year
249
Bundle Size
13.43 kB
Minified
4.52 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.2
Package Id
ectomapper@1.0.2
Unpacked Size
132.19 kB
Size
30.53 kB
File Count
7
NPM Version
10.8.1
Node Version
22.4.1
Publised On
03 Oct 2024
Total Downloads
Cumulative downloads
Total Downloads
249
Last day
0%
1
Compared to previous day
Last week
50%
3
Compared to previous week
Last month
-37.5%
10
Compared to previous month
Last year
0%
249
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
ectomapper
A lightweight, flexible, and type-safe AutoMapper utility for TypeScript, allowing you to easily map between different object types.
Features
- Type-safe mappings: Leverage TypeScript generics to ensure type safety when mapping between object types.
- Flexible configuration: Define custom mappings between any two types using string keys.
Installation
To install the package:
1npm i ectomapper
Usage
Basic example
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' }
Advanced example
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' }
Error Handling
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}
API
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.
- sourceKey: A string that identifies the source type.
- destinationKey: A string that identifies the destination type.
- mappingFunction: A function that defines how to map from the source type to the destination type.
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.
- source: The object to map.
- sourceKey: The key identifying the source type.
- destinationKey: The key identifying the destination type.
- Returns: The mapped object of the destination type.
Contributing
Contributions are welcome! Please submit issues and pull requests to help improve the package.
License
This project is licensed under the terms of the MIT License.
No vulnerabilities found.
No security vulnerabilities found.