diff-leven 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Kushal
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/README.md ADDED
@@ -0,0 +1,220 @@
1
+ # diff-leven
2
+
3
+ > Git-like diff between two strings or objects, powered by the Levenshtein distance algorithm
4
+
5
+ [![npm version](https://badge.fury.io/js/diff-leven.svg)](https://badge.fury.io/js/diff-leven)
6
+ [![npm](https://img.shields.io/npm/dm/diff-leven.svg)](https://www.npmjs.com/package/diff-leven)
7
+ ![License](https://img.shields.io/npm/l/diff-leven.svg)
8
+
9
+ ---
10
+
11
+ ## ✨ Features
12
+
13
+ - **Advanced Diff Generation**: Uses the Levenshtein distance algorithm for meaningful diffs
14
+ - **Multiple Data Type Support**:
15
+ - Objects (including nested structures)
16
+ - Arrays (smart matching by content similarity)
17
+ - Strings (character-level differences)
18
+ - Numbers, Booleans, and any serializable value
19
+ - **Rich Output Options**:
20
+ - Terminal-friendly colorized output (toggleable)
21
+ - Git-style diff format with clear additions/removals
22
+ - **Flexible Configuration**:
23
+ - `color`: Toggle color output (default: `true`)
24
+ - `keysOnly`: Compare only object structure/keys (default: `false`)
25
+ - `full`: Output the entire object tree, not just differences (default: `false`)
26
+ - `outputKeys`: Always include specified keys in output for objects with differences
27
+ - `ignoreKeys`: Skip specified keys when comparing objects
28
+ - `ignoreValues`: Ignore value differences, focus on structure
29
+
30
+ ---
31
+
32
+ ## 🚀 Quick Start
33
+
34
+ ### 1. Install
35
+
36
+ ```bash
37
+ npm install diff-leven
38
+ ```
39
+
40
+ ### 2. Usage
41
+
42
+ ```js
43
+ const { diff } = require('diff-leven');
44
+
45
+ console.log(diff({ foo: 'bar' }, { foo: 'baz' }));
46
+ // Output:
47
+ // {
48
+ // - foo: "bar"
49
+ // + foo: "baz"
50
+ // }
51
+ ```
52
+
53
+ ---
54
+
55
+ ## 🛠️ API Reference
56
+
57
+ ### `diff(a, b, options?)`
58
+
59
+ Compare two values (strings, objects, arrays, etc.) and return a formatted diff string.
60
+
61
+ #### **Parameters**
62
+
63
+ - `a`, `b`: Anything serializable (object, array, string, number, etc.)
64
+ - `options` _(optional object)_:
65
+ - `color` _(boolean)_: Use colors in output (default: `true`)
66
+ - `keysOnly` _(boolean)_: Only compare object keys (default: `false`)
67
+ - `full` _(boolean)_: Output the entire JSON tree (default: `false`)
68
+ - `outputKeys` _(string[])_: Always include these keys in output (default: `[]`)
69
+ - `ignoreKeys` _(string[])_: Ignore these keys when comparing (default: `[]`)
70
+ - `ignoreValues` _(boolean)_: Ignore value differences (default: `false`)
71
+
72
+ #### **Returns**
73
+
74
+ - A string representing the diff between `a` and `b`.
75
+
76
+ ### `diffRaw(a, b, options?)`
77
+
78
+ Compare two values (strings, objects, arrays, etc.) and return a structured diff result object.
79
+
80
+ #### **Parameters**
81
+
82
+ - `a`, `b`: Anything serializable (object, array, string, number, etc.)
83
+ - `options` _(optional object)_:
84
+ - `color` _(boolean)_: Use colors in output (default: `true`)
85
+ - `keysOnly` _(boolean)_: Only compare object keys (default: `false`)
86
+ - `full` _(boolean)_: Output the entire JSON tree (default: `false`)
87
+ - `outputKeys` _(string[])_: Always include these keys in output (default: `[]`)
88
+ - `ignoreKeys` _(string[])_: Ignore these keys when comparing (default: `[]`)
89
+ - `ignoreValues` _(boolean)_: Ignore value differences (default: `false`)
90
+
91
+ #### **Returns**
92
+
93
+ - A structured object representing the diff between `a` and `b`.
94
+
95
+ #### **Examples**
96
+
97
+ ```js
98
+ const { diff, diffRaw } = require('diff-leven');
99
+
100
+ // Basic diff (string output)
101
+ console.log(diff({ foo: 'bar' }, { foo: 'baz' }));
102
+ // Output:
103
+ // {
104
+ // - foo: "bar"
105
+ // + foo: "baz"
106
+ // }
107
+
108
+ // Raw diff object
109
+ const rawDiff = diffRaw({ foo: 'bar' }, { foo: 'baz' });
110
+ console.log(JSON.stringify(rawDiff, null, 2));
111
+ // Output:
112
+ // {
113
+ // "type": "changed",
114
+ // "path": [],
115
+ // "oldValue": { "foo": "bar" },
116
+ // "newValue": { "foo": "baz" },
117
+ // "children": [
118
+ // {
119
+ // "type": "changed",
120
+ // "path": ["foo"],
121
+ // "oldValue": "bar",
122
+ // "newValue": "baz"
123
+ // }
124
+ // ]
125
+ // }
126
+
127
+ // No colors
128
+ console.log(diff({ foo: 'bar' }, { foo: 'baz' }, { color: false }));
129
+ // Output:
130
+ // {
131
+ // - foo: "bar"
132
+ // + foo: "baz"
133
+ // }
134
+
135
+ // Full output
136
+ console.log(diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 }, { full: true }));
137
+ // Output:
138
+ // {
139
+ // - foo: "bar"
140
+ // + foo: "baz"
141
+ // b: 3
142
+ // }
143
+
144
+ // Ignore keys
145
+ console.log(
146
+ diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 }, { ignoreKeys: ['b'] }),
147
+ );
148
+ // Output:
149
+ // {
150
+ // - foo: "bar"
151
+ // + foo: "baz"
152
+ // }
153
+
154
+ // Ignore values
155
+ console.log(
156
+ diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 }, { ignoreValues: true }),
157
+ );
158
+ // Output showing structural differences only
159
+
160
+ // Output specific keys
161
+ console.log(
162
+ diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 }, { outputKeys: ['foo'] }),
163
+ );
164
+ // Output:
165
+ // {
166
+ // - foo: "bar"
167
+ // + foo: "baz"
168
+ // }
169
+
170
+ // Combine options
171
+ console.log(
172
+ diff(
173
+ { foo: 'bar', b: 3 },
174
+ { foo: 'baz', b: 3 },
175
+ {
176
+ keysOnly: true,
177
+ ignoreKeys: ['b'],
178
+ ignoreValues: true,
179
+ outputKeys: ['foo'],
180
+ full: true,
181
+ color: false,
182
+ },
183
+ ),
184
+ );
185
+ ```
186
+
187
+ ---
188
+
189
+ ## ⚙️ Options Matrix
190
+
191
+ | Option | Type | Default | Description |
192
+ | -------------- | -------- | ------- | -------------------------------------------- |
193
+ | `color` | boolean | true | Use colorized output |
194
+ | `keysOnly` | boolean | false | Only compare object keys |
195
+ | `full` | boolean | false | Output the entire object tree |
196
+ | `outputKeys` | string[] | [] | Always include these keys in output |
197
+ | `ignoreKeys` | string[] | [] | Ignore these keys when comparing |
198
+ | `ignoreValues` | boolean | false | Ignore value differences, focus on structure |
199
+
200
+ ---
201
+
202
+ ## 📦 Examples
203
+
204
+ See [`examples/basic.js`](examples/basic.js) for more usage patterns.
205
+
206
+ ---
207
+
208
+ ## 🤝 Contributing
209
+
210
+ 1. Fork the repo
211
+ 2. Create your feature branch (`git checkout -b feature/YourFeature`)
212
+ 3. Commit your changes (`git commit -am 'Add new feature'`)
213
+ 4. Push to the branch (`git push origin feature/YourFeature`)
214
+ 5. Open a pull request
215
+
216
+ ---
217
+
218
+ ## 📄 License
219
+
220
+ MIT © [kushalshit27](LICENSE)
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Options for diff generation
3
+ */
4
+ interface DiffOptions {
5
+ /**
6
+ * Whether to use colorized output
7
+ * @default true
8
+ */
9
+ color?: boolean;
10
+ /**
11
+ * Only compare object keys/structure (ignore values)
12
+ * @default false
13
+ */
14
+ keysOnly?: boolean;
15
+ /**
16
+ * Output the entire object tree, not just differences
17
+ * @default false
18
+ */
19
+ full?: boolean;
20
+ /**
21
+ * Always include these keys in output for objects with differences
22
+ * @default []
23
+ */
24
+ outputKeys?: string[];
25
+ /**
26
+ * Skip these keys when comparing objects
27
+ * @default []
28
+ */
29
+ ignoreKeys?: string[];
30
+ /**
31
+ * Ignore value differences, focus only on structure
32
+ * @default false
33
+ */
34
+ ignoreValues?: boolean;
35
+ }
36
+ /**
37
+ * Enum for diff change types
38
+ */
39
+ declare enum DiffType {
40
+ ADDED = "added",
41
+ REMOVED = "removed",
42
+ CHANGED = "changed",
43
+ UNCHANGED = "unchanged"
44
+ }
45
+ /**
46
+ * Represents a change between two values
47
+ */
48
+ interface DiffResult {
49
+ type: DiffType;
50
+ path?: string[];
51
+ oldValue?: any;
52
+ newValue?: any;
53
+ children?: DiffResult[];
54
+ /**
55
+ * Additional metadata about the diff, such as Levenshtein distance metrics
56
+ */
57
+ meta?: {
58
+ /** Levenshtein distance between strings */
59
+ levenDistance?: number;
60
+ /** Similarity ratio (0-1) where 1 means identical */
61
+ similarity?: number;
62
+ /** Any other metadata properties */
63
+ [key: string]: any;
64
+ };
65
+ }
66
+ /**
67
+ * Type for handling any serializable value
68
+ */
69
+ type SerializableValue = string | number | boolean | null | undefined | {
70
+ [key: string]: SerializableValue;
71
+ } | SerializableValue[];
72
+
73
+ /**
74
+ * Compare two values and generate a detailed diff result object
75
+ *
76
+ * @param oldValue - Original value to compare from
77
+ * @param newValue - New value to compare against
78
+ * @param options - Configuration options for the diff
79
+ * @returns A structured diff result object
80
+ */
81
+ declare function diffRaw(oldValue: SerializableValue, newValue: SerializableValue, options?: DiffOptions): DiffResult;
82
+ /**
83
+ * Compare two values and generate a formatted diff string
84
+ *
85
+ * @param oldValue - Original value to compare from
86
+ * @param newValue - New value to compare against
87
+ * @param options - Configuration options for the diff
88
+ * @returns A formatted string representation of the diff
89
+ */
90
+ declare function diff(oldValue: SerializableValue, newValue: SerializableValue, options?: DiffOptions): string;
91
+
92
+ export { type DiffOptions, type DiffResult, DiffType, type SerializableValue, diff, diffRaw };
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Options for diff generation
3
+ */
4
+ interface DiffOptions {
5
+ /**
6
+ * Whether to use colorized output
7
+ * @default true
8
+ */
9
+ color?: boolean;
10
+ /**
11
+ * Only compare object keys/structure (ignore values)
12
+ * @default false
13
+ */
14
+ keysOnly?: boolean;
15
+ /**
16
+ * Output the entire object tree, not just differences
17
+ * @default false
18
+ */
19
+ full?: boolean;
20
+ /**
21
+ * Always include these keys in output for objects with differences
22
+ * @default []
23
+ */
24
+ outputKeys?: string[];
25
+ /**
26
+ * Skip these keys when comparing objects
27
+ * @default []
28
+ */
29
+ ignoreKeys?: string[];
30
+ /**
31
+ * Ignore value differences, focus only on structure
32
+ * @default false
33
+ */
34
+ ignoreValues?: boolean;
35
+ }
36
+ /**
37
+ * Enum for diff change types
38
+ */
39
+ declare enum DiffType {
40
+ ADDED = "added",
41
+ REMOVED = "removed",
42
+ CHANGED = "changed",
43
+ UNCHANGED = "unchanged"
44
+ }
45
+ /**
46
+ * Represents a change between two values
47
+ */
48
+ interface DiffResult {
49
+ type: DiffType;
50
+ path?: string[];
51
+ oldValue?: any;
52
+ newValue?: any;
53
+ children?: DiffResult[];
54
+ /**
55
+ * Additional metadata about the diff, such as Levenshtein distance metrics
56
+ */
57
+ meta?: {
58
+ /** Levenshtein distance between strings */
59
+ levenDistance?: number;
60
+ /** Similarity ratio (0-1) where 1 means identical */
61
+ similarity?: number;
62
+ /** Any other metadata properties */
63
+ [key: string]: any;
64
+ };
65
+ }
66
+ /**
67
+ * Type for handling any serializable value
68
+ */
69
+ type SerializableValue = string | number | boolean | null | undefined | {
70
+ [key: string]: SerializableValue;
71
+ } | SerializableValue[];
72
+
73
+ /**
74
+ * Compare two values and generate a detailed diff result object
75
+ *
76
+ * @param oldValue - Original value to compare from
77
+ * @param newValue - New value to compare against
78
+ * @param options - Configuration options for the diff
79
+ * @returns A structured diff result object
80
+ */
81
+ declare function diffRaw(oldValue: SerializableValue, newValue: SerializableValue, options?: DiffOptions): DiffResult;
82
+ /**
83
+ * Compare two values and generate a formatted diff string
84
+ *
85
+ * @param oldValue - Original value to compare from
86
+ * @param newValue - New value to compare against
87
+ * @param options - Configuration options for the diff
88
+ * @returns A formatted string representation of the diff
89
+ */
90
+ declare function diff(oldValue: SerializableValue, newValue: SerializableValue, options?: DiffOptions): string;
91
+
92
+ export { type DiffOptions, type DiffResult, DiffType, type SerializableValue, diff, diffRaw };