UNPKG

prismejs

Version:

Presentation and transformation layer for complex/dirty API's data output.

1 lines 3.32 kB
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define("prismejs",[],r):"object"==typeof exports?exports.prismejs=r():e.prismejs=r()}(this,function(){return function(e){function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}var t={};return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},r.p="",r(r.s=0)}([function(e,r,t){"use strict";function n(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(r,"__esModule",{value:!0});var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},a=function(){function e(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(r,t,n){return t&&e(r.prototype,t),n&&e(r,n),r}}(),i=function(){function e(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"json";n(this,e),this.config={version:"1.0.6",availableReturnTypes:["json"]},this.setReturnType(r),this.grammar={regex:/\.|\:|(\[.*\])/g,regexObjectSeparator:/\./g,regexPropCheck:/(.*?):(.*)(\[(.*)\])?/g,objectSeperator:".",propCheck:":",endValue:/\[(.*?)\]/g}}return a(e,[{key:"setReturnType",value:function(e){try{if(!this.config.availableReturnTypes.includes(e))throw new Error("InvalidReturnType");this.returnType=e}catch(r){console.warn("PrismeJS [ "+r.name+": "+r.message+' ] — "'+e+'"" is an invalid return type. Please set an available return type.'),console.log("PrismeJS — Available return types : "+this.availableReturnTypes+" ")}}},{key:"format",value:function(e,r){var t=void 0;return"json"===this.returnType&&(t=this.formatJSON(e,r)),t}},{key:"formatJSON",value:function(e,r){var t=this;return e instanceof Array||(e=[e]),e.map(function(e){return t.formatJSONObject(e,r)})}},{key:"formatJSONObject",value:function(e,r){var t={};for(var n in r){var o=this.formatJSONProp(e,r[n]);null!==o&&(t[n]=o)}return t}},{key:"formatJSONProp",value:function(e,r){var t=null;return"string"==typeof r&&(t=e[r],r.match(this.grammar.regex)&&(t=this.tracePath(e,r,r.split(this.grammar.regexObjectSeparator)))),"object"===(void 0===r?"undefined":o(r))&&(t=this.formatJSONObject(e,r)),t}},{key:"tracePath",value:function(e,r,t){var n=this,o=[],a=!1;return t.forEach(function(e){if(e.match(n.grammar.propCheck)){var r=e.split(n.grammar.propCheck)[0];o.push(r)}else o.push(e)}),t.map(function(r){if(r.match(n.grammar.regexPropCheck)){var t=r.split(n.grammar.propCheck),i=n.grammar.endValue.exec(t[1]),u=void 0;null!==i&&(u=i[1]);var c=e;o.some(function(e){if(e===t[0])for(var r=0;r<c.length;){if(c[r][e]===t[1].replace(n.grammar.endValue,""))return a=u?c[r][u]:c[r];r++}c=c[e]})}else if(r.match(n.grammar.endValue)){var f=n.grammar.endValue.exec(r);a=e[r.replace(f[0],"")][f[1]]}}),a}}]),e}();r.default=i}])});