Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

refector(rateFn): RegularExpression optimize #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function unit(name) {
*/
function getConverted(val, base) {
let ret = val;
const num = val.match(/((-|\+)*[0-9]+)%/);
const num = val.match(getConvertedRegNum);

if (num && num.length >= 1) {
ret = `${base * (parseFloat(num[1]) / 100)}px`;
Expand All @@ -64,6 +64,8 @@ function getConverted(val, base) {

return ret;
}
// previous regex = /((-|\+)*[0-9]+)%/
const getConvertedRegNum = /([-+]?[0-9.]+)%/;

/**
* Parse a transform atom value.
Expand All @@ -75,14 +77,16 @@ function getConverted(val, base) {
* which is called very frequently.
*/
function toParsedFloat(val) {
const m = val.match(/((-|\+)*[\d|.]+)(px|deg|rad)*/);
const m = val.match(toParsedFloatRegNum);
let ret;

if (m && m.length >= 1) {
ret = {"num": parseFloat(m[1]), "unit": m[3]};
}
return ret;
}
// previous regex = /((-|\+)*[\d|.]+)(px|deg|rad)*/
const toParsedFloatRegNum = /([-+]?[0-9.]+)(px|deg|rad)?/;

function correctUnit(transform, width, height) {
let m;
Expand Down