@thejaredwilcurt/csslop 0.0.39 → 0.0.41

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.
@@ -47,8 +47,10 @@ function roundCompactNumber (value, precision = 3) {
47
47
  if (!Number.isFinite(number)) {
48
48
  return String(value);
49
49
  }
50
- // Strip trailing zeros and trailing decimal point from the fixed-precision string
51
- let result = number.toFixed(precision).replace(/0+$/, '').replace(/\.$/, '');
50
+ // Strip the zeros trailing the fraction, then the decimal point they leave
51
+ // behind. Only zeros written after a decimal point are droppable, so the
52
+ // match has to start at one: the zeros of a whole number are digits.
53
+ let result = number.toFixed(precision).replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '');
52
54
  if (result.startsWith('0.')) {
53
55
  result = result.slice(1);
54
56
  }
@@ -58,6 +60,44 @@ function roundCompactNumber (value, precision = 3) {
58
60
  return result;
59
61
  }
60
62
 
63
+ /**
64
+ * The most characters the result of a resolved calculation is written with,
65
+ * counting the digits and the decimal point but not the sign. A calculation is
66
+ * only replaced by its result to save characters, and a result that repeats
67
+ * can always be written out longer, so its digits are cut off where the
68
+ * accuracy they add stops paying for the characters they cost.
69
+ *
70
+ * @type {number}
71
+ */
72
+ const MAXIMUM_RESOLVED_NUMBER_CHARACTERS = 7;
73
+
74
+ /**
75
+ * Formats what a calculation resolved to as the number a stylesheet states it
76
+ * with: rounded to however many decimal places are left over once the whole
77
+ * part and the decimal point have taken their share of the character budget a
78
+ * resolved number is written within.
79
+ *
80
+ * @param {number|string} value The number the calculation resolved to.
81
+ * @param {number} maximumCharacters The most characters the written number may take.
82
+ * @return {string} The number as it is written into the stylesheet.
83
+ */
84
+ function formatResolvedNumber (value, maximumCharacters = MAXIMUM_RESOLVED_NUMBER_CHARACTERS) {
85
+ const number = typeof value === 'number' ? value : parseFloat(value);
86
+ if (!Number.isFinite(number)) {
87
+ return String(value);
88
+ }
89
+ const wholePart = Math.floor(Math.abs(number));
90
+ // A magnitude below one is written without its leading zero, so nothing of
91
+ // its whole part is spent from the budget
92
+ let wholeDigitCount = 0;
93
+ if (wholePart !== 0) {
94
+ wholeDigitCount = String(wholePart).length;
95
+ }
96
+ // The decimal point itself takes one of the characters the digits could have
97
+ const decimalPlaces = Math.max(maximumCharacters - wholeDigitCount - 1, 0);
98
+ return roundCompactNumber(number, decimalPlaces);
99
+ }
100
+
61
101
  /**
62
102
  * Converts an absolute CSS length value (pt, pc, in, cm, mm, q) to its pixel equivalent using standard conversion factors.
63
103
  *
@@ -84,15 +124,16 @@ function convertAbsoluteLengthToPx (value, unit) {
84
124
  }
85
125
 
86
126
  /**
87
- * Formats a numeric value with its CSS unit as a compact string, rounding to the specified precision.
127
+ * Formats what a calculation resolved to as the dimension a stylesheet states
128
+ * it with, rounding the number into the character budget a resolved number is
129
+ * written within.
88
130
  *
89
- * @param {number} value The numeric dimension value.
90
- * @param {string} unit The CSS unit suffix (e.g. "px", "%").
91
- * @param {number} precision The number of decimal places to keep.
92
- * @return {string} The formatted dimension string.
131
+ * @param {number} value The numeric result of the calculation.
132
+ * @param {string} unit The CSS unit suffix the result carries (e.g. "px", "%").
133
+ * @return {string} The dimension as it is written into the stylesheet.
93
134
  */
94
- function formatDimension (value, unit, precision = 3) {
95
- return roundCompactNumber(value, precision) + unit;
135
+ function formatResolvedDimension (value, unit) {
136
+ return formatResolvedNumber(value) + unit;
96
137
  }
97
138
 
98
139
  /**
@@ -197,7 +238,8 @@ export {
197
238
  collapseShorthandParts,
198
239
  convertAbsoluteLengthToPx,
199
240
  formatCompactNumber,
200
- formatDimension,
241
+ formatResolvedDimension,
242
+ formatResolvedNumber,
201
243
  hasSubstitutedParts,
202
244
  normalizeScaleComponent,
203
245
  parseAlphaString,
package/vite.config.js CHANGED
@@ -27,10 +27,6 @@ const config = defineConfig({
27
27
  test: /node_modules\/@codemirror\/lang-css/,
28
28
  name: 'codemirror-lang-css'
29
29
  },
30
- {
31
- test: /node_modules\/@csstools\/css-calc/,
32
- name: 'css-calc'
33
- },
34
30
  {
35
31
  test: /node_modules\/@node-projects\/css-parser/,
36
32
  name: 'css-parser'