Skip to content

Commit

Permalink
Add FOLD.convert.toJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Jan 19, 2019
1 parent fc4b002 commit 14bb95a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
21 changes: 21 additions & 0 deletions dist/fold.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,27 @@ convert.faces_vertices_to_edges = function(mesh) {
return mesh;
};

convert.toJSON = function(fold) {
var key, obj, value;
return "{\n" + ((function() {
var results;
results = [];
for (key in fold) {
value = fold[key];
results.push((" " + (JSON.stringify(key)) + ": ") + (Array.isArray(value) ? "[\n" + ((function() {
var j, len, results1;
results1 = [];
for (j = 0, len = value.length; j < len; j++) {
obj = value[j];
results1.push(" " + (JSON.stringify(obj)));
}
return results1;
})()).join(',\n') + "\n ]" : JSON.stringify(value)));
}
return results;
})()).join(',\n') + "\n}\n";
};

convert.extensions = {};

convert.converters = {};
Expand Down
2 changes: 2 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ for details.

File format conversion (supported formats are `"fold"` and `"opx"`):

* `FOLD.convert.toJSON(fold)`:
Given a FOLD object, convert into a nicely formatted JSON string.
* `FOLD.convert.convertFromTo(data, fromFormat, toFormat)`: Convert the
specified data from one format to another.
* `FOLD.convert.convertFrom(data, fromFormat)`: Convert the specified data
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fold",
"version": "0.10.1",
"version": "0.10.2",
"description": "FOLD file format for origami models, crease patterns, etc.",
"main": "lib/index.js",
"bin": {
Expand Down
14 changes: 14 additions & 0 deletions src/convert.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ convert.faces_vertices_to_edges = (mesh) ->
)
mesh

convert.toJSON = (fold) ->
## Convert FOLD object into nicely formatted JSON
"{\n" +
(for key, value of fold
" #{JSON.stringify key}: " +
if Array.isArray value
"[\n" +
(" #{JSON.stringify(obj)}" for obj in value).join(',\n') +
"\n ]"
else
JSON.stringify value
).join(',\n') +
"\n}\n"

convert.extensions = {}
convert.converters = {}
convert.getConverter = (fromExt, toExt) ->
Expand Down
4 changes: 2 additions & 2 deletions src/file.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ file.toFile = (fold, output, converter = null) ->
return
result = converter fold
if typeof result != 'string'
result = JSON.stringify result, null, 1
result = convert.toJSON result
fs.writeFileSync output, result, 'utf-8'

file.fileToFile = (input, output, converter = null) ->
Expand Down Expand Up @@ -59,7 +59,7 @@ file.fileToFile = (input, output, converter = null) ->
console.log input, '->', output
result = converter fs.readFileSync input, 'utf-8'
if typeof result != 'string'
result = JSON.stringify result, null, 1
result = convert.toJSON result
fs.writeFileSync output, result, 'utf-8'

file.main = (args = process.argv[2..]) ->
Expand Down

0 comments on commit 14bb95a

Please sign in to comment.