UNPKG

mured

Version:

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

1 lines 3.84 kB
"use strict";function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(a){if(Symbol.iterator in Object(a)||"[object Arguments]"===Object.prototype.toString.call(a))return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++)c[b]=a[b];return c}}function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}(function(a){if("object"===("undefined"==typeof exports?"undefined":_typeof(exports))&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,b.mure=a()}})(function(){return function(){function b(d,e,g){function a(j,i){if(!e[j]){if(!d[j]){var f="function"==typeof require&&require;if(!i&&f)return f(j,!0);if(h)return h(j,!0);var c=new Error("Cannot find module '"+j+"'");throw c.code="MODULE_NOT_FOUND",c}var k=e[j]={exports:{}};d[j][0].call(k.exports,function(b){var c=d[j][1][b];return a(c||b)},k,k.exports,b,d,e,g)}return e[j].exports}for(var h="function"==typeof require&&require,c=0;c<g.length;c++)a(g[c]);return a}return b}()({1:[function(a,b){var c=function(b){return b.reduce(function(a,b){return a.concat(b)},[])},d={axiom:"MI"},e=["M","I","U",""," "],f=[/III/gi,/UU/gi],g=e[0],h=e[1],j=e[2],i=e.slice(3),k=function(a){if(a)throw new Error("Invalid MIU String");else throw new Error("Invalid Rule")},l=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},[])},m=[function(a){return a.endsWith(h)},function(a){return a[0]===g},function(a){return null!==a.match(f[0])},function(a){return null!==a.match(f[1])}],n=[function(a){return a+=j},function(a){return a+=a.slice(1)},function(a){return l(a,h,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)&&j:c}).join("")})},function(a){return l(a,j,2).map(function(a){return Array.from(a.input).filter(function(b,c){return c<a.index||c>a.index+1}).join("")})}];d.isValid=function(a){return _toConsumableArray(a).reduce(function(a,b){return e.includes(b.toUpperCase())&&a},!0)},d.canApply=function(a,b){return b-1 in m?m[b-1](a.toUpperCase()):k(0)},d.applyRule=function(a,b){return d.isValid(a)?d.canApply(a,b)?n[b-1](a.toUpperCase()):k(0):k(1)},d.canApplyWhich=function(b){return m.reduce(function(c,a,e){return c.concat(d.canApply(b,e+1)?[e+1]:[])},[])},d.applyAll=function(a){return c(d.canApplyWhich(a).map(function(b){return d.applyRule(a,b)}))},d.possibility=function(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:d.axiom;return Array(a).fill(0).reduce(function(a){return a[1].add(new Set(a[0]=c(a[0].map(d.applyAll))))&&a},[Array.isArray(b)?_toConsumableArray(b):[b],new Set().add(new Set("string"==typeof b?[b]:b))])[1]},d.lazyPossibility=regeneratorRuntime.mark(function a(){var b,e,f,g,h=arguments;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:b=0<h.length&&void 0!==h[0]?h[0]:d.axiom,e=1<h.length&&void 0!==h[1]?h[1]:1/0,f=Array.isArray(b)?b:[b],g=0;case 4:if(!(g<e)){a.next=9;break}return a.next=7,++g&&new Set(f=c(f.map(function(a){return d.applyAll(a)})));case 7:a.next=4;break;case 9:case"end":return a.stop();}},a)}),b.exports=Object.freeze(d)},{}]},{},[1])(1)});