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();

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

  1. [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 ...

  2. 由js apply与call方法想到的js数据类型(原始类型和引用类型)

    原文地址:由js apply与call方法想到的js数据类型(原始类型和引用类型) js的call方法与apply方法的区别在于第二个参数的不同,他们都有2个参数,第一个为对象(即需要用对象a继承b, ...

  3. io.js入门(一)—— 初识io.js

    io.js可以说是彻底从NodeJS里分离出来的一条分支,其事情始末可以查看这篇报道,此处便也不赘言.既然是分支,io.js便也基本兼容NodeJS的各种API,连执行指令也依旧兼容Node的 nod ...

  4. js相对路径相关(比如:js中的路径依赖导入该js文件的路径)

    问题描述: 前几天调用同事的js接口文件,在他自己的html测试页面ok,在我这边调用时出现问题. debug过程中,将该测试html移到其他位置都不行,放到原html测试页面同层次路径下是OK的. ...

  5. 【转】关于URL编码/javascript/js url 编码/url的三个js编码函数

    来源:http://www.cnblogs.com/huzi007/p/4174519.html 关于URL编码/javascript/js url 编码/url的三个js编码函数escape(),e ...

  6. App.js – 用于移动 Web App 开发的 JS 界面库

    App.js 是一个轻量级的 JavaScript UI 库,用于创建像本地应用程序的移动 Web 应用而不牺牲性能和体验.它是跨平台的,特定的UI设计,配置类似原生的过渡效果.App.js 的目的是 ...

  7. 一个问题提交的实例(js原生动画,原生ajax,js引用加参数)

    document.writeln("<div id=\"tanchuangwai\" class=\"tanchuangwai\" style= ...

  8. JS一般般的网页重构可以使用Node.js做些什么(转)

    一.非计算机背景前端如何快速了解Node.js? 做前端的应该都听过Node.js,偏开发背景的童鞋应该都玩过. 对于一些没有计算机背景的,工作内容以静态页面呈现为主的前端,可能并未把玩过Node.j ...

  9. Js判断对象是否为空,Js判断字符串是否为空

    Js判断对象是否为空,Js判断字符串是否为空,JS检查字符串是否为空字符串 >>>>>>>>>>>>>>>&g ...

随机推荐

  1. EasyInvoice 使用教程 - (1) 认识 EI

    原视频下载地址:EI 主界面介绍 1. 主界面截图 2. 基础资料界面截图 3. 管理员 界面截图

  2. ASP.NET入门 - 错误和异常处理

    当发生错误时,应该向用户提供一个友好的界面,可以屏蔽详细技术信息,也有助提升用户体验,这时就用到了异常处理. 错误类型: 语法错误:因缺少关键字,符号,或其他不正确代码所产生的错误,无法通过编译. 逻 ...

  3. WndProc函数(转)

    WndProc函数作用: 主要用在拦截并处理系统消息和自定义消息 比如:windows程序会产生很多消息,比如你单击鼠标,移动窗口都会产生消息.这个函数就是默认的消息处理函数.你可以重载这个函数来制定 ...

  4. EClipse开发NDK流程

    EClipse开发NDK流程(现在studio也在2.2之后支持了非常简单,只要创建项目的时候勾选c++支持就可以了)   什么情况下使用ndk,1.保护代码,java很容易反编译,c/c++反汇编比 ...

  5. C++中cin输入类型不匹配解决方法

    #include <iostream> #include <set> using namespace std; int main() { int a; cin>>a ...

  6. Hibernate学习笔记(一):mycelipse建立项目流程(未完成)

    1.部署数据库: 2.部署项目: 3.引入Hibernate: 4.url配置

  7. 实现Web虚拟现实的最轻松方案—A-Frame框架

    问题 随着vr的热度那么web虚拟现实是否可行 1. 实现Web虚拟现实的最轻松方案 A-Frame A-Frame是一款开源的可通过定制HTML元素构建WebVR方案的框架.有了这个框架,Web程序 ...

  8. WordPress nginx环境下开启多站点

    在wp-config.php插入 define('WP_ALLOW_MULTISITE', true); 进入管理页面安装网络,子目录模式按提示再在wp-config.php插入 define('MU ...

  9. Java 中字符串的子串搜索

    基友前两天参加了阿里的实习生面试,问了个问题,就是关于字符串的子串搜索的问题.想想实现方式无非就是两层循环,但是 java 中是有现成实现的,于是我就去查查源码,看看 java 语言怎么实现这个的,发 ...

  10. Python学习笔记九-文件读写

    1,读取文件: f=open('目录','读写模式',encoding='gbk,error='egiong') 后三项可以不写但是默认是' r'读模式:open函数打开的文件对象会自动加上read( ...