From 8835b1d39b8678cbd03c8668cb0636b599b4353a Mon Sep 17 00:00:00 2001 From: lukascivil Date: Sat, 22 Jul 2023 10:20:30 -0300 Subject: [PATCH] wip: to object --- dist.browser/json-difference.mjs | 318 +++++++++++++++++++++--- dist.browser/json-difference.umd.js | 2 +- package.json | 5 + src/core/get-diff.spec.ts | 29 ++- src/core/get-diff.ts | 31 ++- src/core/index.ts | 1 + src/core/transform-delta-into-object.ts | 23 ++ src/models/jsondiffer.model.ts | 6 + tsconfig.json | 1 + yarn.lock | 22 ++ 10 files changed, 396 insertions(+), 42 deletions(-) create mode 100644 src/core/transform-delta-into-object.ts diff --git a/dist.browser/json-difference.mjs b/dist.browser/json-difference.mjs index 7810e99..c957834 100644 --- a/dist.browser/json-difference.mjs +++ b/dist.browser/json-difference.mjs @@ -1,38 +1,298 @@ -const c = (e, i) => { - const o = []; - for (const n in e) - if (i.hasOwnProperty(n)) { - if (typeof e[n] == "object" && typeof i[n] == "object" && JSON.stringify(e[n]) === JSON.stringify(i[n])) +const M = (t, e) => { + const n = []; + for (const r in t) + if (e.hasOwnProperty(r)) { + if (typeof t[r] == "object" && typeof e[r] == "object" && JSON.stringify(t[r]) === JSON.stringify(e[r])) continue; - e[n] !== i[n] && o.push([n, e[n], i[n]]); + t[r] !== e[r] && n.push([r, t[r], e[r]]); } - return o; -}, y = (e, i) => { - const o = []; - let n = 0; - for (const f in e) - f in i || (o[n] = [f, e[f]], n++); - return o; -}, g = (e, i, o, n) => { - const f = n ? e ? "[" : "." : "/", t = n ? e ? "]" : "" : e ? "[]" : ""; - return i !== "" ? `${i}${f}${o}${t}` : `${n && e ? "[" : ""}${o}${t}`; -}, d = (e, i = !1, o = {}, n = "") => { - for (const f of Object.keys(e)) { - const t = g(Array.isArray(e), n, f, i); - typeof e[f] == "object" && e[f] !== null ? (Object.keys(e[f]).length === 0 && (o[t] = e[f]), d(e[f], i, o, t)) : o[t] = e[f]; + return n; +}, $ = (t, e) => { + const n = []; + let r = 0; + for (const o in t) + o in e || (n[r] = [o, t[o]], r++); + return n; +}, L = (t, e, n, r) => { + const o = r ? t ? "[" : "." : "/", i = r ? t ? "]" : "" : t ? "[]" : ""; + return e !== "" ? `${e}${o}${n}${i}` : `${r && t ? "[" : ""}${n}${i}`; +}, C = (t, e = !1, n = {}, r = "") => { + for (const o of Object.keys(t)) { + const i = L(Array.isArray(t), r, o, e); + typeof t[o] == "object" && t[o] !== null ? (Object.keys(t[o]).length === 0 && (n[i] = t[o]), C(t[o], e, n, i)) : n[i] = t[o]; } - return o; -}, $ = (e, i, o = !1) => { - const n = { + return n; +}; +var l = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {}, K = "Expected a function", D = "__lodash_hash_undefined__", N = 1 / 0, J = 9007199254740991, U = "[object Function]", X = "[object GeneratorFunction]", q = "[object Symbol]", z = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/, Y = /^\w*$/, B = /^\./, Q = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g, W = /[\\^$.*+?()[\]{}|]/g, Z = /\\(\\)?/g, V = /^\[object .+?Constructor\]$/, k = /^(?:0|[1-9]\d*)$/, tt = typeof l == "object" && l && l.Object === Object && l, et = typeof self == "object" && self && self.Object === Object && self, x = tt || et || Function("return this")(); +function nt(t, e) { + return t == null ? void 0 : t[e]; +} +function rt(t) { + var e = !1; + if (t != null && typeof t.toString != "function") + try { + e = !!(t + ""); + } catch { + } + return e; +} +var ot = Array.prototype, it = Function.prototype, P = Object.prototype, O = x["__core-js_shared__"], T = function() { + var t = /[^.]+$/.exec(O && O.keys && O.keys.IE_PROTO || ""); + return t ? "Symbol(src)_1." + t : ""; +}(), F = it.toString, y = P.hasOwnProperty, H = P.toString, at = RegExp( + "^" + F.call(y).replace(W, "\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, "$1.*?") + "$" +), I = x.Symbol, st = ot.splice, ft = G(x, "Map"), d = G(Object, "create"), w = I ? I.prototype : void 0, E = w ? w.toString : void 0; +function a(t) { + var e = -1, n = t ? t.length : 0; + for (this.clear(); ++e < n; ) { + var r = t[e]; + this.set(r[0], r[1]); + } +} +function ct() { + this.__data__ = d ? d(null) : {}; +} +function ut(t) { + return this.has(t) && delete this.__data__[t]; +} +function dt(t) { + var e = this.__data__; + if (d) { + var n = e[t]; + return n === D ? void 0 : n; + } + return y.call(e, t) ? e[t] : void 0; +} +function pt(t) { + var e = this.__data__; + return d ? e[t] !== void 0 : y.call(e, t); +} +function ht(t, e) { + var n = this.__data__; + return n[t] = d && e === void 0 ? D : e, this; +} +a.prototype.clear = ct; +a.prototype.delete = ut; +a.prototype.get = dt; +a.prototype.has = pt; +a.prototype.set = ht; +function c(t) { + var e = -1, n = t ? t.length : 0; + for (this.clear(); ++e < n; ) { + var r = t[e]; + this.set(r[0], r[1]); + } +} +function lt() { + this.__data__ = []; +} +function _t(t) { + var e = this.__data__, n = g(e, t); + if (n < 0) + return !1; + var r = e.length - 1; + return n == r ? e.pop() : st.call(e, n, 1), !0; +} +function yt(t) { + var e = this.__data__, n = g(e, t); + return n < 0 ? void 0 : e[n][1]; +} +function gt(t) { + return g(this.__data__, t) > -1; +} +function bt(t, e) { + var n = this.__data__, r = g(n, t); + return r < 0 ? n.push([t, e]) : n[r][1] = e, this; +} +c.prototype.clear = lt; +c.prototype.delete = _t; +c.prototype.get = yt; +c.prototype.has = gt; +c.prototype.set = bt; +function s(t) { + var e = -1, n = t ? t.length : 0; + for (this.clear(); ++e < n; ) { + var r = t[e]; + this.set(r[0], r[1]); + } +} +function mt() { + this.__data__ = { + hash: new a(), + map: new (ft || c)(), + string: new a() + }; +} +function Ot(t) { + return b(this, t).delete(t); +} +function vt(t) { + return b(this, t).get(t); +} +function Ct(t) { + return b(this, t).has(t); +} +function xt(t, e) { + return b(this, t).set(t, e), this; +} +s.prototype.clear = mt; +s.prototype.delete = Ot; +s.prototype.get = vt; +s.prototype.has = Ct; +s.prototype.set = xt; +function St(t, e, n) { + var r = t[e]; + (!(y.call(t, e) && R(r, n)) || n === void 0 && !(e in t)) && (t[e] = n); +} +function g(t, e) { + for (var n = t.length; n--; ) + if (R(t[n][0], e)) + return n; + return -1; +} +function jt(t) { + if (!_(t) || Nt(t)) + return !1; + var e = Gt(t) || rt(t) ? at : V; + return e.test(Ht(t)); +} +function $t(t, e, n, r) { + if (!_(t)) + return t; + e = Et(e, t) ? [e] : It(e); + for (var o = -1, i = e.length, u = i - 1, f = t; f != null && ++o < i; ) { + var p = Ft(e[o]), h = n; + if (o != u) { + var m = f[p]; + h = r ? r(m, p, f) : void 0, h === void 0 && (h = _(m) ? m : wt(e[o + 1]) ? [] : {}); + } + St(f, p, h), f = f[p]; + } + return t; +} +function Tt(t) { + if (typeof t == "string") + return t; + if (j(t)) + return E ? E.call(t) : ""; + var e = t + ""; + return e == "0" && 1 / t == -N ? "-0" : e; +} +function It(t) { + return A(t) ? t : Pt(t); +} +function b(t, e) { + var n = t.__data__; + return Dt(e) ? n[typeof e == "string" ? "string" : "hash"] : n.map; +} +function G(t, e) { + var n = nt(t, e); + return jt(n) ? n : void 0; +} +function wt(t, e) { + return e = e ?? J, !!e && (typeof t == "number" || k.test(t)) && t > -1 && t % 1 == 0 && t < e; +} +function Et(t, e) { + if (A(t)) + return !1; + var n = typeof t; + return n == "number" || n == "symbol" || n == "boolean" || t == null || j(t) ? !0 : Y.test(t) || !z.test(t) || e != null && t in Object(e); +} +function Dt(t) { + var e = typeof t; + return e == "string" || e == "number" || e == "symbol" || e == "boolean" ? t !== "__proto__" : t === null; +} +function Nt(t) { + return !!T && T in t; +} +var Pt = S(function(t) { + t = At(t); + var e = []; + return B.test(t) && e.push(""), t.replace(Q, function(n, r, o, i) { + e.push(o ? i.replace(Z, "$1") : r || n); + }), e; +}); +function Ft(t) { + if (typeof t == "string" || j(t)) + return t; + var e = t + ""; + return e == "0" && 1 / t == -N ? "-0" : e; +} +function Ht(t) { + if (t != null) { + try { + return F.call(t); + } catch { + } + try { + return t + ""; + } catch { + } + } + return ""; +} +function S(t, e) { + if (typeof t != "function" || e && typeof e != "function") + throw new TypeError(K); + var n = function() { + var r = arguments, o = e ? e.apply(this, r) : r[0], i = n.cache; + if (i.has(o)) + return i.get(o); + var u = t.apply(this, r); + return n.cache = i.set(o, u), u; + }; + return n.cache = new (S.Cache || s)(), n; +} +S.Cache = s; +function R(t, e) { + return t === e || t !== t && e !== e; +} +var A = Array.isArray; +function Gt(t) { + var e = _(t) ? H.call(t) : ""; + return e == U || e == X; +} +function _(t) { + var e = typeof t; + return !!t && (e == "object" || e == "function"); +} +function Rt(t) { + return !!t && typeof t == "object"; +} +function j(t) { + return typeof t == "symbol" || Rt(t) && H.call(t) == q; +} +function At(t) { + return t == null ? "" : Tt(t); +} +function Mt(t, e, n) { + return t == null ? t : $t(t, e, n); +} +var v = Mt; +const Lt = (t) => { + const e = t.removed.map((o) => { + const i = {}; + return v(i, o[0], o[1]), i; + }), n = t.edited.map((o) => v({}, "cafe/123", "8888")), r = t.added.map((o) => v({}, o[0], o[1])); + return { + removed: e, + edited: n, + added: r + }; +}, Kt = { + isLodashLike: !1 +}, Jt = (t, e, n = Kt) => { + let r = { added: [], removed: [], edited: [] - }, f = d(e, o), t = d(i, o); - return n.removed = y(f, t), n.added = y(t, f), n.edited = c(f, t), n; + }; + const o = C(t, n.isLodashLike), i = C(e, n.isLodashLike); + return r.removed = $(o, i), r.added = $(i, o), r.edited = M(o, i), n.isObjectOutput && (r = Lt(r)), r; }; export { - $ as getDiff, - c as getEditedPaths, - y as getPathsDiff, - d as getStructPaths + Jt as getDiff, + M as getEditedPaths, + $ as getPathsDiff, + C as getStructPaths }; diff --git a/dist.browser/json-difference.umd.js b/dist.browser/json-difference.umd.js index 8a8af29..947aa2b 100644 --- a/dist.browser/json-difference.umd.js +++ b/dist.browser/json-difference.umd.js @@ -1 +1 @@ -(function(d,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(d=typeof globalThis<"u"?globalThis:d||self,c(d["json-difference"]={}))})(this,function(d){"use strict";const c=(e,i)=>{const t=[];for(const f in e)if(i.hasOwnProperty(f)){if(typeof e[f]=="object"&&typeof i[f]=="object"&&JSON.stringify(e[f])===JSON.stringify(i[f]))continue;e[f]!==i[f]&&t.push([f,e[f],i[f]])}return t},g=(e,i)=>{const t=[];let f=0;for(const n in e)n in i||(t[f]=[n,e[n]],f++);return t},y=(e,i,t,f)=>{const n=f?e?"[":".":"/",o=f?e?"]":"":e?"[]":"";return i!==""?`${i}${n}${t}${o}`:`${f&&e?"[":""}${t}${o}`},s=(e,i=!1,t={},f="")=>{for(const n of Object.keys(e)){const o=y(Array.isArray(e),f,n,i);typeof e[n]=="object"&&e[n]!==null?(Object.keys(e[n]).length===0&&(t[o]=e[n]),s(e[n],i,t,o)):t[o]=e[n]}return t},p=(e,i,t=!1)=>{const f={added:[],removed:[],edited:[]},n=s(e,t),o=s(i,t);return f.removed=g(n,o),f.added=g(o,n),f.edited=c(n,o),f};d.getDiff=p,d.getEditedPaths=c,d.getPathsDiff=g,d.getStructPaths=s,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}); +(function(a,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(a=typeof globalThis<"u"?globalThis:a||self,c(a["json-difference"]={}))})(this,function(a){"use strict";const c=(t,e)=>{const n=[];for(const r in t)if(e.hasOwnProperty(r)){if(typeof t[r]=="object"&&typeof e[r]=="object"&&JSON.stringify(t[r])===JSON.stringify(e[r]))continue;t[r]!==e[r]&&n.push([r,t[r],e[r]])}return n},C=(t,e)=>{const n=[];let r=0;for(const o in t)o in e||(n[r]=[o,t[o]],r++);return n},K=(t,e,n,r)=>{const o=r?t?"[":".":"/",i=r?t?"]":"":t?"[]":"";return e!==""?`${e}${o}${n}${i}`:`${r&&t?"[":""}${n}${i}`},l=(t,e=!1,n={},r="")=>{for(const o of Object.keys(t)){const i=K(Array.isArray(t),r,o,e);typeof t[o]=="object"&&t[o]!==null?(Object.keys(t[o]).length===0&&(n[i]=t[o]),l(t[o],e,n,i)):n[i]=t[o]}return n};var _=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},J="Expected a function",D="__lodash_hash_undefined__",E=1/0,U=9007199254740991,X="[object Function]",q="[object GeneratorFunction]",z="[object Symbol]",Y=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,B=/^\w*$/,Q=/^\./,W=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,Z=/[\\^$.*+?()[\]{}|]/g,V=/\\(\\)?/g,k=/^\[object .+?Constructor\]$/,tt=/^(?:0|[1-9]\d*)$/,et=typeof _=="object"&&_&&_.Object===Object&&_,nt=typeof self=="object"&&self&&self.Object===Object&&self,S=et||nt||Function("return this")();function rt(t,e){return t==null?void 0:t[e]}function ot(t){var e=!1;if(t!=null&&typeof t.toString!="function")try{e=!!(t+"")}catch{}return e}var it=Array.prototype,at=Function.prototype,w=Object.prototype,j=S["__core-js_shared__"],P=function(){var t=/[^.]+$/.exec(j&&j.keys&&j.keys.IE_PROTO||"");return t?"Symbol(src)_1."+t:""}(),N=at.toString,y=w.hasOwnProperty,F=w.toString,ft=RegExp("^"+N.call(y).replace(Z,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$"),H=S.Symbol,st=it.splice,ct=R(S,"Map"),p=R(Object,"create"),G=H?H.prototype:void 0,M=G?G.toString:void 0;function f(t){var e=-1,n=t?t.length:0;for(this.clear();++e-1}function mt(t,e){var n=this.__data__,r=g(n,t);return r<0?n.push([t,e]):n[r][1]=e,this}d.prototype.clear=_t,d.prototype.delete=yt,d.prototype.get=gt,d.prototype.has=bt,d.prototype.set=mt;function s(t){var e=-1,n=t?t.length:0;for(this.clear();++e-1&&t%1==0&&t{const e=t.removed.map(o=>{const i={};return x(i,o[0],o[1]),i}),n=t.edited.map(o=>x({},"cafe/123","8888")),r=t.added.map(o=>x({},o[0],o[1]));return{removed:e,edited:n,added:r}},Jt={isLodashLike:!1},Ut=(t,e,n=Jt)=>{let r={added:[],removed:[],edited:[]};const o=l(t,n.isLodashLike),i=l(e,n.isLodashLike);return r.removed=C(o,i),r.added=C(i,o),r.edited=c(o,i),n.isObjectOutput&&(r=Kt(r)),r};a.getDiff=Ut,a.getEditedPaths=c,a.getPathsDiff=C,a.getStructPaths=l,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}); diff --git a/package.json b/package.json index 11c9375..debd806 100644 --- a/package.json +++ b/package.json @@ -49,5 +49,10 @@ "typescript": "4.4.3", "vite": "^4.0.4", "vite-tsconfig-paths": "^4.0.5" + }, + "dependencies": { + "@types/lodash.set": "^4.3.7", + "lodash.set": "^4.3.2", + "remeda": "^1.6.1" } } diff --git a/src/core/get-diff.spec.ts b/src/core/get-diff.spec.ts index 5e703a4..4279c45 100644 --- a/src/core/get-diff.spec.ts +++ b/src/core/get-diff.spec.ts @@ -12,7 +12,7 @@ describe('GetDiff function', () => { const expectedLodashResult: Delta = { edited: [['1.3.4', 6, 5]], added: [], removed: [['1.2', 7]] } const result = getDiff(struct1, struct2) - const lodashResult = getDiff(struct1, struct2, true) + const lodashResult = getDiff(struct1, struct2, { isLodashLike: true }) expect(result).toEqual(expectedResult) expect(lodashResult).toEqual(expectedLodashResult) @@ -39,7 +39,7 @@ describe('GetDiff function', () => { } const result = getDiff(struct1, struct2) - const lodashResult = getDiff(struct1, struct2, true) + const lodashResult = getDiff(struct1, struct2, { isLodashLike: true }) expect(result).toEqual(expectedResult) expect(lodashResult).toEqual(expectedLodashResult) @@ -66,7 +66,7 @@ describe('GetDiff function', () => { } const result = getDiff(struct1, struct2) - const lodashResult = getDiff(struct1, struct2, true) + const lodashResult = getDiff(struct1, struct2, { isLodashLike: true }) expect(result).toEqual(expectedResult) expect(lodashResult).toEqual(expectedLodashResult) @@ -93,7 +93,7 @@ describe('GetDiff function', () => { } const result = getDiff(struct1, struct2) - const lodashResult = getDiff(struct1, struct2, true) + const lodashResult = getDiff(struct1, struct2, { isLodashLike: true }) expect(result).toEqual(expectedResult) expect(expectedLodashResult).toEqual(lodashResult) @@ -109,7 +109,7 @@ describe('GetDiff function', () => { } const result = getDiff(oldStruct, newStruct) - const lodashResult = getDiff(oldStruct, newStruct, true) + const lodashResult = getDiff(oldStruct, newStruct, { isLodashLike: true }) expect(result).toEqual(expectedResult) expect(lodashResult).toEqual(expectedResult) @@ -130,7 +130,7 @@ describe('GetDiff function', () => { } const result = getDiff(oldStruct, newStruct) - const lodashResult = getDiff(oldStruct, newStruct, true) + const lodashResult = getDiff(oldStruct, newStruct, { isLodashLike: true }) expect(result).toEqual(expectedResult) expect(lodashResult).toEqual(expectedResult) @@ -151,7 +151,7 @@ describe('GetDiff function', () => { } const result = getDiff(struct1, struct2) - const lodashResult = getDiff(struct1, struct2, true) + const lodashResult = getDiff(struct1, struct2, { isLodashLike: true }) expect(result).toEqual(expectedResult) expect(lodashResult).toEqual(expectedLodashResult) @@ -164,9 +164,22 @@ describe('GetDiff function', () => { const expectedLodashResult: Delta = { edited: [['1', null, '']], added: [['2', null]], removed: [] } const result = getDiff(struct1, struct2) - const lodashResult = getDiff(struct1, struct2, true) + const lodashResult = getDiff(struct1, struct2, { isLodashLike: true }) expect(result).toEqual(expectedResult) expect(lodashResult).toEqual(expectedLodashResult) }) + + test.only('Should return the difference between two basic structures', () => { + const struct1 = { 1: { 2: 7, 3: { 4: 6 } } } + const struct2 = { 1: { 3: { 4: 5 } } } + const expectedResult: Delta = { edited: [['1/3/4', 6, 5]], added: [], removed: [['1/2', 7]] } + // const expectedLodashResult: Delta = { edited: [['1.3.4', 6, 5]], added: [], removed: [['1.2', 7]] } + + const result = getDiff(struct1, struct2, { isObjectOutput: true }) + // const lodashResult = getDiff(struct1, struct2, true) + + expect(result).toEqual(expectedResult) + // expect(lodashResult).toEqual(expectedLodashResult) + }) }) diff --git a/src/core/get-diff.ts b/src/core/get-diff.ts index c0e9e54..ed984af 100644 --- a/src/core/get-diff.ts +++ b/src/core/get-diff.ts @@ -5,15 +5,34 @@ import { getStructPaths } from './get-struct-paths' // Models import { Delta } from '../models/jsondiffer.model' +import transformDeltaToObject from './transform-delta-into-object' -export const getDiff = (oldStruct: Record, newStruct: Record, isLodashLike = false): Delta => { - const delta: Delta = { +interface OptionsA { + isLodashLike: boolean + isObjectOutput?: never +} + +interface OptionsB { + isLodashLike?: never + isObjectOutput: boolean +} + +const DefaultOptions = { + isLodashLike: false +} + +export const getDiff = ( + oldStruct: Record, + newStruct: Record, + options: OptionsA | OptionsB = DefaultOptions +): Delta => { + let delta: Delta = { added: [], removed: [], edited: [] } - const oldStructPaths = getStructPaths(oldStruct, isLodashLike) - const newStructPaths = getStructPaths(newStruct, isLodashLike) + const oldStructPaths = getStructPaths(oldStruct, options.isLodashLike) + const newStructPaths = getStructPaths(newStruct, options.isLodashLike) // A-B delta.removed = getPathsDiff(oldStructPaths, newStructPaths) @@ -24,5 +43,9 @@ export const getDiff = (oldStruct: Record, newStruct: Recordb delta.edited = getEditedPaths(oldStructPaths, newStructPaths) + if (options.isObjectOutput) { + delta = transformDeltaToObject(delta) + } + return delta } diff --git a/src/core/index.ts b/src/core/index.ts index 0617ebe..af0e822 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -2,3 +2,4 @@ export * from './get-diff' export * from './get-paths-diff' export * from './get-struct-paths' export * from './get-edited-paths' +export * from './get-edited-paths' diff --git a/src/core/transform-delta-into-object.ts b/src/core/transform-delta-into-object.ts new file mode 100644 index 0000000..8379b25 --- /dev/null +++ b/src/core/transform-delta-into-object.ts @@ -0,0 +1,23 @@ +// Packages +import set from 'lodash.set' +import { Delta, DeltaObject } from '../models' + +const transformDeltaToObject = (delta: Delta): DeltaObject => { + const removed = delta.removed.map((item) => { + const newObject = {} + + set(newObject, item[0], item[1]) + + return newObject + }) + const edited = delta.edited.map((item) => set({}, 'cafe/123', '8888')) + const added = delta.added.map((item) => set({}, item[0], item[1])) + + return { + removed, + edited, + added + } +} + +export default transformDeltaToObject diff --git a/src/models/jsondiffer.model.ts b/src/models/jsondiffer.model.ts index 52ff833..a0b2cb2 100644 --- a/src/models/jsondiffer.model.ts +++ b/src/models/jsondiffer.model.ts @@ -9,3 +9,9 @@ export interface Delta { removed: Array edited: Array } + +export interface DeltaObject { + added: Array + removed: Array + edited: Array +} diff --git a/tsconfig.json b/tsconfig.json index 3a4e948..b3f5c3b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "module": "commonjs", "declaration": true, "outDir": "./dist", + "esModuleInterop": true, "strict": true }, "include": ["src"], diff --git a/yarn.lock b/yarn.lock index 4f82af7..3810a4a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1025,6 +1025,18 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ== +"@types/lodash.set@^4.3.7": + version "4.3.7" + resolved "https://registry.yarnpkg.com/@types/lodash.set/-/lodash.set-4.3.7.tgz#784fccea3fbef4d0949d1897a780f592da700942" + integrity sha512-bS5Wkg/nrT82YUfkNYPSccFrNZRL+irl7Yt4iM6OTSQ0VZJED2oUIVm15NkNtUAQ8SRhCe+axqERUV6MJgkeEg== + dependencies: + "@types/lodash" "*" + +"@types/lodash@*": + version "4.14.191" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.191.tgz#09511e7f7cba275acd8b419ddac8da9a6a79e2fa" + integrity sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ== + "@types/node@*": version "16.11.11" resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.11.tgz#6ea7342dfb379ea1210835bada87b3c512120234" @@ -3145,6 +3157,11 @@ lodash.merge@^4.6.2: resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a" integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ== +lodash.set@^4.3.2: + version "4.3.2" + resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" + integrity sha512-4hNPN5jlm/N/HLMCO43v8BXKq9Z7QdAGc/VGrRD61w8gN9g/6jF9A4L1pbUgBLCffi0w9VsXfTOij5x8iTyFvg== + lru-cache@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94" @@ -3514,6 +3531,11 @@ regexpp@^3.0.0, regexpp@^3.2.0: resolved "https://registry.yarnpkg.com/regexpp/-/regexpp-3.2.0.tgz#0425a2768d8f23bad70ca4b90461fa2f1213e1b2" integrity sha512-pq2bWo9mVD43nbts2wGv17XLiNLya+GklZ8kaDLV2Z08gDCsGpnKn9BFMepvWuHCbyVvY7J5o5+BVvoQbmlJLg== +remeda@^1.6.1: + version "1.6.1" + resolved "https://registry.yarnpkg.com/remeda/-/remeda-1.6.1.tgz#9cd7b53217b90bd9bafffadefe883c4120f4dcde" + integrity sha512-ywn4Tv2cPBj6hk0TEYMHusWVXu72yJNCaWUdMK7kc7x/PjQPBSvtMbmBWP4PQH791kpl3rPBe3ZVkJyPj3c+0g== + require-directory@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"