-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { TextDocument } from "../text-document"; | ||
import { BoldCommand } from "./bold-command"; | ||
|
||
describe('BoldCommand', () => { | ||
it('should bold text', () => { | ||
const document = new TextDocument(); | ||
document.setContent('Hello, World!'); | ||
const boldCommand = new BoldCommand(document, 7, 9); | ||
boldCommand.execute(); | ||
expect(document.getContent()).toBe('Hello, **Wo**rld!'); | ||
}) | ||
|
||
it('should undo bold text', () => { | ||
const document = new TextDocument(); | ||
document.setContent('Hello, World!'); | ||
const boldCommand = new BoldCommand(document, 7, 9); | ||
boldCommand.execute(); | ||
boldCommand.undo(); | ||
expect(document.getContent()).toBe('Hello, World!'); | ||
}) | ||
|
||
it('should undo bold text', () => { | ||
const document = new TextDocument(); | ||
document.setContent('Hello, World!'); | ||
const boldCommand = new BoldCommand(document, 7, 5); | ||
expect(() => boldCommand.execute()).toThrow(/Invalid range/); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ICommand, IDocument } from "../interface"; | ||
|
||
export class BoldCommand implements ICommand { | ||
private document: IDocument; | ||
private from: number; | ||
private to: number; | ||
|
||
constructor(document: IDocument, from: number, to: number) { | ||
this.document = document; | ||
this.from = from; | ||
this.to = to; | ||
} | ||
|
||
execute(): void { | ||
if(this.to <= this.from) throw new Error('Invalid range'); | ||
|
||
let doc = this.document.getContent(); | ||
doc = doc.slice(0, this.from) | ||
+ "**" | ||
+ doc.slice(this.from, this.to) | ||
+ "**" | ||
+ doc.slice(this.to); | ||
this.document.setContent(doc); | ||
} | ||
|
||
undo(): void { | ||
let doc = this.document.getContent(); | ||
doc = doc.slice(0, this.from) | ||
+ doc.slice(this.from+2, this.to+2) | ||
+ doc.slice(this.to+4); | ||
this.document.setContent(doc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { TextDocument } from "../text-document"; | ||
import { EraseCommand } from "./erase-command"; | ||
|
||
describe('EraseCommand', () => { | ||
it('should erase', () => { | ||
const document = new TextDocument(); | ||
document.setContent('Hello, World!'); | ||
const eraseCommand = new EraseCommand(document, 7); | ||
eraseCommand.execute(); | ||
expect(document.getContent()).toBe('Hello,'); | ||
}) | ||
|
||
it('should redo erase', () => { | ||
const document = new TextDocument(); | ||
document.setContent('Hello, World!'); | ||
const eraseCommand = new EraseCommand(document, 7); | ||
eraseCommand.execute(); | ||
eraseCommand.undo(); | ||
expect(document.getContent()).toBe('Hello, World!'); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ICommand, IDocument } from "../interface"; | ||
|
||
export class EraseCommand implements ICommand { | ||
private document: IDocument; | ||
private eraseCount: number; | ||
private remainingPart: string = ''; | ||
|
||
constructor(document: IDocument, eraseCount: number) { | ||
this.document = document; | ||
this.eraseCount = eraseCount; | ||
} | ||
|
||
execute(): void { | ||
let doc = this.document.getContent(); | ||
const newDoc = doc.slice(0, -this.eraseCount); | ||
this.remainingPart = doc.slice(doc.length-this.eraseCount); | ||
this.document.setContent(newDoc); | ||
} | ||
|
||
undo(): void { | ||
let doc = this.document.getContent(); | ||
doc += this.remainingPart; | ||
this.document.setContent(doc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './bold-command'; | ||
export * from './erase-command'; | ||
export * from './write-command'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { TextDocument } from "../text-document"; | ||
import { WriteCommand } from "./write-command"; | ||
|
||
describe('WriteCommand', () => { | ||
it('should write text', () => { | ||
const document = new TextDocument(); | ||
const writeCommand = new WriteCommand(document, 'test'); | ||
writeCommand.execute(); | ||
expect(document.getContent()).toBe('test'); | ||
}) | ||
|
||
it('should undo write text', () => { | ||
const document = new TextDocument(); | ||
const writeCommand = new WriteCommand(document, 'hello'); | ||
writeCommand.execute(); | ||
writeCommand.undo(); | ||
expect(document.getContent()).toBe(''); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ICommand, IDocument } from "../interface"; | ||
|
||
export class WriteCommand implements ICommand { | ||
private document: IDocument; | ||
private text: string; | ||
|
||
constructor(document: IDocument, text: string) { | ||
this.document = document; | ||
this.text = text; | ||
} | ||
|
||
execute(): void { | ||
let doc = this.document.getContent(); | ||
doc += this.text; | ||
this.document.setContent(doc); | ||
} | ||
|
||
undo(): void { | ||
let doc = this.document.getContent(); | ||
doc = doc.slice(0, -this.text.length); | ||
this.document.setContent(doc); | ||
} | ||
} |