Skip to content

Commit 10202ad

Browse files
committed
feat(pxtorem): add minValue property to Config and update related functionality
1 parent 0ea1ba2 commit 10202ad

File tree

4 files changed

+122
-5
lines changed

4 files changed

+122
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface Config {
22
rootValue: number
33
unitPrecision: number
4+
minValue: number
45
}

packages/pxtorem/src/index.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ import type { Config } from './contracts/config.type'
44

55
const defultConfig: Config = {
66
rootValue: 16,
7-
unitPrecision: 4
7+
unitPrecision: 4,
8+
minValue: 0
89
}
910

1011
/**
1112
* Convert pixel units to rem units.
1213
* @param {Object} config - The configuration object.
1314
* @param {number} [config.rootValue=16] - The root value for conversion. (default is 16)
1415
* @param {number} [config.unitPrecision=4] - The decimal precision for the converted value. (default is 4)
16+
* @param {number} [config.minValue=0] - The minimum value to be converted. (default is 0)
1517
* @returns {Function} `Length` function to convert pixel values to rem values used by LightningCSS.
1618
*/
1719
function pxtorem(config: Partial<Config> = {}) {
18-
const { rootValue, unitPrecision } = { ...defultConfig, ...config }
20+
const { rootValue, unitPrecision, minValue } = { ...defultConfig, ...config }
1921

2022
validatePositiveInteger(rootValue, 'rootValue')
2123
validatePositiveInteger(unitPrecision, 'unitPrecision')
@@ -28,8 +30,10 @@ function pxtorem(config: Partial<Config> = {}) {
2830

2931
return {
3032
Length({ unit, value }: LengthValue): { unit: LengthUnit, value: number } | undefined {
31-
if (unit === 'px') {
33+
if (unit === 'px' && value >= minValue) {
3234
return { unit: 'rem', value: toFixed(value / rootValue, unitPrecision) }
35+
} else {
36+
return { unit, value }
3337
}
3438
}
3539
}

packages/pxtorem/test/__snapshots__/index.test.ts.snap

+56
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ exports[`pxtorem plugin > should handle custom configuration 1`] = `
4444
"div {
4545
margin: 2rem;
4646
}
47+
48+
.foo {
49+
padding: 8px;
50+
}
4751
"
4852
`;
4953

@@ -68,6 +72,58 @@ exports[`pxtorem plugin > should handle float pixel units 1`] = `
6872
"
6973
`;
7074

75+
exports[`pxtorem plugin > should handle minimum float value 1`] = `
76+
"div {
77+
margin: .0344rem;
78+
}
79+
80+
.foo {
81+
top: .25px;
82+
}
83+
"
84+
`;
85+
86+
exports[`pxtorem plugin > should handle minimum negative value 1`] = `
87+
"div {
88+
margin: -.3125rem;
89+
}
90+
91+
.foo {
92+
top: -20px;
93+
}
94+
"
95+
`;
96+
97+
exports[`pxtorem plugin > should handle minimum positive value 1`] = `
98+
"div {
99+
margin: 1.875rem;
100+
}
101+
102+
.foo {
103+
padding: 8px;
104+
}
105+
"
106+
`;
107+
108+
exports[`pxtorem plugin > should handle minimum zero value 1`] = `
109+
"div {
110+
margin: 0;
111+
}
112+
113+
.foo {
114+
padding: .625rem;
115+
}
116+
117+
.bar {
118+
left: .0156rem;
119+
}
120+
121+
.baz {
122+
top: -.55px;
123+
}
124+
"
125+
`;
126+
71127
exports[`pxtorem plugin > should handle multiple pixel values 1`] = `
72128
"div {
73129
margin: 1rem 2rem;

packages/pxtorem/test/index.test.ts

+58-2
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ describe('pxtorem plugin', () => {
117117
})
118118

119119
it('should handle custom configuration', () => {
120-
const css = 'div { margin: 16px; }'
120+
const css = 'div { margin: 16px; } .foo { padding: 8px; }'
121121

122122
const output = transform({
123123
filename: 'input.css',
124124
code: Buffer.from(css),
125125
minify: false,
126126
sourceMap: false,
127-
visitor: composeVisitors([pxtorem({ rootValue: 8, unitPrecision: 2 })])
127+
visitor: composeVisitors([pxtorem({ rootValue: 8, unitPrecision: 2, minValue: 10 })])
128128
}).code.toString()
129129

130130
expect(output).toMatchSnapshot()
@@ -186,6 +186,62 @@ describe('pxtorem plugin', () => {
186186
expect(output).toMatchSnapshot()
187187
})
188188

189+
it('should handle minimum positive value', () => {
190+
const css = 'div { margin: 30px; } .foo { padding: 8px; }'
191+
192+
const output = transform({
193+
filename: 'input.css',
194+
code: Buffer.from(css),
195+
minify: false,
196+
sourceMap: false,
197+
visitor: composeVisitors([pxtorem({ minValue: 10 })])
198+
}).code.toString()
199+
200+
expect(output).toMatchSnapshot()
201+
})
202+
203+
it('should handle minimum negative value', () => {
204+
const css = 'div { margin: -5px; } .foo { top: -20px; }'
205+
206+
const output = transform({
207+
filename: 'input.css',
208+
code: Buffer.from(css),
209+
minify: false,
210+
sourceMap: false,
211+
visitor: composeVisitors([pxtorem({ minValue: -10 })])
212+
}).code.toString()
213+
214+
expect(output).toMatchSnapshot()
215+
})
216+
217+
it('should handle minimum float value', () => {
218+
const css = 'div { margin: 0.55px; } .foo { top: 0.25px; }'
219+
220+
const output = transform({
221+
filename: 'input.css',
222+
code: Buffer.from(css),
223+
minify: false,
224+
sourceMap: false,
225+
visitor: composeVisitors([pxtorem({ minValue: 0.5 })])
226+
}).code.toString()
227+
228+
expect(output).toMatchSnapshot()
229+
})
230+
231+
it('should handle minimum zero value', () => {
232+
const css = 'div { margin: 0px; } .foo { padding: 10px; } .bar { left: 0.25px; } .baz { top: -0.55px; }'
233+
234+
const output = transform({
235+
filename: 'input.css',
236+
code: Buffer.from(css),
237+
minify: false,
238+
sourceMap: false,
239+
visitor: composeVisitors([pxtorem({ minValue: 0 })])
240+
}).code.toString()
241+
242+
expect(output).toMatchSnapshot()
243+
})
244+
189245
it('should throw error for negative rootValue', () => {
190246
expect(() => pxtorem({ rootValue: -1 })).toThrowError('Invalid rootValue: must not be negative.')
191247
})

0 commit comments

Comments
 (0)