UNPKG

3.86 kBTypeScriptView Raw
1import { AddOperation, RemoveOperation, ReplaceOperation, MoveOperation, CopyOperation, TestOperation, Operation } from './diff';
2export declare class MissingError extends Error {
3 path: string;
4 constructor(path: string);
5}
6export declare class TestError extends Error {
7 actual: any;
8 expected: any;
9 constructor(actual: any, expected: any);
10}
11/**
12> o If the target location specifies an array index, a new value is
13> inserted into the array at the specified index.
14> o If the target location specifies an object member that does not
15> already exist, a new member is added to the object.
16> o If the target location specifies an object member that does exist,
17> that member's value is replaced.
18*/
19export declare function add(object: any, operation: AddOperation): MissingError | null;
20/**
21> The "remove" operation removes the value at the target location.
22> The target location MUST exist for the operation to be successful.
23*/
24export declare function remove(object: any, operation: RemoveOperation): MissingError | null;
25/**
26> The "replace" operation replaces the value at the target location
27> with a new value. The operation object MUST contain a "value" member
28> whose content specifies the replacement value.
29> The target location MUST exist for the operation to be successful.
30
31> This operation is functionally identical to a "remove" operation for
32> a value, followed immediately by an "add" operation at the same
33> location with the replacement value.
34
35Even more simply, it's like the add operation with an existence check.
36*/
37export declare function replace(object: any, operation: ReplaceOperation): MissingError | null;
38/**
39> The "move" operation removes the value at a specified location and
40> adds it to the target location.
41> The operation object MUST contain a "from" member, which is a string
42> containing a JSON Pointer value that references the location in the
43> target document to move the value from.
44> This operation is functionally identical to a "remove" operation on
45> the "from" location, followed immediately by an "add" operation at
46> the target location with the value that was just removed.
47
48> The "from" location MUST NOT be a proper prefix of the "path"
49> location; i.e., a location cannot be moved into one of its children.
50
51TODO: throw if the check described in the previous paragraph fails.
52*/
53export declare function move(object: any, operation: MoveOperation): MissingError | null;
54/**
55> The "copy" operation copies the value at a specified location to the
56> target location.
57> The operation object MUST contain a "from" member, which is a string
58> containing a JSON Pointer value that references the location in the
59> target document to copy the value from.
60> The "from" location MUST exist for the operation to be successful.
61
62> This operation is functionally identical to an "add" operation at the
63> target location using the value specified in the "from" member.
64
65Alternatively, it's like 'move' without the 'remove'.
66*/
67export declare function copy(object: any, operation: CopyOperation): MissingError | null;
68/**
69> The "test" operation tests that a value at the target location is
70> equal to a specified value.
71> The operation object MUST contain a "value" member that conveys the
72> value to be compared to the target location's value.
73> The target location MUST be equal to the "value" value for the
74> operation to be considered successful.
75*/
76export declare function test(object: any, operation: TestOperation): TestError | null;
77export declare class InvalidOperationError extends Error {
78 operation: Operation;
79 constructor(operation: Operation);
80}
81/**
82Switch on `operation.op`, applying the corresponding patch function for each
83case to `object`.
84*/
85export declare function apply(object: any, operation: Operation): MissingError | InvalidOperationError | TestError | null;