@thejaredwilcurt/csslop 0.0.42 → 0.0.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/value/math.js +87 -18
package/package.json
CHANGED
package/src/value/math.js
CHANGED
|
@@ -144,9 +144,58 @@ function rewriteMathFunctionContents (rewriteExpression, value) {
|
|
|
144
144
|
return result + value.slice(index);
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Matches a number written with a decimal point that begins a token of its
|
|
149
|
+
* own, capturing its sign, the digits before its decimal point, and the digits
|
|
150
|
+
* after it. The lookbehind keeps the match from starting partway through an
|
|
151
|
+
* identifier (`--x0.5`) or another number, and the lookahead keeps it from
|
|
152
|
+
* matching a fragment of a dotted name such as `0.5.png`.
|
|
153
|
+
*
|
|
154
|
+
* @type {RegExp}
|
|
155
|
+
*/
|
|
156
|
+
const DECIMAL_NUMBER = /(?<![\w.-])([+-]?)(\d*)\.(\d+)(?![\d.])/g;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Writes a decimal number in the fewest characters that state the same value,
|
|
160
|
+
* dropping the zeros before its whole part (`0.25` → `.25`) and after its
|
|
161
|
+
* fraction (`1.50` → `1.5`), along with a decimal point left with no fraction
|
|
162
|
+
* behind it (`2.0` → `2`). The digits that remain are kept as written, so no
|
|
163
|
+
* precision is lost.
|
|
164
|
+
*
|
|
165
|
+
* @param {string} sign The sign written before the number, empty when there is none.
|
|
166
|
+
* @param {string} wholeDigits The digits written before the decimal point.
|
|
167
|
+
* @param {string} fractionDigits The digits written after the decimal point.
|
|
168
|
+
* @return {string} The number written as compactly as it can be.
|
|
169
|
+
*/
|
|
170
|
+
function shortenDecimalNumber (sign, wholeDigits, fractionDigits) {
|
|
171
|
+
// Leading zeros of the whole part
|
|
172
|
+
const wholePart = wholeDigits.replace(/^0+/, '');
|
|
173
|
+
// Trailing zeros of the fraction
|
|
174
|
+
const fractionPart = fractionDigits.replace(/0+$/, '');
|
|
175
|
+
if (!fractionPart) {
|
|
176
|
+
return sign + (wholePart || '0');
|
|
177
|
+
}
|
|
178
|
+
return sign + wholePart + '.' + fractionPart;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Shortens every decimal number in the contents of a math function. Numbers
|
|
183
|
+
* that follow an operator written without whitespace (`x*0.25s`) are out of
|
|
184
|
+
* reach of the general number shortening, which only looks past a separator.
|
|
185
|
+
*
|
|
186
|
+
* @param {string} expression The contents of a math function.
|
|
187
|
+
* @return {string} The expression with every decimal number written as compactly as it can be.
|
|
188
|
+
*/
|
|
189
|
+
function shortenDecimalNumbers (expression) {
|
|
190
|
+
return expression.replace(DECIMAL_NUMBER, (match, sign, wholeDigits, fractionDigits) => {
|
|
191
|
+
return shortenDecimalNumber(sign, wholeDigits, fractionDigits);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
147
195
|
/**
|
|
148
196
|
* Matches a multiplication by a decimal below one, capturing the digits after
|
|
149
|
-
* its decimal point and the unit written on it
|
|
197
|
+
* its decimal point and the unit written on it (so a multiplier with a unit
|
|
198
|
+
* can be recognized and left alone). The trailing lookahead keeps
|
|
150
199
|
* the match away from numbers that only begin this way, such as the exponent
|
|
151
200
|
* of `.5e3` or a unit name that is still being read.
|
|
152
201
|
*
|
|
@@ -205,28 +254,47 @@ function reciprocalOfDecimalFraction (fractionDigits) {
|
|
|
205
254
|
}
|
|
206
255
|
|
|
207
256
|
/**
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
257
|
+
* Builds the division that scales a value exactly as multiplying it by a
|
|
258
|
+
* unitless decimal below one does, when that division is no longer to write
|
|
259
|
+
* than the multiplication. A multiplier carrying a unit is never rewritten:
|
|
260
|
+
* dividing by a dimension inverts its unit in the type of the result
|
|
261
|
+
* (`x*.25s` is a time, while `x/4s` is per unit of time), so the two would not
|
|
262
|
+
* be equivalent.
|
|
263
|
+
*
|
|
264
|
+
* @param {string} fractionDigits The digits written after the decimal point of the multiplier.
|
|
265
|
+
* @param {string} unit The unit written on the multiplier, empty when it is unitless.
|
|
266
|
+
* @return {string|null} The equivalent division, or null when there is none worth using.
|
|
267
|
+
*/
|
|
268
|
+
function divisionEquivalentToFractionalMultiplier (fractionDigits, unit) {
|
|
269
|
+
if (unit) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
const divisor = reciprocalOfDecimalFraction(fractionDigits);
|
|
273
|
+
if (divisor === null) {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
const division = '/' + divisor;
|
|
277
|
+
// Compared against the shortest the multiplication could have been written
|
|
278
|
+
const multiplication = '*.' + fractionDigits;
|
|
279
|
+
if (division.length > multiplication.length) {
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
return division;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Rewrites a multiplication by a unitless decimal below one as a division by
|
|
287
|
+
* the whole number that decimal is the reciprocal of, which is never longer to
|
|
288
|
+
* write and usually shorter (`calc(var(--x)*.25)` → `calc(var(--x)/4)`).
|
|
289
|
+
* Multipliers with a unit are left as written, since dividing by a dimension
|
|
290
|
+
* would invert its unit.
|
|
213
291
|
*
|
|
214
292
|
* @param {string} expression The contents of a math function.
|
|
215
293
|
* @return {string} The expression with reciprocal multipliers stated as divisions.
|
|
216
294
|
*/
|
|
217
295
|
function convertReciprocalMultiplicationToDivision (expression) {
|
|
218
296
|
return expression.replace(FRACTIONAL_MULTIPLIER, (match, fractionDigits, unit) => {
|
|
219
|
-
|
|
220
|
-
if (divisor === null) {
|
|
221
|
-
return match;
|
|
222
|
-
}
|
|
223
|
-
const division = '/' + divisor + unit;
|
|
224
|
-
// Compared against the shortest the multiplication could have been written
|
|
225
|
-
const multiplication = '*.' + fractionDigits + unit;
|
|
226
|
-
if (division.length > multiplication.length) {
|
|
227
|
-
return match;
|
|
228
|
-
}
|
|
229
|
-
return division;
|
|
297
|
+
return divisionEquivalentToFractionalMultiplier(fractionDigits, unit) ?? match;
|
|
230
298
|
});
|
|
231
299
|
}
|
|
232
300
|
|
|
@@ -596,7 +664,8 @@ const RESOLVABLE_MATH_FUNCTION = new RegExp(
|
|
|
596
664
|
* @return {string} The value with its math functions simplified where possible.
|
|
597
665
|
*/
|
|
598
666
|
function simplifyMathFunctionsOnce (value, property) {
|
|
599
|
-
let result = rewriteMathFunctionContents(
|
|
667
|
+
let result = rewriteMathFunctionContents(shortenDecimalNumbers, value);
|
|
668
|
+
result = rewriteMathFunctionContents(convertReciprocalMultiplicationToDivision, result);
|
|
600
669
|
|
|
601
670
|
// Unwrap calc(1 / (1 / x)) → x (double-reciprocal identity)
|
|
602
671
|
result = result.replace(/calc\(\s*1\s*\/\s*\(\s*1\s*\/\s*([^()]+)\s*\)\s*\)/gi, keepSeparatorAfterDissolvedFunction((match, inner) => {
|