This repository was archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
123 lines (110 loc) · 2.95 KB
/
index.js
File metadata and controls
123 lines (110 loc) · 2.95 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
const _ = require('lodash');
// find entity by id
function findById(id, arr){
if(id) {
for(let i=0; i < arr.length; i++) {
let e = arr[i];
if(e.id === id) {
return e;
}
}
}
throw new Error('cannot find id', id);
};
function createFindById(arr) {
return function(id) {
return findById(id, arr);
};
};
// turn entity into id
function getId(entity) {
return entity.id;
}
// traverse tree
function traverse(obj, cb) {
cb(obj);
for(let i =0; i < obj.children.length; i++) {
let r = obj.children[i];
traverse(r);
}
}
/**
* Handle each entity differently
* it will callback to fn for each sub-entities--- used for serial/deserial
* @param {*Object} obj any Entity
* @param {*Function} fn callback for each entity to be serial/deserial
*/
function handleEntity(obj, fn) {
let className = obj.className;
// let key = [];
if(className === 'Line') {
obj.a = fn(obj.a);
obj.b = fn(obj.b);
}
if(className === 'Arc') {
obj.a = fn(obj.a);
obj.b = fn(obj.b);
obj.center = fn(obj.center);
}
if(className === 'Graph') {
traverse(obj, o => o.data = fn(o.data));
}
if(className === 'Panel') {
obj.outer = obj.outer.map(e => fn(e));
obj.inner = obj.inner.map(e => fn(e));
}
return obj;
}
// Turn ids into entities
function deferenceEntity(obj, entities) {
let find = createFindById(entities);
return handleEntity(obj, find);
}
// Turn entities into ids
function referenceEntity(obj) {
return handleEntity(obj, getId);
}
const pson = {
// write class to json
write(obj) {
},
read(obj, unpack, options) {
let p = new this.Pson()
return p.read(obj, unpack, options);
// if(_.isString(obj)) {
// obj = JSON.parse(obj);
// }
// let newObj = {};
// let entities = obj['entities'].map(e => this.Entity.createEntityFromData(e));
// // link pson array to entities
// _.forOwn(obj, (k, v) => {
// if(_.isArray(v)) {
// newObj[k] = v.map(e => deferenceEntity(e, entities));
// } else {
// newObj[k] = v;
// }
// });
// return newObj;
},
findById(id, entities) {
return findById(id, entities);
},
createFindById(entities) {
return createFindById(entities);
},
Entity: require('./src/Entity'),
Point: require('./src/Point'),
Line: require('./src/Line'),
Arc: require('./src/Arc'),
Panel: require('./src/Panel'),
Pson: require('./src/Pson'),
PointSegment: require('./src/PointSegment'),
Util: require('./src/util'),
test: [
require('./test/sample0.json'),
require('./test/sample1.json'),
require('./test/sample2.json'),
require('./test/sample3.json')
]
};
module.exports = pson;