UNPKG

mured

Version:

A rule engine for MIU System(MU puzzle) from GEB

1 lines 3.42 kB
"use strict";function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_unsupportedIterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){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 _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(c):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _iterableToArray(a){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(a))return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a))return _arrayLikeToArray(a)}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}var flat=function(b){return b.reduce(function(a,b){return a.concat(b)},[])},mure={axiom:"MI"},symbols=["M","I","U",""," "],patt=[/III/gi,/UU/gi],M=symbols[0],I=symbols[1],U=symbols[2],extra=symbols.slice(3),err=function(a){if(a)throw new Error("Invalid MIU String");else throw new Error("Invalid Rule")},matchAll=function(a,b,c){return _toConsumableArray(a).reduce(function(d,e,f){return e===b?Array(c).fill(0).reduce(function(c,d,e){return a[f+e]===b},!0)?d.push({index:f,input:a})&&d:d:d},[])},rules=[function(a){return a.endsWith(I)},function(a){return a[0]===M},function(a){return null!==a.match(patt[0])},function(a){return null!==a.match(patt[1])}],apply=[function(a){return a+=U},function(a){return a+=a.slice(1)},function(a){return matchAll(a,I,3).map(function(a){var b=!!(4<arguments.length&&arguments[4]!==void 0)&&arguments[4];return Array.from(a.input).map(function(c,d){return d>=a.index&&d<=a.index+2?b?"":(b=!0)&&U:c}).join("")})},function(a){return matchAll(a,U,2).map(function(a){return Array.from(a.input).filter(function(b,c){return c<a.index||c>a.index+1}).join("")})}];mure.isValid=function(a){return _toConsumableArray(a).reduce(function(a,b){return symbols.includes(b.toUpperCase())&&a},!0)},mure.canApply=function(a,b){return b-1 in rules?rules[b-1](a.toUpperCase()):err(0)},mure.applyRule=function(a,b){return mure.isValid(a)?mure.canApply(a,b)?apply[b-1](a.toUpperCase()):err(0):err(1)},mure.canApplyWhich=function(b){return rules.reduce(function(c,a,d){return c.concat(mure.canApply(b,d+1)?[d+1]:[])},[])},mure.applyAll=function(a){return flat(mure.canApplyWhich(a).map(function(b){return mure.applyRule(a,b)}))},mure.possibility=function(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:mure.axiom;return Array(a).fill(0).reduce(function(a){return a[1].add(new Set(a[0]=flat(a[0].map(mure.applyAll))))&&a},[Array.isArray(b)?_toConsumableArray(b):[b],new Set().add(new Set("string"==typeof b?[b]:b))])[1]},mure.lazyPossibility=regeneratorRuntime.mark(function a(){var b,c,d,e,f=arguments;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:b=0<f.length&&void 0!==f[0]?f[0]:mure.axiom,c=1<f.length&&void 0!==f[1]?f[1]:1/0,d=Array.isArray(b)?b:[b],e=0;case 4:if(!(e<c)){a.next=9;break}return a.next=7,++e&&new Set(d=flat(d.map(function(a){return mure.applyAll(a)})));case 7:a.next=4;break;case 9:case"end":return a.stop();}},a)}),module.exports=Object.freeze(mure);