@crashbytes/react-version-compare 1.0.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.
- package/LICENSE +201 -0
- package/README.md +176 -0
- package/dist/cjs/index.js +148 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.js +143 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/package.json +86 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# React Compare
|
|
2
|
+
|
|
3
|
+
A React component for comparing strings and arrays with precise word-level and item-level highlighting of differences.
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/js/react-version-compare)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- 🎯 **Precise Highlighting**: Only highlights the actual differences, not entire lines
|
|
11
|
+
- 📝 **String Comparison**: Word-level diff for text content
|
|
12
|
+
- 📋 **Array Comparison**: Item-level diff for arrays of strings
|
|
13
|
+
- 🎨 **Clean Visual Design**: Clear red/green highlighting for changes
|
|
14
|
+
- 📱 **Responsive**: Works on desktop and mobile devices
|
|
15
|
+
- ⚡ **TypeScript Support**: Full TypeScript definitions included
|
|
16
|
+
- 🎛️ **Multiple Views**: Side-by-side or inline comparison modes
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install react-version-compare
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
yarn add react-version-compare
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Basic Usage
|
|
29
|
+
|
|
30
|
+
### String Comparison
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import React from 'react';
|
|
34
|
+
import Compare from 'react-version-compare';
|
|
35
|
+
import 'react-version-compare/dist/styles.css';
|
|
36
|
+
|
|
37
|
+
const App = () => {
|
|
38
|
+
const original = 'I am Captain Kirk, Captain of the USS Enterprise.';
|
|
39
|
+
const modified = 'I am Captain Picard, Captain of the USS Enterprise.';
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<Compare
|
|
43
|
+
original={original}
|
|
44
|
+
modified={modified}
|
|
45
|
+
/>
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**Result**: Only "Kirk" (in red) and "Picard" (in green) will be highlighted.
|
|
51
|
+
|
|
52
|
+
### Array Comparison
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
const originalArray = [
|
|
56
|
+
'First item',
|
|
57
|
+
'Second item',
|
|
58
|
+
'Third item'
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
const modifiedArray = [
|
|
62
|
+
'First item',
|
|
63
|
+
'Modified second item',
|
|
64
|
+
'Third item'
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
<Compare
|
|
68
|
+
original={originalArray}
|
|
69
|
+
modified={modifiedArray}
|
|
70
|
+
/>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## API Reference
|
|
74
|
+
|
|
75
|
+
### Props
|
|
76
|
+
|
|
77
|
+
| Prop | Type | Default | Description |
|
|
78
|
+
|------|------|---------|-------------|
|
|
79
|
+
| `original` | `string \| string[]` | **required** | The original content |
|
|
80
|
+
| `modified` | `string \| string[]` | **required** | The modified content |
|
|
81
|
+
| `viewMode` | `'side-by-side' \| 'inline'` | `'side-by-side'` | How to display the comparison |
|
|
82
|
+
| `className` | `string` | `''` | Custom CSS class name |
|
|
83
|
+
|
|
84
|
+
## Examples
|
|
85
|
+
|
|
86
|
+
### Precise Word-Level Highlighting
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
// Example 1: Minimal changes
|
|
90
|
+
const original = 'I am Captain Kirk, Captain of the USS Enterprise.';
|
|
91
|
+
const modified = 'I am Captain Picard, Captain of the USS Enterprise.';
|
|
92
|
+
// Only "Kirk" → "Picard" highlighted
|
|
93
|
+
|
|
94
|
+
// Example 2: Multiple changes
|
|
95
|
+
const original = 'I am Captain Kirk, Captain of the USS Enterprise.';
|
|
96
|
+
const modified = 'I am Commander Benjamin Sisko, Commander of Deep Space 9.';
|
|
97
|
+
// Highlights: "Captain Kirk, Captain of the USS Enterprise" → "Commander Benjamin Sisko, Commander of Deep Space 9"
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Array Comparison
|
|
101
|
+
|
|
102
|
+
```tsx
|
|
103
|
+
const original = ['Item A', 'Item B', 'Item C'];
|
|
104
|
+
const modified = ['Item A', 'Modified Item B', 'Item C', 'Item D'];
|
|
105
|
+
|
|
106
|
+
<Compare original={original} modified={modified} />
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Inline View
|
|
110
|
+
|
|
111
|
+
```tsx
|
|
112
|
+
<Compare
|
|
113
|
+
original="Original text"
|
|
114
|
+
modified="Modified text"
|
|
115
|
+
viewMode="inline"
|
|
116
|
+
/>
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Styling
|
|
120
|
+
|
|
121
|
+
The component uses CSS classes that you can customize:
|
|
122
|
+
|
|
123
|
+
```css
|
|
124
|
+
.diff-removed {
|
|
125
|
+
/* Styling for removed content (red) */
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
.diff-added {
|
|
129
|
+
/* Styling for added content (green) */
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
.diff-unchanged {
|
|
133
|
+
/* Styling for unchanged content */
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Use Cases
|
|
138
|
+
|
|
139
|
+
- **Document revisions**: Compare different versions of text
|
|
140
|
+
- **Code reviews**: Highlight changes in code snippets
|
|
141
|
+
- **Content management**: Show edits in articles or posts
|
|
142
|
+
- **Data comparison**: Compare lists or arrays of items
|
|
143
|
+
- **Translation work**: Compare original and translated text
|
|
144
|
+
|
|
145
|
+
## How It Works
|
|
146
|
+
|
|
147
|
+
- **String comparison**: Uses word-level diffing to identify precise changes
|
|
148
|
+
- **Array comparison**: Compares items by position and content
|
|
149
|
+
- **Smart highlighting**: Only highlights actual differences, not entire lines
|
|
150
|
+
- **Type safety**: Ensures both inputs are the same type (both strings or both arrays)
|
|
151
|
+
|
|
152
|
+
## License
|
|
153
|
+
|
|
154
|
+
Apache License 2.0 - see the [LICENSE](LICENSE) file for details.
|
|
155
|
+
|
|
156
|
+
## Contributing
|
|
157
|
+
|
|
158
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
159
|
+
|
|
160
|
+
## Support
|
|
161
|
+
|
|
162
|
+
If you encounter any issues or have questions, please [open an issue](https://github.com/CrashBytes/react-version-compare/issues) on GitHub.
|
|
163
|
+
|
|
164
|
+
## Changelog
|
|
165
|
+
|
|
166
|
+
See [CHANGELOG.md](./CHANGELOG.md) for release history.
|
|
167
|
+
|
|
168
|
+
## Versioning
|
|
169
|
+
|
|
170
|
+
To bump the version, use [npm version](https://docs.npmjs.com/cli/v10/commands/npm-version):
|
|
171
|
+
|
|
172
|
+
```sh
|
|
173
|
+
npm version patch # or 'minor' or 'major'
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
This will update `package.json`, create a git tag, and (optionally) commit the change.
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
var diff = require('diff');
|
|
7
|
+
|
|
8
|
+
const Compare = ({
|
|
9
|
+
original,
|
|
10
|
+
modified,
|
|
11
|
+
className = '',
|
|
12
|
+
viewMode = 'side-by-side',
|
|
13
|
+
caseSensitive = true // <-- Default true
|
|
14
|
+
}) => {
|
|
15
|
+
// Handle string comparison
|
|
16
|
+
const renderStringDiff = (orig, mod) => {
|
|
17
|
+
const diff$1 = diff.diffWords(orig, mod, {
|
|
18
|
+
ignoreCase: !caseSensitive
|
|
19
|
+
});
|
|
20
|
+
const originalParts = [];
|
|
21
|
+
const modifiedParts = [];
|
|
22
|
+
for (const part of diff$1) {
|
|
23
|
+
if (part.removed) {
|
|
24
|
+
originalParts.push(jsxRuntime.jsx("span", {
|
|
25
|
+
className: "diff-removed",
|
|
26
|
+
children: part.value
|
|
27
|
+
}, originalParts.length));
|
|
28
|
+
} else if (part.added) {
|
|
29
|
+
modifiedParts.push(jsxRuntime.jsx("span", {
|
|
30
|
+
className: "diff-added",
|
|
31
|
+
children: part.value
|
|
32
|
+
}, modifiedParts.length));
|
|
33
|
+
} else {
|
|
34
|
+
originalParts.push(jsxRuntime.jsx("span", {
|
|
35
|
+
className: "diff-unchanged",
|
|
36
|
+
children: part.value
|
|
37
|
+
}, originalParts.length));
|
|
38
|
+
modifiedParts.push(jsxRuntime.jsx("span", {
|
|
39
|
+
className: "diff-unchanged",
|
|
40
|
+
children: part.value
|
|
41
|
+
}, modifiedParts.length));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
originalParts,
|
|
46
|
+
modifiedParts
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
// Handle array comparison
|
|
50
|
+
const renderArrayDiff = (orig, mod) => {
|
|
51
|
+
let origArr = orig;
|
|
52
|
+
let modArr = mod;
|
|
53
|
+
if (!caseSensitive) {
|
|
54
|
+
origArr = orig.map(s => typeof s === 'string' ? s.toLowerCase() : s);
|
|
55
|
+
modArr = mod.map(s => typeof s === 'string' ? s.toLowerCase() : s);
|
|
56
|
+
}
|
|
57
|
+
const diff$1 = diff.diffArrays(origArr, modArr);
|
|
58
|
+
const originalParts = [];
|
|
59
|
+
const modifiedParts = [];
|
|
60
|
+
for (const part of diff$1) {
|
|
61
|
+
if (part.removed) {
|
|
62
|
+
part.value.forEach((item, index) => {
|
|
63
|
+
originalParts.push(jsxRuntime.jsx("div", {
|
|
64
|
+
className: "diff-removed-line",
|
|
65
|
+
children: orig[origArr.indexOf(item, originalParts.length)] ?? item
|
|
66
|
+
}, `${originalParts.length}-${index}`));
|
|
67
|
+
});
|
|
68
|
+
} else if (part.added) {
|
|
69
|
+
part.value.forEach((item, index) => {
|
|
70
|
+
modifiedParts.push(jsxRuntime.jsx("div", {
|
|
71
|
+
className: "diff-added-line",
|
|
72
|
+
children: mod[modArr.indexOf(item, modifiedParts.length)] ?? item
|
|
73
|
+
}, `${modifiedParts.length}-${index}`));
|
|
74
|
+
});
|
|
75
|
+
} else {
|
|
76
|
+
part.value.forEach((item, index) => {
|
|
77
|
+
originalParts.push(jsxRuntime.jsx("div", {
|
|
78
|
+
className: "diff-unchanged-line",
|
|
79
|
+
children: orig[origArr.indexOf(item, originalParts.length)] ?? item
|
|
80
|
+
}, `${originalParts.length}-${index}`));
|
|
81
|
+
modifiedParts.push(jsxRuntime.jsx("div", {
|
|
82
|
+
className: "diff-unchanged-line",
|
|
83
|
+
children: mod[modArr.indexOf(item, modifiedParts.length)] ?? item
|
|
84
|
+
}, `${modifiedParts.length}-${index}`));
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
originalParts,
|
|
90
|
+
modifiedParts
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
const isStringComparison = typeof original === 'string' && typeof modified === 'string';
|
|
94
|
+
const isArrayComparison = Array.isArray(original) && Array.isArray(modified);
|
|
95
|
+
if (!isStringComparison && !isArrayComparison) {
|
|
96
|
+
return jsxRuntime.jsx("div", {
|
|
97
|
+
className: `compare-error ${className}`,
|
|
98
|
+
children: "Error: Both inputs must be either strings or arrays of strings"
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
let originalParts, modifiedParts;
|
|
102
|
+
if (isStringComparison) {
|
|
103
|
+
({
|
|
104
|
+
originalParts,
|
|
105
|
+
modifiedParts
|
|
106
|
+
} = renderStringDiff(original, modified));
|
|
107
|
+
} else {
|
|
108
|
+
({
|
|
109
|
+
originalParts,
|
|
110
|
+
modifiedParts
|
|
111
|
+
} = renderArrayDiff(original, modified));
|
|
112
|
+
}
|
|
113
|
+
if (viewMode === 'inline') {
|
|
114
|
+
return jsxRuntime.jsx("div", {
|
|
115
|
+
className: `compare-inline ${className}`,
|
|
116
|
+
children: jsxRuntime.jsxs("div", {
|
|
117
|
+
className: "compare-content",
|
|
118
|
+
children: [originalParts, modifiedParts]
|
|
119
|
+
})
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return jsxRuntime.jsxs("div", {
|
|
123
|
+
className: `compare-side-by-side ${className}`,
|
|
124
|
+
children: [jsxRuntime.jsxs("div", {
|
|
125
|
+
className: "compare-panel",
|
|
126
|
+
children: [jsxRuntime.jsx("div", {
|
|
127
|
+
className: "compare-header original-header",
|
|
128
|
+
children: "Original"
|
|
129
|
+
}), jsxRuntime.jsx("div", {
|
|
130
|
+
className: "compare-content original-content",
|
|
131
|
+
children: originalParts
|
|
132
|
+
})]
|
|
133
|
+
}), jsxRuntime.jsxs("div", {
|
|
134
|
+
className: "compare-panel",
|
|
135
|
+
children: [jsxRuntime.jsx("div", {
|
|
136
|
+
className: "compare-header modified-header",
|
|
137
|
+
children: "Modified"
|
|
138
|
+
}), jsxRuntime.jsx("div", {
|
|
139
|
+
className: "compare-content modified-content",
|
|
140
|
+
children: modifiedParts
|
|
141
|
+
})]
|
|
142
|
+
})]
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
exports.Compare = Compare;
|
|
147
|
+
exports.default = Compare;
|
|
148
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/components/Compare.tsx"],"sourcesContent":["import { diffWords, diffArrays } from 'diff';\nimport React from 'react';\n\n// Types\nexport interface CompareProps {\n original: string | string[];\n modified: string | string[];\n className?: string;\n viewMode?: 'side-by-side' | 'inline';\n caseSensitive?: boolean; // <-- Added\n}\n\nexport const Compare: React.FC<CompareProps> = ({\n original,\n modified,\n className = '',\n viewMode = 'side-by-side',\n caseSensitive = true, // <-- Default true\n}) => {\n // Handle string comparison\n const renderStringDiff = (orig: string, mod: string) => {\n const diff = diffWords(orig, mod, { ignoreCase: !caseSensitive });\n const originalParts: any[] = [];\n const modifiedParts: any[] = [];\n for (const part of diff) {\n if (part.removed) {\n originalParts.push(\n <span key={originalParts.length} className=\"diff-removed\">\n {part.value}\n </span>\n );\n } else if (part.added) {\n modifiedParts.push(\n <span key={modifiedParts.length} className=\"diff-added\">\n {part.value}\n </span>\n );\n } else {\n originalParts.push(\n <span key={originalParts.length} className=\"diff-unchanged\">\n {part.value}\n </span>\n );\n modifiedParts.push(\n <span key={modifiedParts.length} className=\"diff-unchanged\">\n {part.value}\n </span>\n );\n }\n }\n return { originalParts, modifiedParts };\n };\n\n // Handle array comparison\n const renderArrayDiff = (orig: string[], mod: string[]) => {\n let origArr = orig;\n let modArr = mod;\n if (!caseSensitive) {\n origArr = orig.map((s) => (typeof s === 'string' ? s.toLowerCase() : s));\n modArr = mod.map((s) => (typeof s === 'string' ? s.toLowerCase() : s));\n }\n const diff = diffArrays(origArr, modArr);\n const originalParts: any[] = [];\n const modifiedParts: any[] = [];\n for (const part of diff) {\n if (part.removed) {\n part.value.forEach((item: string, index: number) => {\n originalParts.push(\n <div key={`${originalParts.length}-${index}`} className=\"diff-removed-line\">\n {orig[origArr.indexOf(item, originalParts.length)] ?? item}\n </div>\n );\n });\n } else if (part.added) {\n part.value.forEach((item: string, index: number) => {\n modifiedParts.push(\n <div key={`${modifiedParts.length}-${index}`} className=\"diff-added-line\">\n {mod[modArr.indexOf(item, modifiedParts.length)] ?? item}\n </div>\n );\n });\n } else {\n part.value.forEach((item: string, index: number) => {\n originalParts.push(\n <div key={`${originalParts.length}-${index}`} className=\"diff-unchanged-line\">\n {orig[origArr.indexOf(item, originalParts.length)] ?? item}\n </div>\n );\n modifiedParts.push(\n <div key={`${modifiedParts.length}-${index}`} className=\"diff-unchanged-line\">\n {mod[modArr.indexOf(item, modifiedParts.length)] ?? item}\n </div>\n );\n });\n }\n }\n return { originalParts, modifiedParts };\n };\n\n const isStringComparison = typeof original === 'string' && typeof modified === 'string';\n const isArrayComparison = Array.isArray(original) && Array.isArray(modified);\n\n if (!isStringComparison && !isArrayComparison) {\n return (\n <div className={`compare-error ${className}`}>\n Error: Both inputs must be either strings or arrays of strings\n </div>\n );\n }\n\n let originalParts: any[], modifiedParts: any[];\n if (isStringComparison) {\n ({ originalParts, modifiedParts } = renderStringDiff(original as string, modified as string));\n } else {\n ({ originalParts, modifiedParts } = renderArrayDiff(original as string[], modified as string[]));\n }\n\n if (viewMode === 'inline') {\n return (\n <div className={`compare-inline ${className}`}>\n <div className=\"compare-content\">\n {originalParts}\n {modifiedParts}\n </div>\n </div>\n );\n }\n\n return (\n <div className={`compare-side-by-side ${className}`}>\n <div className=\"compare-panel\">\n <div className=\"compare-header original-header\">Original</div>\n <div className=\"compare-content original-content\">\n {originalParts}\n </div>\n </div>\n <div className=\"compare-panel\">\n <div className=\"compare-header modified-header\">Modified</div>\n <div className=\"compare-content modified-content\">\n {modifiedParts}\n </div>\n </div>\n </div>\n );\n};\n\nexport default Compare;"],"names":["Compare","original","modified","className","viewMode","caseSensitive","renderStringDiff","orig","mod","diff","diffWords","ignoreCase","originalParts","modifiedParts","part","removed","push","_jsx","children","value","length","added","renderArrayDiff","origArr","modArr","map","s","toLowerCase","diffArrays","forEach","item","index","indexOf","isStringComparison","isArrayComparison","Array","isArray","_j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d,QAAAA,SAAS,EAAC,gCAAgC;;QAAe,EAC9Dc,cAAA,CAAA,KAAA,EAAA;AAAKd,QAAAA,SAAS,EAAC,kCAAkC;AAAAe,QAAAA,QAAA,EAC9CN;AACG,OAAA,CAAA;AAAA,KAAA,CACF,EACNyB,eAAK,CAAA,KAAA,EAAA;AAAAlC,MAAAA,SAAS,EAAC,eAAe;AAAAe,MAAAA,QAAA,EAAA,CAC5BD,cAAK,CAAA,KAAA,EAAA;AAAAd,QAAAA,SAAS,EAAC,gCAAgC;;QAAe,EAC9Dc,cAAA,CAAA,KAAA,EAAA;AAAKd,QAAAA,SAAS,EAAC,kCAAkC;AAAAe,QAAAA,QAAA,EAC9CL;AACG,OAAA,CAAA;AAAA,KAAA,CACF;AACF,GAAA,CAAA;AAEV;;;;;"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
2
|
+
import { diffWords, diffArrays } from 'diff';
|
|
3
|
+
|
|
4
|
+
const Compare = ({
|
|
5
|
+
original,
|
|
6
|
+
modified,
|
|
7
|
+
className = '',
|
|
8
|
+
viewMode = 'side-by-side',
|
|
9
|
+
caseSensitive = true // <-- Default true
|
|
10
|
+
}) => {
|
|
11
|
+
// Handle string comparison
|
|
12
|
+
const renderStringDiff = (orig, mod) => {
|
|
13
|
+
const diff = diffWords(orig, mod, {
|
|
14
|
+
ignoreCase: !caseSensitive
|
|
15
|
+
});
|
|
16
|
+
const originalParts = [];
|
|
17
|
+
const modifiedParts = [];
|
|
18
|
+
for (const part of diff) {
|
|
19
|
+
if (part.removed) {
|
|
20
|
+
originalParts.push(jsx("span", {
|
|
21
|
+
className: "diff-removed",
|
|
22
|
+
children: part.value
|
|
23
|
+
}, originalParts.length));
|
|
24
|
+
} else if (part.added) {
|
|
25
|
+
modifiedParts.push(jsx("span", {
|
|
26
|
+
className: "diff-added",
|
|
27
|
+
children: part.value
|
|
28
|
+
}, modifiedParts.length));
|
|
29
|
+
} else {
|
|
30
|
+
originalParts.push(jsx("span", {
|
|
31
|
+
className: "diff-unchanged",
|
|
32
|
+
children: part.value
|
|
33
|
+
}, originalParts.length));
|
|
34
|
+
modifiedParts.push(jsx("span", {
|
|
35
|
+
className: "diff-unchanged",
|
|
36
|
+
children: part.value
|
|
37
|
+
}, modifiedParts.length));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
originalParts,
|
|
42
|
+
modifiedParts
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
// Handle array comparison
|
|
46
|
+
const renderArrayDiff = (orig, mod) => {
|
|
47
|
+
let origArr = orig;
|
|
48
|
+
let modArr = mod;
|
|
49
|
+
if (!caseSensitive) {
|
|
50
|
+
origArr = orig.map(s => typeof s === 'string' ? s.toLowerCase() : s);
|
|
51
|
+
modArr = mod.map(s => typeof s === 'string' ? s.toLowerCase() : s);
|
|
52
|
+
}
|
|
53
|
+
const diff = diffArrays(origArr, modArr);
|
|
54
|
+
const originalParts = [];
|
|
55
|
+
const modifiedParts = [];
|
|
56
|
+
for (const part of diff) {
|
|
57
|
+
if (part.removed) {
|
|
58
|
+
part.value.forEach((item, index) => {
|
|
59
|
+
originalParts.push(jsx("div", {
|
|
60
|
+
className: "diff-removed-line",
|
|
61
|
+
children: orig[origArr.indexOf(item, originalParts.length)] ?? item
|
|
62
|
+
}, `${originalParts.length}-${index}`));
|
|
63
|
+
});
|
|
64
|
+
} else if (part.added) {
|
|
65
|
+
part.value.forEach((item, index) => {
|
|
66
|
+
modifiedParts.push(jsx("div", {
|
|
67
|
+
className: "diff-added-line",
|
|
68
|
+
children: mod[modArr.indexOf(item, modifiedParts.length)] ?? item
|
|
69
|
+
}, `${modifiedParts.length}-${index}`));
|
|
70
|
+
});
|
|
71
|
+
} else {
|
|
72
|
+
part.value.forEach((item, index) => {
|
|
73
|
+
originalParts.push(jsx("div", {
|
|
74
|
+
className: "diff-unchanged-line",
|
|
75
|
+
children: orig[origArr.indexOf(item, originalParts.length)] ?? item
|
|
76
|
+
}, `${originalParts.length}-${index}`));
|
|
77
|
+
modifiedParts.push(jsx("div", {
|
|
78
|
+
className: "diff-unchanged-line",
|
|
79
|
+
children: mod[modArr.indexOf(item, modifiedParts.length)] ?? item
|
|
80
|
+
}, `${modifiedParts.length}-${index}`));
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
originalParts,
|
|
86
|
+
modifiedParts
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
const isStringComparison = typeof original === 'string' && typeof modified === 'string';
|
|
90
|
+
const isArrayComparison = Array.isArray(original) && Array.isArray(modified);
|
|
91
|
+
if (!isStringComparison && !isArrayComparison) {
|
|
92
|
+
return jsx("div", {
|
|
93
|
+
className: `compare-error ${className}`,
|
|
94
|
+
children: "Error: Both inputs must be either strings or arrays of strings"
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
let originalParts, modifiedParts;
|
|
98
|
+
if (isStringComparison) {
|
|
99
|
+
({
|
|
100
|
+
originalParts,
|
|
101
|
+
modifiedParts
|
|
102
|
+
} = renderStringDiff(original, modified));
|
|
103
|
+
} else {
|
|
104
|
+
({
|
|
105
|
+
originalParts,
|
|
106
|
+
modifiedParts
|
|
107
|
+
} = renderArrayDiff(original, modified));
|
|
108
|
+
}
|
|
109
|
+
if (viewMode === 'inline') {
|
|
110
|
+
return jsx("div", {
|
|
111
|
+
className: `compare-inline ${className}`,
|
|
112
|
+
children: jsxs("div", {
|
|
113
|
+
className: "compare-content",
|
|
114
|
+
children: [originalParts, modifiedParts]
|
|
115
|
+
})
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return jsxs("div", {
|
|
119
|
+
className: `compare-side-by-side ${className}`,
|
|
120
|
+
children: [jsxs("div", {
|
|
121
|
+
className: "compare-panel",
|
|
122
|
+
children: [jsx("div", {
|
|
123
|
+
className: "compare-header original-header",
|
|
124
|
+
children: "Original"
|
|
125
|
+
}), jsx("div", {
|
|
126
|
+
className: "compare-content original-content",
|
|
127
|
+
children: originalParts
|
|
128
|
+
})]
|
|
129
|
+
}), jsxs("div", {
|
|
130
|
+
className: "compare-panel",
|
|
131
|
+
children: [jsx("div", {
|
|
132
|
+
className: "compare-header modified-header",
|
|
133
|
+
children: "Modified"
|
|
134
|
+
}), jsx("div", {
|
|
135
|
+
className: "compare-content modified-content",
|
|
136
|
+
children: modifiedParts
|
|
137
|
+
})]
|
|
138
|
+
})]
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export { Compare, Compare as default };
|
|
143
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/components/Compare.tsx"],"sourcesContent":["import { diffWords, diffArrays } from 'diff';\nimport React from 'react';\n\n// Types\nexport interface CompareProps {\n original: string | string[];\n modified: string | string[];\n className?: string;\n viewMode?: 'side-by-side' | 'inline';\n caseSensitive?: boolean; // <-- Added\n}\n\nexport const Compare: React.FC<CompareProps> = ({\n original,\n modified,\n className = '',\n viewMode = 'side-by-side',\n caseSensitive = true, // <-- Default true\n}) => {\n // Handle string comparison\n const renderStringDiff = (orig: string, mod: string) => {\n const diff = diffWords(orig, mod, { ignoreCase: !caseSensitive });\n const originalParts: any[] = [];\n const modifiedParts: any[] = [];\n for (const part of diff) {\n if (part.removed) {\n originalParts.push(\n <span key={originalParts.length} className=\"diff-removed\">\n {part.value}\n </span>\n );\n } else if (part.added) {\n modifiedParts.push(\n <span key={modifiedParts.length} className=\"diff-added\">\n {part.value}\n </span>\n );\n } else {\n originalParts.push(\n <span key={originalParts.length} className=\"diff-unchanged\">\n {part.value}\n </span>\n );\n modifiedParts.push(\n <span key={modifiedParts.length} className=\"diff-unchanged\">\n {part.value}\n </span>\n );\n }\n }\n return { originalParts, modifiedParts };\n };\n\n // Handle array comparison\n const renderArrayDiff = (orig: string[], mod: string[]) => {\n let origArr = orig;\n let modArr = mod;\n if (!caseSensitive) {\n origArr = orig.map((s) => (typeof s === 'string' ? s.toLowerCase() : s));\n modArr = mod.map((s) => (typeof s === 'string' ? s.toLowerCase() : s));\n }\n const diff = diffArrays(origArr, modArr);\n const originalParts: any[] = [];\n const modifiedParts: any[] = [];\n for (const part of diff) {\n if (part.removed) {\n part.value.forEach((item: string, index: number) => {\n originalParts.push(\n <div key={`${originalParts.length}-${index}`} className=\"diff-removed-line\">\n {orig[origArr.indexOf(item, originalParts.length)] ?? item}\n </div>\n );\n });\n } else if (part.added) {\n part.value.forEach((item: string, index: number) => {\n modifiedParts.push(\n <div key={`${modifiedParts.length}-${index}`} className=\"diff-added-line\">\n {mod[modArr.indexOf(item, modifiedParts.length)] ?? item}\n </div>\n );\n });\n } else {\n part.value.forEach((item: string, index: number) => {\n originalParts.push(\n <div key={`${originalParts.length}-${index}`} className=\"diff-unchanged-line\">\n {orig[origArr.indexOf(item, originalParts.length)] ?? item}\n </div>\n );\n modifiedParts.push(\n <div key={`${modifiedParts.length}-${index}`} className=\"diff-unchanged-line\">\n {mod[modArr.indexOf(item, modifiedParts.length)] ?? item}\n </div>\n );\n });\n }\n }\n return { originalParts, modifiedParts };\n };\n\n const isStringComparison = typeof original === 'string' && typeof modified === 'string';\n const isArrayComparison = Array.isArray(original) && Array.isArray(modified);\n\n if (!isStringComparison && !isArrayComparison) {\n return (\n <div className={`compare-error ${className}`}>\n Error: Both inputs must be either strings or arrays of strings\n </div>\n );\n }\n\n let originalParts: any[], modifiedParts: any[];\n if (isStringComparison) {\n ({ originalParts, modifiedParts } = renderStringDiff(original as string, modified as string));\n } else {\n ({ originalParts, modifiedParts } = renderArrayDiff(original as string[], modified as string[]));\n }\n\n if (viewMode === 'inline') {\n return (\n <div className={`compare-inline ${className}`}>\n <div className=\"compare-content\">\n {originalParts}\n {modifiedParts}\n </div>\n </div>\n );\n }\n\n return (\n <div className={`compare-side-by-side ${className}`}>\n <div className=\"compare-panel\">\n <div className=\"compare-header original-header\">Original</div>\n <div className=\"compare-content original-content\">\n {originalParts}\n </div>\n </div>\n <div className=\"compare-panel\">\n <div className=\"compare-header modified-header\">Modified</div>\n <div className=\"compare-content modified-content\">\n {modifiedParts}\n </div>\n </div>\n </div>\n );\n};\n\nexport default Compare;"],"names":["Compare","original","modified","className","viewMode","caseSensitive","renderStringDiff","orig","mod","diff","diffWords","ignoreCase","originalParts","modifiedParts","part","removed","push","_jsx","children","value","length","added","renderArrayDiff","origArr","modArr","map","s","toLowerCase","diffArrays","forEach","item","index","indexOf","isStringComparison","isArrayComparison","Array","isArray","_jsxs"],"mappings":";;;AAYO,MAAMA,OAAO,GAA2BA,CAAC;EAC9CC,QAAQ;EACRC,QAAQ;AACRC,EAAAA,SAAS,GAAG,EAAE;AACdC,EAAAA,QAAQ,GAAG,cAAc;EACzBC,aAAa,GAAG,IAAI;AACrB,CAAA,KAAI;AACH;AACA,EAAA,MAAMC,gBAAgB,GAAGA,CAACC,IAAY,EAAEC,GAAW,KAAI;AACrD,IAAA,MAAMC,IAAI,GAAGC,SAAS,CAACH,IAAI,EAAEC,GAAG,EAAE;AAAEG,MAAAA,UAAU,EAAE,CAACN;AAAa,KAAE,CAAC;IACjE,MAAMO,aAAa,GAAU,EAAE;IAC/B,MAAMC,aAAa,GAAU,EAAE;AAC/B,IAAA,KAAK,MAAMC,IAAI,IAAIL,IAAI,EAAE;MACvB,IAAIK,IAAI,CAACC,OAAO,EAAE;AAChBH,QAAAA,aAAa,CAACI,IAAI,CAChBC,GAAiC,CAAA,MAAA,EAAA;AAAAd,UAAAA,SAAS,EAAC,cAAc;UAAAe,QAAA,EACtDJ,IAAI,CAACK;AADG,SAAA,EAAAP,aAAa,CAACQ,MAAM,CAExB,CACR;AACH,OAAC,MAAM,IAAIN,IAAI,CAACO,KAAK,EAAE;AACrBR,QAAAA,aAAa,CAACG,IAAI,CAChBC,GAAiC,CAAA,MAAA,EAAA;AAAAd,UAAAA,SAAS,EAAC,YAAY;UAAAe,QAAA,EAC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A,EAAAmB,IAAA,CAAA,KAAA,EAAA;AAAKlC,QAAAA,SAAS,EAAC,iBAAiB;mBAC7BS,aAAa,EACbC,aAAa;OACV;AAAA,KAAA,CACF;AAEV;EAEA,OACEwB;IAAKlC,SAAS,EAAE,CAAwBA,qBAAAA,EAAAA,SAAS,CAAE,CAAA;AACjDe,IAAAA,QAAA,EAAA,CAAAmB,IAAA,CAAA,KAAA,EAAA;AAAKlC,MAAAA,SAAS,EAAC,eAAe;AAC5Be,MAAAA,QAAA,EAAA,CAAAD,GAAA,CAAA,KAAA,EAAA;AAAKd,QAAAA,SAAS,EAAC,gCAAgC;;QAAe,EAC9Dc,GAAA,CAAA,KAAA,EAAA;AAAKd,QAAAA,SAAS,EAAC,kCAAkC;AAAAe,QAAAA,QAAA,EAC9CN;AACG,OAAA,CAAA;AAAA,KAAA,CACF,EACNyB,IAAK,CAAA,KAAA,EAAA;AAAAlC,MAAAA,SAAS,EAAC,eAAe;AAAAe,MAAAA,QAAA,EAAA,CAC5BD,GAAK,CAAA,KAAA,EAAA;AAAAd,QAAAA,SAAS,EAAC,gCAAgC;;QAAe,EAC9Dc,GAAA,CAAA,KAAA,EAAA;AAAKd,QAAAA,SAAS,EAAC,kCAAkC;AAAAe,QAAAA,QAAA,EAC9CL;AACG,OAAA,CAAA;AAAA,KAAA,CACF;AACF,GAAA,CAAA;AAEV;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
interface CompareProps {
|
|
4
|
+
original: string | string[];
|
|
5
|
+
modified: string | string[];
|
|
6
|
+
className?: string;
|
|
7
|
+
viewMode?: 'side-by-side' | 'inline';
|
|
8
|
+
caseSensitive?: boolean;
|
|
9
|
+
}
|
|
10
|
+
declare const Compare: React.FC<CompareProps>;
|
|
11
|
+
|
|
12
|
+
export { Compare, Compare as default };
|
|
13
|
+
export type { CompareProps };
|
package/package.json
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@crashbytes/react-version-compare",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A React component for comparing strings and arrays with precise word-level and item-level highlighting of differences.",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"module": "dist/esm/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "rollup -c",
|
|
17
|
+
"dev": "rollup -c -w",
|
|
18
|
+
"test": "jest",
|
|
19
|
+
"test:watch": "jest --watch",
|
|
20
|
+
"clean": "rm -rf dist",
|
|
21
|
+
"prepublishOnly": "npm run clean && npm run build",
|
|
22
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
23
|
+
"type-check": "tsc --noEmit",
|
|
24
|
+
"demo": "vite",
|
|
25
|
+
"build-demo": "vite build"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"react",
|
|
29
|
+
"diff",
|
|
30
|
+
"compare",
|
|
31
|
+
"comparison",
|
|
32
|
+
"side-by-side",
|
|
33
|
+
"word-diff",
|
|
34
|
+
"array-diff",
|
|
35
|
+
"typescript",
|
|
36
|
+
"highlight",
|
|
37
|
+
"changes"
|
|
38
|
+
],
|
|
39
|
+
"author": "Your Name <your.email@example.com>",
|
|
40
|
+
"license": "Apache-2.0",
|
|
41
|
+
"homepage": "https://github.com/CrashBytes/react-version-compare#readme",
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/CrashBytes/react-version-compare.git"
|
|
45
|
+
},
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/CrashBytes/react-version-compare/issues"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"react": ">=16.8.0",
|
|
51
|
+
"react-dom": ">=16.8.0"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"diff": "^8.0.1",
|
|
55
|
+
"tslib": "^2.8.1"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@babel/core": "^7.27.1",
|
|
59
|
+
"@babel/preset-env": "^7.27.2",
|
|
60
|
+
"@babel/preset-react": "^7.27.1",
|
|
61
|
+
"@babel/preset-typescript": "^7.27.1",
|
|
62
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
63
|
+
"@rollup/plugin-commonjs": "^28.0.3",
|
|
64
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
65
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
66
|
+
"@testing-library/jest-dom": "^6.1.0",
|
|
67
|
+
"@testing-library/react": "^14.0.0",
|
|
68
|
+
"@testing-library/user-event": "^14.5.0",
|
|
69
|
+
"@types/jest": "^29.5.0",
|
|
70
|
+
"@types/react": "^18.2.0",
|
|
71
|
+
"@types/react-dom": "^18.2.0",
|
|
72
|
+
"@vitejs/plugin-react": "^4.6.0",
|
|
73
|
+
"babel-jest": "^29.7.0",
|
|
74
|
+
"identity-obj-proxy": "^3.0.0",
|
|
75
|
+
"jest": "^29.7.0",
|
|
76
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
77
|
+
"react": "^18.2.0",
|
|
78
|
+
"react-dom": "^18.2.0",
|
|
79
|
+
"rollup": "^4.40.2",
|
|
80
|
+
"rollup-plugin-dts": "^6.2.1",
|
|
81
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
82
|
+
"rollup-plugin-postcss": "^4.0.2",
|
|
83
|
+
"typescript": "^5.8.3",
|
|
84
|
+
"vite": "^7.0.2"
|
|
85
|
+
}
|
|
86
|
+
}
|