You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 lines
5.3 KiB

2 years ago
import t from"postcss-value-parser";import n from"vm";function e(t){return t*(180/Math.PI)}const r={turn:function(t){return 2*t*Math.PI},deg:function(t){return t*(Math.PI/180)},grad:function(t){return t*(Math.PI/200)}};function u(t){return"word"===t.type}const i=["+","-","*","/"];var o;function a(t){return!Number.isNaN(t)&&Number.isFinite(t)}function s(e,u=!1){let l=!0;const f=[];if(e.filter((t=>"function"===t.type)).forEach((t=>{var n;if(!l)return;if(""!==t.value)return void(l=!1);const e=s(t.nodes.slice(0),u),r=1===e.length,i=Number((null==(n=e[0])?void 0:n.value)||"");r&&"word"===e[0].type&&!Number.isNaN(i)?(c(t),t.value=e[0].value):l=!1})),!l)return e;const v=e.filter((t=>"word"===t.type||i.includes(t.value)));let m=o.Number;const h=[];let N;const b=(t,n,e)=>{if(m===n){if(n===o.Number){const n=e||"";h.includes(n)||h.push({number:t,unit:n,index:f.length})}f.push(t),m=n===o.Number?o.Operation:o.Number}else l=!1};for(let n=0,e=v.length;n<e&&l;n++){const e=v[n];if(i.includes(e.value)){b(e.value,o.Operation);continue}if("pi"===e.value){b(Math.PI.toString(),o.Number);continue}if("e"===e.value){b(Math.E.toString(),o.Number);continue}const a=t.unit(e.value);if(!a){l=!1;break}if(u){if(N||(N=a.unit),N!==a.unit){l=!1;break}b(e.value,o.Operation)}else a.unit?"rad"!==a.unit&&"function"!=typeof r[a.unit]?l=!1:b(a.number,o.Number,a.unit):b(e.value,o.Number)}if(!l)return e;if(f.length%2==0||f.length<3)return e;let d;try{let t="";const e=new Set(h.map((t=>t.unit)));if(e.size>1)if(e.has("")){if(2!==e.size)throw new Error;[t]=Array.from(e).filter((t=>""!==t))}else h.forEach((t=>{if("rad"!==t.unit){const n=r[t.unit](Number(t.number));if(!a(n))throw new Error;f[t.index]=n.toString()}}));const u=n.createContext({result:NaN});new n.Script(`result = ${f.join(" ")}`).runInContext(u),"number"==typeof u.result&&a(u.result)&&(t&&(u.result=r[t](u.result)),a(u.result)&&(d=u.result))}catch(t){}if(void 0!==d){let t=d.toString();N&&(t+=N);const n=e[0].sourceIndex,r=t.length;e.length=0,e.push({type:"word",value:t,sourceIndex:n,sourceEndIndex:r})}return e}function c(t){delete t.nodes;const n=t;return n.type="word",n}function l(t,n){if(!Number.isNaN(t)){if(t>Number.MAX_SAFE_INTEGER)return"infinity";if(t<Number.MIN_SAFE_INTEGER)return"-infinity"}return Number(t.toFixed(n)).toString()}function f(n){let e,r="";if("infinity"===n.toLowerCase()?e=1/0:"-infinity"===n.toLowerCase()?e=-1/0:"pi"===n?e=Math.PI:"e"===n&&(e=Math.E),!e){const u=t.unit(n);if(!u)return!1;e=Number(u.number),Number.isNaN(e)||(r=u.unit)}return{number:e,unit:r}}function v(t,n=!0){t.nodes=s(t.nodes);const e=t.nodes.filter(u);if(1!==t.nodes.length||1!==e.length)return;const{value:i}=e[0],o=f(i);if(!o)return;let a=o.number;if(n){if(o.unit&&"rad"!==o.unit){if(!r[o.unit])return;a=r[o.unit](a)}}else if(o.unit)return;return[c(t),a]}!function(t){t[t.Number=0]="Number",t[t.Operation=1]="Operation"}(o||(o={}));const m=[{check:"asin(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"asin"!==t.value)return;const n=v(t,!1);if(!n)return;const[r,u]=n;let i=Math.asin(u);Number.isNaN(i)||"number"!=typeof i||(i=`${l(e(i),2)}deg`),r.value=i+""})),r.toString()}},{check:"acos(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"acos"!==t.value)return;const n=v(t,!1);if(!n)return;const[r,u]=n;let i=Math.acos(u);Number.isNaN(i)||"number"!=typeof i||(i=`${l(e(i),2)}deg`),r.value=i+""})),r.toString()}},{check:"atan(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"atan"!==t.value)return;const n=v(t,!1);if(!n)return;const[r,u]=n;let i=Math.atan(u);Number.isNaN(i)||"number"!=typeof i||(i=`${l(e(i),2)}deg`),r.value=i+""})),r.toString()}},{check:"atan2(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"atan2"!==t.value)return;const n=t.nodes.findIndex((t=>"div"===t.type&&","===t.value));if(n<0)return;let r=t.nodes.slice(0,n).filter(u),i=t.nodes.slice(n+1).filter(u);if(0===r.length||0===i.length)return;if(r.length>1&&(r=s(r,!0)),i.length>1&&(i=s(i,!0)),1!==r.length||1!==i.leng