rfc6902

  • Version 5.2.0
  • Published
  • 97.1 kB
  • No dependencies
  • MIT license

Install

npm i rfc6902
yarn add rfc6902
pnpm add rfc6902

Overview

Complete implementation of RFC6902 (patch and diff)

Index

Functions

function applyPatch

applyPatch: (
object: any,
patch: Operation[],
options?: Options
) => (
| import('./patch').MissingError
| import('./patch').TestError
| import('./patch').InvalidOperationError
| null
)[];
  • Apply a 'application/json-patch+json'-type patch to an object.

    patch *must* be an array of operations.

    > Operation objects MUST have exactly one "op" member, whose value > indicates the operation to perform. Its value MUST be one of "add", > "remove", "replace", "move", "copy", or "test"; other values are > errors.

    This method mutates the target object in-place.

    Parameter object

    The object to apply the patch to

    Parameter patch

    Array of operations to apply

    Parameter options

    Optional customization of patch application behavior

    Returns

    list of results, one for each operation: null indicated success, otherwise, the result will be an instance of one of the Error classes: MissingError, InvalidOperationError, or TestError.

function createPatch

createPatch: (input: any, output: any, diff?: VoidableDiff) => Operation[];
  • Produce a 'application/json-patch+json'-type patch to get from one object to another.

    This does not alter input or output unless they have a property getter with side-effects (which is not a good idea anyway).

    diff is called on each pair of comparable non-primitive nodes in the input/output object trees, producing nested patches. Return undefined to fall back to default behaviour.

    Returns list of operations to perform on input to produce output.

function createTests

createTests: (input: any, patch: Operation[]) => TestOperation[];
  • Produce an 'application/json-patch+json'-type list of tests, to verify that existing values in an object are identical to the those captured at some checkpoint (whenever this function is called).

    This does not alter input or output unless they have a property getter with side-effects (which is not a good idea anyway).

    Returns list of test operations.

Classes

class Pointer

class Pointer {}
  • JSON Pointer representation

constructor

constructor(tokens?: string[]);

    property tokens

    tokens: string[];

      method add

      add: (token: string) => Pointer;
      • token should be a String. It'll be coerced to one anyway.

        immutable (shallowly)

      method evaluate

      evaluate: (object: any) => PointerEvaluation;
      • Returns an object with 'parent', 'key', and 'value' properties. In the special case that this Pointer's path == "", this object will be {parent: null, key: '', value: object}. Otherwise, parent and key will have the property such that parent[key] == value.

      method fromJSON

      static fromJSON: (path: string) => Pointer;
      • path *must* be a properly escaped string.

      method get

      get: (object: any) => any;

        method parent

        parent: () => Pointer;
        • Create a new Pointer representing the parent of this one.

          The parent of the empty pointer is the empty pointer.

          immutable (shallowly)

        method push

        push: (token: string) => void;

          method set

          set: (object: any, value: any) => void;

            method toString

            toString: () => string;

              Interfaces

              interface Options

              interface Options {}

                property implicitArrayCreation

                implicitArrayCreation?: boolean;
                • When true, "add" operations with path ending in "/-" will implicitly create an empty array where possible.

                  For example, with this option enabled, for the object {live: true}, the operation add "/tag/-" 123 will result in {live: true, tag: [123]}. Subsequent operations behave normally: another add "/tag/-" 456 will result in {live: true, tag: [123, 456]}.

                  If the indicated array property already exists but is not an array, this will produce an error.

                  Only the leaf array will be inferred; missing parent objects will still produce errors.

                interface TestOperation

                interface TestOperation {}

                  property op

                  op: 'test';

                    property path

                    path: string;

                      property value

                      value: any;

                        Type Aliases

                        type Operation

                        type Operation =
                        | AddOperation
                        | RemoveOperation
                        | ReplaceOperation
                        | MoveOperation
                        | CopyOperation
                        | TestOperation;

                          type Patch

                          type Patch = Operation[];

                            Package Files (4)

                            Dependencies (0)

                            No dependencies.

                            Dev Dependencies (8)

                            Peer Dependencies (0)

                            No peer dependencies.

                            Badge

                            To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                            You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/rfc6902.

                            • Markdown
                              [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/rfc6902)
                            • HTML
                              <a href="https://www.jsdocs.io/package/rfc6902"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>