forked from jscad/OpenJSCAD.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (63 loc) · 1.77 KB
/
index.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
/*
JSCAD Object to JSON Notation Serialization
## License
Copyright (c) JSCAD Organization https://github.com/jscad
All code released under MIT license
Notes:
1) geom2 conversion to:
JSON
2) geom3 conversion to:
JSON
3) path2 conversion to:
JSON
*/
/**
* Serializer of JSCAD geometries to JSON strings.
* @module io/json-serializer
* @example
* const { serializer, mimeType } = require('@jscad/json-serializer')
*/
const { utils } = require('@jscad/modeling')
// Replace all typed arrays in geometries with standard Arrays
// NOTE: 'this' in replacer is the object in which key was found
const replacer = (key, value) => {
switch (key) {
case 'transforms':
case 'plane':
return Array.from(value)
case 'points':
case 'vertices':
return value.map((v) => Array.from(v))
case 'sides':
return value.map((s) => [Array.from(s[0]), Array.from(s[1])])
default:
break
}
return value
}
/**
* Serialize the give objects to JSON.
* @param {Object} options - options for serialization, REQUIRED
* @param {Object|Array} objects - objects to serialize as JSON
* @returns {Array} serialized contents as JSON string
* @alias module:io/json-serializer.serialize
* @example
* const geometry = primitives.cube()
* const jsonData = serializer({}, geometry)
*/
const serialize = (options, ...objects) => {
const defaults = {
statusCallback: null
}
options = Object.assign({}, defaults, options)
objects = utils.flatten(objects)
options.statusCallback && options.statusCallback({ progress: 0 })
const notation = JSON.stringify(objects, replacer)
options.statusCallback && options.statusCallback({ progress: 100 })
return [notation]
}
const mimeType = 'application/json'
module.exports = {
serialize,
mimeType
}