[Immutable.js] Working with Subsets of an Immutable.js Map()
2023-09-14 08:59:20 时间
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();
相关文章
- Node.js 工具库 BlueBird 的一些例子:map,each 和 some
- java map 转string_java-将Map <String,Object>转换为Map <String,String>
- java中map根据value找key_Java Map和Map<String, Object>类型,map通过value找key
- node.js(1)
- js 对象转数组 的方法「建议收藏」
- js中四舍五入的方法_JS取整
- 集Oracle中存储Map集的新方法(oracle保存map)
- 解决Redis频繁修改Map难题(redis频繁修改map)
- Redis批量获取多个Map(redis获取多个map)
- Redis脚本快速遍历Map集(redis脚本遍历map)
- Redis存储Map一次全方位试验(redis能存map吗)
- jsdefineSetter-给js的"class"自动增加一个set的属性(方法)
- 比较简单实用的使用正则三种版本的js去空格处理方法
- JS函数验证总结(方便js客户端输入验证)
- js弹出框替代浏览器的弹出框
- js中页面的重新加载(当前页面/上级页面)及frame或iframe元素引用介绍
- JS阻止冒泡事件以及默认事件发生的简单方法
- js获取下拉列表的值和元素个数示例
- node.js中的fs.close方法使用说明
- node.js中的fs.appendFile方法使用说明