Save.js 985 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. export class Save {
  2. constructor() {
  3. this.prefix = "untitled"
  4. }
  5. _get() {
  6. return JSON.parse(localStorage.getItem(`${this.prefix}-savedata`) || "{}")
  7. }
  8. _set(value) {
  9. localStorage.setItem(`${this.prefix}-savedata`, JSON.stringify(value))
  10. }
  11. setKey(key, value) {
  12. let saveData = this._get()
  13. saveData[key] = value
  14. this._set(saveData)
  15. }
  16. appendKey(key, value) {
  17. let saveData = this._get()
  18. if(!saveData[key]) {
  19. saveData[key] = []
  20. }
  21. saveData[key].push(value)
  22. this._set(saveData)
  23. }
  24. incrementKey(key, value) {
  25. let saveData = this._get()
  26. if(!saveData[key]) {
  27. saveData[key] = 0
  28. }
  29. saveData[key]+= value
  30. this._set(saveData)
  31. }
  32. getKey(key) {
  33. let saveData = this._get()
  34. return saveData[key]
  35. }
  36. delete() {
  37. localStorage.removeItem(`${this.prefix}-savedata`)
  38. }
  39. }