UNPKG

@tripetto/block-variable

Version:

Variable block for Tripetto.

3 lines (2 loc) 4.43 kB
/*! Tripetto Variable Block 3.0.3 - Copyright (C) 2023 Tripetto B.V. - All Rights Reserved */ import{tripetto as e,condition as t,ConditionBlock as r,isFilledString as a,castToBoolean as s,isString as i,Slots as o,isNumberFinite as l,DateTime as n,Num as u,isVariable as c,Str as p,mountNamespace as h,unmountNamespace as d,HeadlessBlock as f,assert as v}from"@tripetto/runner";import{calculator as b}from"@tripetto/block-calculator/runner";function V(e,t,r,a){var s,i=arguments.length,o=i<3?t:null===a?a=Object.getOwnPropertyDescriptor(t,r):a;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(e,t,r,a);else for(var l=e.length-1;l>=0;l--)(s=e[l])&&(o=(i<3?s(o):i>3?s(t,r,o):s(t,r))||o);return i>3&&o&&Object.defineProperty(t,r,o),o}let g=class extends r{verify(){const e=this.valueOf();if(e)switch(this.props.mode){case"true":return e.hasValue&&s(e.value);case"false":return e.hasValue&&!s(e.value);case"equal":case"not-equal":const t=a(this.props.value)&&this.variableFor(this.props.value)||void 0;return t&&(e.hasValue?s(e.value):void 0)===(t.hasValue?s(t.value):void 0)||!1;case"defined":return e.hasValue;case"undefined":return!e.hasValue}return!1}};V([t],g.prototype,"verify",null),g=V([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-variable:boolean"})],g);let y=class extends r{getValue(e,t){if(i(t)&&e instanceof o.Date){const r=this.variableFor(t);return r&&r.hasValue?e.toValue(r.value):void 0}return e.toValue(l(t)?t:n.UTC)}verify(){const e=this.valueOf();if(e){const t=this.getValue(e.slot,this.props.value),r=e.hasValue?e.slot.toValue(e.value):void 0;switch(this.props.mode){case"equal":return r===t;case"not-equal":return r!==t;case"before":return l(t)&&l(r)&&r<t;case"after":return l(t)&&l(r)&&r>t;case"between":case"not-between":const a=this.getValue(e.slot,this.props.to);return l(t)&&l(a)&&(l(r)&&r>=u.min(t,a)&&r<=u.max(t,a))===("between"===this.props.mode);case"defined":return e.hasValue;case"undefined":return!e.hasValue}}return!1}};V([t],y.prototype,"verify",null),y=V([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-variable:date"})],y);let m=class extends r{getValue(e,t){if(i(t)&&e instanceof o.Numeric){const r=this.variableFor(t);return r&&r.hasValue?e.toValue(r.value):void 0}return l(t)?t:void 0}verify(){const e=this.valueOf();if(e){const t=this.getValue(e.slot,this.props.value);switch(this.props.mode){case"equal":return(e.hasValue?e.value:void 0)===t;case"not-equal":return(e.hasValue?e.value:void 0)!==t;case"below":return l(t)&&e.hasValue&&e.value<t;case"above":return l(t)&&e.hasValue&&e.value>t;case"between":case"not-between":const r=this.getValue(e.slot,this.props.to);return l(t)&&l(r)&&(e.hasValue&&e.value>=u.min(t,r)&&e.value<=u.max(t,r))===("between"===this.props.mode);case"defined":return e.hasValue;case"undefined":return!e.hasValue}}return!1}};V([t],m.prototype,"verify",null),m=V([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-variable:numeric"})],m);let x=class extends r{getMatchString(){if(c(this.props.match)){const e=this.variableFor(this.props.match);return e&&e.hasValue?e.string:""}return this.parseVariables(this.props.match||"","",!0)}isEqual(){const e=this.valueOf();if(e){const t=this.props.ignoreCase?p.lowercase(this.getMatchString()):this.getMatchString(),r=this.props.ignoreCase?p.lowercase(e.string):e.string;switch(this.props.mode){case"exact":return r===t;case"not-exact":return r!==t;case"contains":return t&&-1!==r.indexOf(t)||!1;case"not-contains":return t&&-1===r.indexOf(t)||!1;case"starts":return t&&0===r.indexOf(t)||!1;case"ends":return t&&r.length>=t.length&&r.lastIndexOf(t)===r.length-t.length||!1;case"defined":return""!==r;case"undefined":return""===r}}return!1}};V([t],x.prototype,"isEqual",null),x=V([e({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-variable:text"})],x),h("@tripetto/block-variable"),d();let w=class extends f{constructor(){super(...arguments),this.variableSlot=v(this.valueOf("variable","static",{modifier:e=>this.props.calculator&&this.props.operations&&e.slot instanceof o.Numeric&&{value:b(this.context,this.props.operations,e,void 0,(e=>this.variableFor(e)),(e=>this.parseVariables(e)))}||void 0}))}do(){this.variableSlot.reset(a(this.props.prefill)?this.parseVariables(this.props.prefill,"",!0):this.props.prefill)}};w=V([e({type:"headless",legacyBlock:!0,identifier:"@tripetto/block-variable"})],w);export{w as Variable};