Immutable.js offers methods to break immutable structures into subsets much like Array--for instance it has the all powerful slice()--and unlike Array it offers functional methods like take() and skip(). You get the best of both the imperative and functional worlds.

mocha.setup('bdd');
const expect = chai.expect; class Todo { constructor(title="", text="", completed=false) {
this.id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
this.title = title;
this.text = text;
this.completed = completed;
} } function addTodo(todos, todo) {
return todos.set(todo.id, todo);
} function retrieveFinalPair(todos) {
return todos.slice(todos.size-2, todos.size);
// Alernatively, you can use this terser syntax
//return todos.slice(-2);
} function removeLastEntry(todos) {
return todos.slice(0, -1);
} function removeFirstEntry(todos) {
return todos.slice(1, todos.size);
} function removeFirstFive(todos) {
return todos.skip(5);
} function findMeMonkey(todos) {
return todos.skipUntil(todo => todo.text === "monkey" );
} function stopAtMonkey(todos) {
return todos.skipWhile(todo => todo.text === "monkey" );
} describe('Working with Subsets of an Immutable.js Map()', () => { it('should retrieve last two entries using slice()', () => { var todos = Immutable.Map(); _.each(_.range(10), (index) => {
todos = addTodo(todos, new Todo("Todo" + index, "I'm a todo!", false));
}); const lastTwoTodos = retrieveFinalPair(todos); expect(lastTwoTodos.size).to.equal(2); todos.takeLast(2).forEach(todo => {
expect(lastTwoTodos.get(todo.id)).to.equal(todo);
}); }); it('should remove last entry using negative slice()', () => { var todos = Immutable.Map(); _.each(_.range(10), (index) => {
todos = addTodo(todos, new Todo("Todo" + index, "I'm a todo!", false));
}); const todosWithoutLast = removeLastEntry(todos); todos.butLast().forEach(todo => {
expect(todosWithoutLast.get(todo.id)).to.equal(todo);
}); }); it('should remove first entry using slice()', () => { var todos = Immutable.Map(); _.each(_.range(10), (index) => {
todos = addTodo(todos, new Todo("Todo" + index, "I'm a todo!", false));
}); const todosWithoutFirst = removeFirstEntry(todos); todos.rest().forEach(todo => {
expect(todosWithoutFirst.get(todo.id)).to.equal(todo);
}); }); it('should return last 5 todos using skip()', () => { var todos = Immutable.Map(); _.each(_.range(10), (index) => {
todos = addTodo(todos, new Todo("Todo" + index, "I'm a todo!", false));
}); const lastFive = removeFirstFive(todos); todos.takeLast(5).forEach(todo => {
expect(lastFive.get(todo.id)).to.equal(todo);
}); }); it('should return todos after reaching \"monkey\" using skipUntil()', () => { var texts = ["dog", "cat", "frog", "monkey", "octopus", "horse", "orangutan"];
var todos = Immutable.Map(); _.each(_.range(texts.length), (index) => {
todos = addTodo(todos, new Todo("Todo" + index, texts[index], false));
}); const monkeyAndAfter = findMeMonkey(todos); todos.takeLast(4).forEach(todo => {
expect(monkeyAndAfter.get(todo.id)).to.equal(todo);
}); }); it('should return todos up to reaching \"monkey\" using skipWhile()', () => { var texts = ["dog", "cat", "frog", "monkey", "octopus", "horse", "orangutan"];
var todos = Immutable.Map(); _.each(_.range(texts.length), (index) => {
todos = addTodo(todos, new Todo("Todo" + index, texts[index], false));
}); const upToMonkey = stopAtMonkey(todos); todos.take(4).forEach(todo => {
expect(upToMonkey.get(todo.id)).to.equal(todo);
}); }); }); mocha.run();

最新文章

  1. [网站公告]数据库服务器IOPS跑满造成网站不能正常访问
  2. EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(八)
  3. 配置自己的OpenGL库,glew、freeglut库编译,库冲突解决(附OpenGL Demo程序)
  4. 1011MySQL Query Cache学习笔记
  5. 【BZOJ】1132: [POI2008]Tro
  6. 关于postgresql——常用操作指令
  7. java类加载器学习2——自定义类加载器和父类委托机制带来的问题
  8. 总结JavaScript输出内容(document.write)
  9. 利用Comparator排序
  10. Objective-c @property和@Synthesize
  11. 使用pdfbox分页保存pdf为图片
  12. C-fopen,fwrite,fread,fseek,fgets,popen,access笔记
  13. Jarvis OJ A Piece Of Cake
  14. 论坛IP地址追踪&路由器密码嗅探
  15. 【算法python实现】 -- 不同路径II
  16. Lua 与 C 交互值 函数调用(2)
  17. 开启mac上印象笔记的代码块
  18. Swagger介绍-一套流行的API框架
  19. 小程序学习一 .json 文件配置
  20. 【MySQL】【3】String和Date相互转换

热门文章

  1. WebSphere配置数据库连接池
  2. nagios和zabbix自定义监控脚本
  3. (转载)Linux网络配置和setup工具包安装
  4. python-文件压缩和解压
  5. 使用RDS不得不知的注意事项
  6. 使用date转换UNIX时间戳
  7. FTS下载地址
  8. CentOS 6.5 伪分布式 安装 hadoop 2.6.0
  9. hdu 1232畅通工程
  10. IOS 开发-- 常用-- 核心代码