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 * Create a JSONPatch for a remove action
* @param {Path} path * @param {Path} path
* @return {ESONPatch} * @return {ESONPatchDocument}
*/ */
export function remove (path) { export function remove (path) {
return [{ return [{
@ -299,7 +299,7 @@ export function remove (path) {
/** /**
* Create a JSONPatch for a multiple remove action * Create a JSONPatch for a multiple remove action
* @param {Path[]} paths * @param {Path[]} paths
* @return {ESONPatch} * @return {ESONPatchDocument}
*/ */
export function removeAll (paths) { export function removeAll (paths) {
return paths return paths

View File

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

View File

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

View File

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

View File

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