forked from hanshuebner/html-scrabble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icebox.js
105 lines (97 loc) · 3.58 KB
/
icebox.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
function thaw(object, prototypeMap) {
var objectsThawed = [];
if (prototypeMap && prototypeMap.length) {
// convert to hash map
var newPrototypeMap = {};
prototypeMap.forEach(function(constructor) { newPrototypeMap[constructor.name] = constructor; });
prototypeMap = newPrototypeMap
}
function thawTree(object) {
if (object && (typeof object == 'object')) {
if (object._ref !== undefined) {
if (objectsThawed[object._ref]) {
return objectsThawed[object._ref];
} else {
throw "invalid reference to id " + object._ref;
}
} else {
var thawed;
if (object._constructorName == 'Date') {
thawed = new Date(object._isoString);
} else {
thawed = (typeof object.length == 'undefined') ? {} : [];
if (object.hasOwnProperty('_constructorName')) {
var constructor = prototypeMap[object._constructorName];
if (constructor) {
thawed.__proto__ = constructor['prototype'];
} else {
console.log('no prototype for ' + object._constructorName);
}
}
if (object.hasOwnProperty('_id')) {
objectsThawed[object._id] = thawed;
}
for (var prop in object) {
if (object.hasOwnProperty(prop)
&& prop != '_constructorName'
&& prop != '_id') {
thawed[prop] = thawTree(object[prop]);
}
}
}
return thawed;
}
} else {
return object;
}
}
return thawTree(object);
}
function freeze(object) {
var objectsFrozen = [];
var id = 0;
function freezeObject(object) {
object._frozen = (typeof object.length == 'undefined') ? {} : [];
Object.defineProperty(object, '_frozen', { enumerable: false });
objectsFrozen[id] = object;
object._frozen._id = id;
id++;
return object._frozen;
}
function freezeTree(object) {
if (object && (typeof object == 'object')) {
if (!object.constructor) {
console.log('freeze strange object', object);
return object;
} else if (object._frozen && typeof object.length == 'undefined') {
return { _ref: object._frozen._id };
} else {
var frozen = freezeObject(object);
if (!object.constructor.name.match(/^(Array|Object)$/)) {
frozen._constructorName = object.constructor.name;
}
if (object.constructor.name == 'Date') {
frozen._isoString = object.toISOString();
} else {
for (var prop in object) {
if (object.hasOwnProperty(prop) && prop != '_constructorName') {
frozen[prop] = freezeTree(object[prop]);
}
}
}
return frozen;
}
} else {
return object;
}
}
var after = freezeTree(object);
for (var i in objectsFrozen) {
delete objectsFrozen[i]._frozen;
}
return after;
}
if (typeof exports == 'object') {
exports.freeze = freeze;
exports.thaw = thaw;
}