Javascript 版的对象映射 Automapper.js

码农软件 · 软件分类 · 常用JavaScript包 · 2019-04-04 17:42:48

软件介绍

在C#项目开发中,我们经常需要很多对象模型,如视图模型、传输模型、领域模型、实体模型等,我相信很多开发的朋友都明白最头疼的无法使模型之间的赋值,传统的方法就是一一赋值。终于春天来了,AutoMapper 的出现让我们解放了,各种映射完全秒秒钟的事情。

最近在Javascript开发过程中也遇到了相同的需求,果断 github 搜索一番,纳尼?没有!!!没办法,谁叫我是代码工呢,果断撸一把,不一会,automapper.js 诞生了。

使用文档

<script src="automapper.js" type="text/javascript"></script>
  • 示例演示

// # 示例一

var srcObj = { name: "百小僧", age: 25, gender: '男' }; // 源类型
var destObj= { name: null, age: null, gender: null };   // 目标类型

// 创建映射关系
automapper.createMap("src","dest");
// 开始映射
automapper.map("src","dest",srcObj,destObj);
// 输出结果:
// srcObj => { name: "百小僧", age: 25, gender: '男' }
// destObj => { name: "百小僧", age: 25, gender: '男' }


// # 示例二

var srcObj = { name: "百小僧", age: 25, gender: '男' }; // 源类型
var destObj = { firstName: null, age: null, sex: null, birthday: null };    // 目标类型

// 创建映射关系
automapper.createMap("src", "dest")
    .forMember("firstName", function (src, dest) { return "Mr." + src.name })
    .forMember("sex", srcObj.gender)
    .forMember("birthday", '1993-05-27');
// 开始映射
automapper.map("src", "dest", srcObj, destObj);
// 输出结果:
// srcObj => { name: "百小僧", age: 25, gender: '男' }
// destObj => {firstName: "Mr.百小僧", age: 25, sex: "男", birthday: "1993-05-27"}


automapper.js 目前还是第一版,实现代码也非常简单

; !function (win) {
    "use strict";

    var AutoMapper = function () {
        this.profiles = {};
        this.version = '1.0.0';
    };

    AutoMapper.prototype.createMap = function (srcKey, destKey) {
        var that = this, combineKey = srcKey + "->" + destKey;
        if (!that.profiles.hasOwnProperty(combineKey)) that.profiles[combineKey] = {};

        var funcs = {
            forMember: function (prop, any) {
                that.profiles[combineKey][prop] = any;
                return funcs;
            }
        };
        return funcs;
    };

    AutoMapper.prototype.map = function (srcKey, destKey, srcObj, destObj) {
        var that = this, combineKey = srcKey + "->" + destKey;
        if (!that.profiles.hasOwnProperty(combineKey)) throw "Could not find mapping with a source of " + srcKey + " and a destination of " + destKey;

        var profile = that.profiles[combineKey];
        for (var prop in destObj) {
            if (!profile.hasOwnProperty(prop)) destObj[prop] = srcObj.hasOwnProperty(prop) ? srcObj[prop] : destObj[prop];
            else {
                var output = profile[prop];
                if (typeof output === "function") destObj[prop] = output(srcObj, destObj);
                else destObj[prop] = output;
            }
        }
    };

    win.automapper = new AutoMapper();
}(window);

本文地址:https://codercto.com/soft/d/2858.html

The Seasoned Schemer

The Seasoned Schemer

Daniel P. Friedman、Matthias Felleisen / The MIT Press / 1995-12-21 / USD 38.00

drawings by Duane Bibbyforeword and afterword by Guy L. Steele Jr.The notion that "thinking about computing is one of the most exciting things the human mind can do" sets both The Little Schemer (form......一起来看看 《The Seasoned Schemer》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码