UNPKG

simple-undo-redo

Version:

Simple undo-redo functionality with branching support for JavaScript applications

54 lines (45 loc) 1.3 kB
// Simple utility functions for undo-redo-js // Deep copy using JSON (simple and works everywhere) const deepCopy = (obj) => { // Handle special cases that JSON can't handle if (obj === undefined) return undefined; if (obj === null) return null; return JSON.parse(JSON.stringify(obj)); }; // Generate unique ID (timestamp + random) const generateId = () => { const time = Date.now().toString(36); const random = Math.random().toString(36).substr(2); return time + random; }; // Check if two objects are equal const isEqual = (obj1, obj2) => { return JSON.stringify(obj1) === JSON.stringify(obj2); }; // Save to localStorage (simple) const saveToStorage = (key, data) => { if (typeof localStorage === 'undefined') return false; try { localStorage.setItem(key, JSON.stringify(data)); return true; } catch (error) { return false; } }; // Load from localStorage (simple) const loadFromStorage = (key) => { if (typeof localStorage === 'undefined') return null; try { const saved = localStorage.getItem(key); return saved ? JSON.parse(saved) : null; } catch (error) { return null; } }; module.exports = { deepCopy, generateId, isEqual, saveToStorage, loadFromStorage };