react-gh-like-diff
Version:
Generate Github like diff result in react.
2 lines (1 loc) • 6.08 kB
JavaScript
;function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("react")),n=e(require("prop-types")),r=require("difflib"),i=require("recompose"),o=/%[sdj%]/g;function u(e,t){var n={seen:[],stylize:s};return arguments.length>=3&&(n.depth=arguments[2]),arguments.length>=4&&(n.colors=arguments[3]),p(t)?n.showHidden=t:t&&z(n,t),y(n.showHidden)&&(n.showHidden=!1),y(n.depth)&&(n.depth=2),y(n.colors)&&(n.colors=!1),y(n.customInspect)&&(n.customInspect=!0),n.colors&&(n.stylize=l),c(n,e,n.depth)}function l(e,t){var n=u.styles[t];return n?"["+u.colors[n][0]+"m"+e+"["+u.colors[n][1]+"m":e}function s(e,t){return e}function c(e,t,n){if(e.customInspect&&t&&j(t.inspect)&&t.inspect!==u&&(!t.constructor||t.constructor.prototype!==t)){var r=t.inspect(n,e);return d(r)||(r=c(e,r,n)),r}var i=function(e,t){if(y(t))return e.stylize("undefined","undefined");if(d(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(p(t))return e.stylize(""+t,"boolean");if(g(t))return e.stylize("null","null")}(e,t);if(i)return i;var o=Object.keys(t),l=function(e){var t={};return e.forEach((function(e,n){t[e]=!0})),t}(o);if(e.showHidden&&(o=Object.getOwnPropertyNames(t)),v(t)&&(o.indexOf("message")>=0||o.indexOf("description")>=0))return a(t);if(0===o.length){if(j(t)){var s=t.name?": "+t.name:"";return e.stylize("[Function"+s+"]","special")}if(m(t))return e.stylize(RegExp.prototype.toString.call(t),"regexp");if(b(t))return e.stylize(Date.prototype.toString.call(t),"date");if(v(t))return a(t)}var h,S,z="",x=!1,w=["{","}"];(h=t,Array.isArray(h)&&(x=!0,w=["[","]"]),j(t))&&(z=" [Function"+(t.name?": "+t.name:"")+"]");return m(t)&&(z=" "+RegExp.prototype.toString.call(t)),b(t)&&(z=" "+Date.prototype.toUTCString.call(t)),v(t)&&(z=" "+a(t)),0!==o.length||x&&0!=t.length?n<0?m(t)?e.stylize(RegExp.prototype.toString.call(t),"regexp"):e.stylize("[Object]","special"):(e.seen.push(t),S=x?function(e,t,n,r,i){for(var o=[],u=0,l=t.length;u<l;++u)O(t,String(u))?o.push(f(e,t,n,r,String(u),!0)):o.push("");return i.forEach((function(i){i.match(/^\d+$/)||o.push(f(e,t,n,r,i,!0))})),o}(e,t,n,l,o):o.map((function(r){return f(e,t,n,l,r,x)})),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,z,w)):w[0]+z+w[1]}function a(e){return"["+Error.prototype.toString.call(e)+"]"}function f(e,t,n,r,i,o){var u,l,s;if((s=Object.getOwnPropertyDescriptor(t,i)||{value:t[i]}).get?l=s.set?e.stylize("[Getter/Setter]","special"):e.stylize("[Getter]","special"):s.set&&(l=e.stylize("[Setter]","special")),O(r,i)||(u="["+i+"]"),l||(e.seen.indexOf(s.value)<0?(l=g(n)?c(e,s.value,null):c(e,s.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")),y(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 p(e){return"boolean"==typeof e}function g(e){return null===e}function d(e){return"string"==typeof e}function y(e){return void 0===e}function m(e){return h(e)&&"[object RegExp]"===S(e)}function h(e){return"object"==typeof e&&null!==e}function b(e){return h(e)&&"[object Date]"===S(e)}function v(e){return h(e)&&("[object Error]"===S(e)||e instanceof Error)}function j(e){return"function"==typeof e}function S(e){return Object.prototype.toString.call(e)}function z(e,t){if(!t||!h(t))return e;for(var n=Object.keys(t),r=n.length;r--;)e[n[r]]=t[n[r]];return e}function O(e,t){return Object.prototype.hasOwnProperty.call(e,t)}u.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]},u.styles={special:"cyan",number:"yellow",boolean:"yellow",undefined:"grey",null:"bold",string:"green",date:"magenta",regexp:"red"};var x={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},w=function(e){var t=e.diffString,n=e.options;return require("diff2html").html(t,n)},F=i.compose(w,(function(e){var t=e.past,n=e.current,i=e.options,l=Object.assign({},x,i),s=t.split(/\r|\n|\r\n/),c=n.split(/\r|\n|\r\n/),a=r.unifiedDiff(s,c,{fromfile:l.originalFileName,tofile:l.updatedFileName});return{diffString:function(e){if(!d(e)){for(var t=[],n=0;n<arguments.length;n++)t.push(u(arguments[n]));return t.join(" ")}n=1;for(var r=arguments,i=r.length,l=String(e).replace(o,(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}})),s=r[n];n<i;s=r[++n])g(s)||!h(s)?l+=" "+s:l+=" "+u(s);return l}("diff --git %s %s\n%s",l.originalFileName,l.updatedFileName,a.join("\n")),options:l}})),N=function(e){var n=e.genDiffHTML;return t.createElement("div",{dangerouslySetInnerHTML:{__html:n}})},E=i.compose(i.defaultProps({diffString:"",past:"",current:"",options:x}),i.onlyUpdateForPropTypes,i.setPropTypes({past:n.string,current:n.string,options:n.object,diffString:n.string}),i.branch((function(e){return 0!==e.diffString.length}),(function(){return function(e){return t.createElement(N,{genDiffHTML:w(e)})}}),i.mapProps((function(e){return{genDiffHTML:F(e)}}))))(N);N.propTypes={genDiffHTML:n.string},exports.ReactGhLikeDiff=E,exports.RenderDiffResult=N;