Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 18x 18x 18x 18x 254x 254x 40x 205x 203x 199x 85x 71x 14x 199x 27x 27x 27x 16x 16x 8x 8x 8x 8x 8x | import type {CompactRemoveOp, OPCODE_REMOVE} from '../codec/compact/types'; import {AbstractOp} from './AbstractOp'; import type {OperationRemove} from '../types'; import {find, isObjectReference, isArrayReference, type Path, formatJsonPointer} from '@jsonjoy.com/json-pointer'; import {OPCODE} from '../constants'; import type {IMessagePackEncoder} from '@jsonjoy.com/json-pack/lib/msgpack'; /** * @category JSON Patch */ export class OpRemove extends AbstractOp<'remove'> { constructor( path: Path, public readonly oldValue: unknown, ) { super(path); } public op() { return 'remove' as const; } public code() { return OPCODE.remove; } public apply(doc: unknown) { const ref = find(doc, this.path); if (ref.val === undefined) throw new Error('NOT_FOUND'); if (isObjectReference(ref)) delete ref.obj[ref.key]; else if (isArrayReference(ref)) { if (ref.val !== undefined) ref.obj.splice(ref.key, 1); } else doc = null; return {doc, old: ref.val}; } public toJson(parent?: AbstractOp): OperationRemove { const json: OperationRemove = { op: 'remove', path: formatJsonPointer(this.path), }; if (this.oldValue !== undefined) (json as any).oldValue = this.oldValue; return json; } public toCompact(parent: undefined | AbstractOp, verbose: boolean): CompactRemoveOp { const opcode: OPCODE_REMOVE = verbose ? 'remove' : OPCODE.remove; return this.oldValue === undefined ? ([opcode, this.path] as CompactRemoveOp) : ([opcode, this.path, this.oldValue] as CompactRemoveOp); } public encode(encoder: IMessagePackEncoder, parent?: AbstractOp) { const hasOldValue = this.oldValue !== undefined; encoder.encodeArrayHeader(hasOldValue ? 3 : 2); encoder.writer.u8(OPCODE.remove); encoder.encodeArray(this.path as unknown[]); if (hasOldValue) encoder.encodeAny(this.oldValue); } } |