diff --git a/src/index.ts b/src/index.ts index d1858c1..ff2ab2d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import { isGradient, resolveGradient } from './js/css-gradient'; import { cssVar } from './js/css-var'; import { extractDashedIdent, + isAbsoluteFontSize, isAbsoluteSizeOrLength, isColor, resolveLengthInPixels, @@ -23,6 +24,7 @@ export const utils = { cssCalc, cssVar, extractDashedIdent, + isAbsoluteFontSize, isAbsoluteSizeOrLength, isColor, isGradient, diff --git a/src/js/util.ts b/src/js/util.ts index 7c4d93c..0ca502c 100644 --- a/src/js/util.ts +++ b/src/js/util.ts @@ -461,3 +461,28 @@ export const isAbsoluteSizeOrLength = ( } return value === 0; }; + +/** + * is absolute font size + * @param value - value + * @returns result + */ +export const isAbsoluteFontSize = (value: unknown): boolean => { + if (isString(value)) { + const size = value.toLowerCase().trim() as string; + if (/^[a-z-]+$/.test(size)) { + return absoluteFontSize.has(size); + } else { + const [, val, unit] = /^(\d+(?:\.\d+)?|\.\d+)([a-z-]+)?$/.exec( + size + ) as RegExpExecArray; + if (unit) { + return absoluteLength.has(unit); + } else if (val) { + const num = parseFloat(val); + return num === 0; + } + } + } + return false; +}; diff --git a/test/util.test.ts b/test/util.test.ts index b118465..c6e38d2 100644 --- a/test/util.test.ts +++ b/test/util.test.ts @@ -880,3 +880,62 @@ describe('is absolute size or length', () => { assert.strictEqual(res, true, 'result'); }); }); + +describe('is absolute font size', () => { + const func = util.isAbsoluteFontSize; + + it('should get false', () => { + const res = func('foo'); + assert.strictEqual(res, false, 'result'); + }); + + it('should get false', () => { + const res = func('smaller'); + assert.strictEqual(res, false, 'result'); + }); + + it('should get true', () => { + const res = func('medium'); + assert.strictEqual(res, true, 'result'); + }); + + it('should get false', () => { + const res = func('1em'); + assert.strictEqual(res, false, 'result'); + }); + + it('should get false', () => { + const res = func('1rem'); + assert.strictEqual(res, false, 'result'); + }); + + it('should get true', () => { + const res = func('1px'); + assert.strictEqual(res, true, 'result'); + }); + + it('should get true', () => { + const res = func('1pt'); + assert.strictEqual(res, true, 'result'); + }); + + it('should get false', () => { + const res = func('1'); + assert.strictEqual(res, false, 'result'); + }); + + it('should get true', () => { + const res = func('0'); + assert.strictEqual(res, true, 'result'); + }); + + it('should get true', () => { + const res = func('0.0'); + assert.strictEqual(res, true, 'result'); + }); + + it('should get true', () => { + const res = func('.0'); + assert.strictEqual(res, true, 'result'); + }); +});