-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
185 lines (162 loc) · 3.35 KB
/
index.js
File metadata and controls
185 lines (162 loc) · 3.35 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
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
/**
* Object.prototype.beDitto(schema)
*
* ditto - 百变怪
*
*/
const {
prototype: ArrayProto,
} = Array;
const {
get,
set,
has,
defineProperty,
getPrototypeOf,
setPrototypeOf,
} = Reflect;
const {
defineProperties,
freeze,
create,
assign,
prototype: ObjectProto,
} = Object;
let cachSchema = new Map;
const SCHEMA = 'schema';
const variableMax = 10;
const getProp = {
enumerable: false,
configurable: false,
};
const baseProp = {
...getProp,
writable: false,
};
const baseSymbol = {
entries: {
...baseProp,
value: function* () {},
},
[Symbol.toStringTag]: {
...getProp,
get() {
return 'Ditto';
},
},
[Symbol.toPrimitive]: {
...baseProp,
value: () => 0,
},
[Symbol.iterator]: {
...baseProp,
value: iteratorGenerator,
},
};
const ditto = freeze(dittoGen());
const isProto = (target, proto) => target === proto || getPrototypeOf(target) === proto;
const isSelfExecute = key => ['toString', 'toLocaleString'].indexOf(key) > -1;
const isEqual = (target, source) => target === source;
const clearCachLater = () => setTimeout(() => cachSchema.clear());
function dittoGen() {
const base = {};
defineProperties(base, assign({
length: {
...baseProp,
value: 0,
},
valueOf: {
...baseProp,
value: false,
},
toString: {
...baseProp,
value: false,
},
[SCHEMA]: {
...getProp,
get() {
return void 0;
},
},
}, baseSymbol));
setPrototypeOf(base,
new Proxy({}, {
get(target, key, context, result = ditto) {
if (key in ArrayProto) {
return dittoFunc(key);
}
const { schema = cachSchema.get(SCHEMA) } = context;
if (schema && has(schema, key)) {
result = valueOfKlass(get(schema, key));
}
return result;
},
set() {
return false;
}
}));
return base;
};
function dittoFunc(key) {
if (isSelfExecute(key)) {
return () => ObjectProto[key].call(ditto);
}
return () => ditto;
};
function* iteratorGenerator() {
yield* Array.from({ length: variableMax }).fill(ditto);
};
function valueOfKlass(Klass, schema, result) {
try {
result = new Klass;
} catch(err) {
console.warn(`${Klass} - is not a constructorrrr!!!`);
return getDitto(schema);
}
if (isEqual(Klass, Object)) {
return getDitto(schema);
}
return result.valueOf();
};
function getDitto(schema, key, result = ditto) {
if (key in ArrayProto) {
return dittoFunc(key);
}
if (isEqual(typeof schema, 'object')) {
result = create(result);
defineProperty(result, SCHEMA, {
...baseProp,
value: schema,
});
}
return result;
};
function dittoWrapper(inst, schema) {
return create(new Proxy(inst, {
get(target, key) {
let value = get(target, key);
const thisType = get(schema || {}, key);
if (isEqual(value, void 0)) {
value = thisType ? valueOfKlass(thisType, schema) : getDitto(schema, key);
}
return value;
},
}), baseSymbol);
};
defineProperty(ObjectProto, 'beDitto', {
...baseProp,
value(schema) {
if (isProto(this, ditto)) {
return this;
}
cachSchema.set(SCHEMA, schema);
const result = dittoWrapper(this, schema);
clearCachLater(this);
return result;
},
});
module.exports = {
ditto,
variableMax,
};