🔽🔽🔽🔽🔽🔽🔽�Старый бредовый код🔽🔽🔽🔽🔽🔽
⏫⏫⏫⏫⏫⏫⏫⏫⏫⏫⏫⏫⏫⏫⏫⏫⏫⏫⏫⏫
Кроме того, я могу построить колеса.Ты боишься, что тебя спросят на собеседовании? Стройте колесо в день, и все готово.
Аспект
- Рукописные вопросы для письменного теста и собеседования
1. UNDO
it("前进undo ", () => {
const history = createHistory()
history.push({num: 1})
history.push({num: 2})
history.push({num: 3})
history.undo()
expect(history.present.num).toBe(2)
});
2. REDO
it("回退redo ", () => {
const history = createHistory()
history.push({num: 1})
history.push({num: 2})
history.push({num: 3})
history.push({num: 4})
history.undo()
history.undo()
history.undo()
history.redo()
expect(history.present.num).toBe(2)
});
it("定点回退 ", () => {
const history = createHistory()
history.push({num: 1})
history.push({num: 2})
history.push({num: 3})
history.gotoState(1)
expect(history.present.num).toBe(2)
});
module.exports = createHistory = () => {
const timeline = {};
timeline.past = [];
timeline.futrue = [];
timeline.gotoState = (index) => {
const allState = [...timeline.past, timeline.present, ...timeline.futrue];
timeline.present = allState[index];
timeline.past = allState.slice(0, index);
timeline.futrue = allState.slice(index + 1, allState.length);
};
timeline.getIndex = () => {
// 获取当前状态index
return timeline.past.length;
};
// 保存当前状态
timeline.push = (currentState) => {
// 将状态都保存,并更新当前状态
if (timeline.present) {
timeline.past.push(timeline.present);
}
timeline.present = currentState;
};
// 后退
timeline.undo = () => {
if (timeline.past.length !== 0) {
timeline.gotoState(timeline.getIndex() - 1);
}
};
// 前进
timeline.redo = () => {
if (timeline.futrue.length !== 0) {
timeline.gotoState(timeline.getIndex() + 1);
}
};
return timeline;
};