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 | 16x 16x 16x 16x 16x 16x 106x 106x 10x 64x 64x 63x 62x 22x 16x 16x 8x 8x 8x 8x | import type {CompactCopyOp, OPCODE_COPY} from '../codec/compact/types'; import {AbstractOp} from './AbstractOp'; import type {OperationCopy} from '../types'; import {type Path, find, formatJsonPointer} from '@jsonjoy.com/json-pointer'; import {OpAdd} from './OpAdd'; import {clone as deepClone} from '@jsonjoy.com/util/lib/json-clone/clone'; import {OPCODE} from '../constants'; import type {IMessagePackEncoder} from '@jsonjoy.com/json-pack/lib/msgpack'; /** * @category JSON Patch */ export class OpCopy extends AbstractOp<'copy'> { constructor( path: Path, public readonly from: Path, ) { super(path); } public op() { return 'copy' as const; } public code() { return OPCODE.copy; } public apply(doc: unknown) { const {val} = find(doc, this.from); if (val === undefined) throw new Error('NOT_FOUND'); const add = new OpAdd(this.path, deepClone(val)).apply(doc); return add; } public toJson(parent?: AbstractOp): OperationCopy { return { op: 'copy', path: formatJsonPointer(this.path), from: formatJsonPointer(this.from), }; } public toCompact(parent: undefined | AbstractOp, verbose: boolean): CompactCopyOp { const opcode: OPCODE_COPY = verbose ? 'copy' : OPCODE.copy; return [opcode, this.path, this.from]; } public encode(encoder: IMessagePackEncoder, parent?: AbstractOp) { encoder.encodeArrayHeader(3); encoder.writer.u8(OPCODE.copy); encoder.encodeArray(this.path as unknown[]); encoder.encodeArray(this.from as unknown[]); } } |