Skip to content

Commit 5621761

Browse files
authored
fix: reduction(#121)
1 parent 111a48d commit 5621761

File tree

2 files changed

+218
-277
lines changed

2 files changed

+218
-277
lines changed

src/__tests__/index.js

+68-12
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,70 @@ test(
116116
'should reduce additions and subtractions (4)',
117117
testValue,
118118
'calc((100vw - 50em) / 2)',
119-
'calc((100vw - 50em)/2)',
119+
'calc(50vw - 25em)',
120120
);
121121

122122
test(
123123
'should reduce additions and subtractions (5)',
124124
testValue,
125125
'calc(10px - (100vw - 50em) / 2)',
126-
'calc(10px - (100vw - 50em)/2)',
126+
'calc(10px - 50vw + 25em)',
127+
);
128+
129+
test(
130+
'should reduce additions and subtractions (6)',
131+
testValue,
132+
'calc(1px - (2em + 4vh + 3%))',
133+
'calc(1px - 2em - 4vh - 3%)',
134+
);
135+
136+
test(
137+
'should reduce additions and subtractions (7)',
138+
testValue,
139+
'calc(0px - (24px - (var(--a) - var(--b)) / 2 + var(--c)))',
140+
'calc(-24px + var(--a)/2 - var(--b)/2 - var(--c))',
141+
);
142+
143+
test(
144+
'should reduce additions and subtractions (8)',
145+
testValue,
146+
'calc(1px + (2em + (3vh + 4px)))',
147+
'calc(5px + 2em + 3vh)',
148+
);
149+
150+
test(
151+
'should reduce additions and subtractions (9)',
152+
testValue,
153+
'calc(1px - (2em + 4px - 6vh) / 2)',
154+
'calc(-1px - 1em + 3vh)',
155+
);
156+
157+
test(
158+
'should reduce multiplication',
159+
testValue,
160+
'calc(((var(--a) + 4px) * 2) * 2)',
161+
'calc(var(--a)*2*2 + 16px)',
162+
);
163+
164+
test(
165+
'should reduce multiplication before reducing additions',
166+
testValue,
167+
'calc(((var(--a) + 4px) * 2) * 2 + 4px)',
168+
'calc(var(--a)*2*2 + 20px)',
169+
);
170+
171+
test(
172+
'should reduce division',
173+
testValue,
174+
'calc(((var(--a) + 4px) / 2) / 2)',
175+
'calc(var(--a)/2/2 + 1px)',
176+
);
177+
178+
test(
179+
'should reduce division before reducing additions',
180+
testValue,
181+
'calc(((var(--a) + 4px) / 2) / 2 + 4px)',
182+
'calc(var(--a)/2/2 + 5px)',
127183
);
128184

129185
test(
@@ -243,7 +299,7 @@ test(
243299
'should ignore calc with css variables (7)',
244300
testValue,
245301
'calc(var(--popupHeight) / 2 + var(--popupWidth) / 2)',
246-
'calc((var(--popupHeight) + var(--popupWidth))/2)',
302+
'calc(var(--popupHeight)/2 + var(--popupWidth)/2)',
247303
);
248304

249305

@@ -372,35 +428,35 @@ test(
372428
'should reduce subtracted expression from zero',
373429
testValue,
374430
'calc( 0 - calc(1px + 1em) )',
375-
'calc(-1px + -1em)',
431+
'calc(-1px - 1em)',
376432
);
377433

378434
test(
379435
'should reduce substracted expression from zero (1)',
380436
testValue,
381437
'calc( 0 - (100vw - 10px) / 2 )',
382-
'calc((-100vw - -10px)/2)',
438+
'calc(-50vw + 5px)',
383439
);
384440

385441
test(
386442
'should reduce substracted expression from zero (2)',
387443
testValue,
388444
'calc( 0px - (100vw - 10px))',
389-
'calc(-100vw - -10px)',
445+
'calc(10px - 100vw)',
390446
);
391447

392448
test(
393449
'should reduce substracted expression from zero (3)',
394450
testValue,
395-
'calc( 0px - (100vw - 10px) * 2px )',
396-
'calc((-100vw - -10px)*2px)',
451+
'calc( 0px - (100vw - 10px) * 2 )',
452+
'calc(20px - 200vw)',
397453
);
398454

399455
test(
400456
'should reduce substracted expression from zero (4)',
401457
testValue,
402458
'calc( 0px - (100vw + 10px))',
403-
'calc(-100vw + -10px)',
459+
'calc(-10px - 100vw)',
404460
);
405461

406462
test(
@@ -587,14 +643,14 @@ test(
587643
'should handle complex calculations (reduce-css-calc#45) (1)',
588644
testValue,
589645
'calc(100% + (2 * 100px) - ((75.37% - 63.5px) - 900px))',
590-
'calc(100% + 200px - 75.37% + 963.5px)',
646+
'calc(24.63% + 1163.5px)',
591647
);
592648

593649
test(
594650
'should handle complex calculations (reduce-css-calc#45) (2)',
595651
testValue,
596652
'calc(((((100% + (2 * 30px) + 63.5px) / 0.7537) - (100vw - 60px)) / 2) + 30px)',
597-
'calc(((100% + 123.5px)/0.7537 - 100vw + 60px)/2 + 30px)',
653+
'calc(66.33939% + 141.92915px - 50vw)',
598654
);
599655

600656
test(
@@ -783,7 +839,7 @@ test(
783839
'should handle nested calc function (#13)',
784840
testValue,
785841
'calc(100vh - 5rem - calc(10rem + 100px))',
786-
'calc(100vh - 5rem - 10rem - 100px)',
842+
'calc(100vh - 15rem - 100px)',
787843
);
788844

789845
test(

0 commit comments

Comments
 (0)