Javascript 版的对象映射 Automapper.js
- 授权协议: MIT
- 开发语言: JavaScript
- 操作系统: 跨平台
- 软件首页: https://gitee.com/monksoul/automapper.js
- 软件文档: https://gitee.com/monksoul/automapper.js
软件介绍
在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);
算法导论
[美] Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest、Clifford Stein / 高等教育出版社 / 2002-5 / 68.00元
《算法导论》自第一版出版以来,已经成为世界范围内广泛使用的大学教材和专业人员的标准参考手册。 这本书全面论述了算法的内容,从一定深度上涵盖了算法的诸多方面,同时其讲授和分析方法又兼顾了各个层次读者的接受能力。各章内容自成体系,可作为独立单元学习。所有算法都用英文和伪码描述,使具备初步编程经验的人也可读懂。全书讲解通俗易懂,且不失深度和数学上的严谨性。第二版增加了新的章节,如算法作用、概率分析......一起来看看 《算法导论》 这本书的介绍吧!
