zoukankan      html  css  js  c++  java
  • [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();
  • 相关阅读:
    软件安装的list(0918)
    putty配色备份
    曹工谈Spring Boot:Spring boot中怎么进行外部化配置,一不留神摔一跤;一路debug,原来是我太年轻了
    使用Hystrix的插件机制,解决在使用线程隔离时,threadlocal的传递问题
    曹工谈并发:Synchronized升级为重量级锁后,靠什么 API 来阻塞自己
    曹工力荐:调试 jdk 中 rt.jar 包部分的源码(可自由增加注释,修改代码并debug)
    曹工杂谈--只用一个命令,centos系统里装了啥软件,啥时候装的,全都清清楚楚
    曹工说Redis源码(7)-- redis server 的周期执行任务,到底要做些啥
    曹工说Redis源码(6)-- redis server 主循环大体流程解析
    曹工说Redis源码(5)-- redis server 启动过程解析,以及EventLoop每次处理事件前的前置工作解析(下)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5229557.html
Copyright © 2011-2022 走看看