-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathstorage.go
55 lines (42 loc) · 1.42 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package godom
// This file implements Storage interface
// https://developer.mozilla.org/en-US/docs/Web/API/Storage
import (
"github.com/gopherjs/gopherjs/js"
)
type Storage struct {
*js.Object
}
var LocalStorage = &Storage{Window.Get("localStorage")}
var SessionStorage = &Storage{Window.Get("sessionStorage")}
// Properties
// https://developer.mozilla.org/en-US/docs/Web/API/Storage/length
func (s *Storage) Length() int {
return s.Get("length").Int()
}
// Methods
// https://developer.mozilla.org/en-US/docs/Web/API/Storage/key
func (s *Storage) Key(index int) string {
return s.Call("key", index).String()
}
// https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem
func (s *Storage) GetItem(keyName string) string {
return s.Call("getItem", keyName).String()
}
// https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem
func (s *Storage) SetItem(keyName, keyValue string) {
s.Call("setItem", keyName, keyValue)
}
// https://developer.mozilla.org/en-US/docs/Web/API/Storage/removeItem
func (s *Storage) RemoveItem(keyName string) {
s.Call("removeItem", keyName)
}
// https://developer.mozilla.org/en-US/docs/Web/API/Storage/clear
func (s *Storage) Clear() {
s.Call("clear")
}
// IsKeyExist is not part of Web Storage API. This method tests if a key exists
// in the Storage (localStorage/sessionStorage) interface.
func (s *Storage) IsKeyExist(keyName string) bool {
return s.Call("getItem", keyName) != nil
}