Skip to content

Commit

Permalink
wip: to object
Browse files Browse the repository at this point in the history
  • Loading branch information
lukascivil committed Jul 23, 2023
1 parent ad1bf8a commit bed82f2
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 2 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,10 @@
"typescript": "5.1.6",
"vite": "^4.4.6",
"vite-tsconfig-paths": "^4.2.0"
},
"dependencies": {
"@types/lodash.set": "^4.3.7",
"lodash.set": "^4.3.2",
"remeda": "^1.6.1"
}
}
13 changes: 13 additions & 0 deletions src/core/get-diff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,17 @@ describe('GetDiff function', () => {
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)
})
})
8 changes: 6 additions & 2 deletions src/core/get-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export const getDiff = (oldStruct: Record<string, any>, newStruct: Record<string
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)
Expand All @@ -48,5 +48,9 @@ export const getDiff = (oldStruct: Record<string, any>, newStruct: Record<string
// a->b
delta.edited = getEditedPaths(oldStructPaths, newStructPaths)

if (options.isObjectOutput) {
delta = transformDeltaToObject(delta)
}

return delta
}
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
23 changes: 23 additions & 0 deletions src/core/transform-delta-into-object.ts
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"esModuleInterop": true,
"strict": true
},
"include": ["src"],
Expand Down
22 changes: 22 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,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"
Expand Down Expand Up @@ -3327,6 +3339,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"
Expand Down Expand Up @@ -3725,6 +3742,11 @@ regexp.prototype.flags@^1.4.3:
define-properties "^1.1.3"
functions-have-names "^1.2.2"

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"
Expand Down

0 comments on commit bed82f2

Please sign in to comment.