UNPKG

react-gh-like-diff

Version:
2 lines (1 loc) 5.97 kB
import e from"react";import t from"prop-types";import{unifiedDiff as n}from"difflib";import{compose as r,defaultProps as i,onlyUpdateForPropTypes as o,setPropTypes as u,branch as l,mapProps as c}from"recompose";var s=/%[sdj%]/g;function a(e,t){var n={seen:[],stylize:p};return arguments.length>=3&&(n.depth=arguments[2]),arguments.length>=4&&(n.colors=arguments[3]),y(t)?n.showHidden=t:t&&F(n,t),v(n.showHidden)&&(n.showHidden=!1),v(n.depth)&&(n.depth=2),v(n.colors)&&(n.colors=!1),v(n.customInspect)&&(n.customInspect=!0),n.colors&&(n.stylize=f),g(n,e,n.depth)}function f(e,t){var n=a.styles[t];return n?"["+a.colors[n][0]+"m"+e+"["+a.colors[n][1]+"m":e}function p(e,t){return e}function g(e,t,n){if(e.customInspect&&t&&x(t.inspect)&&t.inspect!==a&&(!t.constructor||t.constructor.prototype!==t)){var r=t.inspect(n,e);return b(r)||(r=g(e,r,n)),r}var i=function(e,t){if(v(t))return e.stylize("undefined","undefined");if(b(t)){var n="'"+JSON.stringify(t).replace(/^"|"$/g,"").replace(/'/g,"\\'").replace(/\\"/g,'"')+"'";return e.stylize(n,"string")}if(r=t,"number"==typeof r)return e.stylize(""+t,"number");var r;if(y(t))return e.stylize(""+t,"boolean");if(h(t))return e.stylize("null","null")}(e,t);if(i)return i;var o=Object.keys(t),u=function(e){var t={};return e.forEach((function(e,n){t[e]=!0})),t}(o);if(e.showHidden&&(o=Object.getOwnPropertyNames(t)),O(t)&&(o.indexOf("message")>=0||o.indexOf("description")>=0))return d(t);if(0===o.length){if(x(t)){var l=t.name?": "+t.name:"";return e.stylize("[Function"+l+"]","special")}if(S(t))return e.stylize(RegExp.prototype.toString.call(t),"regexp");if(z(t))return e.stylize(Date.prototype.toString.call(t),"date");if(O(t))return d(t)}var c,s,f="",p=!1,j=["{","}"];(c=t,Array.isArray(c)&&(p=!0,j=["[","]"]),x(t))&&(f=" [Function"+(t.name?": "+t.name:"")+"]");return S(t)&&(f=" "+RegExp.prototype.toString.call(t)),z(t)&&(f=" "+Date.prototype.toUTCString.call(t)),O(t)&&(f=" "+d(t)),0!==o.length||p&&0!=t.length?n<0?S(t)?e.stylize(RegExp.prototype.toString.call(t),"regexp"):e.stylize("[Object]","special"):(e.seen.push(t),s=p?function(e,t,n,r,i){for(var o=[],u=0,l=t.length;u<l;++u)N(t,String(u))?o.push(m(e,t,n,r,String(u),!0)):o.push("");return i.forEach((function(i){i.match(/^\d+$/)||o.push(m(e,t,n,r,i,!0))})),o}(e,t,n,u,o):o.map((function(r){return m(e,t,n,u,r,p)})),e.seen.pop(),function(e,t,n){if(e.reduce((function(e,t){return t.indexOf("\n"),e+t.replace(/\u001b\[\d\d?m/g,"").length+1}),0)>60)return n[0]+(""===t?"":t+"\n ")+" "+e.join(",\n ")+" "+n[1];return n[0]+t+" "+e.join(", ")+" "+n[1]}(s,f,j)):j[0]+f+j[1]}function d(e){return"["+Error.prototype.toString.call(e)+"]"}function m(e,t,n,r,i,o){var u,l,c;if((c=Object.getOwnPropertyDescriptor(t,i)||{value:t[i]}).get?l=c.set?e.stylize("[Getter/Setter]","special"):e.stylize("[Getter]","special"):c.set&&(l=e.stylize("[Setter]","special")),N(r,i)||(u="["+i+"]"),l||(e.seen.indexOf(c.value)<0?(l=h(n)?g(e,c.value,null):g(e,c.value,n-1)).indexOf("\n")>-1&&(l=o?l.split("\n").map((function(e){return" "+e})).join("\n").substr(2):"\n"+l.split("\n").map((function(e){return" "+e})).join("\n")):l=e.stylize("[Circular]","special")),v(u)){if(o&&i.match(/^\d+$/))return l;(u=JSON.stringify(""+i)).match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)?(u=u.substr(1,u.length-2),u=e.stylize(u,"name")):(u=u.replace(/'/g,"\\'").replace(/\\"/g,'"').replace(/(^"|"$)/g,"'"),u=e.stylize(u,"string"))}return u+": "+l}function y(e){return"boolean"==typeof e}function h(e){return null===e}function b(e){return"string"==typeof e}function v(e){return void 0===e}function S(e){return j(e)&&"[object RegExp]"===w(e)}function j(e){return"object"==typeof e&&null!==e}function z(e){return j(e)&&"[object Date]"===w(e)}function O(e){return j(e)&&("[object Error]"===w(e)||e instanceof Error)}function x(e){return"function"==typeof e}function w(e){return Object.prototype.toString.call(e)}function F(e,t){if(!t||!j(t))return e;for(var n=Object.keys(t),r=n.length;r--;)e[n[r]]=t[n[r]];return e}function N(e,t){return Object.prototype.hasOwnProperty.call(e,t)}a.colors={bold:[1,22],italic:[3,23],underline:[4,24],inverse:[7,27],white:[37,39],grey:[90,39],black:[30,39],blue:[34,39],cyan:[36,39],green:[32,39],magenta:[35,39],red:[31,39],yellow:[33,39]},a.styles={special:"cyan",number:"yellow",boolean:"yellow",undefined:"grey",null:"bold",string:"green",date:"magenta",regexp:"red"};var E={originalFileName:"Unknown-File-Name",updatedFileName:"Unknown-File-Name",inputFormat:"diff",outputFormat:"side-by-side",showFiles:!1,matching:"none",matchWordsThreshold:.25,matchingMaxComparisons:2500,maxLineSizeInBlockForComparison:200,maxLineLengthHighlight:1e4,renderNothingWhenEmpty:!1},H=function(e){var t=e.diffString,n=e.options;return require("diff2html").html(t,n)},D=r(H,(function(e){var t=e.past,r=e.current,i=e.options,o=Object.assign({},E,i),u=t.split(/\r|\n|\r\n/),l=r.split(/\r|\n|\r\n/),c=n(u,l,{fromfile:o.originalFileName,tofile:o.updatedFileName});return{diffString:function(e){if(!b(e)){for(var t=[],n=0;n<arguments.length;n++)t.push(a(arguments[n]));return t.join(" ")}n=1;for(var r=arguments,i=r.length,o=String(e).replace(s,(function(e){if("%%"===e)return"%";if(n>=i)return e;switch(e){case"%s":return String(r[n++]);case"%d":return Number(r[n++]);case"%j":try{return JSON.stringify(r[n++])}catch(e){return"[Circular]"}default:return e}})),u=r[n];n<i;u=r[++n])h(u)||!j(u)?o+=" "+u:o+=" "+a(u);return o}("diff --git %s %s\n%s",o.originalFileName,o.updatedFileName,c.join("\n")),options:o}})),L=function(t){var n=t.genDiffHTML;return e.createElement("div",{dangerouslySetInnerHTML:{__html:n}})},T=r(i({diffString:"",past:"",current:"",options:E}),o,u({past:t.string,current:t.string,options:t.object,diffString:t.string}),l((function(e){return 0!==e.diffString.length}),(function(){return function(t){return e.createElement(L,{genDiffHTML:H(t)})}}),c((function(e){return{genDiffHTML:D(e)}}))))(L);L.propTypes={genDiffHTML:t.string};export{T as ReactGhLikeDiff,L as RenderDiffResult};