[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 ...
随机推荐
- Node.js 之 express 入门 ejs include公共部分
1. 直接进入express安装 因为之前有一篇文章我已经讲过怎么安装node了 而网上的教程也是非常多.所有直接进入到express.教程简陋 由于我比较笨 所有只要写到我自己明白就行. 这里有个教 ...
- VCS仿真生成vpd文件(verilog)
VCS仿真生成vpd文件(verilog) 一.环境与文件 Linux平台 csh环境 VCS 64bit 代码文件请参考<一个简单的Verilog计数器模型> 二.开始仿真 1.com ...
- (转载)Linux网络配置和setup工具包安装
查看网卡是否正常安装 命令:lspci |grep Ether 1.修改网卡配置 命令: vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth ...
- 让DIV垂直居中的几种办法
1.使用CSS3 的伸缩盒布局 <!doctype html> <html> <head> <meta charset="utf-8"&g ...
- web 调用OPC HRESULT:0x80070005 (E_ACCESSDENIED))
除了配置DCOM外,还需要在web.config里面添加设置系统管理员权限的帐号和密码<identity impersonate="true" userName=" ...
- lnmp、lamp、lnmpa一键安装包(Updated: 2015-10-25)
lnmp.lamp.lnmpa一键安装包(Updated: 2015-10-25) 2014-12-26 Posted by yeho 这个脚本是使用shell编写,为了快速在生产环境上部署lnmp/ ...
- C/C++中的const int*和int * const
代码: #include <iostream> using namespace std; int main(){ const int *p; ; p = &a; a = ; cou ...
- hdu5323 Solve this interesting problem(爆搜)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Solve this interesting problem Time Limit ...
- React java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/com.edaixi.activity/lib-main/libgnustl_shared.so" is 32-bit instead of 64-bit
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.edaixi.activity, PID: 3659 at java.lang.Runtime ...
- CSU 1120 病毒
最长公共上升子序列(LCIS) 裸的算法题. 动态规划: 两组数组a[n].b[m]. f[i][i]表示以a[i].b[j]结尾的两个数组的LCIS. 转移方程: a[i]!=b[j] : f[i] ...