[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 ...
随机推荐
- openstack中glance组件images的全部python API 汇总
感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限.错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- ListVeiw新增记录及 滚动条移动到指定位置
C# 自带的ListView控件的滚动条移动到指定位置. lvwList为ListView控件 lvwList.EnsureVisible(lvwList.Items.Count - 1); 新增记录 ...
- VS2010 ReportViewer导出文件下载保存不能识别文件类型
今天测试项目时,突然发现导出报表下载保存的保存,不能识别文件的类型,文件名称为:.xls[3] 检查代码发现在指定报表路径时多了一个方法: ReportViewer1.LocalReport.Load ...
- Windows 不能在 本地计算机 启动 SQL Server(MSSQLSERVER)。错误码126
结合自己的解决方案和网络上搜到的内容,现总结如下: 首先你要知道问题出在了什么地方才能针对性处理. 1.打开事件查看器 计算机右击——管理 右侧会出现错误列表,在其中找到SQL server有关的查看 ...
- angular 指令梳理 —— 前端校验
angular js内置校验的扩展 校验成功则 scope.formName.$valid=true 校验失败 元素的class: ng-invalid 成功:.ng-valid /** * 校验指 ...
- (二)backbone - DEMO - user list
Demo介绍 学习了如何基本的使用Backbone,展示用户信息 使用JQuery操作DOM,backbone.localStorage.js操作localstorage 大体实现 •创建user M ...
- HDU 4268 Alice and Bob set用法
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4268 贪心思想,用set实现平衡树,但是set有唯一性,所以要用 multiset AC代码: #i ...
- Core Data (一)备
序 恩,用Core Data也有一段时间了.大大小小的坑也都坑过了.重来没有认真的记录一次.这次需要好好的理一理Core Data.就当一次绝好的机会记录下来.也为了自己加深认识. 为什么要用Core ...
- #define XBYTE ((unsigned char volatile xdata *) 0)
今天在看别人的CAN总线程序的时候,突然发现了这么一句宏定义:#define XBYTE ((unsigned char volatile xdata *) 0),以前都没注意到过.后来查了一下,发现 ...
- 开心菜鸟学习系列笔记-----Javascript(1)
js 一些常见的使用方法 // target : 不管是否出现冒泡,他都是代表最开始引发事件的对象 // this : 是指当前函数. //ie 事件对象 : window ...