Repository navigation
Expand file tree
/
Copy pathmisc.js
More file actions
65 lines (57 loc) · 1.78 KB
/
Copy pathmisc.js
File metadata and controls
65 lines (57 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import printNumber from "../../utils/print-number.js";
import printString from "../../utils/print-string.js";
import { maybeToLowerCase } from "../utils/index.js";
import CSS_UNITS from "./css-units.evaluate.js";
function printUnit(unit) {
const lowercased = unit.toLowerCase();
return CSS_UNITS.has(lowercased) ? CSS_UNITS.get(lowercased) : unit;
}
const STRING_REGEX = /(["'])(?:(?!\1)[^\\]|\\.)*\1/gsu;
const NUMBER_REGEX = /(?:\d*\.\d+|\d+\.?)(?:e[+-]?\d+)?/giu;
const STANDARD_UNIT_REGEX = /[a-z]+/giu;
const WORD_PART_REGEX = /[$@]?[_a-z\u0080-\uFFFF][\w\u0080-\uFFFF-]*/giu;
const ADJUST_NUMBERS_REGEX = new RegExp(
STRING_REGEX.source +
"|" +
// eslint-disable-next-line regexp/no-misleading-capturing-group
`(${WORD_PART_REGEX.source})?` +
`(${NUMBER_REGEX.source})` +
`(${STANDARD_UNIT_REGEX.source})?`,
"giu",
);
function adjustStrings(value, options) {
return value.replaceAll(STRING_REGEX, (match) => printString(match, options));
}
function quoteAttributeValue(value, options) {
const quote = options.singleQuote ? "'" : '"';
return value.includes('"') || value.includes("'")
? value
: quote + value + quote;
}
function adjustNumbers(value) {
return value.replaceAll(
ADJUST_NUMBERS_REGEX,
(match, quote, wordPart, number, unit) =>
!wordPart && number
? printCssNumber(number) + maybeToLowerCase(unit || "")
: match,
);
}
function printCssNumber(rawNumber) {
return (
printNumber(rawNumber)
// Remove trailing `.0`.
.replace(/\.0(?=$|e)/u, "")
);
}
function shouldPrintTrailingComma(options) {
return options.trailingComma === "es5" || options.trailingComma === "all";
}
export {
adjustNumbers,
adjustStrings,
printCssNumber,
printUnit,
quoteAttributeValue,
shouldPrintTrailingComma,
};