mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 02:11:54 +00:00
refactor apply operation to account for arrays (#38)
* refactor apply operation to account for arrays * fix buig
This commit is contained in:
@@ -39,7 +39,7 @@ export function applyOperation<T> (
|
|||||||
const add = { op: PatchOp.ADD, path, value: current} as const
|
const add = { op: PatchOp.ADD, path, value: current} as const
|
||||||
const replace = { op: PatchOp.REPLACE, path, value: current } as const
|
const replace = { op: PatchOp.REPLACE, path, value: current } as const
|
||||||
|
|
||||||
doc.data = recursiveApply(doc.data, jsonPathToKeyArray(path), value)
|
doc.data = recursiveApply(doc.data, jsonPathToKeyArray(path), op, value)
|
||||||
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case PatchOp.REMOVE:
|
case PatchOp.REMOVE:
|
||||||
@@ -54,14 +54,22 @@ export function applyOperation<T> (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function recursiveApply<T extends Record<string, T>> (data: T, path: readonly string[], value?: any): T {
|
function recursiveApply<T extends Record<string, any> | any[]> (data: T, path: readonly string[], op: PatchOp, value?: any): T {
|
||||||
if (!path.length) return value
|
if (!path.length) return value
|
||||||
|
|
||||||
if (!isObject(data)) {
|
// object
|
||||||
throw Error('Patch cannot be applied. Path contains non object')
|
if (isObject(data)) {
|
||||||
|
return recursiveApplyObject(data, path, op, value)
|
||||||
|
// array
|
||||||
|
} else if (Array.isArray(data)) {
|
||||||
|
return recursiveApplyArray(data, path, op, value)
|
||||||
|
} else {
|
||||||
|
throw 'unreachable'
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const updated = recursiveApply(data[path[0]], path.slice(1), value)
|
function recursiveApplyObject<T extends Record<string, any>> (data: T, path: readonly string[], op: PatchOp, value?: any): T {
|
||||||
|
const updated = recursiveApply(data[path[0]], path.slice(1), op, value)
|
||||||
const result = {
|
const result = {
|
||||||
...data,
|
...data,
|
||||||
[path[0]]: updated,
|
[path[0]]: updated,
|
||||||
@@ -74,8 +82,24 @@ function recursiveApply<T extends Record<string, T>> (data: T, path: readonly st
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function recursiveApplyArray<T extends any[]> (data: T, path: readonly string[], op: PatchOp, value?: any): T {
|
||||||
|
const index = parseInt(path[0])
|
||||||
|
|
||||||
|
const updated = recursiveApply(data[index], path.slice(1), op, value)
|
||||||
|
const result = [...data] as T
|
||||||
|
if (op === PatchOp.ADD) {
|
||||||
|
result.splice(index, 0, updated)
|
||||||
|
} else if (op === PatchOp.REPLACE) {
|
||||||
|
result.splice(index, 1, updated)
|
||||||
|
} else {
|
||||||
|
result.splice(index, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
function isObject (val: any): val is Record<string, unknown> {
|
function isObject (val: any): val is Record<string, unknown> {
|
||||||
return typeof val === 'object' && val !== null
|
return typeof val === 'object' && val !== null && !Array.isArray(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
function jsonPathToKeyArray (path: string): string[] {
|
function jsonPathToKeyArray (path: string): string[] {
|
||||||
|
|||||||
Reference in New Issue
Block a user