-
Notifications
You must be signed in to change notification settings - Fork 7
/
RolodETH.js
95 lines (84 loc) · 2.42 KB
/
RolodETH.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const KFS = require("key-file-storage").default;
var fs = require('fs');
function RolodETH(filename, chainID) {
this.filename = filename;
this.chainID = chainID;
this.kfs = KFS(filename)
}
RolodETH.prototype.createIfNotExists = function (address) {
const nAddress = this.normalizeAddress(address);
if (this.v[nAddress] == null) {
this.v[nAddress] = {
tags: []
}
}
}
RolodETH.prototype.normalizeAddress = function (address) {
return address.toLowerCase()
}
RolodETH.prototype.get = function (address) {
let nAddress = this.normalizeAddress(address);
return (this.kfs[nAddress]);
}
RolodETH.prototype.getFiles = function () {
const files = fs.readdirSync(this.filename);
return files;
}
RolodETH.prototype.cleanTag = function(tag) {
tag = tag.toLowerCase();
if (tag == "blocked") {
return ""
}
tag = tag.replace(" ", "-")
tag = tag.replace("_", "-")
tag = tag.replace(".", "-")
return tag;
}
RolodETH.prototype.addProperty = async function (address, propertyName, value, overwrite = true) {
console.log(address, propertyName, value)
if (value == null || value == "") {
return;
}
const nAddress = this.normalizeAddress(address);
const existing = this.kfs[nAddress];
if (existing == null) {
let obj = {
tags: []
};
obj[propertyName] = value;
this.kfs[nAddress] = obj;
} else {
if (existing[propertyName] == null || overwrite) {
existing[propertyName] = value;
this.kfs[nAddress] = existing;
}
}
}
RolodETH.prototype.removeProperty = async function (address, propertyName) {
const nAddress = this.normalizeAddress(address);
const existing = this.kfs[nAddress];
if (existing == null) {
} else {
delete existing[propertyName];
this.kfs[nAddress] = existing;
}
}
RolodETH.prototype.addTag = function (address, tagName) {
tagName = this.cleanTag(tagName)
if (tagName == null || tagName == "") {
return;
}
const nAddress = this.normalizeAddress(address);
const existing = this.kfs[nAddress];
if (existing == null) {
this.kfs[nAddress] = {
tags: [tagName]
}
} else {
if (existing.tags.indexOf(tagName) == -1) {
existing.tags.push(tagName);
this.kfs[nAddress] = existing;
}
}
}
module.exports = RolodETH;