@dudousxd/nestjs-catalog-react 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,124 +0,0 @@
1
- /**
2
- * Two texts, lined up, computed in this file and nowhere else.
3
- *
4
- * WHY THIS IS HAND-ROLLED
5
- * -----------------------
6
- * The two strings this console ever diffs are a transform's code and a saved
7
- * query's SQL. Those are the most sensitive strings in the product: a transform
8
- * is executable code with the catalog's own database reachable from it, and a
9
- * saved query is somebody's SQL against military logistics data — the column
10
- * names alone describe a fleet. A diff library is a dependency that gets to see
11
- * both, on every render, and "it only renders locally" is a property of the
12
- * version you audited rather than of the package. There is no version of this
13
- * feature worth a supply-chain question mark on that content, so there is no
14
- * dependency here at all: `diffLines` is forty lines of dynamic programming with
15
- * no imports, and the whole of what it can do is visible on this screen.
16
- *
17
- * It is also the cheaper answer. This package already declines dependencies it
18
- * could plausibly justify — `charts/css.tsx` draws bars with divs, `ui/button.tsx`
19
- * hand-rolls what `class-variance-authority` would compile to, and `export/pdf.ts`
20
- * is a seam rather than a renderer — for the same reason each time: every
21
- * consumer pays for it whether or not they render this screen.
22
- *
23
- * WHAT IT DOES NOT DO
24
- * -------------------
25
- * No word- or character-level diff inside a changed line, and no move detection.
26
- * A changed line is reported as a removal and an addition, which is what a
27
- * unified diff has always done and what the renderer colours. Both are real
28
- * improvements somebody could make; neither is the question this screen exists
29
- * to answer, which is "which lines are different between the version that ran
30
- * and the version that is current".
31
- */
32
- /**
33
- * The largest DP table this will build, in cells.
34
- *
35
- * The comparison is O(n·m) in the CHANGED region only — the common prefix and
36
- * suffix are trimmed first, so the ordinary case (somebody edited four lines of
37
- * a three-hundred-line transform) is a handful of cells regardless of file size.
38
- * This bound exists for the case that survives the trim: two bodies that share
39
- * almost nothing, which is what a rewrite looks like.
40
- *
41
- * 1.5M cells is a `Uint32Array` of 6MB and roughly a 1,200×1,200 changed region.
42
- * Past that the table costs more memory than the browser tab should spend on a
43
- * comparison nobody could read anyway, so {@link diffLines} stops computing and
44
- * SAYS it stopped — see `alignment`. A silent fallback would be the same failure
45
- * `capLines` names one package over: a truncation nobody is told about.
46
- */
47
- export declare const DIFF_MAX_CELLS = 1500000;
48
- /** What happened to one line between the two bodies. */
49
- export type DiffOp = 'same' | 'added' | 'removed';
50
- export interface DiffLine {
51
- op: DiffOp;
52
- /** 1-based line number in the earlier body. Null on an added line. */
53
- before: number | null;
54
- /** 1-based line number in the later body. Null on a removed line. */
55
- after: number | null;
56
- text: string;
57
- }
58
- export interface LineDiff {
59
- lines: DiffLine[];
60
- added: number;
61
- removed: number;
62
- /**
63
- * The two bodies are byte-for-byte the same.
64
- *
65
- * A separate field rather than `added === 0 && removed === 0`, because the
66
- * caller has to distinguish "these two versions are identical" from "there is
67
- * no second version to compare against", and only one of those is a statement
68
- * about the texts. See `RevisionDiff`, where conflating them is the exact
69
- * thing the empty states are written to avoid.
70
- */
71
- identical: boolean;
72
- /**
73
- * How the lines were matched up.
74
- *
75
- * `exact` means every line was lined up against its counterpart. `coarse`
76
- * means the changed region was past {@link DIFF_MAX_CELLS} and the whole of it
77
- * is reported as removed-then-added — true, but blunter than it could be, and
78
- * the screen says so rather than letting somebody read "everything changed" as
79
- * a finding about their code.
80
- */
81
- alignment: 'exact' | 'coarse';
82
- }
83
- /**
84
- * Line up two bodies.
85
- *
86
- * `before` is the earlier version, `after` the later one. The order matters to
87
- * the reading and not to the algorithm: swapping them turns every addition into
88
- * a removal, which is why the caller that pins "the version that ran" on the
89
- * left is the one that gets a screen answering "what changed since".
90
- */
91
- export declare function diffLines(before: string, after: string): LineDiff;
92
- /** Lines to keep either side of a change before folding the rest away. */
93
- export declare const DIFF_CONTEXT_LINES = 3;
94
- /**
95
- * The smallest run worth putting a button on.
96
- *
97
- * Folding two lines behind a control that says "2 unchanged lines" costs a row
98
- * to save two, and it costs a click. Below this the run is simply shown.
99
- */
100
- export declare const DIFF_MIN_FOLD = 4;
101
- export type DiffSection = {
102
- kind: 'shown';
103
- lines: DiffLine[];
104
- } | {
105
- kind: 'folded';
106
- lines: DiffLine[];
107
- };
108
- /**
109
- * Collapse the long unchanged stretches, keeping context around every change.
110
- *
111
- * A transform can be hundreds of lines and the answer to "why did Tuesday's load
112
- * differ from Monday's" is almost never more than a handful of them. Rendering
113
- * all of it puts the four lines that matter somewhere in the middle of a wall
114
- * and makes the reader scroll for them.
115
- *
116
- * Folded rather than dropped, and the sections carry their lines rather than a
117
- * count, so the renderer can expand one in place. A diff that hides code with no
118
- * way to see it is a diff you cannot trust, and this is code somebody is about
119
- * to make a decision about.
120
- *
121
- * A run at the very start or end of the file gets no context on that side —
122
- * there is no change up there to be in the context of.
123
- */
124
- export declare function foldUnchanged(lines: DiffLine[], context?: number, minFold?: number): DiffSection[];
@@ -1,318 +0,0 @@
1
- /**
2
- * Two texts, lined up, computed in this file and nowhere else.
3
- *
4
- * WHY THIS IS HAND-ROLLED
5
- * -----------------------
6
- * The two strings this console ever diffs are a transform's code and a saved
7
- * query's SQL. Those are the most sensitive strings in the product: a transform
8
- * is executable code with the catalog's own database reachable from it, and a
9
- * saved query is somebody's SQL against military logistics data — the column
10
- * names alone describe a fleet. A diff library is a dependency that gets to see
11
- * both, on every render, and "it only renders locally" is a property of the
12
- * version you audited rather than of the package. There is no version of this
13
- * feature worth a supply-chain question mark on that content, so there is no
14
- * dependency here at all: `diffLines` is forty lines of dynamic programming with
15
- * no imports, and the whole of what it can do is visible on this screen.
16
- *
17
- * It is also the cheaper answer. This package already declines dependencies it
18
- * could plausibly justify — `charts/css.tsx` draws bars with divs, `ui/button.tsx`
19
- * hand-rolls what `class-variance-authority` would compile to, and `export/pdf.ts`
20
- * is a seam rather than a renderer — for the same reason each time: every
21
- * consumer pays for it whether or not they render this screen.
22
- *
23
- * WHAT IT DOES NOT DO
24
- * -------------------
25
- * No word- or character-level diff inside a changed line, and no move detection.
26
- * A changed line is reported as a removal and an addition, which is what a
27
- * unified diff has always done and what the renderer colours. Both are real
28
- * improvements somebody could make; neither is the question this screen exists
29
- * to answer, which is "which lines are different between the version that ran
30
- * and the version that is current".
31
- */
32
- /**
33
- * The largest DP table this will build, in cells.
34
- *
35
- * The comparison is O(n·m) in the CHANGED region only — the common prefix and
36
- * suffix are trimmed first, so the ordinary case (somebody edited four lines of
37
- * a three-hundred-line transform) is a handful of cells regardless of file size.
38
- * This bound exists for the case that survives the trim: two bodies that share
39
- * almost nothing, which is what a rewrite looks like.
40
- *
41
- * 1.5M cells is a `Uint32Array` of 6MB and roughly a 1,200×1,200 changed region.
42
- * Past that the table costs more memory than the browser tab should spend on a
43
- * comparison nobody could read anyway, so {@link diffLines} stops computing and
44
- * SAYS it stopped — see `alignment`. A silent fallback would be the same failure
45
- * `capLines` names one package over: a truncation nobody is told about.
46
- */
47
- export const DIFF_MAX_CELLS = 1_500_000;
48
- /**
49
- * Split on `\n` alone, and keep whatever that leaves.
50
- *
51
- * A lone `\r` therefore stays inside the line text, so a body that changed from
52
- * CRLF to LF reports every line as changed. That is noisy and it is also true:
53
- * the stored bodies differ on every line, a connector pinned to the old version
54
- * would run different bytes, and a diff that normalised the endings away would
55
- * be answering a question nobody asked ("would these be the same if…") in place
56
- * of the one they did.
57
- *
58
- * An empty body is zero lines rather than one empty line, so writing the first
59
- * version of a transform reads as N additions rather than N additions and one
60
- * mysterious deletion. A body ending in a newline genuinely does have a trailing
61
- * empty line and keeps it.
62
- */
63
- function splitLines(text) {
64
- return text.length === 0 ? [] : text.split('\n');
65
- }
66
- /**
67
- * Line up two bodies.
68
- *
69
- * `before` is the earlier version, `after` the later one. The order matters to
70
- * the reading and not to the algorithm: swapping them turns every addition into
71
- * a removal, which is why the caller that pins "the version that ran" on the
72
- * left is the one that gets a screen answering "what changed since".
73
- */
74
- export function diffLines(before, after) {
75
- const beforeLines = splitLines(before);
76
- const afterLines = splitLines(after);
77
- if (before === after) {
78
- return {
79
- lines: beforeLines.map((text, index) => ({
80
- op: 'same',
81
- before: index + 1,
82
- after: index + 1,
83
- text,
84
- })),
85
- added: 0,
86
- removed: 0,
87
- identical: true,
88
- alignment: 'exact',
89
- };
90
- }
91
- // The trim, which is what makes this cheap on the case that actually happens.
92
- // A four-line edit inside a three-hundred-line transform leaves a DP table of
93
- // sixteen cells; without it, ninety thousand.
94
- let head = 0;
95
- while (head < beforeLines.length &&
96
- head < afterLines.length &&
97
- beforeLines[head] === afterLines[head]) {
98
- head += 1;
99
- }
100
- let tail = 0;
101
- while (tail < beforeLines.length - head &&
102
- tail < afterLines.length - head &&
103
- beforeLines[beforeLines.length - 1 - tail] === afterLines[afterLines.length - 1 - tail]) {
104
- tail += 1;
105
- }
106
- const beforeMiddle = beforeLines.slice(head, beforeLines.length - tail);
107
- const afterMiddle = afterLines.slice(head, afterLines.length - tail);
108
- const middle = beforeMiddle.length * afterMiddle.length > DIFF_MAX_CELLS
109
- ? coarse(beforeMiddle, afterMiddle, head)
110
- : align(beforeMiddle, afterMiddle, head);
111
- const lines = [
112
- ...beforeLines.slice(0, head).map((text, index) => sameLine(text, index + 1, index + 1)),
113
- ...middle.lines,
114
- ...beforeLines
115
- .slice(beforeLines.length - tail)
116
- .map((text, index) => sameLine(text, beforeLines.length - tail + index + 1, afterLines.length - tail + index + 1)),
117
- ];
118
- return {
119
- lines,
120
- added: middle.added,
121
- removed: middle.removed,
122
- identical: false,
123
- alignment: middle.alignment,
124
- };
125
- }
126
- function sameLine(text, before, after) {
127
- return { op: 'same', before, after, text };
128
- }
129
- /**
130
- * The longest common subsequence of two line arrays, walked into a diff.
131
- *
132
- * The table is filled from the end so the walk can go forwards, which is what
133
- * keeps the output in file order without a reversal at the end. `Uint32Array`
134
- * rather than nested arrays because the table is the only thing here that has a
135
- * size worth thinking about, and a flat typed array is one allocation.
136
- *
137
- * When neither direction is better the walk prefers the REMOVAL, which is what
138
- * puts every deleted line of a hunk above every added one — the unified-diff
139
- * order, and the order the renderer's red-then-green block depends on.
140
- */
141
- function align(beforeMiddle, afterMiddle, offset) {
142
- const m = beforeMiddle.length;
143
- const n = afterMiddle.length;
144
- const width = n + 1;
145
- const table = lcsTable(beforeMiddle, afterMiddle);
146
- const lines = [];
147
- let added = 0;
148
- let removed = 0;
149
- let i = 0;
150
- let j = 0;
151
- while (i < m && j < n) {
152
- if (beforeMiddle[i] === afterMiddle[j]) {
153
- lines.push(sameLine(beforeMiddle[i] ?? '', offset + i + 1, offset + j + 1));
154
- i += 1;
155
- j += 1;
156
- }
157
- else if ((table[(i + 1) * width + j] ?? 0) >= (table[i * width + j + 1] ?? 0)) {
158
- lines.push(removedLine(beforeMiddle[i] ?? '', offset + i + 1));
159
- removed += 1;
160
- i += 1;
161
- }
162
- else {
163
- lines.push(addedLine(afterMiddle[j] ?? '', offset + j + 1));
164
- added += 1;
165
- j += 1;
166
- }
167
- }
168
- // Whatever is left when one side runs out. Removals first, again, so a hunk at
169
- // the very end of the file reads the same way as one in the middle.
170
- const tail = drain(beforeMiddle.slice(i), afterMiddle.slice(j), offset + i, offset + j);
171
- lines.push(...tail.lines);
172
- return {
173
- lines,
174
- added: added + tail.added,
175
- removed: removed + tail.removed,
176
- alignment: 'exact',
177
- };
178
- }
179
- /**
180
- * The remainder of one side, once the walk has exhausted the other.
181
- *
182
- * At most one of these two arrays is non-empty — the walk only stops when an
183
- * index reaches its end — so this is not a second comparison. It is the same
184
- * shape as {@link coarse}, and calling that would have been tempting; it is
185
- * separate because `coarse` means "we declined to line these up" and this means
186
- * "there was nothing left to line them up against", and a screen that reads
187
- * `alignment` must not be told the first when the second happened.
188
- */
189
- function drain(beforeRest, afterRest, beforeOffset, afterOffset) {
190
- return {
191
- lines: [
192
- ...beforeRest.map((text, index) => removedLine(text, beforeOffset + index + 1)),
193
- ...afterRest.map((text, index) => addedLine(text, afterOffset + index + 1)),
194
- ],
195
- added: afterRest.length,
196
- removed: beforeRest.length,
197
- };
198
- }
199
- /**
200
- * `table[i * width + j]` is the length of the longest common subsequence of
201
- * `a.slice(i)` and `b.slice(j)`.
202
- *
203
- * Filled from the END, which is what lets {@link align} walk forwards and emit
204
- * lines already in file order rather than reversing at the finish.
205
- */
206
- function lcsTable(a, b) {
207
- const width = b.length + 1;
208
- const table = new Uint32Array((a.length + 1) * width);
209
- for (let i = a.length - 1; i >= 0; i -= 1) {
210
- for (let j = b.length - 1; j >= 0; j -= 1) {
211
- table[i * width + j] =
212
- a[i] === b[j]
213
- ? (table[(i + 1) * width + j + 1] ?? 0) + 1
214
- : Math.max(table[(i + 1) * width + j] ?? 0, table[i * width + j + 1] ?? 0);
215
- }
216
- }
217
- return table;
218
- }
219
- function removedLine(text, before) {
220
- return { op: 'removed', before, after: null, text };
221
- }
222
- function addedLine(text, after) {
223
- return { op: 'added', before: null, after, text };
224
- }
225
- /** The whole changed region as one removal and one addition. See {@link DIFF_MAX_CELLS}. */
226
- function coarse(beforeMiddle, afterMiddle, offset) {
227
- return {
228
- lines: [
229
- ...beforeMiddle.map((text, index) => ({
230
- op: 'removed',
231
- before: offset + index + 1,
232
- after: null,
233
- text,
234
- })),
235
- ...afterMiddle.map((text, index) => ({
236
- op: 'added',
237
- before: null,
238
- after: offset + index + 1,
239
- text,
240
- })),
241
- ],
242
- added: afterMiddle.length,
243
- removed: beforeMiddle.length,
244
- alignment: 'coarse',
245
- };
246
- }
247
- /** Lines to keep either side of a change before folding the rest away. */
248
- export const DIFF_CONTEXT_LINES = 3;
249
- /**
250
- * The smallest run worth putting a button on.
251
- *
252
- * Folding two lines behind a control that says "2 unchanged lines" costs a row
253
- * to save two, and it costs a click. Below this the run is simply shown.
254
- */
255
- export const DIFF_MIN_FOLD = 4;
256
- /**
257
- * Collapse the long unchanged stretches, keeping context around every change.
258
- *
259
- * A transform can be hundreds of lines and the answer to "why did Tuesday's load
260
- * differ from Monday's" is almost never more than a handful of them. Rendering
261
- * all of it puts the four lines that matter somewhere in the middle of a wall
262
- * and makes the reader scroll for them.
263
- *
264
- * Folded rather than dropped, and the sections carry their lines rather than a
265
- * count, so the renderer can expand one in place. A diff that hides code with no
266
- * way to see it is a diff you cannot trust, and this is code somebody is about
267
- * to make a decision about.
268
- *
269
- * A run at the very start or end of the file gets no context on that side —
270
- * there is no change up there to be in the context of.
271
- */
272
- export function foldUnchanged(lines, context = DIFF_CONTEXT_LINES, minFold = DIFF_MIN_FOLD) {
273
- const sections = [];
274
- let shown = [];
275
- const flush = () => {
276
- if (shown.length > 0) {
277
- sections.push({ kind: 'shown', lines: shown });
278
- shown = [];
279
- }
280
- };
281
- let index = 0;
282
- while (index < lines.length) {
283
- const line = lines[index];
284
- if (!line)
285
- break;
286
- if (line.op !== 'same') {
287
- shown.push(line);
288
- index += 1;
289
- continue;
290
- }
291
- const end = runEnd(lines, index);
292
- const run = lines.slice(index, end);
293
- // No context on a side there is no change on: a run touching the start of
294
- // the file has nothing above it to be the context of, and neither does one
295
- // touching the end.
296
- const leading = index === 0 ? 0 : context;
297
- const trailing = end === lines.length ? 0 : context;
298
- if (run.length > leading + trailing + minFold) {
299
- shown.push(...run.slice(0, leading));
300
- flush();
301
- sections.push({ kind: 'folded', lines: run.slice(leading, run.length - trailing) });
302
- shown.push(...run.slice(run.length - trailing));
303
- }
304
- else {
305
- shown.push(...run);
306
- }
307
- index = end;
308
- }
309
- flush();
310
- return sections;
311
- }
312
- /** Where the run of unchanged lines starting at `start` stops. */
313
- function runEnd(lines, start) {
314
- let end = start;
315
- while (end < lines.length && lines[end]?.op === 'same')
316
- end += 1;
317
- return end;
318
- }