diff-leven 0.2.0 → 0.3.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/README.md CHANGED
@@ -93,10 +93,26 @@ Compare two values (strings, objects, arrays, etc.) and return a structured diff
93
93
 
94
94
  - A structured object representing the diff between `a` and `b`.
95
95
 
96
+ ### `isDiff(a, b, options?)`
97
+
98
+ Check if two values (strings, objects, arrays, etc.) are different and return a boolean result.
99
+
100
+ #### **Parameters**
101
+
102
+ - `a`, `b`: Anything serializable (object, array, string, number, etc.)
103
+ - `options` _(optional object)_:
104
+ - `keysOnly` _(boolean)_: Only compare object keys (default: `false`)
105
+ - `ignoreKeys` _(string[])_: Ignore these keys when comparing (default: `[]`)
106
+ - `ignoreValues` _(boolean)_: Ignore value differences (default: `false`)
107
+
108
+ #### **Returns**
109
+
110
+ - A boolean indicating if the values are different (`true` = different, `false` = identical).
111
+
96
112
  #### **Examples**
97
113
 
98
114
  ```js
99
- const { diff, diffRaw } = require('diff-leven');
115
+ const { diff, diffRaw, isDiff } = require('diff-leven');
100
116
 
101
117
  // Basic diff (string output)
102
118
  console.log(diff({ foo: 'bar' }, { foo: 'baz' }));
@@ -125,6 +141,23 @@ console.log(JSON.stringify(rawDiff, null, 2));
125
141
  // ]
126
142
  // }
127
143
 
144
+ // Boolean diff check
145
+ console.log(isDiff({ foo: 'bar' }, { foo: 'baz' }));
146
+ // Output: true
147
+
148
+ console.log(isDiff({ foo: 'bar' }, { foo: 'bar' }));
149
+ // Output: false
150
+
151
+ // With options
152
+ console.log(
153
+ isDiff(
154
+ { foo: 'bar', timestamp: 123 },
155
+ { foo: 'bar', timestamp: 456 },
156
+ { ignoreKeys: ['timestamp'] },
157
+ ),
158
+ );
159
+ // Output: false (identical when ignoring timestamp)
160
+
128
161
  // No colors
129
162
  console.log(diff({ foo: 'bar' }, { foo: 'baz' }, { color: false }));
130
163
  // Output:
package/dist/index.d.mts CHANGED
@@ -93,5 +93,14 @@ declare function diffRaw(oldValue: SerializableValue, newValue: SerializableValu
93
93
  * @returns A formatted string representation of the diff
94
94
  */
95
95
  declare function diff(oldValue: SerializableValue, newValue: SerializableValue, options?: DiffOptions): string;
96
+ /**
97
+ * Check if two values are different
98
+ *
99
+ * @param oldValue - Original value to compare from
100
+ * @param newValue - New value to compare against
101
+ * @param options - Configuration options for the diff
102
+ * @returns A boolean indicating if the values are different
103
+ */
104
+ declare function isDiff(oldValue: SerializableValue, newValue: SerializableValue, options?: DiffOptions): boolean;
96
105
 
97
- export { type DiffOptions, type DiffResult, DiffType, type SerializableValue, diff, diffRaw };
106
+ export { type DiffOptions, type DiffResult, DiffType, type SerializableValue, diff, diffRaw, isDiff };
package/dist/index.d.ts CHANGED
@@ -93,5 +93,14 @@ declare function diffRaw(oldValue: SerializableValue, newValue: SerializableValu
93
93
  * @returns A formatted string representation of the diff
94
94
  */
95
95
  declare function diff(oldValue: SerializableValue, newValue: SerializableValue, options?: DiffOptions): string;
96
+ /**
97
+ * Check if two values are different
98
+ *
99
+ * @param oldValue - Original value to compare from
100
+ * @param newValue - New value to compare against
101
+ * @param options - Configuration options for the diff
102
+ * @returns A boolean indicating if the values are different
103
+ */
104
+ declare function isDiff(oldValue: SerializableValue, newValue: SerializableValue, options?: DiffOptions): boolean;
96
105
 
97
- export { type DiffOptions, type DiffResult, DiffType, type SerializableValue, diff, diffRaw };
106
+ export { type DiffOptions, type DiffResult, DiffType, type SerializableValue, diff, diffRaw, isDiff };
package/dist/index.js CHANGED
@@ -1,18 +1,18 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _leven = require('leven'); var _leven2 = _interopRequireDefault(_leven);var g=(o=>(o.ADDED="added",o.REMOVED="removed",o.CHANGED="changed",o.UNCHANGED="unchanged",o))(g||{});function h(e,t,f={},i=[]){let{keysOnly:o=!1,ignoreValues:D=!1}=f;if(e===void 0&&t===void 0)return{type:"unchanged"};if(e===void 0)return{type:"added",path:i,newValue:t};if(t===void 0)return{type:"removed",path:i,oldValue:e};if(typeof e!="object"||typeof t!="object"||e===null||t===null){if(o||D)return{type:"unchanged",path:i,oldValue:e,newValue:t,meta:{ignored:!0}};if(e===t)return{type:"unchanged",path:i,oldValue:e,newValue:t};if(typeof e=="string"&&typeof t=="string"){let $=_leven2.default.call(void 0, e,t),l=Math.max(e.length,t.length),n=l>0?1-$/l:1;return{type:"changed",path:i,oldValue:e,newValue:t,meta:{levenDistance:$,similarity:n}}}return{type:"changed",path:i,oldValue:e,newValue:t}}return Array.isArray(e)&&Array.isArray(t)?A(e,t,f,i):R(e,t,f,i)}function R(e,t,f,i){let{ignoreKeys:o=[]}=f,D=new Set([...Object.keys(e).filter(n=>!o.includes(n)),...Object.keys(t).filter(n=>!o.includes(n))]),$=[],l=!1;for(let n of D){let u=[...i,n],y=e[n],r=t[n],a=h(y,r,f,u);a.type!=="unchanged"&&(l=!0),$.push(a)}return l?{type:"changed",path:i,oldValue:e,newValue:t,children:$}:{type:"unchanged",path:i,oldValue:e,newValue:t}}function A(e,t,f,i){if(e.length!==t.length){let $=[],l=Math.max(e.length,t.length);for(let n=0;n<l;n++){let u=[...i,n.toString()];n>=e.length?$.push({type:"added",path:u,newValue:t[n]}):n>=t.length?$.push({type:"removed",path:u,oldValue:e[n]}):$.push(h(e[n],t[n],f,u))}return{type:"changed",path:i,oldValue:e,newValue:t,children:$}}let o=[],D=!1;for(let $=0;$<e.length;$++){let l=[...i,$.toString()],n=e[$],u=t[$],y;if(typeof n=="string"&&typeof u=="string")if(n===u)y={type:"unchanged",path:l,oldValue:n,newValue:u};else{let r=_leven2.default.call(void 0, n,u),a=Math.max(n.length,u.length),m=a>0?1-r/a:1;y={type:"changed",path:l,oldValue:n,newValue:u,meta:{levenDistance:r,similarity:m}},D=!0}else y=h(n,u,f,l),y.type!=="unchanged"&&(D=!0);o.push(y)}return D?{type:"changed",path:i,oldValue:e,newValue:t,children:o}:{type:"unchanged",path:i,oldValue:e,newValue:t}}var s={reset:"\x1B[0m",red:"\x1B[31m",green:"\x1B[32m",gray:"\x1B[90m"};function E(e,t={}){let{color:f=!0,full:i=!1,withSimilarity:o=!1}=t;return e.type==="unchanged"&&!i&&(!e.children||e.children.length===0)?"":!e.children||e.children.length===0?C(e,f,o):G(e,t,0)}function C(e,t,f=!1){let i=H(e.type),o="";switch(e.type){case"added":return o=c(e.newValue),t?`${s.green}${i} ${o}${s.reset}`:`${i} ${o}`;case"removed":return o=c(e.oldValue),t?`${s.red}${i} ${o}${s.reset}`:`${i} ${o}`;case"changed":let D=c(e.oldValue),$=c(e.newValue),l="";if(f&&_optionalChain([e, 'access', _ => _.meta, 'optionalAccess', _2 => _2.similarity])!==void 0&&typeof e.oldValue=="string"&&typeof e.newValue=="string"){let y=Math.round(e.meta.similarity*100);l=t?`${s.gray} (${y}% similar)${s.reset}`:` (${y}% similar)`}let n=t?`${s.red}- ${D}${s.reset}`:`- ${D}`,u=t?`${s.green}+ ${$}${l}${s.reset}`:`+ ${$}${l}`;return`${n}
2
- ${u}`;default:return o=c(_nullishCoalesce(e.newValue, () => (e.oldValue))),t?`${s.gray}${i} ${o}${s.reset}`:`${i} ${o}`}}function G(e,t,f=0){let{full:i=!1}=t;return e.type==="unchanged"&&!i&&(!e.children||e.children.length===0||typeof e.oldValue!="object"&&typeof e.newValue!="object")?"":Array.isArray(e.oldValue)||Array.isArray(e.newValue)?p(e,t,f):d(e,t,f)}function p(e,t,f=0){let{color:i=!0,full:o=!1,withSimilarity:D=!1}=t,$=" ".repeat(f),l=" ".repeat(f+2),n=`[
3
- `,u=0;if(e.children)for(let y=0;y<e.children.length;y++){let r=e.children[y];if(!(r.type==="unchanged"&&!o&&(!r.children||r.children.length===0)))if(r.children&&r.children.length>0){let a=Array.isArray(r.newValue||r.oldValue)?p(r,t,f+2):d(r,t,f+2);a.trim().length>2&&(u>0&&(n+=`,
4
- `),n+=l,n+=a,u++)}else switch(u>0&&(n+=`,
5
- `),r.type){case"added":n+=i?`${l}${s.green}+ ${c(r.newValue)}${s.reset}`:`${l}+ ${c(r.newValue)}`,u++;break;case"removed":n+=i?`${l}${s.red}- ${c(r.oldValue)}${s.reset}`:`${l}- ${c(r.oldValue)}`,u++;break;case"unchanged":_optionalChain([r, 'access', _3 => _3.meta, 'optionalAccess', _4 => _4.ignored])&&r.path?n+=i?`${l}${s.gray} ${r.path[r.path.length-1]||""}${s.reset}`:`${l} ${r.path[r.path.length-1]||""}`:n+=i?`${l}${s.gray} ${c(r.newValue)}${s.reset}`:`${l} ${c(r.newValue)}`,u++;break;case"changed":n+=i?`${l}${s.red}- ${c(r.oldValue)}${s.reset}
6
- ${l}${s.green}+ ${c(r.newValue)}${_optionalChain([r, 'access', _5 => _5.meta, 'optionalAccess', _6 => _6.similarity])!==void 0&&D?i?`${s.gray} (${Math.round(r.meta.similarity*100)}% similar)${s.reset}`:` (${Math.round(r.meta.similarity*100)}% similar)`:""}${s.reset}`:`${l}- ${c(r.oldValue)}
7
- ${l}+ ${c(r.newValue)}${_optionalChain([r, 'access', _7 => _7.meta, 'optionalAccess', _8 => _8.similarity])!==void 0&&D?` (${Math.round(r.meta.similarity*100)}% similar)`:""}`,u++;break}}return u>0&&(n+=`
8
- `),n+=`${$}]`,n}function d(e,t,f=0){let{color:i=!0,full:o=!1,withSimilarity:D=!1}=t,$=" ".repeat(f),l=" ".repeat(f+2),n=`{
9
- `,u=0;if(e.children){let y=e.children.filter(r=>{if(r.type!=="unchanged"||o||r.children&&r.children.length>0)return!0;let a=_optionalChain([r, 'access', _9 => _9.path, 'optionalAccess', _10 => _10[r.path.length-1]])||"";return!!_optionalChain([t, 'access', _11 => _11.outputKeys, 'optionalAccess', _12 => _12.includes, 'call', _13 => _13(a)])});for(let r=0;r<y.length;r++){let a=y[r],m=_optionalChain([a, 'access', _14 => _14.path, 'optionalAccess', _15 => _15[a.path.length-1]])||"",N=r===y.length-1;if(u>0&&!n.endsWith(`
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _leven = require('leven'); var _leven2 = _interopRequireDefault(_leven);var g=(o=>(o.ADDED="added",o.REMOVED="removed",o.CHANGED="changed",o.UNCHANGED="unchanged",o))(g||{});function h(e,t,u={},r=[]){let{keysOnly:o=!1,ignoreValues:D=!1}=u;if(e===void 0&&t===void 0)return{type:"unchanged"};if(e===void 0)return{type:"added",path:r,newValue:t};if(t===void 0)return{type:"removed",path:r,oldValue:e};if(typeof e!="object"||typeof t!="object"||e===null||t===null){if(o||D)return{type:"unchanged",path:r,oldValue:e,newValue:t,meta:{ignored:!0}};if(e===t)return{type:"unchanged",path:r,oldValue:e,newValue:t};if(typeof e=="string"&&typeof t=="string"){let $=_leven2.default.call(void 0, e,t),l=Math.max(e.length,t.length),n=l>0?1-$/l:1;return{type:"changed",path:r,oldValue:e,newValue:t,meta:{levenDistance:$,similarity:n}}}return{type:"changed",path:r,oldValue:e,newValue:t}}return Array.isArray(e)&&Array.isArray(t)?C(e,t,u,r):A(e,t,u,r)}function A(e,t,u,r){let{ignoreKeys:o=[]}=u,D=new Set([...Object.keys(e).filter(n=>!o.includes(n)),...Object.keys(t).filter(n=>!o.includes(n))]),$=[],l=!1;for(let n of D){let f=[...r,n],y=e[n],i=t[n],a=h(y,i,u,f);a.type!=="unchanged"&&(l=!0),$.push(a)}return l?{type:"changed",path:r,oldValue:e,newValue:t,children:$}:{type:"unchanged",path:r,oldValue:e,newValue:t}}function C(e,t,u,r){if(e.length!==t.length){let $=[],l=Math.max(e.length,t.length);for(let n=0;n<l;n++){let f=[...r,n.toString()];n>=e.length?$.push({type:"added",path:f,newValue:t[n]}):n>=t.length?$.push({type:"removed",path:f,oldValue:e[n]}):$.push(h(e[n],t[n],u,f))}return{type:"changed",path:r,oldValue:e,newValue:t,children:$}}let o=[],D=!1;for(let $=0;$<e.length;$++){let l=[...r,$.toString()],n=e[$],f=t[$],y;if(typeof n=="string"&&typeof f=="string")if(n===f)y={type:"unchanged",path:l,oldValue:n,newValue:f};else{let i=_leven2.default.call(void 0, n,f),a=Math.max(n.length,f.length),m=a>0?1-i/a:1;y={type:"changed",path:l,oldValue:n,newValue:f,meta:{levenDistance:i,similarity:m}},D=!0}else y=h(n,f,u,l),y.type!=="unchanged"&&(D=!0);o.push(y)}return D?{type:"changed",path:r,oldValue:e,newValue:t,children:o}:{type:"unchanged",path:r,oldValue:e,newValue:t}}var s={reset:"\x1B[0m",red:"\x1B[31m",green:"\x1B[32m",gray:"\x1B[90m"};function E(e,t={}){let{color:u=!0,full:r=!1,withSimilarity:o=!1}=t;return e.type==="unchanged"&&!r&&(!e.children||e.children.length===0)?"":!e.children||e.children.length===0?G(e,u,o):H(e,t,0)}function G(e,t,u=!1){let r=x(e.type),o="";switch(e.type){case"added":return o=c(e.newValue),t?`${s.green}${r} ${o}${s.reset}`:`${r} ${o}`;case"removed":return o=c(e.oldValue),t?`${s.red}${r} ${o}${s.reset}`:`${r} ${o}`;case"changed":let D=c(e.oldValue),$=c(e.newValue),l="";if(u&&_optionalChain([e, 'access', _ => _.meta, 'optionalAccess', _2 => _2.similarity])!==void 0&&typeof e.oldValue=="string"&&typeof e.newValue=="string"){let y=Math.round(e.meta.similarity*100);l=t?`${s.gray} (${y}% similar)${s.reset}`:` (${y}% similar)`}let n=t?`${s.red}- ${D}${s.reset}`:`- ${D}`,f=t?`${s.green}+ ${$}${l}${s.reset}`:`+ ${$}${l}`;return`${n}
2
+ ${f}`;default:return o=c(_nullishCoalesce(e.newValue, () => (e.oldValue))),t?`${s.gray}${r} ${o}${s.reset}`:`${r} ${o}`}}function H(e,t,u=0){let{full:r=!1}=t;return e.type==="unchanged"&&!r&&(!e.children||e.children.length===0||typeof e.oldValue!="object"&&typeof e.newValue!="object")?"":Array.isArray(e.oldValue)||Array.isArray(e.newValue)?p(e,t,u):d(e,t,u)}function p(e,t,u=0){let{color:r=!0,full:o=!1,withSimilarity:D=!1}=t,$=" ".repeat(u),l=" ".repeat(u+2),n=`[
3
+ `,f=0;if(e.children)for(let y=0;y<e.children.length;y++){let i=e.children[y];if(!(i.type==="unchanged"&&!o&&(!i.children||i.children.length===0)))if(i.children&&i.children.length>0){let a=Array.isArray(i.newValue||i.oldValue)?p(i,t,u+2):d(i,t,u+2);a.trim().length>2&&(f>0&&(n+=`,
4
+ `),n+=l,n+=a,f++)}else switch(f>0&&(n+=`,
5
+ `),i.type){case"added":n+=r?`${l}${s.green}+ ${c(i.newValue)}${s.reset}`:`${l}+ ${c(i.newValue)}`,f++;break;case"removed":n+=r?`${l}${s.red}- ${c(i.oldValue)}${s.reset}`:`${l}- ${c(i.oldValue)}`,f++;break;case"unchanged":_optionalChain([i, 'access', _3 => _3.meta, 'optionalAccess', _4 => _4.ignored])&&i.path?n+=r?`${l}${s.gray} ${i.path[i.path.length-1]||""}${s.reset}`:`${l} ${i.path[i.path.length-1]||""}`:n+=r?`${l}${s.gray} ${c(i.newValue)}${s.reset}`:`${l} ${c(i.newValue)}`,f++;break;case"changed":n+=r?`${l}${s.red}- ${c(i.oldValue)}${s.reset}
6
+ ${l}${s.green}+ ${c(i.newValue)}${_optionalChain([i, 'access', _5 => _5.meta, 'optionalAccess', _6 => _6.similarity])!==void 0&&D?r?`${s.gray} (${Math.round(i.meta.similarity*100)}% similar)${s.reset}`:` (${Math.round(i.meta.similarity*100)}% similar)`:""}${s.reset}`:`${l}- ${c(i.oldValue)}
7
+ ${l}+ ${c(i.newValue)}${_optionalChain([i, 'access', _7 => _7.meta, 'optionalAccess', _8 => _8.similarity])!==void 0&&D?` (${Math.round(i.meta.similarity*100)}% similar)`:""}`,f++;break}}return f>0&&(n+=`
8
+ `),n+=`${$}]`,n}function d(e,t,u=0){let{color:r=!0,full:o=!1,withSimilarity:D=!1}=t,$=" ".repeat(u),l=" ".repeat(u+2),n=`{
9
+ `,f=0;if(e.children){let y=e.children.filter(i=>{if(i.type!=="unchanged"||o||i.children&&i.children.length>0)return!0;let a=_optionalChain([i, 'access', _9 => _9.path, 'optionalAccess', _10 => _10[i.path.length-1]])||"";return!!_optionalChain([t, 'access', _11 => _11.outputKeys, 'optionalAccess', _12 => _12.includes, 'call', _13 => _13(a)])});for(let i=0;i<y.length;i++){let a=y[i],m=_optionalChain([a, 'access', _14 => _14.path, 'optionalAccess', _15 => _15[a.path.length-1]])||"",R=i===y.length-1;if(f>0&&!n.endsWith(`
10
10
  `)&&(n+=`,
11
- `),a.children&&a.children.length>0){let b=Array.isArray(a.newValue||a.oldValue)?p(a,t,f+2):d(a,t,f+2);b.trim().length>2&&(n+=`${l}${m}: ${b}`,u++)}else switch(a.type){case"added":n+=i?`${l}${s.green}+ ${m}: ${c(a.newValue)}${s.reset}`:`${l}+ ${m}: ${c(a.newValue)}`,u++;break;case"removed":n+=i?`${l}${s.red}- ${m}: ${c(a.oldValue)}${s.reset}`:`${l}- ${m}: ${c(a.oldValue)}`,u++;break;case"changed":n+=i?`${l}${s.red}- ${m}: ${c(a.oldValue)}${s.reset}
12
- ${l}${s.green}+ ${m}: ${c(a.newValue)}${_optionalChain([a, 'access', _16 => _16.meta, 'optionalAccess', _17 => _17.similarity])!==void 0&&D?i?`${s.gray} (${Math.round(a.meta.similarity*100)}% similar)${s.reset}`:` (${Math.round(a.meta.similarity*100)}% similar)`:""}${s.reset}`:`${l}- ${m}: ${c(a.oldValue)}
13
- ${l}+ ${m}: ${c(a.newValue)}${_optionalChain([a, 'access', _18 => _18.meta, 'optionalAccess', _19 => _19.similarity])!==void 0&&D?` (${Math.round(a.meta.similarity*100)}% similar)`:""}`,u++;break;case"unchanged":(o||_optionalChain([t, 'access', _20 => _20.outputKeys, 'optionalAccess', _21 => _21.includes, 'call', _22 => _22(m)]))&&(_optionalChain([a, 'access', _23 => _23.meta, 'optionalAccess', _24 => _24.ignored])?n+=i?`${l}${s.gray} ${m}${s.reset}`:`${l} ${m}`:n+=i?`${l}${s.gray} ${m}: ${c(a.newValue)}${s.reset}`:`${l} ${m}: ${c(a.newValue)}`,u++);break}!N&&!n.endsWith(`
11
+ `),a.children&&a.children.length>0){let b=Array.isArray(a.newValue||a.oldValue)?p(a,t,u+2):d(a,t,u+2);b.trim().length>2&&(n+=`${l}${m}: ${b}`,f++)}else switch(a.type){case"added":n+=r?`${l}${s.green}+ ${m}: ${c(a.newValue)}${s.reset}`:`${l}+ ${m}: ${c(a.newValue)}`,f++;break;case"removed":n+=r?`${l}${s.red}- ${m}: ${c(a.oldValue)}${s.reset}`:`${l}- ${m}: ${c(a.oldValue)}`,f++;break;case"changed":n+=r?`${l}${s.red}- ${m}: ${c(a.oldValue)}${s.reset}
12
+ ${l}${s.green}+ ${m}: ${c(a.newValue)}${_optionalChain([a, 'access', _16 => _16.meta, 'optionalAccess', _17 => _17.similarity])!==void 0&&D?r?`${s.gray} (${Math.round(a.meta.similarity*100)}% similar)${s.reset}`:` (${Math.round(a.meta.similarity*100)}% similar)`:""}${s.reset}`:`${l}- ${m}: ${c(a.oldValue)}
13
+ ${l}+ ${m}: ${c(a.newValue)}${_optionalChain([a, 'access', _18 => _18.meta, 'optionalAccess', _19 => _19.similarity])!==void 0&&D?` (${Math.round(a.meta.similarity*100)}% similar)`:""}`,f++;break;case"unchanged":(o||_optionalChain([t, 'access', _20 => _20.outputKeys, 'optionalAccess', _21 => _21.includes, 'call', _22 => _22(m)]))&&(_optionalChain([a, 'access', _23 => _23.meta, 'optionalAccess', _24 => _24.ignored])?n+=r?`${l}${s.gray} ${m}${s.reset}`:`${l} ${m}`:n+=r?`${l}${s.gray} ${m}: ${c(a.newValue)}${s.reset}`:`${l} ${m}: ${c(a.newValue)}`,f++);break}!R&&!n.endsWith(`
14
14
  `)&&(n+=","),n.endsWith(`
15
15
  `)||(n+=`
16
- `)}}return u>0&&!n.endsWith(`
16
+ `)}}return f>0&&!n.endsWith(`
17
17
  `)&&(n+=`
18
- `),n+=`${$}}`,n}function H(e){switch(e){case"added":return"+";case"removed":return"-";case"changed":return"!";default:return" "}}function c(e){return e===void 0?"undefined":e===null?"null":typeof e=="string"?`'${e}'`:typeof e=="number"||typeof e=="boolean"?e.toString():typeof e=="object"?JSON.stringify(e,null,2):String(e)}function x(e,t,f={}){return h(e,t,f)}function B(e,t,f={}){let i=x(e,t,f);return E(i,f)}exports.DiffType = g; exports.diff = B; exports.diffRaw = x;
18
+ `),n+=`${$}}`,n}function x(e){switch(e){case"added":return"+";case"removed":return"-";case"changed":return"!";default:return" "}}function c(e){return e===void 0?"undefined":e===null?"null":typeof e=="string"?`'${e}'`:typeof e=="number"||typeof e=="boolean"?e.toString():typeof e=="object"?JSON.stringify(e,null,2):String(e)}function N(e,t,u={}){return h(e,t,u)}function B(e,t,u={}){let r=N(e,t,u);return E(r,u)}function F(e,t,u={}){return N(e,t,u).type!=="unchanged"}exports.DiffType = g; exports.diff = B; exports.diffRaw = N; exports.isDiff = F;
package/dist/index.mjs CHANGED
@@ -1,18 +1,18 @@
1
- import V from"leven";var g=(o=>(o.ADDED="added",o.REMOVED="removed",o.CHANGED="changed",o.UNCHANGED="unchanged",o))(g||{});function h(e,t,f={},i=[]){let{keysOnly:o=!1,ignoreValues:D=!1}=f;if(e===void 0&&t===void 0)return{type:"unchanged"};if(e===void 0)return{type:"added",path:i,newValue:t};if(t===void 0)return{type:"removed",path:i,oldValue:e};if(typeof e!="object"||typeof t!="object"||e===null||t===null){if(o||D)return{type:"unchanged",path:i,oldValue:e,newValue:t,meta:{ignored:!0}};if(e===t)return{type:"unchanged",path:i,oldValue:e,newValue:t};if(typeof e=="string"&&typeof t=="string"){let $=V(e,t),l=Math.max(e.length,t.length),n=l>0?1-$/l:1;return{type:"changed",path:i,oldValue:e,newValue:t,meta:{levenDistance:$,similarity:n}}}return{type:"changed",path:i,oldValue:e,newValue:t}}return Array.isArray(e)&&Array.isArray(t)?A(e,t,f,i):R(e,t,f,i)}function R(e,t,f,i){let{ignoreKeys:o=[]}=f,D=new Set([...Object.keys(e).filter(n=>!o.includes(n)),...Object.keys(t).filter(n=>!o.includes(n))]),$=[],l=!1;for(let n of D){let u=[...i,n],y=e[n],r=t[n],a=h(y,r,f,u);a.type!=="unchanged"&&(l=!0),$.push(a)}return l?{type:"changed",path:i,oldValue:e,newValue:t,children:$}:{type:"unchanged",path:i,oldValue:e,newValue:t}}function A(e,t,f,i){if(e.length!==t.length){let $=[],l=Math.max(e.length,t.length);for(let n=0;n<l;n++){let u=[...i,n.toString()];n>=e.length?$.push({type:"added",path:u,newValue:t[n]}):n>=t.length?$.push({type:"removed",path:u,oldValue:e[n]}):$.push(h(e[n],t[n],f,u))}return{type:"changed",path:i,oldValue:e,newValue:t,children:$}}let o=[],D=!1;for(let $=0;$<e.length;$++){let l=[...i,$.toString()],n=e[$],u=t[$],y;if(typeof n=="string"&&typeof u=="string")if(n===u)y={type:"unchanged",path:l,oldValue:n,newValue:u};else{let r=V(n,u),a=Math.max(n.length,u.length),m=a>0?1-r/a:1;y={type:"changed",path:l,oldValue:n,newValue:u,meta:{levenDistance:r,similarity:m}},D=!0}else y=h(n,u,f,l),y.type!=="unchanged"&&(D=!0);o.push(y)}return D?{type:"changed",path:i,oldValue:e,newValue:t,children:o}:{type:"unchanged",path:i,oldValue:e,newValue:t}}var s={reset:"\x1B[0m",red:"\x1B[31m",green:"\x1B[32m",gray:"\x1B[90m"};function E(e,t={}){let{color:f=!0,full:i=!1,withSimilarity:o=!1}=t;return e.type==="unchanged"&&!i&&(!e.children||e.children.length===0)?"":!e.children||e.children.length===0?C(e,f,o):G(e,t,0)}function C(e,t,f=!1){let i=H(e.type),o="";switch(e.type){case"added":return o=c(e.newValue),t?`${s.green}${i} ${o}${s.reset}`:`${i} ${o}`;case"removed":return o=c(e.oldValue),t?`${s.red}${i} ${o}${s.reset}`:`${i} ${o}`;case"changed":let D=c(e.oldValue),$=c(e.newValue),l="";if(f&&e.meta?.similarity!==void 0&&typeof e.oldValue=="string"&&typeof e.newValue=="string"){let y=Math.round(e.meta.similarity*100);l=t?`${s.gray} (${y}% similar)${s.reset}`:` (${y}% similar)`}let n=t?`${s.red}- ${D}${s.reset}`:`- ${D}`,u=t?`${s.green}+ ${$}${l}${s.reset}`:`+ ${$}${l}`;return`${n}
2
- ${u}`;default:return o=c(e.newValue??e.oldValue),t?`${s.gray}${i} ${o}${s.reset}`:`${i} ${o}`}}function G(e,t,f=0){let{full:i=!1}=t;return e.type==="unchanged"&&!i&&(!e.children||e.children.length===0||typeof e.oldValue!="object"&&typeof e.newValue!="object")?"":Array.isArray(e.oldValue)||Array.isArray(e.newValue)?p(e,t,f):d(e,t,f)}function p(e,t,f=0){let{color:i=!0,full:o=!1,withSimilarity:D=!1}=t,$=" ".repeat(f),l=" ".repeat(f+2),n=`[
3
- `,u=0;if(e.children)for(let y=0;y<e.children.length;y++){let r=e.children[y];if(!(r.type==="unchanged"&&!o&&(!r.children||r.children.length===0)))if(r.children&&r.children.length>0){let a=Array.isArray(r.newValue||r.oldValue)?p(r,t,f+2):d(r,t,f+2);a.trim().length>2&&(u>0&&(n+=`,
4
- `),n+=l,n+=a,u++)}else switch(u>0&&(n+=`,
5
- `),r.type){case"added":n+=i?`${l}${s.green}+ ${c(r.newValue)}${s.reset}`:`${l}+ ${c(r.newValue)}`,u++;break;case"removed":n+=i?`${l}${s.red}- ${c(r.oldValue)}${s.reset}`:`${l}- ${c(r.oldValue)}`,u++;break;case"unchanged":r.meta?.ignored&&r.path?n+=i?`${l}${s.gray} ${r.path[r.path.length-1]||""}${s.reset}`:`${l} ${r.path[r.path.length-1]||""}`:n+=i?`${l}${s.gray} ${c(r.newValue)}${s.reset}`:`${l} ${c(r.newValue)}`,u++;break;case"changed":n+=i?`${l}${s.red}- ${c(r.oldValue)}${s.reset}
6
- ${l}${s.green}+ ${c(r.newValue)}${r.meta?.similarity!==void 0&&D?i?`${s.gray} (${Math.round(r.meta.similarity*100)}% similar)${s.reset}`:` (${Math.round(r.meta.similarity*100)}% similar)`:""}${s.reset}`:`${l}- ${c(r.oldValue)}
7
- ${l}+ ${c(r.newValue)}${r.meta?.similarity!==void 0&&D?` (${Math.round(r.meta.similarity*100)}% similar)`:""}`,u++;break}}return u>0&&(n+=`
8
- `),n+=`${$}]`,n}function d(e,t,f=0){let{color:i=!0,full:o=!1,withSimilarity:D=!1}=t,$=" ".repeat(f),l=" ".repeat(f+2),n=`{
9
- `,u=0;if(e.children){let y=e.children.filter(r=>{if(r.type!=="unchanged"||o||r.children&&r.children.length>0)return!0;let a=r.path?.[r.path.length-1]||"";return!!t.outputKeys?.includes(a)});for(let r=0;r<y.length;r++){let a=y[r],m=a.path?.[a.path.length-1]||"",N=r===y.length-1;if(u>0&&!n.endsWith(`
1
+ import V from"leven";var g=(o=>(o.ADDED="added",o.REMOVED="removed",o.CHANGED="changed",o.UNCHANGED="unchanged",o))(g||{});function h(e,t,u={},r=[]){let{keysOnly:o=!1,ignoreValues:D=!1}=u;if(e===void 0&&t===void 0)return{type:"unchanged"};if(e===void 0)return{type:"added",path:r,newValue:t};if(t===void 0)return{type:"removed",path:r,oldValue:e};if(typeof e!="object"||typeof t!="object"||e===null||t===null){if(o||D)return{type:"unchanged",path:r,oldValue:e,newValue:t,meta:{ignored:!0}};if(e===t)return{type:"unchanged",path:r,oldValue:e,newValue:t};if(typeof e=="string"&&typeof t=="string"){let $=V(e,t),l=Math.max(e.length,t.length),n=l>0?1-$/l:1;return{type:"changed",path:r,oldValue:e,newValue:t,meta:{levenDistance:$,similarity:n}}}return{type:"changed",path:r,oldValue:e,newValue:t}}return Array.isArray(e)&&Array.isArray(t)?C(e,t,u,r):A(e,t,u,r)}function A(e,t,u,r){let{ignoreKeys:o=[]}=u,D=new Set([...Object.keys(e).filter(n=>!o.includes(n)),...Object.keys(t).filter(n=>!o.includes(n))]),$=[],l=!1;for(let n of D){let f=[...r,n],y=e[n],i=t[n],a=h(y,i,u,f);a.type!=="unchanged"&&(l=!0),$.push(a)}return l?{type:"changed",path:r,oldValue:e,newValue:t,children:$}:{type:"unchanged",path:r,oldValue:e,newValue:t}}function C(e,t,u,r){if(e.length!==t.length){let $=[],l=Math.max(e.length,t.length);for(let n=0;n<l;n++){let f=[...r,n.toString()];n>=e.length?$.push({type:"added",path:f,newValue:t[n]}):n>=t.length?$.push({type:"removed",path:f,oldValue:e[n]}):$.push(h(e[n],t[n],u,f))}return{type:"changed",path:r,oldValue:e,newValue:t,children:$}}let o=[],D=!1;for(let $=0;$<e.length;$++){let l=[...r,$.toString()],n=e[$],f=t[$],y;if(typeof n=="string"&&typeof f=="string")if(n===f)y={type:"unchanged",path:l,oldValue:n,newValue:f};else{let i=V(n,f),a=Math.max(n.length,f.length),m=a>0?1-i/a:1;y={type:"changed",path:l,oldValue:n,newValue:f,meta:{levenDistance:i,similarity:m}},D=!0}else y=h(n,f,u,l),y.type!=="unchanged"&&(D=!0);o.push(y)}return D?{type:"changed",path:r,oldValue:e,newValue:t,children:o}:{type:"unchanged",path:r,oldValue:e,newValue:t}}var s={reset:"\x1B[0m",red:"\x1B[31m",green:"\x1B[32m",gray:"\x1B[90m"};function E(e,t={}){let{color:u=!0,full:r=!1,withSimilarity:o=!1}=t;return e.type==="unchanged"&&!r&&(!e.children||e.children.length===0)?"":!e.children||e.children.length===0?G(e,u,o):H(e,t,0)}function G(e,t,u=!1){let r=x(e.type),o="";switch(e.type){case"added":return o=c(e.newValue),t?`${s.green}${r} ${o}${s.reset}`:`${r} ${o}`;case"removed":return o=c(e.oldValue),t?`${s.red}${r} ${o}${s.reset}`:`${r} ${o}`;case"changed":let D=c(e.oldValue),$=c(e.newValue),l="";if(u&&e.meta?.similarity!==void 0&&typeof e.oldValue=="string"&&typeof e.newValue=="string"){let y=Math.round(e.meta.similarity*100);l=t?`${s.gray} (${y}% similar)${s.reset}`:` (${y}% similar)`}let n=t?`${s.red}- ${D}${s.reset}`:`- ${D}`,f=t?`${s.green}+ ${$}${l}${s.reset}`:`+ ${$}${l}`;return`${n}
2
+ ${f}`;default:return o=c(e.newValue??e.oldValue),t?`${s.gray}${r} ${o}${s.reset}`:`${r} ${o}`}}function H(e,t,u=0){let{full:r=!1}=t;return e.type==="unchanged"&&!r&&(!e.children||e.children.length===0||typeof e.oldValue!="object"&&typeof e.newValue!="object")?"":Array.isArray(e.oldValue)||Array.isArray(e.newValue)?p(e,t,u):d(e,t,u)}function p(e,t,u=0){let{color:r=!0,full:o=!1,withSimilarity:D=!1}=t,$=" ".repeat(u),l=" ".repeat(u+2),n=`[
3
+ `,f=0;if(e.children)for(let y=0;y<e.children.length;y++){let i=e.children[y];if(!(i.type==="unchanged"&&!o&&(!i.children||i.children.length===0)))if(i.children&&i.children.length>0){let a=Array.isArray(i.newValue||i.oldValue)?p(i,t,u+2):d(i,t,u+2);a.trim().length>2&&(f>0&&(n+=`,
4
+ `),n+=l,n+=a,f++)}else switch(f>0&&(n+=`,
5
+ `),i.type){case"added":n+=r?`${l}${s.green}+ ${c(i.newValue)}${s.reset}`:`${l}+ ${c(i.newValue)}`,f++;break;case"removed":n+=r?`${l}${s.red}- ${c(i.oldValue)}${s.reset}`:`${l}- ${c(i.oldValue)}`,f++;break;case"unchanged":i.meta?.ignored&&i.path?n+=r?`${l}${s.gray} ${i.path[i.path.length-1]||""}${s.reset}`:`${l} ${i.path[i.path.length-1]||""}`:n+=r?`${l}${s.gray} ${c(i.newValue)}${s.reset}`:`${l} ${c(i.newValue)}`,f++;break;case"changed":n+=r?`${l}${s.red}- ${c(i.oldValue)}${s.reset}
6
+ ${l}${s.green}+ ${c(i.newValue)}${i.meta?.similarity!==void 0&&D?r?`${s.gray} (${Math.round(i.meta.similarity*100)}% similar)${s.reset}`:` (${Math.round(i.meta.similarity*100)}% similar)`:""}${s.reset}`:`${l}- ${c(i.oldValue)}
7
+ ${l}+ ${c(i.newValue)}${i.meta?.similarity!==void 0&&D?` (${Math.round(i.meta.similarity*100)}% similar)`:""}`,f++;break}}return f>0&&(n+=`
8
+ `),n+=`${$}]`,n}function d(e,t,u=0){let{color:r=!0,full:o=!1,withSimilarity:D=!1}=t,$=" ".repeat(u),l=" ".repeat(u+2),n=`{
9
+ `,f=0;if(e.children){let y=e.children.filter(i=>{if(i.type!=="unchanged"||o||i.children&&i.children.length>0)return!0;let a=i.path?.[i.path.length-1]||"";return!!t.outputKeys?.includes(a)});for(let i=0;i<y.length;i++){let a=y[i],m=a.path?.[a.path.length-1]||"",R=i===y.length-1;if(f>0&&!n.endsWith(`
10
10
  `)&&(n+=`,
11
- `),a.children&&a.children.length>0){let b=Array.isArray(a.newValue||a.oldValue)?p(a,t,f+2):d(a,t,f+2);b.trim().length>2&&(n+=`${l}${m}: ${b}`,u++)}else switch(a.type){case"added":n+=i?`${l}${s.green}+ ${m}: ${c(a.newValue)}${s.reset}`:`${l}+ ${m}: ${c(a.newValue)}`,u++;break;case"removed":n+=i?`${l}${s.red}- ${m}: ${c(a.oldValue)}${s.reset}`:`${l}- ${m}: ${c(a.oldValue)}`,u++;break;case"changed":n+=i?`${l}${s.red}- ${m}: ${c(a.oldValue)}${s.reset}
12
- ${l}${s.green}+ ${m}: ${c(a.newValue)}${a.meta?.similarity!==void 0&&D?i?`${s.gray} (${Math.round(a.meta.similarity*100)}% similar)${s.reset}`:` (${Math.round(a.meta.similarity*100)}% similar)`:""}${s.reset}`:`${l}- ${m}: ${c(a.oldValue)}
13
- ${l}+ ${m}: ${c(a.newValue)}${a.meta?.similarity!==void 0&&D?` (${Math.round(a.meta.similarity*100)}% similar)`:""}`,u++;break;case"unchanged":(o||t.outputKeys?.includes(m))&&(a.meta?.ignored?n+=i?`${l}${s.gray} ${m}${s.reset}`:`${l} ${m}`:n+=i?`${l}${s.gray} ${m}: ${c(a.newValue)}${s.reset}`:`${l} ${m}: ${c(a.newValue)}`,u++);break}!N&&!n.endsWith(`
11
+ `),a.children&&a.children.length>0){let b=Array.isArray(a.newValue||a.oldValue)?p(a,t,u+2):d(a,t,u+2);b.trim().length>2&&(n+=`${l}${m}: ${b}`,f++)}else switch(a.type){case"added":n+=r?`${l}${s.green}+ ${m}: ${c(a.newValue)}${s.reset}`:`${l}+ ${m}: ${c(a.newValue)}`,f++;break;case"removed":n+=r?`${l}${s.red}- ${m}: ${c(a.oldValue)}${s.reset}`:`${l}- ${m}: ${c(a.oldValue)}`,f++;break;case"changed":n+=r?`${l}${s.red}- ${m}: ${c(a.oldValue)}${s.reset}
12
+ ${l}${s.green}+ ${m}: ${c(a.newValue)}${a.meta?.similarity!==void 0&&D?r?`${s.gray} (${Math.round(a.meta.similarity*100)}% similar)${s.reset}`:` (${Math.round(a.meta.similarity*100)}% similar)`:""}${s.reset}`:`${l}- ${m}: ${c(a.oldValue)}
13
+ ${l}+ ${m}: ${c(a.newValue)}${a.meta?.similarity!==void 0&&D?` (${Math.round(a.meta.similarity*100)}% similar)`:""}`,f++;break;case"unchanged":(o||t.outputKeys?.includes(m))&&(a.meta?.ignored?n+=r?`${l}${s.gray} ${m}${s.reset}`:`${l} ${m}`:n+=r?`${l}${s.gray} ${m}: ${c(a.newValue)}${s.reset}`:`${l} ${m}: ${c(a.newValue)}`,f++);break}!R&&!n.endsWith(`
14
14
  `)&&(n+=","),n.endsWith(`
15
15
  `)||(n+=`
16
- `)}}return u>0&&!n.endsWith(`
16
+ `)}}return f>0&&!n.endsWith(`
17
17
  `)&&(n+=`
18
- `),n+=`${$}}`,n}function H(e){switch(e){case"added":return"+";case"removed":return"-";case"changed":return"!";default:return" "}}function c(e){return e===void 0?"undefined":e===null?"null":typeof e=="string"?`'${e}'`:typeof e=="number"||typeof e=="boolean"?e.toString():typeof e=="object"?JSON.stringify(e,null,2):String(e)}function x(e,t,f={}){return h(e,t,f)}function B(e,t,f={}){let i=x(e,t,f);return E(i,f)}export{g as DiffType,B as diff,x as diffRaw};
18
+ `),n+=`${$}}`,n}function x(e){switch(e){case"added":return"+";case"removed":return"-";case"changed":return"!";default:return" "}}function c(e){return e===void 0?"undefined":e===null?"null":typeof e=="string"?`'${e}'`:typeof e=="number"||typeof e=="boolean"?e.toString():typeof e=="object"?JSON.stringify(e,null,2):String(e)}function N(e,t,u={}){return h(e,t,u)}function B(e,t,u={}){let r=N(e,t,u);return E(r,u)}function F(e,t,u={}){return N(e,t,u).type!=="unchanged"}export{g as DiffType,B as diff,N as diffRaw,F as isDiff};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "diff-leven",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Git like diff between two strings, using the Levenshtein distance algorithm",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",