-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
224 lines (175 loc) · 5.36 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
function isPrimitive(arg) {
const type = typeof arg;
return arg === null || (type !== 'object' && type != 'function');
}
function isDefined(v) {
return v !== undefined;
}
class PackerNode {
constructor(obj, packer, isPacked = false) {
Object.defineProperty(obj, '__packerNode', { value : this });
this.pos = null;
this._obj = obj;
this._id = packer.newId();
this._packer = packer;
this.links = new Set();
this._isPacked = isPacked;
this._isWalked = false;
}
get id() {
return this._id;
}
/**
* Returns clone of raw object if present
*/
get obj() {
return this._packed || this._obj;
}
get packed() {
if(!this._packed) this._packed = Array.isArray(this._obj)?
this._obj.slice() : Object.assign({}, this._obj);
return this._packed;
}
get childs() {
return this._childs || (this._childs = new Map());
}
get parents() {
return this._parents || (this._parents = new Map());
}
addParent(node, propName) {
let stored = this.parents.get(node) || [];
this.parents.set(node, stored.concat([propName]));
this.links.add(node.id + '.' + propName);
node.addChild(this);
}
addChild(node) {
this.childs.set(node);
}
pack() {
if(this._isPacked) return;
this._isPacked = true;
for(const [child] of this.childs) child.pack();
if(this.links.size > 1) {
this._packer.take(this._obj, null, false);
}
for(const [node, props] of this.parents) {
for(const prop of props) {
if(this.pos) {
node.packed[prop] = this._packer._options.prefix + this.pos;
continue;
}
node.packed[prop] = this.obj;
}
}
}
unpack(isChild = false) {
if(!isChild && !this._isPacked) return;
this._isPacked = false;
this.walk();
for(const [child] of this.childs) child.unpack(true);
for(const key in this._obj) {
if(!this._obj.hasOwnProperty(key)) continue;
const pos = this._packer.extractPos(this._obj[key]);
if(pos !== null) this._obj[key] = this._packer.give(pos);
}
}
walk() {
if(this._isWalked) return;
this._isWalked = true;
const obj = this._obj;
for(let key in obj) {
if(!obj.hasOwnProperty(key) || isPrimitive(obj[key])) continue;
const node = PackerNode.getNode(obj[key], this._packer);
node.addParent(this, key);
node.walk();
}
}
static getNode(obj, packer, isPacked) {
return obj.__packerNode || new this(obj, packer, isPacked);
}
}
class Packer {
/**
* Ininialize store
* @param {String|undefined} store with packs
*/
constructor(pack = {}) {
this._names = pack.names || {};
this._store = pack.store || [];
this._storeStocks = pack.storeStocks || [];
this._options = Object.assign({
prefix : '…'
}, pack.options);
this._counter = 0;
this._reKey = new RegExp(`^${this._options.prefix}([0-9]+)$`);
if(this._store.length) {
this._store = this._store.map((obj, i) => {
const node = PackerNode.getNode(obj, this, true);
node.pos = i;
return node;
});
}
}
newId() {
return ++this._counter;
}
extractPos(str) {
const res = this._reKey.exec(str);
return res? parseInt(res[1]) : null;
}
/**
* Mark object for feature packing
* @param {Object} obj object for packing
* @param {String} name specific name for object
* @param {Boolean} [isRoot=true] false for sub take (when work pack)
*/
take(obj, objName, isRoot = true) {
const node = PackerNode.getNode(obj, this);
isRoot && node.links.add('root');
let pos = node.pos;
if(pos === null) {
pos = this._store.push(node) - 1;
this._storeStocks[pos] = 0;
node.pos = pos;
}
this._storeStocks[pos] += isRoot
? 1
: node.links.has('root')
? node.links.size - 1
: node.links.size;
if(objName) this._names[objName] = pos;
return node.pos;
}
/**
* @param {Number|String} pos Name of position or position index
* @returns {?Object}
*/
give(pos) {
pos = typeof pos === 'string'? this._names[pos] : pos;
const node = this._store[pos];
if(!node) return null;
if(node._isPacked) node.unpack();
if(--this._storeStocks[pos] < 1) delete this._store[pos];
return node.obj;
}
/**
* Packing store with replace object keys
* @returns {Object} pack
*/
pack() {
const len = this._store.length;
for(let i = 0; i < len; i++) {
this._store[i].walk();
}
for(let i = 0; i < len; i++) {
this._store[i].pack();
}
return {
options : this._options,
names : this._names,
storeStocks : this._storeStocks,
store : this._store.map(node => node.obj)
};
}
}
module.exports = Packer;