UNPKG

@yantra-core/sutra

Version:

A JavaScript behavior tree library for easily creating and managing complex behavior patterns in game development.

2 lines 23.1 kB
!function(t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).SUTRA=t()}(function(){return function r(o,i,a){function u(e,t){if(!i[e]){if(!o[e]){var n="function"==typeof require&&require;if(!t&&n)return n(e,!0);if(s)return s(e,!0);throw(t=new Error("Cannot find module '"+e+"'")).code="MODULE_NOT_FOUND",t}n=i[e]={exports:{}},o[e][0].call(n.exports,function(t){return u(o[e][1][t]||t)},n,n.exports,r,o,i,a)}return i[e].exports}for(var s="function"==typeof require&&require,t=0;t<a.length;t++)u(a[t]);return u}({1:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),Object.defineProperty(n,"Sutra",{enumerable:!0,get:function(){return r.default}}),n.createSutra=function(){return new r.default};var r=(n=t("./lib/sutra.js"))&&n.__esModule?n:{default:n}},{"./lib/sutra.js":11}],2:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=function(t,e,n){var r,o=this;r="function"==typeof e?e(n):e;switch(t.op){case"and":return t.conditions.every(function(t){return o.evaluateCondition(t,r,n)});case"or":return t.conditions.some(function(t){return o.evaluateCondition(t,r,n)});case"not":return!this.evaluateCondition(t.conditions,r,n);default:return!1}}},{}],3:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=function(t,e,n){var r,o=3<arguments.length&&void 0!==arguments[3]?arguments[3]:this;r="function"==typeof e?e(n):e;o.conditions;{if("string"==typeof t){e=o.conditions[t];if(e=e||this.conditions[t])return Array.isArray(e)?e.every(function(t){return"function"==typeof t.func?t.func(r,n):o.evaluateDSLCondition(t.original,r,n)}):["and","or","not"].includes(e.op)?o.evaluateCompositeCondition(e,r,n):o.evaluateSingleCondition(e,r,n);throw console.log("Warning: Condition not found: "+t+". About to throw an error.\nPlease define the missing condition in your sutra script."),new Error('Condition "'.concat(t,'" not found'))}if("function"==typeof t)return t(r,n);if(Array.isArray(t))return t.every(function(t){return o.evaluateCondition(t,r,n)})}return!1}},{}],4:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=function(t,e,n){var r,o,i=this,a=this.resolveOperator(t.op);r="function"==typeof e?e(n):e;o=t.gamePropertyPath?this.getNestedValue(n,t.gamePropertyPath):t.gameProperty?n[t.gameProperty]:r[t.property];void 0===o&&(o=0);switch(a){case"lessThan":return o<t.value;case"greaterThan":return o>t.value;case"equals":return o===t.value;case"notEquals":return o!==t.value;case"lessThanOrEqual":return o<=t.value;case"greaterThanOrEqual":return o>=t.value;case"and":return t.conditions.every(function(t){return i.evaluateDSLCondition(t,r,n)});case"or":return t.conditions.some(function(t){return i.evaluateDSLCondition(t,r,n)});case"not":return!this.evaluateDSLCondition(t.condition,r,n);default:return!1}}},{}],5:[function(t,e,n){"use strict";function o(t){return(o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}Object.defineProperty(n,"__esModule",{value:!0}),n.default=function(t,e,n){e="function"==typeof e?e(n):e;if("string"==typeof t){var r=this.conditions[t];if(r){if(["and","or","not"].includes(r.op))return this.evaluateCompositeCondition(r,e,n);if("function"==typeof r)return r(e,n);if("object"===o(r.original))return this.evaluateDSLCondition(r.original,e,n)}}return"function"==typeof t&&t(e,n)}},{}],6:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=h;var r=(n=t("./i18n.js"))&&n.__esModule?n:{default:n};function i(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=n){var r,o,i,a,u=[],s=!0,c=!1;try{if(i=(n=n.call(t)).next,0===e){if(Object(n)!==n)return;s=!1}else for(;!(s=(r=i.call(n)).done)&&(u.push(r.value),u.length!==e);s=!0);}catch(t){c=!0,o=t}finally{try{if(!s&&null!=n.return&&(a=n.return(),Object(a)!==a))return}finally{if(c)throw o}}return u}}(t,e)||function(t,e){var n;if(t)return"string"==typeof t?o(t,e):"Map"===(n="Object"===(n=Object.prototype.toString.call(t).slice(8,-1))&&t.constructor?t.constructor.name:n)||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?o(t,e):void 0}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function o(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function a(t){return(a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}var f=function r(t,o){return"object"!==a(t)||null===t?"".concat(o).concat(t):Object.entries(t).map(function(t){var t=i(t,2),e=t[0],t=t[1],n="";return n="function"==typeof t?"".concat(t.name,"()"):"object"===a(t)&&null!==t?"\n".concat(r(t,o+" ")):t,"".concat(o).concat(e,": ").concat(n)}).join("\n")};function h(){function u(t,n){var e,r,o=" ".repeat(2*n),i=(" ".repeat(2*n)," ".repeat(2*n+2)," ".repeat(2*(n+2))),a="";return t.action?"".concat(o).concat(t.action):(t.if&&(e=Array.isArray(t.if)?t.if.join(" ".concat(l.andKeyword," ")):t.if,e="".concat(o).concat(l.ifKeyword," ").concat(e),t.then&&(r=t.then.map(function(t){var e=u(t,n+1);return t.data&&(e+="\n".concat(f(t.data,i))),e}).join("\n"),e+="\n".concat(r)),t.else&&(r=t.else.map(function(t){var e=u(t,n+1);return t.data&&(e+="\n".concat(f(t.data,i))),e}).join("\n"),e+="\n".concat(o).concat(l.elseKeyword,"\n").concat(r)),a+=e),t.subtree&&(r=s.subtrees[t.subtree])&&(a+="".concat(o," @").concat(t.subtree,"=>\n").concat(h.call(r,n+1,c))),0===n&&(a+="\n"),a)}var s=this,e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:0,c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"en",l=r.default[c]||r.default.en;return this.tree.map(function(t){return u(t,e)}).join("\n").concat("")+"\n"}},{"./i18n.js":7}],7:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=void 0;n.default={en:{ifKeyword:"if",thenKeyword:"then",elseKeyword:"else",andKeyword:"and",subtreeKeyword:"subtree"},zh:{ifKeyword:"如果",thenKeyword:"那么",elseKeyword:"否则",andKeyword:"和",subtreeKeyword:"子树"},ja:{ifKeyword:"もし",thenKeyword:"ならば",elseKeyword:"それ以外",andKeyword:"と",subtreeKeyword:"サブツリー"}}},{}],8:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=void 0;n.default={eq:"equals","==":"equals",neq:"notEquals","!=":"notEquals",lt:"lessThan","<":"lessThan",lte:"lessThanOrEqual","<=":"lessThanOrEqual",gt:"greaterThan",">":"greaterThan",gte:"greaterThanOrEqual",">=":"greaterThanOrEqual","&&":"and","||":"or","!":"not"}},{}],9:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=function(t){return t.split(".").reduce(function(t,e){e=e.match(/([^\[]+)(\[\d+\])?/);return e&&(t.push(e[1]),e[2])&&t.push(parseInt(e[2].replace(/[\[\]]/g,""))),t},[])}},{}],10:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=function(){var t,e={tree:this.tree.map(function e(t){t.parent;t=o(t,r);t.then&&Array.isArray(t.then)&&(t.then=t.then.map(function(t){return e(t)}));t.else&&Array.isArray(t.else)&&(t.else=t.else.map(function(t){return e(t)}));return t}),conditions:{}};for(t in this.conditions)"function"==typeof this.conditions[t]&&(e.conditions[t]="object"===i(this.conditions[t].original)?this.conditions[t].original:{type:"customFunction"});return JSON.stringify(e,null,2)};var r=["parent"];function o(t,e){if(null==t)return{};var n,r=function(t,e){if(null==t)return{};var n,r,o={},i=Object.keys(t);for(r=0;r<i.length;r++)n=i[r],0<=e.indexOf(n)||(o[n]=t[n]);return o}(t,e);if(Object.getOwnPropertySymbols)for(var o=Object.getOwnPropertySymbols(t),i=0;i<o.length;i++)n=o[i],0<=e.indexOf(n)||Object.prototype.propertyIsEnumerable.call(t,n)&&(r[n]=t[n]);return r}function i(t){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}},{}],11:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=void 0;var o=r(t("./exportToEnglish.js")),i=r(t("./serializeToJson.js")),a=r(t("./evaluateCondition.js")),u=r(t("./evaluateSingleCondition.js")),s=r(t("./evaluateDSLCondition.js")),c=r(t("./evaluateCompositeCondition.js")),l=r(t("./parsePath.js")),f=r(t("./operatorAliases.js"));function r(t){return t&&t.__esModule?t:{default:t}}function h(t){return(h="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function d(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=n){var r,o,i,a,u=[],s=!0,c=!1;try{if(i=(n=n.call(t)).next,0===e){if(Object(n)!==n)return;s=!1}else for(;!(s=(r=i.call(n)).done)&&(u.push(r.value),u.length!==e);s=!0);}catch(t){c=!0,o=t}finally{try{if(!s&&null!=n.return&&(a=n.return(),Object(a)!==a))return}finally{if(c)throw o}}return u}}(t,e)||p(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function y(t){return function(t){if(Array.isArray(t))return v(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||p(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function p(t,e){var n;if(t)return"string"==typeof t?v(t,e):"Map"===(n="Object"===(n=Object.prototype.toString.call(t).slice(8,-1))&&t.constructor?t.constructor.name:n)||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?v(t,e):void 0}function v(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function b(e,t){var n,r=Object.keys(e);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(e),t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)),r}function g(r){for(var t=1;t<arguments.length;t++){var o=null!=arguments[t]?arguments[t]:{};t%2?b(Object(o),!0).forEach(function(t){var e,n;e=r,n=o[t=t],(t=j(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n}):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(o)):b(Object(o)).forEach(function(t){Object.defineProperty(r,t,Object.getOwnPropertyDescriptor(o,t))})}return r}function m(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,j(r.key),r)}}function j(t){t=function(t,e){if("object"!==h(t)||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0===n)return("string"===e?String:Number)(t);n=n.call(t,e||"default");if("object"!==h(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}(t,"string");return"symbol"===h(t)?t:String(t)}t=function(){function t(){if(!(this instanceof t))throw new TypeError("Cannot call a class as a function");this.tree=[],this.conditions={},this.listeners={},this.maps={},this.operators=["equals","notEquals","greaterThan","lessThan","greaterThanOrEqual","lessThanOrEqual","and","or","not"],this.operatorAliases=f.default,this.exportToEnglish=o.default,this.serializeToJson=i.default,this.toJSON=i.default,this.toEnglish=o.default,this.evaluateCondition=a.default,this.evaluateSingleCondition=u.default,this.evaluateDSLCondition=s.default,this.evaluateCompositeCondition=c.default,this.parsePath=l.default,this.nodeIdCounter=0}var e,n,r;return e=t,(n=[{key:"use",value:function(t,n){var r=this,e=(2<arguments.length&&void 0!==arguments[2]||this.tree.length,!(3<arguments.length&&void 0!==arguments[3])||arguments[3]);this.subtrees=this.subtrees||{},t.isSubtree=!0,(t.parent=this).subtrees[n]=t,e&&(this.sharedListeners=!0,this.listeners=g(g({},this.listeners),t.listeners),this.anyListeners=[].concat(y(this.anyListeners||[]),y(t.anyListeners||[])),t.listeners=this.listeners,t.anyListeners=this.anyListeners),t.conditions&&Object.entries(t.conditions).forEach(function(t){var t=d(t,2),e=t[0],t=t[1];r.conditions[e]&&console.warn("Condition '".concat(e,"' from subtree '").concat(n,"' will overwrite an existing condition in the main Sutra.")),r.addCondition(e,t)}),t.conditions=g(g({},this.conditions),t.conditions)}},{key:"on",value:function(t,e){this.listeners[t]||(this.listeners[t]=[]),this.listeners[t].push(e)}},{key:"emit",value:function(t){for(var e,n=arguments.length,r=new Array(1<n?n-1:0),o=1;o<n;o++)r[o-1]=arguments[o];this.emitLocal.apply(this,[t].concat(r)),this.isSubtree&&this.sharedListeners&&this.parent&&(e=this.parent).emitShared.apply(e,[t].concat(r))}},{key:"emitLocal",value:function(e){for(var t=arguments.length,n=new Array(1<t?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];this.listeners[e]&&this.listeners[e].forEach(function(t){return t.apply(void 0,n)}),this.anyListeners&&this.anyListeners.forEach(function(t){return t.apply(void 0,[e].concat(n))})}},{key:"emitShared",value:function(e){for(var t=arguments.length,n=new Array(1<t?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];this.listeners[e]&&this.listeners[e].forEach(function(t){return t.apply(void 0,n)}),this.anyListeners&&this.anyListeners.forEach(function(t){return t.apply(void 0,[e].concat(n))}),Object.values(this.subtrees).forEach(function(t){t.listeners[e]&&t.listeners[e].forEach(function(t){return t.apply(void 0,n)}),t.anyListeners&&t.anyListeners.forEach(function(t){return t.apply(void 0,[e].concat(n))})})}},{key:"onAny",value:function(t){this.anyListeners=this.anyListeners||[],this.anyListeners.push(t)}},{key:"if",value:function(){var t=Array.from(arguments),e=0<this.tree.length?this.tree[this.tree.length-1]:null;return e&&!e.then?(Array.isArray(e.if)||(e.if=[e.if]),e.if=e.if.concat(t)):(e={if:1<t.length?t:t[0]},this.addAction(e)),this}},{key:"then",value:function(t){var n,e,r,o=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,i=this.tree[this.tree.length-1];return i.then||(i.then=[]),"function"==typeof t?t(n={if:function(t){t={if:t,then:[]};return t.sutraPath="".concat(i.sutraPath,".then[").concat(i.then.length,"]"),i.then.push(t),n},then:function(t){var e=i.then[i.then.length-1],t=(e.then||(e.then=[]),{action:t});return t.sutraPath="".concat(e.sutraPath,".then[").concat(e.then.length,"]"),e.then.push(t),n},else:function(t){var e=i.then[i.then.length-1],t=(e.else||(e.else=[]),{action:t});return t.sutraPath="".concat(e.sutraPath,".else[").concat(e.else.length,"]"),e.else.push(t),n},map:function(t){return i.then.push({map:t}),n}}):(this.subtrees&&(e=this.subtrees[t]),r={},o&&(r.data=o),e?(i.subtree=t,delete i.then):(r.action=t,r.sutraPath="".concat(i.sutraPath,".then[").concat(i.then.length,"]"),i.then.push(r))),this}},{key:"else",value:function(t){var n,r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,o=this.tree[this.tree.length-1];return o.else||(o.else=[]),"function"==typeof t?t(n={if:function(t){return o.else.push({if:t,then:[],else:[]}),n},then:function(t){var e=o.else[o.else.length-1];return e.then||(e.then=[]),e.then.push({action:t,data:r}),n},else:function(t){var e=o.else[o.else.length-1];return e.else||(e.else=[]),e.else.push({action:t,data:r}),n}}):o.else.push({action:t,data:r}),this}},{key:"addMap",value:function(t,e){e={map:t,func:e};this.maps[t]=e,this.generateSutraPath(e,"tree",this.tree.length-1,null)}},{key:"executeMap",value:function(t,e,n){if("function"==typeof t.func){t=t.func(e,n);if(void 0!==t)return t}return e}},{key:"map",value:function(t){var e=this.tree[this.tree.length-1];return(e=e||{then:[]}).then||(e.then=[]),e.then.push({map:t}),this}},{key:"addAction",value:function(t){this.tree.push(t),this.generateSutraPath(t,"tree",this.tree.length-1,null)}},{key:"addCondition",value:function(t,e){var r=this;this.originalConditions=this.originalConditions||{},Array.isArray(e)?this.conditions[t]=e.map(function(n){return"function"==typeof n?(r.originalConditions[t]=r.originalConditions[t]||[],r.originalConditions[t].push({type:"function",func:n}),{func:function(t,e){return n(t,e)},original:null}):(r.originalConditions[t]=r.originalConditions[t]||[],r.originalConditions[t].push(n),{func:function(t,e){return r.evaluateDSLCondition(n,t,e)},original:n})}):this.storeSingleCondition(t,e)}},{key:"removeCondition",value:function(t){return!!this.conditions[t]&&(delete this.conditions[t],this.originalConditions&&this.originalConditions[t]&&delete this.originalConditions[t],!0)}},{key:"updateCondition",value:function(e,n){var t,r=this;if(!this.conditions[e])return!1;if("function"==typeof n)this.conditions[e]=n;else{if("object"!==h(n))return!1;Array.isArray(n)?n.forEach(function(n){var t;"and"===n.op||"or"===n.op||"not"===n.op?((t=function(t,e){return r.evaluateDSLCondition(n,t,e)}).original=n,r.conditions[e]=t):((t=function(t,e){return r.evaluateDSLCondition(n,t,e)}).original=n,r.conditions[e]=t)}):"and"===n.op||"or"===n.op||"not"===n.op?((t=function(t,e){return r.evaluateDSLCondition(n,t,e)}).original=n,this.conditions[e]=t):((t=function(t,e){return r.evaluateDSLCondition(n,t,e)}).original=n,this.conditions[e]=t)}return this.originalConditions[e]=n,!0}},{key:"storeSingleCondition",value:function(t,r){var e,n=this;"function"==typeof r&&r.original||(this.originalConditions=this.originalConditions||{},this.originalConditions[t]=r),"and"===r.op||"or"===r.op||"not"===r.op?(this.conditions[t]=r,this.originalConditions[t]=r):"function"==typeof r?this.conditions[t]=function(t,e){var n=!1;try{n=r(t,e)}catch(t){}return n}:((e=function(t,e){return n.evaluateDSLCondition(r,t,e)}).original=r,this.conditions[t]=e)}},{key:"resolveOperator",value:function(t){return this.operatorAliases[t]||t}},{key:"setOperatorAlias",value:function(t,e){this.operatorAliases[t]=e}},{key:"getConditionFunction",value:function(t){return this.conditions[t]}},{key:"getCondition",value:function(t){return this.originalConditions?this.originalConditions[t]:void 0}},{key:"getOperators",value:function(){return Object.keys(this.operatorAliases)}},{key:"traverseNode",value:function(t,e,n){var r=3<arguments.length&&void 0!==arguments[3]?arguments[3]:null;if(t.subtree)(o=this.subtrees[t.subtree])?t.if&&!this.evaluateCondition(t.if,e,n,o)||o.tick(e,n):console.warn("Subtree '".concat(t.subtree,"' not found."));else{var o=r||e;if(t.map){e=this.maps[t.map];if(!e)throw new Error('Map "'.concat(t.map,'" not found'));e=this.executeMap(e,o,n);void 0!==e&&(r=e)}t.action?this.executeAction(t.action,r||o,t,n):!t.if||this.evaluateCondition(t.if,r||o,n)?this.processBranch(t.then,r||o,n,r):this.processBranch(t.else,r||o,n,r)}}},{key:"processBranch",value:function(t,e,n){var r=this,o=3<arguments.length&&void 0!==arguments[3]?arguments[3]:null;Array.isArray(t)&&t.forEach(function(t){return r.traverseNode(t,e,n,o)})}},{key:"executeAction",value:function(t,e,n,r){var o={},i=(n.data||(n.data={}),e);Object.entries(i).forEach(function(t){var t=d(t,2),e=t[0],t=t[1];o[e]="function"==typeof t?t(i,r,n):t}),Object.entries(n.data).forEach(function(t){var t=d(t,2),e=t[0],t=t[1];o[e]="function"==typeof t?t(i,r,n):t}),this.emit(t,o,n,r)}},{key:"updateEntity",value:function(n,t,e){Object.entries(t).forEach(function(t){var t=d(t,2),e=t[0],t=t[1];n[e]="function"==typeof t?t():t})}},{key:"generateSutraPath",value:function(n,t,e,r){var o=this,i=-1===e?t:"".concat(t,"[").concat(e,"]");n.sutraPath=i,n.parent=r,n.then&&Array.isArray(n.then)&&n.then.forEach(function(t,e){return o.generateSutraPath(t,"".concat(i,".then"),e,n)}),n.else&&Array.isArray(n.else)&&n.else.forEach(function(t,e){return o.generateSutraPath(t,"".concat(i,".else"),e,n)})}},{key:"getNestedValue",value:function(t,e){return this.parsePath(e).reduce(function(t,e){return t&&void 0!==t[e]?t[e]:void 0},t)}},{key:"findNode",value:function(t){var e,t=this.parsePath(t),n=this,r=function(t,e){var n,r,o,i,a="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(a)return r=!(n=!0),{s:function(){a=a.call(t)},n:function(){var t=a.next();return n=t.done,t},e:function(t){r=!0,o=t},f:function(){try{n||null==a.return||a.return()}finally{if(r)throw o}}};if(Array.isArray(t)||(a=p(t))||e&&t&&"number"==typeof t.length)return a&&(t=a),i=0,{s:e=function(){},n:function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}},e:function(t){throw t},f:e};throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(t);try{for(r.s();!(e=r.n()).done;){var o=e.value;if(void 0===n[o])return;n=n[o]}}catch(t){r.e(t)}finally{r.f()}return n}},{key:"removeNode",value:function(t){for(var e=this.parsePath(t),n=this,r=0;r<e.length-1;r++){var o=e[r];if(void 0===n[o])return;n=n[o]}var i,t=e[e.length-1];Array.isArray(n)&&"number"==typeof t?n.length>t&&(n.splice(t,1),i=e.slice(0,-1).reduce(function(t,e,n){return 0===n?e:isNaN(e)?"".concat(t,".").concat(e):"".concat(t,"[").concat(e,"]")},""),this.updateSutraPaths(n,t,i)):void 0!==n[t]&&delete n[t]}},{key:"updateSutraPaths",value:function(t,e,n){for(var r=e;r<t.length;r++){var o=n.replace(/\.(\d+)(?=\[|$)/g,"[$1]");this.generateSutraPath(t[r],o,r,t[r].parent)}}},{key:"updateNode",value:function(t,e){t=this.findNode(t);return!!t&&(Object.assign(t,e),!0)}},{key:"tick",value:function(e){var n=this,r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},o=!1;this.tree.forEach(function(t){t.subtree&&(o=!0),n.traverseNode(t,e,r)}),this.subtrees&&!o&&Object.values(this.subtrees).forEach(function(t){t.tick(e,r)})}},{key:"getSubtree",value:function(t){return this.subtrees[t]}},{key:"getReadableSutraPath",value:function(t){t=this.findNode(t);return t?function t(e){var n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"";return e.parent?t(e=e.parent,(e.if?"".concat(e.if):e.action?"".concat(e.action):"unknown")+(n?" and "+n:"")):n}(t):"Invalid path"}}])&&m(e.prototype,n),r&&m(e,r),Object.defineProperty(e,"prototype",{writable:!1}),t}();n.default=t},{"./evaluateCompositeCondition.js":2,"./evaluateCondition.js":3,"./evaluateDSLCondition.js":4,"./evaluateSingleCondition.js":5,"./exportToEnglish.js":6,"./operatorAliases.js":8,"./parsePath.js":9,"./serializeToJson.js":10}]},{},[1])(1)}); //# sourceMappingURL=sutra.min.js.map