-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
79 lines (75 loc) · 2.24 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
79
const path = require('path');
const fs = require('fs');
const xmldom = require('xmldom');
const normalize = require('normalize-svg-coords');
/**
* Promise all
* @author Loreto Parisi (loretoparisi at gmail dot com)
*/
function promiseAllP(items, block) {
var promises = [];
items.forEach(function(item, index) {
promises.push(
(function(item, i) {
return new Promise(function(resolve, reject) {
return block.apply(this, [item, index, resolve, reject]);
});
})(item, index),
);
});
return Promise.all(promises);
} //promiseAll
/**
* read files
* @param dirname string
* @return Promise
* @author Loreto Parisi (loretoparisi at gmail dot com)
* @see http://stackoverflow.com/questions/10049557/reading-all-files-in-a-directory-store-them-in-objects-and-send-the-object
*/
function readFiles(dirname) {
return new Promise((resolve, reject) => {
fs.readdir(dirname, function(err, filenames) {
if (err) return reject(err);
promiseAllP(filenames, (filename, index, resolve, reject) => {
fs.readFile(path.resolve(dirname, filename), 'utf-8', function(
err,
content,
) {
if (err) return reject(err);
return resolve({filename: filename, contents: content});
});
})
.then(results => {
return resolve(results);
})
.catch(error => {
return reject(error);
});
});
});
}
var DOMParser = require('xmldom').DOMParser;
var parser = new DOMParser();
readFiles('./data/')
.then(files => {
console.log('loaded ', files.length);
files.forEach((item, index) => {
var svg = parser.parseFromString(item.contents, 'image/svg+xml');
var viewBox = svg.getElementsByTagName('svg')[0].getAttribute('viewBox');
var path = svg.getElementsByTagName('path')[0].getAttribute('d');
console.log('\n---> Parsing', item.filename);
console.log(' Current ViewBox', viewBox);
console.log(' Current Path', path);
var normalizedPath = normalize({
viewBox,
path,
min: 0,
max: 512,
asList: false,
});
console.log(' Normalized Path', normalizedPath);
});
})
.catch(error => {
console.log(error);
});