@dynamic-mapper/mapper

中文文档
1.10.4发布于 2023/11/11MITTypeScriptESM + CJS零依赖近 30 天下载 3153

Object to object mapping in JavaScript

原始使用文档
文档目录6 节
  1. Installation
  2. Documentation
  3. Motivation
  4. Usage
  5. Integrations
  6. License

DynamicMapper

npm version CircleCI

DynamicMapper is a dependency free library that provides support for object to object mapping to JavaScript for both browser and node.js environment. Inspired by AutoMapper.

For best experience, use DynamicMapper together with TypeScript.

Installation

npm i @dynamic-mapper/mapper --save

Documentation

Complete documentation available here.

Motivation

Take a UI application where data are consumed in a form of Response DTO interface that needs to be transformed to Domain interface that is later transformed into a UI view (i.e. Angular Reactive Form value). Modified view then needs to be transformed back to Domain and this updated domain object should be send back to the server in form of Request DTO. Pretty tedious to write those mapping in an imperative way for each individual domain object.

Example

Usage

import { MappingPair, MapperConfiguration } from '@dynamic-mapper/mapper';

interface CustomerDto {
    firstName: string;
    lastName: string;
}

interface Customer {
    firstName: string;
    lastName: string;
    fullName: string;
}

const CustomerDtoToCustomer = new MappingPair<CustomerDto, Customer>();

const configuration = new MapperConfiguration(cfg => {
    cfg.createAutoMap(CustomerDtoToCustomer, {
        fullName: opt => opt.mapFrom(src => `${src.firstName} ${src.lastName}`)
    });
});

const mapper = configuration.createMapper();

const customerDto: CustomerDto = {
    firstName: 'John',
    lastName: 'Doe'
};

const customer = mapper.map(CustomerDtoToCustomer, customerDto);
// {
//      firstName: 'John',
//      lastName: 'Doe',
//      fullName: 'John Doe'
// }

Integrations

License

MIT

版本与发布记录

@dynamic-mapper/mapper 共发布 21 个版本,首次发布于 2019/09/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.10.4latest2023/11/113 年前
1.10.22023/05/203 年前
1.10.12023/05/203 年前
1.10.02023/05/193 年前
1.9.12020/05/206 年前
1.9.02020/05/066 年前

其他发布通道:alpha → 1.0.0-alpha.0。

关于 @dynamic-mapper/mapper 的常见问题

@dynamic-mapper/mapper 是什么?

Object to object mapping in JavaScript

如何安装 @dynamic-mapper/mapper?

在项目目录运行 npm install @dynamic-mapper/mapper,或使用 pnpm add / yarn add / bun add。

@dynamic-mapper/mapper 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dynamic-mapper/mapper 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dynamic-mapper/mapper 可以商用吗?

@dynamic-mapper/mapper 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dynamic-mapper/mapper 的下载量有多少?

根据 npm 官方下载统计,@dynamic-mapper/mapper 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,153 次,日均约 105 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dynamic-mapper/mapper 还在维护吗?

最新版本 1.10.4 发布于 2023/11/11(3 年前);至今共发布 21 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/09/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。