[Immutable.js] Working with Subsets of an Immutable.js Map()

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();
时间: 2024-10-12 17:04:23

[Immutable.js] Working with Subsets of an Immutable.js Map()的相关文章

[Immutable.js] Converting Immutable.js Structures to Javascript and other Immutable Types

Immutable.js provides several conversion methods to migrate one structure to another. Each Immutable.js class contains a prefixed "to" method like Map.toList(), Map.toSet(), etc. Converting these types sometimes results in a loss of data, as we

HTML5 画布上的 Three.js 环境灯光(HTML5 Canvas Three.js Ambient Lighting)

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. HTML5 画布上的 Three.js 环境灯光HTML5 Canvas Three.js Ambient Lighting <!DOCTY

JS中单引号/双引号以及外部js引入的一些问题

一.单引号和双引号的用法的问题 在JavaScript中可以使用单引号.双引号,二者也可以混合使用.但是,身为菜鸟的我,却碰到了一些引号的使用问题. <body> <div style="border:1px solid red; height:150px;width:150px;" onclick="alert("test");" id="test">This is a test. </div&

《Node.js入门》Windows 7下Node.js Web开发环境搭建笔记

近期想尝试一下在IBM Bluemix上使用Node.js创建Web应用程序.所以须要在本地搭建Node.js Web的开发測试环境. 这里讲的是Windows下的搭建方法,使用CentOS 的小伙伴请參考:<Node.js入门>CentOS 6.5下Node.js Web开发环境搭建笔记 Node.js是什么? 我们看看百科里怎么说的? JavaScript是一种执行在浏览器的脚本,它简单,轻巧.易于编辑,这样的脚本通经常使用于浏览器的前端编程.可是一位开发人员Ryan有一天发现这样的前端式

JS中的模块化开发之Sea.JS

模块化开发的好处: 1:减少冲突 2:提高性能 用sea.js为例:sea.js模块库下载地址:http://seajs.org/docs/#downloads 例子:获取非行间样式的模块化开发: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="

iOS JS 交互之利用系统JSContext实现 JS调用oc方法

ios js 交互分为两块: 1.oc调用js 这一块实现起来比较简单, 我的项目中加载的是本地的html,js,css,需要注意的是当你向工程中拖入这些文件时,选择如下操作,(拖入的文件夹是蓝色的,相对路径),不然css,js 的路径会存在问题 加载本地html: oc调用js:一句代码搞定 2.js 调用oc js调用oc又分为两种: 1.js端是直接调用方法 这里就要说到ios7才推出的一个新的api    JavaScriptCore,首先我们引入这个类,并初始化一个JSContext对

js便签笔记(8)——js加载XML字符串或文件

1. 加载XML文件 方法1:ajax方式.代码如下: var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); xhr.open("GET", "data.xml", false); xhr.send(null); var xmlDoc = xhr.responseXML; console.log(xmlDoc

phantomjs helloworld.js报错: Can&#39;t open &#39;helloworld.js&#39;

PhantomJS是一个无界面的,可脚本编程的WebKit浏览器引.它原生支持多种web 标准:DOM 操作,CSS选择器,JSON,Canvas 以及SVG. 当我安装好PhantomJS后,写下第一个js文件,运行发现报错. 我尝试了一些方法:比如将helloworld.js文件所在路径放到环境变量path下,发现这样运行徒劳无功. 后来,在http://bbs.fishc.com/forum.php?mod=viewthread&do=tradeinfo&tid=79336这个网页上

JS 循环遍历JSON数据 分类: JS技术 JS JQuery 2010-12-01 13:56 43646人阅读 评论(5) 收藏 举报 jsonc JSON数据如:{&amp;quot;options&amp;quot;:&amp;quot;[{

JS 循环遍历JSON数据 分类: JS技术 JS JQuery2010-12-01 13:56 43646人阅读 评论(5) 收藏 举报 jsonc JSON数据如:{"options":"[{/"text/":/"王家湾/",/"value/":/"9/"},{/"text/":/"李家湾/",/"valu e/":/"10