MediumPro challengeTypeScript

Memento

TypeScriptDesign PatternsBehavioralMemento

Capture and externalize an object's internal state so it can be restored later, without violating encapsulation — a text editor's undo stack.

Implement `Editor.save()` (returns an EditorMemento snapshot of the current content) and Editor.restore(memento) (resets content back to that snapshot).

solve(ops) replays { op: 'type', text } | { op: 'save' } | { op: 'undo' } steps and returns the final content — 'undo' restores the most recent save.

solve([{op:'type',text:'Hello'},{op:'save'},{op:'type',text:' World'},{op:'undo'}])'Hello'

Sample tests

Test #1No undo — full content
Input: [[{"op":"type","text":"Hello"},{"op":"save"},{"op":"type","text":" World"}]]
Output: "Hello World"
Test #2Undo restores the last save
Input: [[{"op":"type","text":"Hello"},{"op":"save"},{"op":"type","text":" World"},{"op":"undo"}]]
Output: "Hello"
Test #3Undo with no prior save is a no-op
Input: [[{"op":"type","text":"A"},{"op":"type","text":"B"},{"op":"undo"}]]
Output: "AB"