-
Notifications
You must be signed in to change notification settings - Fork 0
/
Obj.js
95 lines (84 loc) · 1.92 KB
/
Obj.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
/**
* Created by army8735 on 2018/8/28.
*/
'use strict';
const migi = require('migi');
const util = migi.util;
const encodeHtml = util.encodeHtml;
const stringify = util.stringify;
const Element = migi.Element;
let joinArray = util.joinArray = function(arr, prop) {
var res = '';
for(var i = 0, len = arr.length; i < len; i++) {
var item = arr[i];
if(Array.isArray(item)) {
res += joinArray(item);
}
else if(item instanceof Element) {
res += prop ? encodeHtml(item.toString(), prop) : item.toString();
}
else if(item && item.str) {
res += prop ? encodeHtml(item.str, prop) : item.str;
}
else if(item instanceof Obj) {
res += item.toString(prop);
}
else {
res += encodeHtml(stringify(item), prop);
}
}
return res;
};
let joinSourceArray = util.joinSourceArray = function(arr) {
var res = '';
for(var i = 0, len = arr.length; i < len; i++) {
var item = arr[i];
if(Array.isArray(item)) {
res += joinSourceArray(item);
}
else if(item && item.str) {
res += stringify(item.str);
}
else {
res += stringify(item);
}
}
return res;
};
function Obj(k, cb, single, vBind) {
this.k = k;
this.cb = cb;
this.single = single;
this.vBind = vBind;
this.setV(cb());
}
Obj.prototype.setV = function(v) {
this.v = v;
};
Obj.prototype.toString = function(prop) {
if(Array.isArray(this.v)) {
return util.joinArray(this.v, prop);
}
if(this.v && this.v.str) {
return this.v.str;
}
var s = util.stringify(this.v);
if(prop) {
return util.encodeHtml(s, prop);
}
return util.encodeHtml(s);
};
Obj.prototype.toSourceString = function() {
if(Array.isArray(this.v)) {
return util.joinSourceArray(this.v);
}
return util.stringify(this.v);
};
Obj.prototype.update = function(ov) {
var nv = this.cb();
if(!util.equal(ov, nv)) {
this.setV(nv);
return true;
}
};
module.exports = Obj;