[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();
[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 apply与call方法想到的js数据类型(原始类型和引用类型)
原文地址:由js apply与call方法想到的js数据类型(原始类型和引用类型) js的call方法与apply方法的区别在于第二个参数的不同,他们都有2个参数,第一个为对象(即需要用对象a继承b, ...
- io.js入门(一)—— 初识io.js
io.js可以说是彻底从NodeJS里分离出来的一条分支,其事情始末可以查看这篇报道,此处便也不赘言.既然是分支,io.js便也基本兼容NodeJS的各种API,连执行指令也依旧兼容Node的 nod ...
- js相对路径相关(比如:js中的路径依赖导入该js文件的路径)
问题描述: 前几天调用同事的js接口文件,在他自己的html测试页面ok,在我这边调用时出现问题. debug过程中,将该测试html移到其他位置都不行,放到原html测试页面同层次路径下是OK的. ...
- 【转】关于URL编码/javascript/js url 编码/url的三个js编码函数
来源:http://www.cnblogs.com/huzi007/p/4174519.html 关于URL编码/javascript/js url 编码/url的三个js编码函数escape(),e ...
- App.js – 用于移动 Web App 开发的 JS 界面库
App.js 是一个轻量级的 JavaScript UI 库,用于创建像本地应用程序的移动 Web 应用而不牺牲性能和体验.它是跨平台的,特定的UI设计,配置类似原生的过渡效果.App.js 的目的是 ...
- 一个问题提交的实例(js原生动画,原生ajax,js引用加参数)
document.writeln("<div id=\"tanchuangwai\" class=\"tanchuangwai\" style= ...
- JS一般般的网页重构可以使用Node.js做些什么(转)
一.非计算机背景前端如何快速了解Node.js? 做前端的应该都听过Node.js,偏开发背景的童鞋应该都玩过. 对于一些没有计算机背景的,工作内容以静态页面呈现为主的前端,可能并未把玩过Node.j ...
- Js判断对象是否为空,Js判断字符串是否为空
Js判断对象是否为空,Js判断字符串是否为空,JS检查字符串是否为空字符串 >>>>>>>>>>>>>>>&g ...
随机推荐
- Android开发所有视频教程汇总
1.Mars的Android开发视频教程作者讲解的很详细,很全面,系统.以前出了两套视频,分别是<Java4Android视频教程>.<Android视频教程>,以及最新刚新出 ...
- View事件传递之父View和子View之间的那点事
Android事件传递流程在网上可以找到很多资料,FrameWork层输入事件和消费事件,可以参考: Touch事件派发过程详解 这篇blog阐述了底层是如何处理屏幕输,并往上传递的.Touch事件传 ...
- android studio 报ambiguous method call
如题,在android studio中调用this.toString时,提示的错误信息是ambiguous method call. both get class () in object and g ...
- 让qq图标在自己的网站上显示方法
代码如下: <div id="xixi" onmouseover="toBig()" style="top: 260px; left: 5px; ...
- linux make
linux make file 以下是转载 感谢原作者 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 ...
- iOS 关于时间戳的一些细节
最近遇到一个bug,在iPhone上获取当前的时间戳,没有问题,而用iPad来测试的时候,出现了时间戳不对.当时的代码 (需要精确到毫秒,所以*1000) NSTimeInterval t = [[N ...
- UISearchDisplayController简单使用
最近在做一个简单的app入门,中间有一个页面用到了搜索框,本来以为很简单的控件,没想到用到的时候才发现很麻烦. 搜索框使用过程大约有以下几个状态:不活跃-活跃-输入关键词-根据关键词动态列出相关结果- ...
- 在windows中搭建php开发环境
一.wampserver wampserver是一个安装集成包,包含了开发所需的apache,mysql,php,简单方便. 下载地址 http://www.xiazaiba.com/html/279 ...
- jquery validate form 异步提交
jQuery取得select选中的值 jQuery("#select1 option:selected").text(); 相信很多人都用过jquery validate插件,非 ...
- wordpress安装地址与博客地址
可千万别乱改动你的wordpress安装地址和博客地址 一月 27th, 2009 Posted in web学习, 博客建设 | 12 Comments » 我最近一个月学习数据库的一些使用,把他爱 ...