-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibs.js
More file actions
51 lines (46 loc) · 1.2 KB
/
libs.js
File metadata and controls
51 lines (46 loc) · 1.2 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
const { IMGData, Matrix, Vector } = require('./math/index');
function iguale(obj, props, original){
for(var prop of props) obj[prop] = obj[original];
}
function igualeAll(obj, cases){
for(var [props, original] of cases)
iguale(obj, props, original);
}
function $save(_key, value){
if(typeof value !== 'object')
return value;
else if('save' in value && typeof value.save === 'function')
return $save(value.save());
else if(value instanceof IMGData)
return { type: 'math/imgdata', data: value.data, dims: value.dims };
else if(value instanceof Matrix)
return { type: 'math/matrix', data: value.elements };
else if(value instanceof Vector)
return { type: 'math/vector', data: value.elements };
else
return value;
}
function load(obj){
if('type' in obj){
switch(obj.type){
case 'math/imgdata':
obj = new IMGData(obj.data, obj.dims); break;
case 'math/matrix':
obj = new Matrix(obj.data); break;
case 'math/vector':
obj = new Vector(obj.data); break;
default: break;
}
}
return obj;
}
/**
* Saves data
*
* @param { any } data
* @returns { string } Saved data
*/
function save(data){
return JSON.stringify(data, $save);
}
module.exports = { iguale, igualeAll, save, load };