-
Notifications
You must be signed in to change notification settings - Fork 2
/
optim.js
51 lines (40 loc) · 1.3 KB
/
optim.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
const traverse = require('traverse');
const REMOVE = ['nm', 'mn', 'ix', 'np', 'cix'];
const REMOVE_K = ['r', 's'];
module.exports = function compress(input, maxPrecision = 2) {
let nodesDeleted = 0;
if (typeof input === 'string') input = JSON.parse(input);
const inputBytes = JSON.stringify(input).length;
const outputObject = traverse(input).forEach(function(node) {
if (this.key === 'hd' && node === false) {
nodesDeleted++;
return this.delete();
}
if (REMOVE.includes(this.key)) {
nodesDeleted++;
return this.delete();
}
if (this.parent != null && this.parent.key === 'k' && REMOVE_K.includes(this.key)) {
nodesDeleted++;
return this.delete();
}
if (typeof node === 'number') {
// Don't round color nodes
if (
this.parent != null &&
this.parent.key === 'k' &&
this.parent.parent != null &&
this.parent.parent.key === 'c'
)
return;
return this.update(round(node, maxPrecision));
}
});
const output = JSON.stringify(outputObject);
const outputBytes = output.length;
return {output, outputObject, inputBytes, outputBytes, nodesDeleted};
};
function round(value = 0, decimals = 0) {
multiplier = Math.pow(10, decimals);
return Math.round(value * multiplier) / multiplier;
}