Rename terminology of JSONPatch

This commit is contained in:
jos 2018-08-29 12:27:33 +02:00
parent 56124cf17f
commit dc814a3aa5
6 changed files with 65 additions and 57 deletions

View File

@ -287,7 +287,7 @@ export function append (json, parentPath, type) {
/**
* Create a JSONPatch for a remove action
* @param {Path} path
* @return {ESONPatch}
* @return {ESONPatchDocument}
*/
export function remove (path) {
return [{
@ -299,7 +299,7 @@ export function remove (path) {
/**
* Create a JSONPatch for a multiple remove action
* @param {Path[]} paths
* @return {ESONPatch}
* @return {ESONPatchDocument}
*/
export function removeAll (paths) {
return paths

View File

@ -322,20 +322,20 @@ export default class TextMode extends Component {
/**
* Apply a JSONPatch to the current JSON document
* @param {JSONPatch} actions JSONPatch actions
* @return {ESONPatchAction} Returns a JSONPatch result containing the
* @param {JSONPatchDocument} operations JSONPatch operations
* @return {JSONPatchResult} Returns a patch result containing the
* patch, a patch to revert the action, and
* an error object which is null when successful
*/
patch (actions) {
patch (operations) {
const json = this.get()
const result = immutableJsonPatch(json, actions)
const result = immutableJsonPatch(json, operations)
this.set(result.data)
return {
patch: actions,
patch: operations,
revert: result.revert,
error: result.error
}

View File

@ -57,7 +57,7 @@ import {
nextSearchResult, pathsFromSelection, previousSearchResult,
search,
syncEson,
toEsonPatchAction
toEsonPatchOperation
} from '../eson'
const AJV_OPTIONS = {
@ -721,14 +721,14 @@ export default class TreeMode extends PureComponent {
/**
* Apply a JSONPatch to the current JSON document and emit a change event
* @param {JSONPatch} actions
* @param {JSONPatchDocument} operations
* @private
*/
handlePatch = (actions) => {
handlePatch = (operations) => {
// apply changes
const result = this.patch(actions)
const result = this.patch(operations)
this.emitOnChange (actions, result.revert, result.json)
this.emitOnChange (operations, result.revert, result.json)
}
handleTouchStart = (event) => {
@ -854,8 +854,8 @@ export default class TreeMode extends PureComponent {
* Emit an onChange event when there is a listener for it.
* events will be fired on the next tick (after any changed state is applied)
* @private
* @param {ESONPatch} patch
* @param {ESONPatch} revert
* @param {ESONPatchDocument} patch
* @param {ESONPatchDocument} revert
* @param {JSON} json
*/
emitOnChange (patch, revert, json) {
@ -905,7 +905,7 @@ export default class TreeMode extends PureComponent {
const historyItem = history[historyIndex]
const jsonResult = immutableJsonPatch(this.state.json, historyItem.undo)
const esonResult = immutableJsonPatch(this.state.eson, historyItem.undo.map(toEsonPatchAction))
const esonResult = immutableJsonPatch(this.state.eson, historyItem.undo.map(toEsonPatchOperation))
// FIXME: apply search
this.setState({
@ -926,7 +926,7 @@ export default class TreeMode extends PureComponent {
const historyItem = history[historyIndex]
const jsonResult = immutableJsonPatch(this.state.json, historyItem.redo)
const esonResult = immutableJsonPatch(this.state.eson, historyItem.undo.map(toEsonPatchAction))
const esonResult = immutableJsonPatch(this.state.eson, historyItem.undo.map(toEsonPatchOperation))
// FIXME: apply search
this.setState({
@ -942,25 +942,25 @@ export default class TreeMode extends PureComponent {
/**
* Apply a JSONPatch to the current JSON document
* @param {JSONPatch} actions ESONPatch actions
* @return {Object} Returns a object result containing the
* @param {JSONPatchDocument} operations JSON Patch operations
* @return {JSONPatchResult} Returns a object result containing the
* patch, a patch to revert the action, and
* an error object which is null when successful
*/
patch (actions) {
if (!Array.isArray(actions)) {
patch (operations) {
if (!Array.isArray(operations)) {
throw new TypeError('Array with patch actions expected')
}
console.log('patch', actions)
console.log('patch', operations) // TODO: cleanup
const jsonResult = immutableJsonPatch(this.state.json, actions)
const esonResult = immutableJsonPatch(this.state.eson, actions.map(toEsonPatchAction))
const jsonResult = immutableJsonPatch(this.state.json, operations)
const esonResult = immutableJsonPatch(this.state.eson, operations.map(toEsonPatchOperation))
if (this.props.history !== false) {
// update data and store history
const historyItem = {
redo: actions,
redo: operations,
undo: jsonResult.revert
}
@ -986,7 +986,7 @@ export default class TreeMode extends PureComponent {
}
return {
patch: actions,
patch: operations,
revert: jsonResult.revert,
error: jsonResult.error,
json: jsonResult.json // FIXME: shouldn't pass json here?

View File

@ -530,13 +530,13 @@ export function pathsFromSelection (eson, selection) {
/**
* Convert the value of a JSON Patch action into a ESON object
* @param {JSONPatchAction} action
* @returns {ESONPatchAction}
* @param {JSONPatchOperation} operation
* @returns {ESONPatchOperation}
*/
export function toEsonPatchAction (action) {
return ('value' in action)
? setIn(action, ['value'], syncEson(action.value))
: action
export function toEsonPatchOperation (operation) {
return ('value' in operation)
? setIn(operation, ['value'], syncEson(operation.value))
: operation
}
// TODO: comment

View File

@ -9,21 +9,21 @@ import { parseJSONPointer, compileJSONPointer } from './jsonPointer'
* The original JSON object will not be changed,
* instead, the patch is applied in an immutable way
* @param {JSON} json
* @param {JSONPatch} patch Array with JSON patch actions
* @return {{json: JSON, revert: JSONPatch, error: Error | null}}
* @param {JSONPatchDocument} operations Array with JSON patch actions
* @return {{json: JSON, revert: JSONPatchDocument, error: Error | null}}
*/
export function immutableJsonPatch (json, patch) {
export function immutableJsonPatch (json, operations) {
let updatedJson = json
let revert = []
for (let i = 0; i < patch.length; i++) {
const action = patch[i]
const path = action.path ? parseJSONPointer(action.path) : null
const from = action.from ? parseJSONPointer(action.from) : null
for (let i = 0; i < operations.length; i++) {
const operation = operations[i]
const path = operation.path ? parseJSONPointer(operation.path) : null
const from = operation.from ? parseJSONPointer(operation.from) : null
switch (action.op) {
switch (operation.op) {
case 'add': {
const result = add(updatedJson, path, action.value)
const result = add(updatedJson, path, operation.value)
updatedJson = result.json
revert = result.revert.concat(revert)
break
@ -38,7 +38,7 @@ export function immutableJsonPatch (json, patch) {
}
case 'replace': {
const result = replace(updatedJson, path, action.value)
const result = replace(updatedJson, path, operation.value)
updatedJson = result.json
revert = result.revert.concat(revert)
@ -46,11 +46,11 @@ export function immutableJsonPatch (json, patch) {
}
case 'copy': {
if (!action.from) {
if (!operation.from) {
return {
json: updatedJson,
revert: [],
error: new Error('Property "from" expected in copy action ' + JSON.stringify(action))
error: new Error('Property "from" expected in copy action ' + JSON.stringify(operation))
}
}
@ -62,11 +62,11 @@ export function immutableJsonPatch (json, patch) {
}
case 'move': {
if (!action.from) {
if (!operation.from) {
return {
json: updatedJson,
revert: [],
error: new Error('Property "from" expected in move action ' + JSON.stringify(action))
error: new Error('Property "from" expected in move action ' + JSON.stringify(operation))
}
}
@ -79,7 +79,7 @@ export function immutableJsonPatch (json, patch) {
case 'test': {
// when a test fails, cancel the whole patch and return the error
const error = test(updatedJson, path, action.value)
const error = test(updatedJson, path, operation.value)
if (error) {
return { json, revert: [], error}
}
@ -92,7 +92,7 @@ export function immutableJsonPatch (json, patch) {
return {
json,
revert: [],
error: new Error('Unknown JSONPatch op ' + JSON.stringify(action.op))
error: new Error('Unknown JSONPatch op ' + JSON.stringify(operation.op))
}
}
}
@ -110,7 +110,7 @@ export function immutableJsonPatch (json, patch) {
* @param {JSON} json
* @param {Path} path
* @param {JSON} value
* @return {{json: JSON, revert: JSONPatch}}
* @return {{json: JSON, revert: JSONPatchDocument}}
*/
export function replace (json, path, value) {
const oldValue = getIn(json, path)
@ -129,7 +129,7 @@ export function replace (json, path, value) {
* Remove an item or property
* @param {JSON} json
* @param {Path} path
* @return {{json: JSON, revert: JSONPatch}}
* @return {{json: JSON, revert: JSONPatchDocument}}
*/
export function remove (json, path) {
const oldValue = getIn(json, path)
@ -148,7 +148,7 @@ export function remove (json, path) {
* @param {JSON} json
* @param {Path} path
* @param {JSON} value
* @return {{json: JSON, revert: JSONPatch}}
* @return {{json: JSON, revert: JSONPatchDocument}}
* @private
*/
export function add (json, path, value) {
@ -188,7 +188,7 @@ export function add (json, path, value) {
* @param {JSON} json
* @param {Path} path
* @param {Path} from
* @return {{json: JSON, revert: ESONPatch}}
* @return {{json: JSON, revert: ESONPatchDocument}}
* @private
*/
export function copy (json, path, from) {
@ -202,7 +202,7 @@ export function copy (json, path, from) {
* @param {JSON} json
* @param {Path} path
* @param {Path} from
* @return {{json: JSON, revert: ESONPatch}}
* @return {{json: JSON, revert: ESONPatchDocument}}
* @private
*/
export function move (json, path, from) {

View File

@ -11,7 +11,7 @@
* modes: string[]?,
* history: boolean?,
* indentation: number | string?,
* onChange: function (patch: ESONPatch, revert: ESONPatch)?,
* onChange: function (patch: ESONPatchDocument, revert: ESONPatchDocument)?,
* onChangeText: function ()?,
* onChangeMode: function (mode: string, prevMode: string)?,
* onError: function (err: Error)?,
@ -66,11 +66,11 @@
* path: string,
* from?: string,
* value?: *
* }} JSONPatchAction
* }} JSONPatchOperation
*/
/**
* @typedef {JSONPatchAction[]} JSONPatch
* @typedef {JSONPatchOperation[]} JSONPatchDocument
*/
/**
@ -80,11 +80,19 @@
* from?: string,
* value?: *,
* meta?: ESONPatchOptions
* }} ESONPatchAction
* }} ESONPatchOperation
*/
/**
* @typedef {ESONPatchAction[]} ESONPatch
* @typedef {ESONPatchOperation[]} ESONPatchDocument
*/
/**
* @typedef {{
* patch: JSONPatchDocument,
* revert: JSONPatchDocument,
* error: Error | null
* }} JSONPatchResult
*/
/**