| [9af201e] | 1 | "use strict";
|
|---|
| 2 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 3 | value: true
|
|---|
| 4 | });
|
|---|
| 5 | function _export(target, all) {
|
|---|
| 6 | for(var name in all)Object.defineProperty(target, name, {
|
|---|
| 7 | enumerable: true,
|
|---|
| 8 | get: all[name]
|
|---|
| 9 | });
|
|---|
| 10 | }
|
|---|
| 11 | _export(exports, {
|
|---|
| 12 | hasMathFn: function() {
|
|---|
| 13 | return hasMathFn;
|
|---|
| 14 | },
|
|---|
| 15 | addWhitespaceAroundMathOperators: function() {
|
|---|
| 16 | return addWhitespaceAroundMathOperators;
|
|---|
| 17 | }
|
|---|
| 18 | });
|
|---|
| 19 | const LOWER_A = 0x61;
|
|---|
| 20 | const LOWER_Z = 0x7a;
|
|---|
| 21 | const UPPER_A = 0x41;
|
|---|
| 22 | const UPPER_Z = 0x5a;
|
|---|
| 23 | const LOWER_E = 0x65;
|
|---|
| 24 | const UPPER_E = 0x45;
|
|---|
| 25 | const ZERO = 0x30;
|
|---|
| 26 | const NINE = 0x39;
|
|---|
| 27 | const ADD = 0x2b;
|
|---|
| 28 | const SUB = 0x2d;
|
|---|
| 29 | const MUL = 0x2a;
|
|---|
| 30 | const DIV = 0x2f;
|
|---|
| 31 | const OPEN_PAREN = 0x28;
|
|---|
| 32 | const CLOSE_PAREN = 0x29;
|
|---|
| 33 | const COMMA = 0x2c;
|
|---|
| 34 | const SPACE = 0x20;
|
|---|
| 35 | const PERCENT = 0x25;
|
|---|
| 36 | const MATH_FUNCTIONS = [
|
|---|
| 37 | "calc",
|
|---|
| 38 | "min",
|
|---|
| 39 | "max",
|
|---|
| 40 | "clamp",
|
|---|
| 41 | "mod",
|
|---|
| 42 | "rem",
|
|---|
| 43 | "sin",
|
|---|
| 44 | "cos",
|
|---|
| 45 | "tan",
|
|---|
| 46 | "asin",
|
|---|
| 47 | "acos",
|
|---|
| 48 | "atan",
|
|---|
| 49 | "atan2",
|
|---|
| 50 | "pow",
|
|---|
| 51 | "sqrt",
|
|---|
| 52 | "hypot",
|
|---|
| 53 | "log",
|
|---|
| 54 | "exp",
|
|---|
| 55 | "round"
|
|---|
| 56 | ];
|
|---|
| 57 | function hasMathFn(input) {
|
|---|
| 58 | return input.indexOf("(") !== -1 && MATH_FUNCTIONS.some((fn)=>input.includes(`${fn}(`));
|
|---|
| 59 | }
|
|---|
| 60 | function addWhitespaceAroundMathOperators(input) {
|
|---|
| 61 | // Bail early if there are no math functions in the input
|
|---|
| 62 | if (!MATH_FUNCTIONS.some((fn)=>input.includes(fn))) {
|
|---|
| 63 | return input;
|
|---|
| 64 | }
|
|---|
| 65 | let result = "";
|
|---|
| 66 | let formattable = [];
|
|---|
| 67 | let valuePos = null;
|
|---|
| 68 | let lastValuePos = null;
|
|---|
| 69 | for(let i = 0; i < input.length; i++){
|
|---|
| 70 | let char = input.charCodeAt(i);
|
|---|
| 71 | // Track if we see a number followed by a unit, then we know for sure that
|
|---|
| 72 | // this is not a function call.
|
|---|
| 73 | if (char >= ZERO && char <= NINE) {
|
|---|
| 74 | valuePos = i;
|
|---|
| 75 | } else if (valuePos !== null && (char === PERCENT || char >= LOWER_A && char <= LOWER_Z || char >= UPPER_A && char <= UPPER_Z)) {
|
|---|
| 76 | valuePos = i;
|
|---|
| 77 | } else {
|
|---|
| 78 | lastValuePos = valuePos;
|
|---|
| 79 | valuePos = null;
|
|---|
| 80 | }
|
|---|
| 81 | // Determine if we're inside a math function
|
|---|
| 82 | if (char === OPEN_PAREN) {
|
|---|
| 83 | result += input[i];
|
|---|
| 84 | // Scan backwards to determine the function name. This assumes math
|
|---|
| 85 | // functions are named with lowercase alphanumeric characters.
|
|---|
| 86 | let start = i;
|
|---|
| 87 | for(let j = i - 1; j >= 0; j--){
|
|---|
| 88 | let inner = input.charCodeAt(j);
|
|---|
| 89 | if (inner >= ZERO && inner <= NINE) {
|
|---|
| 90 | start = j // 0-9
|
|---|
| 91 | ;
|
|---|
| 92 | } else if (inner >= LOWER_A && inner <= LOWER_Z) {
|
|---|
| 93 | start = j // a-z
|
|---|
| 94 | ;
|
|---|
| 95 | } else {
|
|---|
| 96 | break;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | let fn = input.slice(start, i);
|
|---|
| 100 | // This is a known math function so start formatting
|
|---|
| 101 | if (MATH_FUNCTIONS.includes(fn)) {
|
|---|
| 102 | formattable.unshift(true);
|
|---|
| 103 | continue;
|
|---|
| 104 | } else if (formattable[0] && fn === "") {
|
|---|
| 105 | formattable.unshift(true);
|
|---|
| 106 | continue;
|
|---|
| 107 | }
|
|---|
| 108 | // This is not a known math function so don't format it
|
|---|
| 109 | formattable.unshift(false);
|
|---|
| 110 | continue;
|
|---|
| 111 | } else if (char === CLOSE_PAREN) {
|
|---|
| 112 | result += input[i];
|
|---|
| 113 | formattable.shift();
|
|---|
| 114 | } else if (char === COMMA && formattable[0]) {
|
|---|
| 115 | result += `, `;
|
|---|
| 116 | continue;
|
|---|
| 117 | } else if (char === SPACE && formattable[0] && result.charCodeAt(result.length - 1) === SPACE) {
|
|---|
| 118 | continue;
|
|---|
| 119 | } else if ((char === ADD || char === MUL || char === DIV || char === SUB) && formattable[0]) {
|
|---|
| 120 | let trimmed = result.trimEnd();
|
|---|
| 121 | let prev = trimmed.charCodeAt(trimmed.length - 1);
|
|---|
| 122 | let prevPrev = trimmed.charCodeAt(trimmed.length - 2);
|
|---|
| 123 | let next = input.charCodeAt(i + 1);
|
|---|
| 124 | // Do not add spaces for scientific notation, e.g.: `-3.4e-2`
|
|---|
| 125 | if ((prev === LOWER_E || prev === UPPER_E) && prevPrev >= ZERO && prevPrev <= NINE) {
|
|---|
| 126 | result += input[i];
|
|---|
| 127 | continue;
|
|---|
| 128 | } else if (prev === ADD || prev === MUL || prev === DIV || prev === SUB) {
|
|---|
| 129 | result += input[i];
|
|---|
| 130 | continue;
|
|---|
| 131 | } else if (prev === OPEN_PAREN || prev === COMMA) {
|
|---|
| 132 | result += input[i];
|
|---|
| 133 | continue;
|
|---|
| 134 | } else if (input.charCodeAt(i - 1) === SPACE) {
|
|---|
| 135 | result += `${input[i]} `;
|
|---|
| 136 | } else if (// Previous is a digit
|
|---|
| 137 | prev >= ZERO && prev <= NINE || // Next is a digit
|
|---|
| 138 | next >= ZERO && next <= NINE || // Previous is end of a function call (or parenthesized expression)
|
|---|
| 139 | prev === CLOSE_PAREN || // Next is start of a parenthesized expression
|
|---|
| 140 | next === OPEN_PAREN || // Next is an operator
|
|---|
| 141 | next === ADD || next === MUL || next === DIV || next === SUB || // Previous position was a value (+ unit)
|
|---|
| 142 | lastValuePos !== null && lastValuePos === i - 1) {
|
|---|
| 143 | result += ` ${input[i]} `;
|
|---|
| 144 | } else {
|
|---|
| 145 | result += input[i];
|
|---|
| 146 | }
|
|---|
| 147 | } else {
|
|---|
| 148 | result += input[i];
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 | return result;
|
|---|
| 152 | }
|
|---|