UNPKG

itako-text-transformer-dictionary

Version:
2 lines 11.3 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.ItakoTextTransformerDictionary=t()}(this,function(){"use strict";function e(e,t){return t={exports:{}},e(t,t.exports),t.exports}function t(e){for(var t=["rewrite","replace","toggle","exchange"],r=0;r<t.length;r++){var n=t[r];if(e.hasOwnProperty(n))return n}}function r(e){return"/"===e[0]?new RegExp(e.replace(/(^\/|\/$)/g,""),"gi"):new RegExp(s(e),"gi")}function n(e){return"{"===e[0]?p.parse(e):"string"==typeof e?{value:e}:e}function i(e){if(e instanceof Array)return e.map(function(e){var t=n(e.replacement);return f["extends"]({method:"replace"},e,{replacement:t})});var r=[];for(var i in e)if(e.hasOwnProperty(i)!==!1){var o=e[i],a=void 0,u=void 0,c=void 0;"string"==typeof o?(a="replace",u=n(o)):(a=t(o),u=n(o[a]),c=o.onMatch),r.push({pattern:i,method:a,replacement:u,onMatch:c})}return r}function o(e){var t=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];for(var r in t)if(t.hasOwnProperty(r)!==!1){var n="set"+r[0].toUpperCase()+r.slice(1);"function"==typeof e[n]&&e[n](t[r])}return e}function a(e){var t=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];if("string"!=typeof e)return e;var r=e.replace(/\$(\d+)/g,function(e,r){return t[r-1]});return/^0x[0-9a-f]+$/i.test(r)||/^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(r)?Number(r):r}function u(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],t=arguments.length<=1||void 0===arguments[1]?[]:arguments[1],r={};for(var n in e)if(e.hasOwnProperty(n)!==!1){var i=a(n,t);"object"===f["typeof"](e[n])?r[i]=u(e[n],t):r[i]=a(e[n],t)}return r}var f={};f["typeof"]="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},f.classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},f.createClass=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),f["extends"]=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},f.toArray=function(e){return Array.isArray(e)?e:Array.from(e)};var c=e(function(e){e.exports=function(e){return String(e).replace(/([.*+?=^!:${}()|[\]\/\\])/g,"\\$1")}}),s=c&&"object"===("undefined"==typeof c?"undefined":f["typeof"](c))&&"default"in c?c["default"]:c,l=e(function(e,t){var r="object"===("undefined"==typeof t?"undefined":f["typeof"](t))?t:{};r.parse=function(){var e,t,r,n,i,o,a={"'":"'",'"':'"',"\\":"\\","/":"/","\n":"",b:"\b",f:"\f",n:"\n",r:"\r",t:" "},u=[" "," ","\r","\n","\x0B","\f"," ","\ufeff"],c=function(e){return""===e?"EOF":"'"+e+"'"},s=function N(n){var N=new SyntaxError;throw N.message=n+" at line "+t+" column "+r+" of the JSON5 data. Still to read: "+JSON.stringify(i.substring(e-1,e+19)),N.at=e,N.lineNumber=t,N.columnNumber=r,N},l=function(o){return o&&o!==n&&s("Expected "+c(o)+" instead of "+c(n)),n=i.charAt(e),e++,r++,("\n"===n||"\r"===n&&"\n"!==p())&&(t++,r=0),n},p=function(){return i.charAt(e)},d=function(){var e=n;for("_"!==n&&"$"!==n&&("a">n||n>"z")&&("A">n||n>"Z")&&s("Bad identifier as unquoted key");l()&&("_"===n||"$"===n||n>="a"&&"z">=n||n>="A"&&"Z">=n||n>="0"&&"9">=n);)e+=n;return e},v=function A(){var A,e="",t="",r=10;if("-"!==n&&"+"!==n||(e=n,l(n)),"I"===n)return A=x(),("number"!=typeof A||isNaN(A))&&s("Unexpected word for number"),"-"===e?-A:A;if("N"===n)return A=x(),isNaN(A)||s("expected word to be NaN"),A;switch("0"===n&&(t+=n,l(),"x"===n||"X"===n?(t+=n,l(),r=16):n>="0"&&"9">=n&&s("Octal literal")),r){case 10:for(;n>="0"&&"9">=n;)t+=n,l();if("."===n)for(t+=".";l()&&n>="0"&&"9">=n;)t+=n;if("e"===n||"E"===n)for(t+=n,l(),"-"!==n&&"+"!==n||(t+=n,l());n>="0"&&"9">=n;)t+=n,l();break;case 16:for(;n>="0"&&"9">=n||n>="A"&&"F">=n||n>="a"&&"f">=n;)t+=n,l()}return A="-"===e?-t:+t,isFinite(A)?A:void s("Bad number")},h=function S(){var e,t,r,i,S="";if('"'===n||"'"===n)for(r=n;l();){if(n===r)return l(),S;if("\\"===n)if(l(),"u"===n){for(i=0,t=0;4>t&&(e=parseInt(l(),16),isFinite(e));t+=1)i=16*i+e;S+=String.fromCharCode(i)}else if("\r"===n)"\n"===p()&&l();else{if("string"!=typeof a[n])break;S+=a[n]}else{if("\n"===n)break;S+=n}}s("Bad string")},y=function(){"/"!==n&&s("Not an inline comment");do if(l(),"\n"===n||"\r"===n)return void l();while(n)},g=function(){"*"!==n&&s("Not a block comment");do for(l();"*"===n;)if(l("*"),"/"===n)return void l("/");while(n);s("Unterminated block comment")},m=function(){"/"!==n&&s("Not a comment"),l("/"),"/"===n?y():"*"===n?g():s("Unrecognized comment")},b=function(){for(;n;)if("/"===n)m();else{if(!(u.indexOf(n)>=0))return;l()}},x=function(){switch(n){case"t":return l("t"),l("r"),l("u"),l("e"),!0;case"f":return l("f"),l("a"),l("l"),l("s"),l("e"),!1;case"n":return l("n"),l("u"),l("l"),l("l"),null;case"I":return l("I"),l("n"),l("f"),l("i"),l("n"),l("i"),l("t"),l("y"),1/0;case"N":return l("N"),l("a"),l("N"),NaN}s("Unexpected "+c(n))},w=function j(){var j=[];if("["===n)for(l("["),b();n;){if("]"===n)return l("]"),j;if(","===n?s("Missing array element"):j.push(o()),b(),","!==n)return l("]"),j;l(","),b()}s("Bad array")},O=function k(){var e,k={};if("{"===n)for(l("{"),b();n;){if("}"===n)return l("}"),k;if(e='"'===n||"'"===n?h():d(),b(),l(":"),k[e]=o(),b(),","!==n)return l("}"),k;l(","),b()}s("Bad object")};return o=function(){switch(b(),n){case"{":return O();case"[":return w();case'"':case"'":return h();case"-":case"+":case".":return v();default:return n>="0"&&"9">=n?v():x()}},function(a,u){var c;return i=String(a),e=0,t=1,r=1,n=" ",c=o(),b(),n&&s("Syntax error"),"function"==typeof u?function l(e,t){var r,n,i=e[t];if(i&&"object"===("undefined"==typeof i?"undefined":f["typeof"](i)))for(r in i)Object.prototype.hasOwnProperty.call(i,r)&&(n=l(i,r),void 0!==n?i[r]=n:delete i[r]);return u.call(e,t,i)}({"":c},""):c}}(),r.stringify=function(e,t,n){function i(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e||e>="0"&&"9">=e||"_"===e||"$"===e}function o(e){return e>="a"&&"z">=e||e>="A"&&"Z">=e||"_"===e||"$"===e}function a(e){if("string"!=typeof e)return!1;if(!o(e[0]))return!1;for(var t=1,r=e.length;r>t;){if(!i(e[t]))return!1;t++}return!0}function u(e){return Array.isArray?Array.isArray(e):"[object Array]"===Object.prototype.toString.call(e)}function c(e){return"[object Date]"===Object.prototype.toString.call(e)}function s(e){for(var t=0;t<y.length;t++)if(y[t]===e)throw new TypeError("Converting circular structure to JSON")}function l(e,t,r){if(!e)return"";e.length>10&&(e=e.substring(0,10));for(var n=r?"":"\n",i=0;t>i;i++)n+=e;return n}function p(e){return g.lastIndex=0,g.test(e)?'"'+e.replace(g,function(e){var t=m[e];return"string"==typeof t?t:"\\u"+("0000"+e.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+e+'"'}function d(e,t,r){var n,i,o=v(e,t,r);switch(o&&!c(o)&&(o=o.valueOf()),"undefined"==typeof o?"undefined":f["typeof"](o)){case"boolean":return o.toString();case"number":return isNaN(o)||!isFinite(o)?"null":o.toString();case"string":return p(o.toString());case"object":if(null===o)return"null";if(u(o)){s(o),n="[",y.push(o);for(var g=0;g<o.length;g++)i=d(o,g,!1),n+=l(h,y.length),n+=null===i||"undefined"==typeof i?"null":i,g<o.length-1?n+=",":h&&(n+="\n");y.pop(),n+=l(h,y.length,!0)+"]"}else{s(o),n="{";var m=!1;y.push(o);for(var b in o)if(o.hasOwnProperty(b)){var x=d(o,b,!1);r=!1,"undefined"!=typeof x&&null!==x&&(n+=l(h,y.length),m=!0,t=a(b)?b:p(b),n+=t+":"+(h?" ":"")+x+",")}y.pop(),n=m?n.substring(0,n.length-1)+l(h,y.length)+"}":"{}"}return n;default:return}}if(t&&"function"!=typeof t&&!u(t))throw new Error("Replacer must be a function or an array");var v=function(e,r,n){var i=e[r];return i&&i.toJSON&&"function"==typeof i.toJSON&&(i=i.toJSON()),"function"==typeof t?t.call(e,r,i):t?n||u(e)||t.indexOf(r)>=0?i:void 0:i};r.isWord=a;var h,y=[];n&&("string"==typeof n?h=n:"number"==typeof n&&n>=0&&(h=l(" ",n,!0)));var g=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,m={"\b":"\\b"," ":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},b={"":e};return void 0===e?v(b,"",!0):d(b,"",!0)}}),p=l&&"object"===("undefined"==typeof l?"undefined":f["typeof"](l))&&"default"in l?l["default"]:l,d=function(){function e(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];f.classCallCheck(this,e),this.name="dictionary",this.opts=f["extends"]({trim:!0},t)}return f.createClass(e,[{key:"transform",value:function(e){var t=this,r=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],n=i(r);return n.reduce(function(e,r){switch(r.method){case"rewrite":return t.rewrite(e,r);case"replace":return e.reduce(function(e,n){return e.concat(t.replace(n,r))},[]);case"toggle":return e.reduce(function(e,n){return e.concat(t.toggle(n,r))},[]);case"exchange":return e.reduce(function(e,n){return e.concat(t.exchange(n,r))},[]);default:return e}},e)}},{key:"rewrite",value:function(e,t){for(var n=t.pattern,i=t.replacement,a=r(n),c=0;c<e.length;c++){var s=e[c];if("text"===s.type){var l=a.exec(s.value)||[],p=f.toArray(l),d=p[0],v=p.slice(1);if(void 0!==d){var h=u(i,v),y=o(s.clone({transformer:this,define:t}).setValue(d),h),g=this.opts.onMatch||function(){return!0};return g.call(this,t,s,y,v)===!1?e:[y]}}}return e}},{key:"replace",value:function(e,t){var n=t.pattern,i=t.replacement;if("text"!==e.type)return e;var o=r(n),a=o.exec(e.value)||[""],u=f.toArray(a),c=u[0],s=u.slice(1);if("string"==typeof i.value&&c.length){var l=e.clone({transformer:this,define:t}).setValue(e.value.replace(o,i.value)),p=this.opts.onMatch||function(){return!0};if(p.call(this,t,e,l,s)!==!1)return l}return e}},{key:"toggle",value:function(e,t){var n=t.pattern,i=t.replacement;if("text"!==e.type)return e;for(var o=r(n),a=[],c=o.exec(e.value),s=0,l={};c;){var p=c,d=f.toArray(p),v=d[0],h=d.slice(1);if(c.index>s){var y=e.value.slice(s,c.index),g=this.opts.trim?y.trim():y;g&&a.push(e.clone({transformer:this,define:t}).setOptions(l.options).setValue(g))}s=c.index+v.length;var m=u(i,h);l=e.clone({transformer:this,define:t}).setValue(v).setOptions(m);var b=this.opts.onMatch||function(){return!0};if(b.call(this,t,e,l,h)===!1)return e;c=o.exec(e.value)}if(e.value.length>s){var v=e.value.slice(s),x=this.opts.trim?v.trim():v;a.push(e.clone({transformer:this}).setOptions(l.options).setValue(x))}return a}},{key:"exchange",value:function(e,t){var n=t.pattern,i=t.replacement;if("text"!==e.type)return e;for(var a=r(n),c=[],s=a.exec(e.value),l=0;s;){var p=s,d=f.toArray(p),v=d[0],h=d.slice(1);if(s.index>l){var y=e.value.slice(l,s.index),g=this.opts.trim?y.trim():y;g&&c.push(e.clone({transformer:this,define:t}).setValue(g))}l=s.index+v.length;var m=u(i,h),b=o(e.clone({transformer:this,define:t}).setValue(v),m),x=this.opts.onMatch||function(){return!0};if(x.call(this,t,e,b,h)===!1)return e;c.push(b),s=a.exec(e.value)}if(e.value.length>l){var v=e.value.slice(l),w=this.opts.trim?v.trim():v;c.push(e.clone({transformer:this}).setValue(w))}return c}}]),e}();return d}); //# sourceMappingURL=index.js.map