2017-06-18 74 views
0

我正在努力編寫一個有一些方法的類。我不確定我做錯了什麼。用方法編寫一個類

我有以下的測試,我需要看看,以創建這個類:

'use strict'; 
 

 
const Editor = require('../editor'); 
 
const { expect } = require('chai'); 
 

 
describe('Editor',() => { 
 
    it('allows users to write text',() => { 
 
    const editor = new Editor(); 
 

 
    editor.write('Hello - codez'); 
 
    expect(editor.toString()).to.equal('Hello - codez'); 
 

 
    editor.write('moar'); 
 
    expect(editor.toString()).to.equal('Hello - codez moar'); 
 
    }); 
 

 
    xit('allows users to undo writes',() => { 
 
    const editor = new Editor(); 
 

 
    editor.write('Hello - codez'); 
 
    expect(editor.toString()).to.equal('Hello - codez'); 
 

 
    editor.write('Moar stuff'); 
 
    expect(editor.toString()).to.equal('Hello - codezMoar stuff'); 
 

 
    editor.write('Even more'); 
 
    expect(editor.toString()).to.equal('Hello - codezMoar stuffEven more'); 
 

 
    editor.undo(); 
 
    expect(editor.toString()).to.equal('Hello - codezMoar stuff'); 
 

 
    editor.undo(); 
 
    expect(editor.toString()).to.equal('Hello - codez'); 
 

 
    editor.undo(); 
 
    expect(editor.toString()).to.equal(''); 
 
    }); 
 

 
    xit('allows users to find and replace',() => { 
 
    const editor = new Editor(); 
 

 
    editor.write('foo stuff'); 
 
    editor.write(' other foo'); 
 
    editor.replace('foo', 'bar'); 
 
    expect(editor.toString()).to.equal('bar stuff other bar'); 
 
    }); 
 

 
    xit('allows undo replaces',() => { 
 
    const editor = new Editor(); 
 

 
    editor.write('foo stuff'); 
 
    editor.write(' other foo'); 
 
    editor.replace('foo', 'bar'); 
 
    expect(editor.toString()).to.equal('bar stuff other bar'); 
 

 
    editor.undo(); 
 
    expect(editor.toString()).to.equal('foo stuff other foo'); 
 

 
    editor.undo(); 
 
    expect(editor.toString()).to.equal('foo stuff'); 
 
    }); 
 

 
    xit('allows users to redo',() => { 
 
    const editor = new Editor(); 
 

 
    editor.write('foo stuff'); 
 
    editor.write(' other foo'); 
 
    editor.replace('foo', 'bar'); 
 
    expect(editor.toString()).to.equal('bar stuff other bar'); 
 

 
    editor.undo(); 
 
    expect(editor.toString()).to.equal('foo stuff other foo'); 
 

 
    editor.undo(); 
 
    expect(editor.toString()).to.equal('foo stuff'); 
 

 
    editor.redo(); 
 
    expect(editor.toString()).to.equal('foo stuff other foo'); 
 

 
    editor.redo(); 
 
    expect(editor.toString()).to.equal('bar stuff other bar'); 
 
    }); 
 
});

我寫了下面codebut我不知道我在做什麼錯誤或從哪裏去。有人能幫助並告訴我測試中發生了什麼,以及我期望做什麼。 :

class Editor { 
 
    constructor (str) { 
 
    this.str = str; 
 
    } 
 

 
    write(text) { 
 
     let newSentence = text + this.str; 
 
    console.log('This is the this str', newSentence); 
 
    } 
 

 
    toString(){ 
 
    
 
    } 
 
}

+1

this.str + =文本; ... toString(){return this.str;} –

回答

1
class Editor { 
    constructor (str) { 
    this.str = str; 
    this.states = [""]; //for undo 
    this.undos = []; 
    } 
    write(text) { 
    this.undos = []; 
    this.states.push(this.str); 
    this.str += text; 
    } 
    undo(){ 
    this.undos.push(this.str); 
    this.str = this.states.pop() || ""; 
    } 
    redo(){ 
    if(this.undos.length){ 
     this.states.push(this.str); 
     this.str = this.undos.pop(); 
    } 
    } 
    replace(a,b){ 
    this.states.push(this.str); 
    this.str = this.str.split(a).join(b); 
    } 
    toString(){ 
    return this.str; 
    } 
} 

你需要保持你的編輯字符串的歷史...

+0

感謝您撰寫Jonas。你能解釋一下你如何到達創建狀態和撤消數組?您的解決方案非常棒。我想確保我明白它在做什麼。 –

+0

@ivonne terrero撤消變化,你要麼跟蹤他們(很複雜),要麼只是存儲所有狀態。我們存儲的最後一個狀態將在撤消時恢復,因此它的* First in Last out * =>我們需要一個堆棧(push/pop)。同樣的原則適用於重做。 –