Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Store/make-ordered-dictionary.js
1129 views
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
function makeOrderedDictionary(idGetter) {
4
const array = [];
5
const dict = {};
6
const get = (id) => dict[id];
7
const update = (item) => {
8
const id = idGetter(item);
9
const idx = array.findIndex(i => idGetter(i) === id);
10
if (idx >= 0) {
11
array[idx] = item;
12
dict[id] = item;
13
}
14
return false;
15
};
16
const upsert = (item, mode) => {
17
const id = idGetter(item);
18
if (get(id)) {
19
update(item);
20
}
21
else {
22
if (mode === 'append') {
23
array.push(item);
24
}
25
else {
26
array.splice(0, 0, item);
27
}
28
dict[id] = item;
29
}
30
};
31
const remove = (item) => {
32
const id = idGetter(item);
33
const idx = array.findIndex(i => idGetter(i) === id);
34
if (idx >= 0) {
35
array.splice(idx, 1);
36
delete dict[id];
37
return true;
38
}
39
return false;
40
};
41
return {
42
array,
43
get,
44
upsert,
45
update,
46
remove,
47
updateAssign: (id, update) => {
48
const item = get(id);
49
if (item) {
50
Object.assign(item, update);
51
delete dict[id];
52
dict[idGetter(item)] = item;
53
return true;
54
}
55
return false;
56
},
57
clear: () => {
58
array.splice(0, array.length);
59
Object.keys(dict).forEach(key => {
60
delete dict[key];
61
});
62
},
63
filter: (contain) => {
64
let i = 0;
65
while (i < array.length) {
66
if (!contain(array[i])) {
67
delete dict[idGetter(array[i])];
68
array.splice(i, 1);
69
}
70
else {
71
i += 1;
72
}
73
}
74
},
75
toJSON: () => array,
76
fromJSON: (newItems) => {
77
array.splice(0, array.length, ...newItems);
78
}
79
};
80
}
81
exports.default = makeOrderedDictionary;
82
83