@tripetto/block-calculator
Version:
Calculator block for Tripetto.
3 lines (2 loc) • 10.7 kB
JavaScript
/*! Tripetto Calculator Block 4.0.6 - Copyright (C) 2024 Tripetto B.V. - All Rights Reserved */
import{Num as e,isNumberFinite as a,arraySize as t,isFilledString as r,castToFloat as n,each as s,Str as c,Slots as o,DateTime as u,isString as l,tripetto as i,condition as h,ConditionBlock as v,HeadlessBlock as d,assert as f}from"@tripetto/runner";function b(e,a,t,r){var n,s=arguments.length,c=s<3?a:null===r?r=Object.getOwnPropertyDescriptor(a,t):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)c=Reflect.decorate(e,a,t,r);else for(var o=e.length-1;o>=0;o--)(n=e[o])&&(c=(s<3?n(c):s>3?n(a,t,c):n(a,t))||c);return s>3&&c&&Object.defineProperty(a,t,c),c}function g(e,a){switch(a){case"radians":return e;case"gradians":return e*(Math.PI/200);default:return e*(Math.PI/180)}}function k(e,a){switch(a){case"radians":return e;case"gradians":return e*(200/Math.PI);default:return e*(180/Math.PI)}}function m(e){if(e>0){if(e>=.5){const a=[.9999999999998099,676.5203681218851,-1259.1392167224028,771.3234287776531,-176.6150291621406,12.507343278686905,-.13857109526572012,9984369578019572e-21,1.5056327351493116e-7];let t=a[0];e--;for(let r=1;r<9;r++)t+=a[r]/(e+r);const r=e+7.5;return Math.sqrt(2*Math.PI)*Math.pow(r,e+.5)*Math.exp(-r)*t}{const t=m(1-e);return a(t)?Math.PI/(Math.sin(Math.PI*e)*t):void 0}}}function p(e){if(e>=0){if(0===e)return 1;if(e<1)return m(e+1);let t=0;for(let r=1;r<=e;r++){if(!a(t))return;t+=Math.log(r)}return Math.exp(t)}}function M(e,t,r,n){if(l(e)){const t=function(e,a){switch(e){case"π":return Math.PI;case"e":return Math.E;case"γ":return.5772156649015329;case"c":return 299792458;case"random":return Math.random();case"timestamp":return Date.now()/1e3;case"year":return(new Date).getFullYear();case"month":return(new Date).getMonth()+1;case"day":return(new Date).getDate();case"day-of-week":return(new Date).getDay();case"hour":return(new Date).getHours();case"minute":return(new Date).getMinutes();case"second":return(new Date).getSeconds();case"millisecond":return(new Date).getMilliseconds();case"timezone":return 60*(new Date).getTimezoneOffset()*1e3;case"branch":return a.index}}(e,r);if(a(t))return t;const s=n(e);return s&&s.hasValue&&(s.slot instanceof o.Number||s.slot instanceof o.Numeric||s.slot instanceof o.Date)?s.slot.toValue(s.value):void 0}return a(e)?e:t}function w(e,t){return a(e)?t(e):void 0}function x(t,l,i,h,v,d,f){let b=0;switch(i.opcode){case"abs":b=w(M(i.value,h,t,v),(e=>Math.abs(e)));break;case"acos":b=w(M(i.value,h,t,v),(e=>k(Math.acos(e),i.angleUnits)));break;case"acosh":b=w(M(i.value,h,t,v),(e=>Math.acosh(e)));break;case"age":b=(()=>{const n=r(i.value)&&v(i.value);if(n&&n.hasValue&&n.slot instanceof o.Date){const r=M(i.reference,u.UTC,t,v),s=n.slot.toValue(n.value);if(a(r)){const a=n.slot.toValue(r);switch(i.ageIn){case"milliseconds":return a-s;case"seconds":return e.floor((a-s)/1e3);case"minutes":return e.floor((a-s)/6e4);case"hours":return e.floor((a-s)/36e5);case"days":return e.floor((a-s)/864e5);case"months":return function(a,t){const r=new Date(e.min(a,t)),n=new Date(e.max(a,t));return(12*(n.getFullYear()-r.getFullYear())+(n.getMonth()-r.getMonth())-(n.getDate()-r.getDate()<0?1:0))*(a>t?-1:1)}(s,a);default:return function(a,t){const r=new Date(e.min(a,t)),n=new Date(e.max(a,t)),s=n.getMonth()-r.getMonth();let c=n.getFullYear()-r.getFullYear();return(s<0||0===s&&n.getDate()<r.getDate())&&c--,c*(a>t?-1:1)}(s,a)}}}})(),i.ageAbs&&a(b)&&(b=Math.abs(b));break;case"asin":b=w(M(i.value,h,t,v),(e=>k(Math.asin(e),i.angleUnits)));break;case"asinh":b=w(M(i.value,h,t,v),(e=>Math.asinh(e)));break;case"atan":b=w(M(i.value,h,t,v),(e=>k(Math.atan(e),i.angleUnits)));break;case"atanh":b=w(M(i.value,h,t,v),(e=>Math.atanh(e)));break;case"boolean":b=(()=>{const e=r(i.value)&&v(i.value);if(e&&e.slot instanceof o.Boolean)return M(e.hasValue&&!0===e.value?i.outa:i.outb,h,t,v)})();break;case"calc":const l=f&&f(i.id);i.operations&&i.operations.length>0&&(b=h,s(i.operations,(e=>{a(b)&&(b=x(t,e.operator,e,b,v,d,f))}))),l&&l.set(b);break;case"cast":b=(()=>{const e=r(i.value)&&v(i.value);return e&&e.hasValue?n(e.string):0})();break;case"ceil":b=w(M(i.value,h,t,v),(a=>e.ceil(a)));break;case"chars":b=(()=>{const e=r(i.value)&&v(i.value);return e&&e.hasValue?e.string.length:0})();break;case"clamp":b=w(M(i.value,h,t,v),(r=>{const n=M(i.min,h,t,v),s=M(i.max,h,t,v);return r=a(n)?e.max(r,n):r,a(s)?e.min(r,s):r}));break;case"cos":b=w(M(i.value,h,t,v),(e=>Math.cos(g(e,i.angleUnits))));break;case"cosh":b=w(M(i.value,h,t,v),(e=>Math.cosh(e)));break;case"count":b=(()=>{const e=r(i.value)&&v(i.value)||void 0;return e?e.refs.length:0})();break;case"date":case"datetime":b=(()=>{const r=e=>{if(a(e)){const a=new Date(e);return"datetime"===i.opcode?(a.setUTCSeconds(0),a.setUTCMilliseconds(0)):(a.setUTCHours(0),a.setUTCMinutes(0),a.setUTCSeconds(0),a.setUTCMilliseconds(0)),a.getTime()}},n=u.UTC,s=r(M(i.value,n,t,v)),c=r(M(i.cona,n,t,v));let o=!1;switch(i.compareMode){case"equal":o=s===c;break;case"before":o=a(s)&&a(c)&&s<c;break;case"after":o=a(s)&&a(c)&&s>c;break;case"between":const r=M(i.conb,n,t,v);o=a(s)&&a(c)&&a(r)&&s>=e.min(c,r)&&s<=e.max(c,r);break;case"defined":o=a(s)}return M(o?i.outa:i.outb,h,t,v)})();break;case"day-of-month":case"day-of-week":case"hour":case"millisecond":case"minute":case"month":case"second":case"year":b=(()=>{const e=r(i.value)&&v(i.value);if(e&&e.hasValue&&e.slot instanceof o.Date){const a=e.slot.toDate(e.value);switch(i.opcode){case"year":return a.getUTCFullYear();case"month":return a.getUTCMonth()+1;case"day-of-month":return a.getUTCDate();case"day-of-week":return a.getUTCDay();case"hour":return a.getUTCHours();case"minute":return a.getUTCMinutes();case"second":return a.getUTCSeconds();case"millisecond":return a.getUTCMilliseconds()}}})();break;case"equation":b=(()=>{const r=M(i.value,h,t,v),n=M(i.cona,h,t,v);let s=!1;switch(i.compareMode){case"equal":s=r===n;break;case"below":s=a(r)&&a(n)&&r<n;break;case"above":s=a(r)&&a(n)&&r>n;break;case"between":const c=M(i.conb,h,t,v);s=a(r)&&a(n)&&a(c)&&r>=e.min(n,c)&&r<=e.max(n,c);break;case"defined":s=a(r)}return M(s?i.outa:i.outb,h,t,v)})();break;case"evaluate":b=(()=>{let e=!1;const a=r(i.value)&&v(i.value);if(a&&a.hasValue){const t=i.ignoreCase?c.lowercase(a.string):a.string,s=(n=d(r(i.cona)?i.cona:""),i.ignoreCase?c.lowercase(n):n);switch(i.compareMode){case"equal":e=t===s;break;case"contains":e=s&&-1!==t.indexOf(s)||!1;break;case"starts":e=s&&0===t.indexOf(s)||!1;break;case"ends":e=s&&t.length>=s.length&&t.lastIndexOf(s)===t.length-s.length||!1;break;case"defined":e=""!==t}}var n;return M(e?i.outa:i.outb,h,t,v)})();break;case"exp":b=w(M(i.exponent,h,t,v),(e=>Math.exp(e)));break;case"fact":b=w(M(i.value,h,t,v),p);break;case"floor":b=w(M(i.value,h,t,v),(a=>e.floor(a)));break;case"gamma":b=w(M(i.value,h,t,v),m);break;case"lines":b=(()=>{const e=r(i.value)&&v(i.value);return e&&e.hasValue&&e.string&&e.string.split("\n").length||0})();break;case"ln":b=w(M(i.value,h,t,v),(e=>Math.log(e)));break;case"log":b=w(M(i.value,h,t,v),(e=>Math.log10(e)));break;case"max":b=w(M(i.value,h,t,v),(r=>{const n=M(i.max,h,t,v);return a(n)?e.max(r,n):r}));break;case"min":b=w(M(i.value,h,t,v),(r=>{const n=M(i.min,h,t,v);return a(n)?e.min(r,n):r}));break;case"mod":b=w(M(i.value,h,t,v),(e=>{const r=M(i.divisor,h,t,v);return a(r)&&0!==r?e%r:void 0}));break;case"number":b=M(i.value,h,t,v);break;case"occurrences":b=(()=>{const e=r(i.value)&&v(i.value);if(e&&e.hasValue){if("regex"!==i.compareMode){const a=r(i.cona)&&(i.ignoreCase?c.lowercase(i.cona):i.cona)||"",t=i.ignoreCase?c.lowercase(e.string):e.string;return a&&t?t.split(a).length-1:0}try{const a=r(i.cona)&&i.cona||"",t=a.indexOf("/"),n=a.lastIndexOf("/");return 0===t&&n>t&&(e=>{try{return(e.match(new RegExp(a.substring(1,n),a.substr(n+1)))||[]).length}catch(e){return 0}})(e.string)||0}catch(e){return 0}}return 0})();break;case"percentage":b=w(M(i.value,h,t,v),(e=>{const r=M(i.percentage,h,t,v);return a(r)?e/100*r:void 0}));break;case"pow":b=w(M(i.value,h,t,v),(e=>{const r=M(i.exponent,h,t,v);return a(r)?Math.pow(e,r):void 0}));break;case"round":b=w(M(i.value,h,t,v),(a=>e.round(a)));break;case"score":b=(()=>{const e=r(i.value)&&v(i.value)||void 0;if(e&&i.scores){let a=0;return s(e.refs,(e=>{a+=i.scores[e]||0})),a}})();break;case"sgn":b=w(M(i.value,h,t,v),(e=>Math.sign(e)));break;case"sin":b=w(M(i.value,h,t,v),(e=>Math.sin(g(e,i.angleUnits))));break;case"sinh":b=w(M(i.value,h,t,v),(e=>Math.sinh(e)));break;case"sqrt":b=w(M(i.value,h,t,v),(e=>Math.sqrt(e)));break;case"square":b=w(M(i.value,h,t,v),(e=>e*e));break;case"sum":b=(()=>{const e=r(i.value)&&v(i.value)||void 0;return e?n(e.value):void 0})();break;case"tan":b=w(M(i.value,h,t,v),(e=>Math.tan(g(e,i.angleUnits))));break;case"tanh":b=w(M(i.value,h,t,v),(e=>Math.tanh(e)));break;case"trunc":b=w(M(i.value,h,t,v),(e=>Math.trunc(e)));break;case"words":b=(()=>{const e=r(i.value)&&v(i.value);return e&&e.hasValue?e.string.split(" ").filter((e=>""!==e)).length:0})();break;default:return}switch(l){case"+":return a(h)||a(b)?(h||0)+(b||0):void 0;case"-":return a(h)||a(b)?(h||0)-(b||0):void 0;case"*":return a(h)&&a(b)?h*b:void 0;case"/":return a(h)&&a(b)&&0!==b?h/b:void 0;case"=":return b}}function D(e,r,n,s,c,o,u){let l=n&&n.hasValue?n.slot.toValue(n.value):void 0;if(!n||a(l)){const s=t(r);if(s>0){n||(l=0);for(let t=0;t<s;t++){const s=r[t],i=0!==t||n?s.operator:"=";if(l=x(e,i,s,l,c,o,u),!a(l)&&("*"===i||"/"===i))break}}}return s&&s.set(l),l}let V=class extends v{getValue(e,t){if(l(t)&&e instanceof o.Numeric){const a=this.variableFor(t);return a&&a.hasValue?e.toValue(a.value):void 0}return a(t)?t:void 0}verify(){const t=this.valueOf();if(t){const r=this.getValue(t.slot,this.props.value);switch(this.props.mode){case"equal":return(t.hasValue?t.value:void 0)===r;case"not-equal":return(t.hasValue?t.value:void 0)!==r;case"below":return a(r)&&t.hasValue&&t.value<r;case"above":return a(r)&&t.hasValue&&t.value>r;case"between":case"not-between":const n=this.getValue(t.slot,this.props.to);return a(r)&&a(n)&&(t.hasValue&&t.value>=e.min(r,n)&&t.value<=e.max(r,n))===("between"===this.props.mode);case"defined":return t.hasValue;case"undefined":return!t.hasValue}}return!1}};b([h],V.prototype,"verify",null),V=b([i({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-calculator"})],V);let y=class extends d{constructor(){super(...arguments),this.calculatorSlot=f(this.valueOf("calculator"))}do(){D(this.context,this.props.operations||[],void 0,this.calculatorSlot,(e=>this.variableFor(e)),(e=>this.parseVariables(e)),(e=>this.valueOf(e,"dynamic")))}};y=b([i({type:"headless",legacyBlock:!0,identifier:"@tripetto/block-calculator"})],y);export{y as Calculator,D as calculator};