Skip to content

Commit

Permalink
fix: case sensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 14, 2018
1 parent 1399f1e commit 1a64ea1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/parseCssValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export interface CSSValue {
value: number,
unit: string
}

/**
* 解析 CSS 值的数值和单位。
*
* @param value 要解析的值
* @param [defaultUnit='px'] 默认单位
* @returns 解析结果
*/
export default function parseCSSValue(
value: string | number,
defaultUnit: string = 'px'
): CSSValue {
if (typeof value === 'number') {
return {
value,
unit: defaultUnit
}
}
const matches = value.trim().match(/^(-?[\d+.-]+)([a-z]+|%)$/i)
return matches !== null
? {
value: Number(matches[1]),
unit: matches[2]
}
: {
value: Number(value),
unit: defaultUnit
}
}

0 comments on commit 1a64ea1

Please sign in to comment.