2 lines
5.5 KiB
JavaScript
2 lines
5.5 KiB
JavaScript
|
"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 f;function c(t){return!Number.isNaN(t)&&Number.isFinite(t)}function l(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=l(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)?(d(t),t.value=u[0].value):n=!1})),!n)return t;const a=t.filter((t=>"word"===t.type||s.includes(t.value)));let v=f.Number;const m=[];let h;const N=(t,e,r)=>{if(v===e){if(e===f.Number){const e=r||"";m.includes(e)||m.push({number:t,unit:e,index:i.length})}i.push(t),v=e===f.Number?f.Operation:f.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,f.Operation);continue}if("pi"===u.value){N(Math.PI.toString(),f.Number);continue}if("e"===u.value){N(Math.E.toString(),f.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,f.Operation)}else i.unit?"rad"!==i.unit&&"function"!=typeof o[i.unit]?n=!1:N(i.number,f.Number,i.unit):N(u.value,f.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 d(t){delete t.nodes;const e=t;return e.type="word",e}function v(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=l(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[d(t),i]}!function(t){t[t.Number=0]="Number",t[t.Operation=1]="Operation"}(f||(f={}));const N=[{check:"asin(",transform:function(t){const e=r.default(t.value);return e.walk((t=>{if("function"!==t.type||"asin"!==t.value.toLowerCase())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=`${v(i(u),2)}deg`),n.value=u+""}),!0),e.toString()}},{check:"acos(",transform:function(t){const e=r.default(t.value);return e.walk((t=>{if("function"!==t.type||"acos"!==t.value.toLowerCase())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=`${v(i(u),2)}deg`),n.value=u+""}),!0),e.toString()}},{check:"atan(",transform:function(t){const e=r.default(t.value);return e.walk((t=>{if("function"!==t.type||"atan"!==t.value.toLowerCase())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=`${v(i(u),2)}deg`),n.value=u+""}),!0),e.toString()}},{check:"atan2(",transform:function(t){const e=r.default(t.value);return e.walk((t=>{if("function"!==t.type||"atan2"!==t.value.toLowerCase())return;const e=t.nodes.findIn
|