This repository was archived by the owner on May 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (59 loc) · 1.28 KB
/
index.js
File metadata and controls
70 lines (59 loc) · 1.28 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module.exports=dataset;
/*global document*/
// replace namesLikeThis with names-like-this
function toDashed(name) {
return name.replace(/([A-Z])/g, function(u) {
return "-" + u.toLowerCase();
});
}
var fn;
if (typeof document !== "undefined" && document.head && document.head.dataset) {
fn = {
set: function(node, attr, value) {
node.dataset[attr] = value;
},
get: function(node, attr) {
return node.dataset[attr];
},
del: function (node, attr) {
delete node.dataset[attr];
}
};
} else {
fn = {
set: function(node, attr, value) {
node.setAttribute('data-' + toDashed(attr), value);
},
get: function(node, attr) {
return node.getAttribute('data-' + toDashed(attr));
},
del: function (node, attr) {
node.removeAttribute('data-' + toDashed(attr));
}
};
}
function dataset(node, attr, value) {
var self = {
set: set,
get: get,
del: del
};
function set(attr, value) {
fn.set(node, attr, value);
return self;
}
function del(attr) {
fn.del(node, attr);
return self;
}
function get(attr) {
return fn.get(node, attr);
}
if (arguments.length === 3) {
return set(attr, value);
}
if (arguments.length == 2) {
return get(attr);
}
return self;
}