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.4 KiB

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