@preventive/compare 0.0.0-stage → 1.0.1

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.
package/LICENSE-APACHE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/LICENSE-MIT ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nikita Skovoroda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/compare.d.ts ADDED
@@ -0,0 +1,185 @@
1
+ import type { Block } from '@preventive/diff'
2
+
3
+ // An entry of a tree, in the shape tar has it: what @preventive/archive's
4
+ // unpack returns and Vfs.entries yields are these as they are. `name` is a
5
+ // relative path with no empty, `.` or `..` segment, or `.` for the root
6
+ // directory -- or, under `paths: 'keys'`, any non-empty name. A file is `data`
7
+ // under `mode`, 0o644 if not given: text is kept as text, and stands for its
8
+ // UTF-8. A symlink's or a hard link's target is `linkname`, a hard link's
9
+ // spelled as the name of an entry in the same tree. A directory adds nothing,
10
+ // as git keeps none; what is not given a directory entry is implied. `data`
11
+ // and `linkname` are read only where the type uses them; a mode given is
12
+ // checked on any entry, but only a file's is compared, as only a file's is
13
+ // kept by git. A fifo or a device is refused, as is a name, link target or
14
+ // text that is not well-formed Unicode, since git could hold none of them.
15
+ export interface TreeEntry {
16
+ name: string
17
+ type?: 'file' | 'contiguous-file' | 'directory' | 'symlink' | 'hardlink' | 'fifo' | 'character-device' | 'block-device'
18
+ mode?: number
19
+ linkname?: string
20
+ data?: string | Uint8Array
21
+ }
22
+
23
+ // A file or a link as its tree holds it: what git keeps of one. A hard link
24
+ // is a copy of what it names. `data` is the entry's own, text or bytes, not a
25
+ // copy; text and the bytes of its UTF-8 are the same content.
26
+ export type TreeFile =
27
+ | { path: string; type: 'file'; mode: number; data: string | Uint8Array }
28
+ | { path: string; type: 'symlink'; target: string }
29
+
30
+ // git's statuses, A, D, M, R and T.
31
+ export type Status = 'added' | 'deleted' | 'modified' | 'renamed' | 'type-changed'
32
+
33
+ // One path that differs, and what became of it. `old` is null for an added
34
+ // file and `new` for a deleted one; a renamed file has both, under their
35
+ // two paths. `similarity` is a rename's, as git's similarity index: the
36
+ // percentage of the new file the old one accounts for, 100 where the content
37
+ // is unchanged; null for anything else.
38
+ export interface FileStatus {
39
+ status: Status
40
+ old: TreeFile | null
41
+ new: TreeFile | null
42
+ similarity: number | null
43
+ }
44
+
45
+ // A status with what happened to the content: `blocks`, @preventive/diff's
46
+ // change set from the old side to the new, where a file's content is its data
47
+ // and a link's its target, and a side that is not there is empty. `insertions`
48
+ // and `deletions` count its lines, as --numstat does. Content is text when it
49
+ // is UTF-8 with no NUL; where either side is not, `binary` is true, and there
50
+ // are no blocks and no counts.
51
+ export interface FileChange extends FileStatus {
52
+ binary: boolean
53
+ blocks: Block[]
54
+ insertions: number | null
55
+ deletions: number | null
56
+ }
57
+
58
+ // How a change's lines are found, as diff's options: `horizon` is how far a
59
+ // run of lines free to move may go into the lines the two files share at
60
+ // either end -- without limit unless given, as git has it, or 0 to match
61
+ // `diff -U0` and the normal format, the context to match `diff -u` (diff's
62
+ // --horizon-lines); `whitespace` (-w as 'all', -b as 'change', -Z as
63
+ // 'trailing') and `ignoreCase` (-i) say which lines are the same, so that a
64
+ // file changed in nothing else has no blocks.
65
+ export interface LineOptions {
66
+ horizon?: number
67
+ whitespace?: 'none' | 'all' | 'change' | 'trailing'
68
+ ignoreCase?: boolean
69
+ }
70
+
71
+ // `paths: 'keys'` takes names as keys, as a source map spells them
72
+ // (`webpack://app/a.js`, `../a.js`), and not as paths of a tree that must hold
73
+ // together. `renames` is git's -M: the least similarity, as a percentage
74
+ // above 0, at which a deleted file and an added one are one file renamed;
75
+ // `true` is git's default of 50, `false` finds none, and 100 only those whose
76
+ // content is unchanged. `renameGroup`, given, keeps renames within a group:
77
+ // files are paired only where it gives their paths the same group, a
78
+ // package's for one.
79
+ //
80
+ // `renamesByPath`, unless false, also takes a deleted file and an added one
81
+ // for a rename however little alike, where their paths say so: where they
82
+ // differ only by `lib` for `src` or a TypeScript extension for its JavaScript
83
+ // one (`.ts`, `.mts`, `.cts`, `.tsx` for `.js`, `.mjs`, `.cjs`, `.jsx`), or
84
+ // both, the best scoring of a file's such partners; and, as triage's Compare
85
+ // pairs them, where they share a name less its extension, the extension's
86
+ // family (scripts, `.js` to `.tsx`; styles, `.css` `.scss` `.sass` `.less`)
87
+ // and their directories but for one -- not a package's name under
88
+ // node_modules -- each the other's only such partner. Either gives way only
89
+ // to a candidate that is a rename by itself and scores at least halfway from
90
+ // the pair's score to 100%. git has no such pass, so false is git's own -M.
91
+ //
92
+ // `renameLimit` is git's diff.renameLimit: when more deleted files times added
93
+ // files are left, in a group, after the renames that need no scoring than its
94
+ // square, the rest are not looked for. 1000 unless given; Infinity for no
95
+ // limit. `lines: false` gives statuses alone, their lines found by
96
+ // formatPatch when wanted.
97
+ export interface CompareOptions extends LineOptions {
98
+ paths?: 'tree' | 'keys'
99
+ renames?: boolean | number
100
+ renamesByPath?: boolean
101
+ renameGroup?: (path: string) => unknown
102
+ renameLimit?: number
103
+ lines?: boolean
104
+ }
105
+
106
+ // Every path the trees do not have the same, in git's order: by path in code
107
+ // point order, a rename at its new one. A file is the same when its content
108
+ // and its mode are, a link when its target is. Throws a CompareError for a
109
+ // tree that does not hold together, naming the path and which tree. A
110
+ // `lines` not known to be true or false types the result as either.
111
+ export function compareTrees(a: Iterable<TreeEntry>, b: Iterable<TreeEntry>, options: CompareOptions & { lines: false }): FileStatus[]
112
+ export function compareTrees(a: Iterable<TreeEntry>, b: Iterable<TreeEntry>, options?: CompareOptions & { lines?: true }): FileChange[]
113
+ export function compareTrees(a: Iterable<TreeEntry>, b: Iterable<TreeEntry>, options?: CompareOptions): FileStatus[] | FileChange[]
114
+
115
+ // The changes as `git diff` writes them, without its `index` lines, with
116
+ // `context` lines around each change, 3 unless given. A file's mode is
117
+ // written in full, where git would only have 644 or 755. The hunks are each
118
+ // change's own blocks, found under the line options given here, which must
119
+ // carry its old content to its new under them, or @preventive/diff's
120
+ // DiffError is thrown; a change without blocks has them found here.
121
+ export function formatPatch(changes: Iterable<FileStatus | FileChange>, options?: LineOptions & { context?: number }): string
122
+
123
+ // The ranges `[from, to)` that changed within a removed line and the added
124
+ // line it pairs with, word by word (runs of letters and digits, whitespace,
125
+ // any other character), as the shortest edit script pairLines weighs lines by
126
+ // finds them: every character that changed is in one, and two split by
127
+ // whitespace alone are merged, taking it in. None where the lines are the
128
+ // same; null, no word view, where they are mostly replaced, or past 2000
129
+ // characters or 800 words, so that all of either line is taken as changed.
130
+ export function wordRanges(left: string, right: string): { a: [number, number][]; b: [number, number][] } | null
131
+
132
+ // A change's removed lines paired with its added lines, in order: two lines
133
+ // pair only where wordRanges gives them ranges (a word in common, at most six
134
+ // in ten of either changed), the most such pairs, and of those the most alike.
135
+ // Where weighing every pair takes too long (many long lines alike), each
136
+ // removed line in turn takes the most alike of the next six added lines.
137
+ // `[i, j]` indices into the two lists; none where either holds more than 200
138
+ // lines.
139
+ export function pairLines(olds: readonly string[], news: readonly string[]): [number, number][]
140
+
141
+ // Whether diff -w, as `blocks` were found with it, keeps as unchanged a line
142
+ // whose blanks between the same two characters (or before the first or after
143
+ // the last) changed and mix more than one kind, as spaces turned to tabs. True
144
+ // too where `blocks` don't carry `before` to `after` under -w, as such blocks
145
+ // may hide anything.
146
+ export function hidesMixedWhitespace(before: string, after: string, blocks: readonly Pick<Block, 'a0' | 'a1' | 'b0' | 'b1'>[]): boolean
147
+
148
+ // A rename as `git diff --stat` writes one, in parts for a view to color: the
149
+ // directories both share (`head`), what changed (`from`, `to`), down to the
150
+ // extension where that is all unless `extension` is false, and the rest (`tail`).
151
+ export function renameParts(from: string, to: string, options?: { extension?: boolean }): { head: string; from: string; to: string; tail: string }
152
+
153
+ // The directory one tree's files sit under in the other: `base: ''` and
154
+ // `other` the directory where the old tree's `a.js` is the new one's
155
+ // `src/a.js`, or the other way round.
156
+ export interface Root { base: string; other: string }
157
+
158
+ // The root between two trees, or null, and each old path as the new tree lays
159
+ // it out: through the root where the new tree has it there, else as it is where
160
+ // it has that, else through the root clear of every path taken, a `../` further
161
+ // out. A root is taken where it pairs more files than none, and at least half
162
+ // the files of the side without the directory; a tie either way is none.
163
+ export interface Layout { root: Root | null; paths: Map<string, string> }
164
+ export function treeLayout(basePaths: Iterable<string>, otherPaths: Iterable<string>): Layout
165
+
166
+ // An old module's directory where the layout placed one of its `files` (paths
167
+ // within it), else moved with the root: one outside it a `../` further out for
168
+ // each level of the root, `.` as it is.
169
+ export function layoutDir(layout: Layout, dir: string, files: Iterable<string>): string
170
+
171
+ // Both trees' package directories by path, either map null, as the new tree
172
+ // lays out its files: the old ones moved with the root, the new ones kept.
173
+ export function layoutPackageDirs(baseDirs: Map<string, string> | null, otherDirs: Map<string, string> | null, layout: Layout): Map<string, string> | null
174
+
175
+ // A view's directory `prefix` with where the root puts it on each side
176
+ // (`./dir/`, `./src/dir/`); a prefix the root doesn't lead is dropped.
177
+ export function rootLabel(root: Root | null, prefix: string): { prefix: string; from: string | null; to: string | null }
178
+
179
+ // `path` is the name at fault as its entry spelled it, and `tree` which tree
180
+ // holds it.
181
+ export class CompareError extends Error {
182
+ constructor(detail: string, path?: string, tree?: 'old' | 'new')
183
+ path: string | undefined
184
+ tree: 'old' | 'new' | undefined
185
+ }
package/compare.js ADDED
@@ -0,0 +1,29 @@
1
+ // Two file trees compared the way `git diff -M` compares two commits, built
2
+ // on @preventive/diff for what changed inside each file and @exodus/bytes for
3
+ // UTF-8. Nothing here reads a disk.
4
+
5
+ // The trees in, every file that differs out, with what became of it --
6
+ // added, deleted, modified, renamed, or changed in type -- and its line
7
+ // change set. A tree is tar's entries, as @preventive/archive unpacks them
8
+ // or a Vfs yields them, or names taken as keys.
9
+ export { compareTrees } from './src/changes.js'
10
+
11
+ // Those changes as the patch git writes, for git apply or patch to carry out.
12
+ export { formatPatch } from './src/patch.js'
13
+
14
+ // Changed lines read word by word: what changed within a line and which removed
15
+ // line was edited into which added one; and whether diff -w hides blanks of
16
+ // more than one kind changing.
17
+ export { pairLines } from './src/pairs.js'
18
+ export { wordRanges } from './src/words.js'
19
+ export { hidesMixedWhitespace } from './src/whitespace.js'
20
+
21
+ // A rename written as `git diff --stat` writes one.
22
+ export { renameParts } from './src/paths.js'
23
+
24
+ // Two trees taken from different roots lined up: the root between them and
25
+ // each old path, directory and package directory as the new tree lays it out.
26
+ export { layoutDir, layoutPackageDirs, rootLabel, treeLayout } from './src/layout.js'
27
+
28
+ // What a tree that does not hold together is refused with.
29
+ export { CompareError } from './src/error.js'
package/names.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ import type { Block } from '@preventive/diff'
2
+
3
+ // How each text is parsed, by the extension of the file it is: `mjs` a
4
+ // module, `cjs` a CommonJS module, any other (`js` unless given) a module
5
+ // where it says so (`import`, `export`) and a script where not.
6
+ // `otherExtension` is the new text's, the old one's unless given.
7
+ export interface RenameOptions {
8
+ extension?: string
9
+ otherExtension?: string
10
+ }
11
+
12
+ // Why a text keeps its own names: it does not parse, or oxc's semantic check
13
+ // finds an error in it (`unparsed`); it reads a name by its spelling (`eval`,
14
+ // `with`: `dynamic`); it holds JSX (`jsx`); outside strict code it declares a
15
+ // function in a block, which there sets its function's variable of that name
16
+ // too (`block-function`); or the names found would not bind as the old ones
17
+ // did, or a check that should never fail did, or anything else went wrong
18
+ // (`unverified`, what failed among the warnings).
19
+ export type RenameReason = 'unparsed' | 'dynamic' | 'jsx' | 'block-function' | 'unverified'
20
+
21
+ // The diff of two JavaScript files minified on both sides with renamed names
22
+ // set aside. The old side is given the new side's names wherever a minifier
23
+ // renamed a variable alike throughout (a declared variable's short ASCII name,
24
+ // not a script's top level, an export's or import's by its own name, or a
25
+ // shorthand property's), checked to bind every name as before by this
26
+ // package's scope analysis and again by oxc's minifier; formatted again where
27
+ // that changes its layout alone and still binds alike; diffed as diff -U0 has
28
+ // it; and the blocks checked to take `before` to the new side (thrown should
29
+ // they not). `reason` says why no names were given; `reads` the properties
30
+ // either text reads names through (`name`, `toString`, `stack`, `callee`,
31
+ // `caller`), sorted, as code reading its names may act otherwise renamed;
32
+ // `marks` the plain diff's blocks that differ in renamed names alone; and
33
+ // `warnings` what a check that should never fail found.
34
+ export function renamedDiff(before: string, after: string, options?: RenameOptions & { whitespace?: 'none' | 'all' | 'change' | 'trailing' }): Promise<{
35
+ before: string
36
+ blocks: Pick<Block, 'a0' | 'a1' | 'b0' | 'b1'>[]
37
+ reason: RenameReason | null
38
+ reads: string[]
39
+ marks: Pick<Block, 'a0' | 'a1' | 'b0' | 'b1'>[]
40
+ warnings: string[]
41
+ }>
42
+
package/names.js ADDED
@@ -0,0 +1,9 @@
1
+ // JavaScript minified on both sides, its renamed names told apart from its
2
+ // changes: the old side given the new side's names wherever a minifier
3
+ // renamed a variable alike throughout, so that the two then diff as they are.
4
+ // Built on oxc-parser and oxc-minify, and oxfmt for renamedDiff, optional
5
+ // peers: on Node loaded on first use, elsewhere oxc with this module.
6
+ // compare.js loads none. Typed and described in names.d.ts.
7
+
8
+ // The diff of two such files with renamed names set aside, checked to hide no change.
9
+ export { renamedDiff } from './src/names/diff.js'
package/package.json CHANGED
@@ -1,6 +1,133 @@
1
1
  {
2
2
  "name": "@preventive/compare",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
4
+ "description": "Two file trees compared as git diff compares them, with their line change sets and the patch git writes; and minified JavaScript compared with its layout and its renamed names set aside, each step checked to hide no change",
5
+ "type": "module",
6
+ "main": "./compare.js",
7
+ "types": "./compare.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./compare.d.ts",
11
+ "default": "./compare.js"
12
+ },
13
+ "./compare.js": {
14
+ "types": "./compare.d.ts",
15
+ "default": "./compare.js"
16
+ },
17
+ "./names.js": {
18
+ "types": "./names.d.ts",
19
+ "default": "./names.js"
20
+ },
21
+ "./pretty.js": {
22
+ "types": "./pretty.d.ts",
23
+ "default": "./pretty.js"
24
+ }
25
+ },
26
+ "imports": {
27
+ "#same-bytes": {
28
+ "node": "./src/same-bytes.node.js",
29
+ "default": "./src/same-bytes.js"
30
+ },
31
+ "#oxc": {
32
+ "node": "./src/oxc.node.js",
33
+ "default": "./src/oxc.js"
34
+ }
35
+ },
36
+ "files": [
37
+ "LICENSE-APACHE",
38
+ "LICENSE-MIT",
39
+ "compare.d.ts",
40
+ "compare.js",
41
+ "names.d.ts",
42
+ "names.js",
43
+ "pretty.d.ts",
44
+ "pretty.js",
45
+ "src/blocks.js",
46
+ "src/changes.js",
47
+ "src/css.js",
48
+ "src/error.js",
49
+ "src/layout.js",
50
+ "src/marked.js",
51
+ "src/names/analyze.js",
52
+ "src/names/binds.js",
53
+ "src/names/check.js",
54
+ "src/names/diff.js",
55
+ "src/names/misread.js",
56
+ "src/names/pairing.js",
57
+ "src/names/rename.js",
58
+ "src/names/syntax.js",
59
+ "src/oxc.js",
60
+ "src/oxc.node.js",
61
+ "src/patch.js",
62
+ "src/pairs.js",
63
+ "src/paths.js",
64
+ "src/peers.js",
65
+ "src/pretty.js",
66
+ "src/quote.js",
67
+ "src/renames.js",
68
+ "src/same-bytes.js",
69
+ "src/same-bytes.node.js",
70
+ "src/script.js",
71
+ "src/similarity.js",
72
+ "src/text.js",
73
+ "src/tree.js",
74
+ "src/whitespace.js",
75
+ "src/words.js"
76
+ ],
77
+ "engines": {
78
+ "node": ">=24.14.0"
79
+ },
80
+ "scripts": {
81
+ "test": "node --test ./tests/*.test.js"
82
+ },
83
+ "license": "MIT OR Apache-2.0",
84
+ "author": {
85
+ "name": "Nikita Skovoroda",
86
+ "email": "chalkerx@gmail.com",
87
+ "url": "https://github.com/ChALkeR"
88
+ },
89
+ "repository": {
90
+ "type": "git",
91
+ "url": "git+https://github.com/PreventiveMeasures/libraries.git",
92
+ "directory": "compare"
93
+ },
94
+ "bugs": {
95
+ "url": "https://github.com/PreventiveMeasures/libraries/issues"
96
+ },
97
+ "homepage": "https://github.com/PreventiveMeasures/libraries/tree/main/compare",
98
+ "publishConfig": {
99
+ "access": "public"
100
+ },
101
+ "dependencies": {
102
+ "@exodus/bytes": "^1.16.0",
103
+ "@preventive/diff": "^1.0.2"
104
+ },
105
+ "peerDependencies": {
106
+ "css-tree": ">=3.2.1",
107
+ "oxc-minify": ">=0.152.0",
108
+ "oxc-parser": ">=0.152.0",
109
+ "oxfmt": ">=0.72.0"
110
+ },
111
+ "peerDependenciesMeta": {
112
+ "css-tree": {
113
+ "optional": true
114
+ },
115
+ "oxc-minify": {
116
+ "optional": true
117
+ },
118
+ "oxc-parser": {
119
+ "optional": true
120
+ },
121
+ "oxfmt": {
122
+ "optional": true
123
+ }
124
+ },
125
+ "devDependencies": {
126
+ "@preventive/archive": "workspace:*",
127
+ "@preventive/vfs": "workspace:*",
128
+ "css-tree": "^3.2.1",
129
+ "oxc-minify": "^0.152.0",
130
+ "oxc-parser": "^0.152.0",
131
+ "oxfmt": "^0.72.0"
132
+ }
133
+ }
package/pretty.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ // `text` formatted by oxfmt as a file with `extension`, changing what is written
2
+ // no more than layout needs (keys keep their quotes, no trailing commas or arrow
3
+ // parentheses are added, template literals stay), the copy used only where checks
4
+ // of oxfmt's output, apart from oxfmt, find it changes the layout alone:
5
+ // - a script's (`js`, `mjs`, `cjs`, `jsx`, `ts`, `mts`, `cts`, `tsx`) syntax tree as
6
+ // oxc parses it, its comments and its text;
7
+ // - a style sheet's (`css`) tree as css-tree, an optional peer, parses it, but for
8
+ // the case of what CSS reads in any case and zeros in a number that change neither
9
+ // its value nor its type (`.50` as `0.5`, but not `1.0` as `1`, an integer);
10
+ // - JSON's strings and comments as written.
11
+ // Otherwise an error: no check for the kind of file (`unsupported`, CSS without
12
+ // css-tree), it does not parse (`unformattable`) or would change more
13
+ // (`pretty-mismatch`). Rejects only where the optional oxfmt peer is missing.
14
+ export function format(text: string, extension: string): Promise<{ code: string } | { error: 'unsupported' | 'unformattable' | 'pretty-mismatch' }>
package/pretty.js ADDED
@@ -0,0 +1,4 @@
1
+ // Minified code formatted for reading, its copy checked to differ from the
2
+ // file in layout alone. oxfmt formats, oxc-parser and css-tree check: optional
3
+ // peers loaded on first use. Typed and described in pretty.d.ts.
4
+ export { format } from './src/pretty.js'