-
Notifications
You must be signed in to change notification settings - Fork 7
/
valueFormatter.js
69 lines (64 loc) · 1.74 KB
/
valueFormatter.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
var rgbToHexa = require('./rgbToHexa');
const formatPath = (path) => {
let pathParts = path.split(' ')
simplifiedPaths = pathParts.map(path => {
let parts = path.split(',')
return parts.map(item=>{
const parts = /([M|C])?([\-0-9.]+)(z)?/g.exec(item)
if(!parts) {
return item
}
let formattedItem = ''
if(parts[1]) {
formattedItem += parts[1]
}
if(parts[2]) {
formattedItem += Math.ceil(Number(parts[2]) * 100) / 100
}
if(parts[3]) {
formattedItem += parts[3]
}
return formattedItem
//([M|C])?([\-0-9])+(z)?
}).join(',')
})
return simplifiedPaths.join(' ')
}
const formatDashArray = (value) => {
return value.split(' ').map(item=>Math.ceil(item*100)/100).join(' ')
}
const formatMatrix = (value) => {
if(value) {
let matrixNumbers = value.split(/\(|\)/)[1].split(',')
let roundedValues = matrixNumbers.map(matrixNumber=>Math.ceil(matrixNumber*100)/100)
let formattedValue = 'matrix(' + roundedValues.join(',') + ')'
value = formattedValue
} else {
value = 'scale(1)'
}
return value
}
module.exports = function(type, value) {
if (type === 'transform') {
value = formatMatrix(value)
} else if (type === 'style') {
value = value === 'display: none;' ? 'hidden' : 'inherit';
} else if (type === 'stroke-dasharray') {
value = formatDashArray(value);
} else if (type === 'opacity'
|| type === 'fill-opacity'
|| type === 'stroke-opacity'
|| type === 'stroke-width'
|| type === 'stroke-dashoffset') {
value = Math.ceil(value*100)/100;
} else if (type === 'fill' || type === 'stroke') {
if(value) {
value = rgbToHexa(value);
}
} else if (type === 'd') {
value = `path("${formatPath(value)}")`;
} else if (type === 'd--static') {
value = formatPath(value);
}
return value;
}