-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivate-attributes.js
More file actions
52 lines (41 loc) · 1.19 KB
/
private-attributes.js
File metadata and controls
52 lines (41 loc) · 1.19 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
// fixme weakmap
var privateAttributes = new Map();
const UUID_KEY = 'data-uuid';
var cache = {};
cache[UUID_KEY] = new Map();
function setPrivateAttribute (element, name, value) {
if (!privateAttributes.get(element)) {
privateAttributes.set(element, {});
}
privateAttributes.get(element)[name] = value;
cache[name].set(value, element);
}
function getPrivateAttribute (element, name) {
return privateAttributes.get(element) && privateAttributes.get(element)[name];
}
function hasPrivateAttribute (element, name) {
return !!getPrivateAttribute(element, name);
}
function removePrivateAttribute (element, name) {
if (privateAttributes.get(element)) {
delete privateAttributes.get(element)[name];
}
}
// Fixme keep a cache of attributes in another weakmap?
function queryPrivateAttribute (name, value) {
return cache[name].get(value);
//
// for (var element of privateAttributes.keys()) {
// if (getPrivateAttribute(element, name) === value) {
// return element;
// }
// }
// return null;
}
module.exports = {
get: getPrivateAttribute,
has: hasPrivateAttribute,
set: setPrivateAttribute,
query: queryPrivateAttribute,
remove: removePrivateAttribute
};