File tree Expand file tree Collapse file tree 3 files changed +24
-25
lines changed
Expand file tree Collapse file tree 3 files changed +24
-25
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import {
1111 DiffLineContext ,
1212 DiffLineInserted ,
1313} from './types' ;
14+ import { max } from './utils' ;
1415
1516export interface LineByLineRendererConfig extends renderUtils . RenderConfig {
1617 renderNothingWhenEmpty ?: boolean ;
@@ -30,27 +31,6 @@ const baseTemplatesPath = 'line-by-line';
3031const iconsBaseTemplatesPath = 'icon' ;
3132const tagsBaseTemplatesPath = 'tag' ;
3233
33- /**
34- * @description Finds the maximum value in an array of numbers safely
35- *
36- * This function iterates through the array manually instead of using Math.max(...arr)
37- * to avoid "Maximum call stack size exceeded" errors that can occur when spreading
38- * very large arrays as function arguments. This is especially important when dealing
39- * with large diffs that may contain thousands of lines.
40- *
41- * @param arr - The array of numbers
42- * @returns The maximum value in the array
43- */
44- function max ( arr : number [ ] ) : number {
45- const length = arr . length ;
46- let max = - Infinity ;
47-
48- for ( let i = 0 ; i < length ; i ++ ) {
49- max = Math . max ( max , arr [ i ] ) ;
50- }
51- return max ;
52- }
53-
5434export default class LineByLineRenderer {
5535 private readonly hoganUtils : HoganJsUtils ;
5636 private readonly config : typeof defaultLineByLineRendererConfig ;
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import {
1111 DiffLineInserted ,
1212 DiffLineContent ,
1313} from './types' ;
14+ import { max } from './utils' ;
1415
1516export interface SideBySideRendererConfig extends renderUtils . RenderConfig {
1617 renderNothingWhenEmpty ?: boolean ;
@@ -194,10 +195,7 @@ export default class SideBySideRenderer {
194195 matcher : Rematch . MatcherFn < DiffLine > ,
195196 ) : DiffLine [ ] [ ] [ ] {
196197 const comparisons = oldLines . length * newLines . length ;
197- const maxLineSizeInBlock = Math . max . apply (
198- null ,
199- [ 0 ] . concat ( oldLines . concat ( newLines ) . map ( elem => elem . content . length ) ) ,
200- ) ;
198+ const maxLineSizeInBlock = max ( oldLines . concat ( newLines ) . map ( elem => elem . content . length ) ) ;
201199 const doMatching =
202200 comparisons < this . config . matchingMaxComparisons &&
203201 maxLineSizeInBlock < this . config . maxLineSizeInBlockForComparison &&
Original file line number Diff line number Diff line change @@ -52,3 +52,24 @@ export function hashCode(text: string): number {
5252
5353 return hash ;
5454}
55+
56+ /**
57+ * @description Finds the maximum value in an array of numbers safely
58+ *
59+ * This function iterates through the array manually instead of using Math.max(...arr)
60+ * to avoid "Maximum call stack size exceeded" errors that can occur when spreading
61+ * very large arrays as function arguments. This is especially important when dealing
62+ * with large diffs that may contain thousands of lines.
63+ *
64+ * @param arr - The array of numbers
65+ * @returns The maximum value in the array
66+ */
67+ export function max ( arr : number [ ] ) : number {
68+ const length = arr . length ;
69+ let max = - Infinity ;
70+
71+ for ( let i = 0 ; i < length ; i ++ ) {
72+ max = Math . max ( max , arr [ i ] ) ;
73+ }
74+ return max ;
75+ }
You can’t perform that action at this time.
0 commit comments