-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
49 lines (49 loc) · 1.45 KB
/
process.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class CssRemProcess {
constructor(cog) {
this.cog = cog;
this.rePx = /([\d.]+)p(x)?/;
this.rePxAll = /([\d.]+)px/g;
}
/**
* 换px转换成rem
*
* @private
* @param {string} pxStr
*/
pxToRem(pxStr) {
const px = parseFloat(pxStr);
let remValue = +(px / this.cog.rootFontSize).toFixed(this.cog.fixedDigits);
if (this.cog.autoRemovePrefixZero) {
if (remValue.toString().startsWith('0.'))
remValue = remValue.toString().substring(1);
}
return { px: pxStr, pxValue: px, remValue, rem: remValue + 'rem; /* ' + pxStr + 'px */' };
}
/**
* px转rem
*
* @param {string} text 需要转换文本,例如:10px 12p
* @return {Object} { px: '10px', pxValue: 10, rem: '1rem', remValue: 1 }
*/
convert(text) {
let match = text.match(this.rePx);
if (!match)
return null;
return this.pxToRem(match[1]);
}
/** 批量转换 */
convertAll(code) {
if (!code)
return code;
return code.replace(this.rePxAll, (word) => {
const res = this.pxToRem(word);
if (res)
return res.rem;
return word;
});
}
}
exports.CssRemProcess = CssRemProcess;
//# sourceMappingURL=process.js.map