From 0385275a1605f8375e3e83ff434a5d5c85e18b69 Mon Sep 17 00:00:00 2001 From: MellowCo <799478052@qq.com> Date: Sat, 9 Jul 2022 15:41:11 +0800 Subject: [PATCH] feat: add formatting support for clamp, max & min functions https://github.com/unocss/unocss/issues/1155 --- src/utils/handlers/handlers.ts | 2 +- test/handler.test.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/handler.test.ts diff --git a/src/utils/handlers/handlers.ts b/src/utils/handlers/handlers.ts index c8b170d..7c08cd7 100644 --- a/src/utils/handlers/handlers.ts +++ b/src/utils/handlers/handlers.ts @@ -136,7 +136,7 @@ function bracketWithType(str: string, type?: string) { return base .replace(/(url\(.*?\))/g, v => v.replace(/_/g, '\\_')) .replace(/([^\\])_/g, '$1 ') - .replace(/calc\((.*)/g, (v) => { + .replace(/(?:calc|clamp|max|min)\((.*)/g, (v) => { return v.replace(/(-?\d*\.?\d(?!\b-.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([+\-/*])/g, '$1 $2 ') }) } diff --git a/test/handler.test.ts b/test/handler.test.ts new file mode 100644 index 0000000..85f786b --- /dev/null +++ b/test/handler.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from 'vitest' +import { handler as h } from '../src/utils' + +describe('value handler', () => { + it('bracket math function', () => { + expect(h.bracket('[calc(1-2)]')).eql('calc(1 - 2)') + expect(h.bracket('[min(1-2)]')).eql('min(1 - 2)') + expect(h.bracket('[max(1-2)]')).eql('max(1 - 2)') + expect(h.bracket('[clamp(1-2)]')).eql('clamp(1 - 2)') + + expect(h.bracket('[calc(1+2)]')).eql('calc(1 + 2)') + expect(h.bracket('[calc(1/2)]')).eql('calc(1 / 2)') + expect(h.bracket('[calc(1*2)]')).eql('calc(1 * 2)') + }) +})