mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 14:57:10 +00:00
fast-json-patch: Provides its own types (#15645)
This commit is contained in:
@@ -138,6 +138,12 @@
|
||||
"sourceRepoURL": "https://github.com/loopline-systems/electron-builder",
|
||||
"asOfVersion": "2.8.0"
|
||||
},
|
||||
{
|
||||
"libraryName": "JSON-Patch",
|
||||
"typingsPackageName": "fast-json-patch",
|
||||
"sourceRepoURL": "https://github.com/Starcounter-Jack/JSON-Patch",
|
||||
"asOfVersion": "1.1.5"
|
||||
},
|
||||
{
|
||||
"libraryName": "FineUploader",
|
||||
"typingsPackageName": "fine-uploader",
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
import * as jsonpatch from 'fast-json-patch';
|
||||
|
||||
var myobj: {
|
||||
firstName: string,
|
||||
contactDetails: {
|
||||
phoneNumbers: string[]
|
||||
}
|
||||
} = { firstName: "Albert", contactDetails: { phoneNumbers: [] } };
|
||||
var patches: jsonpatch.Patch[] = [
|
||||
{ op: "replace", path: "/firstName", value: "Joachim" },
|
||||
{ op: "add", path: "/lastName", value: "Wester" },
|
||||
{ op: "add", path: "/contactDetails/phoneNumbers/0", value: { number: "555-123" } }
|
||||
];
|
||||
jsonpatch.apply(myobj, patches);
|
||||
|
||||
var myobj2 = { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [{ number: "555-123" }] } };
|
||||
var observer = jsonpatch.observe(myobj2);
|
||||
myobj2.firstName = "Albert";
|
||||
myobj2.contactDetails.phoneNumbers[0].number = "123";
|
||||
myobj2.contactDetails.phoneNumbers.push({ number: "456" });
|
||||
var patches2 = jsonpatch.generate(observer);
|
||||
|
||||
var objA = { user: { firstName: "Albert", lastName: "Einstein" } };
|
||||
var objB = { user: { firstName: "Albert", lastName: "Collins" } };
|
||||
var diff = jsonpatch.compare(objA, objB);
|
||||
|
||||
var obj = { user: { firstName: "Albert" } };
|
||||
var patches3 = [{ op: "replace", path: "/user/firstName", value: "Albert" }, { op: "replace", path: "/user/lastName", value: "Einstein" }];
|
||||
var errors = jsonpatch.validate(patches, obj);
|
||||
if (errors.length === 0) {
|
||||
//there are no errors!
|
||||
} else {
|
||||
for (var i = 0; i < errors.length; i++) {
|
||||
if (!errors[i]) {
|
||||
console.log("Valid patch at index", i, patches[i]);
|
||||
} else {
|
||||
console.error("Invalid patch at index", i, errors[i], patches[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
-104
@@ -1,104 +0,0 @@
|
||||
// Type definitions for JSON-Patch v1.1.8
|
||||
// Project: https://github.com/Starcounter-Jack/JSON-Patch/releases
|
||||
// Definitions by: Francis OBrien <https://github.com/itsFrank>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
declare namespace fastjsonpatch {
|
||||
|
||||
type Patch = AddPatch | RemovePatch | ReplacePatch | MovePatch | CopyPatch | TestPatch;
|
||||
|
||||
interface PatchBase {
|
||||
path: string;
|
||||
}
|
||||
|
||||
interface AddPatch extends PatchBase {
|
||||
op: 'add';
|
||||
value: any;
|
||||
}
|
||||
|
||||
interface RemovePatch extends PatchBase {
|
||||
op: 'remove';
|
||||
}
|
||||
|
||||
interface ReplacePatch extends PatchBase {
|
||||
op: 'replace';
|
||||
value: any;
|
||||
}
|
||||
|
||||
interface MovePatch extends PatchBase {
|
||||
op: 'move';
|
||||
from: string;
|
||||
}
|
||||
|
||||
interface CopyPatch extends PatchBase {
|
||||
op: 'copy';
|
||||
from: string;
|
||||
}
|
||||
|
||||
interface TestPatch extends PatchBase {
|
||||
op: 'test';
|
||||
value: any;
|
||||
}
|
||||
|
||||
interface Observer<T> {
|
||||
object: T
|
||||
patches: Patch[]
|
||||
unobserve(): void
|
||||
}
|
||||
|
||||
type JsonPatchErrorName = 'SEQUENCE_NOT_AN_ARRAY' |
|
||||
'OPERATION_NOT_AN_OBJECT' |
|
||||
'OPERATION_OP_INVALID' |
|
||||
'OPERATION_PATH_INVALID' |
|
||||
'OPERATION_FROM_REQUIRED' |
|
||||
'OPERATION_VALUE_REQUIRED' |
|
||||
'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED' |
|
||||
'OPERATION_PATH_CANNOT_ADD' |
|
||||
'OPERATION_PATH_UNRESOLVABLE' |
|
||||
'OPERATION_FROM_UNRESOLVABLE' |
|
||||
'OPERATION_PATH_ILLEGAL_ARRAY_INDEX' |
|
||||
'OPERATION_VALUE_OUT_OF_BOUNDS';
|
||||
|
||||
interface JsonPatchError {
|
||||
name: JsonPatchErrorName
|
||||
message: string
|
||||
index: number
|
||||
operation: any
|
||||
tree: any
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies an array of patch instructions to an object
|
||||
*/
|
||||
function apply(object: any, patches: Patch[], validate?: boolean): any[]
|
||||
|
||||
/**
|
||||
* Observes changes made to an object, which can then be retieved using generate
|
||||
*/
|
||||
function observe<T>(object: T, callback?: () => void): Observer<T>
|
||||
|
||||
/**
|
||||
* Detach an observer from an object
|
||||
*/
|
||||
function unobserve<T>(object: T, observer: Observer<T>): void
|
||||
|
||||
/**
|
||||
* Generate an array of patches from an observer
|
||||
*/
|
||||
function generate<T>(observer: Observer<T>): Patch[]
|
||||
|
||||
/**
|
||||
* Create an array of patches from the differences in two objects
|
||||
*/
|
||||
function compare(object1: any, object2: any): Patch[]
|
||||
|
||||
/**
|
||||
* Ensure a set of patch instructions is valid
|
||||
*/
|
||||
function validate(patches: Patch[], tree?: any): JsonPatchError[]
|
||||
|
||||
}
|
||||
|
||||
declare module "fast-json-patch" {
|
||||
export = fastjsonpatch
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6",
|
||||
"dom"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"fast-json-patch-tests.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user