From dc814a3aa5a000dc4656caf12da47608ec6329d3 Mon Sep 17 00:00:00 2001 From: jos Date: Wed, 29 Aug 2018 12:27:33 +0200 Subject: [PATCH] Rename terminology of JSONPatch --- src/jsoneditor/actions.js | 4 +-- src/jsoneditor/components/TextMode.js | 10 +++---- src/jsoneditor/components/TreeMode.js | 36 +++++++++++------------ src/jsoneditor/eson.js | 12 ++++---- src/jsoneditor/immutableJsonPatch.js | 42 +++++++++++++-------------- src/jsoneditor/types.js | 18 ++++++++---- 6 files changed, 65 insertions(+), 57 deletions(-) diff --git a/src/jsoneditor/actions.js b/src/jsoneditor/actions.js index 70c74d9..d8bbeef 100644 --- a/src/jsoneditor/actions.js +++ b/src/jsoneditor/actions.js @@ -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 diff --git a/src/jsoneditor/components/TextMode.js b/src/jsoneditor/components/TextMode.js index 47ee735..49ba1b6 100644 --- a/src/jsoneditor/components/TextMode.js +++ b/src/jsoneditor/components/TextMode.js @@ -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 } diff --git a/src/jsoneditor/components/TreeMode.js b/src/jsoneditor/components/TreeMode.js index 9b87964..03e147a 100644 --- a/src/jsoneditor/components/TreeMode.js +++ b/src/jsoneditor/components/TreeMode.js @@ -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? diff --git a/src/jsoneditor/eson.js b/src/jsoneditor/eson.js index 9e3b455..b97a17b 100644 --- a/src/jsoneditor/eson.js +++ b/src/jsoneditor/eson.js @@ -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 diff --git a/src/jsoneditor/immutableJsonPatch.js b/src/jsoneditor/immutableJsonPatch.js index bc8fc5d..0a0a843 100644 --- a/src/jsoneditor/immutableJsonPatch.js +++ b/src/jsoneditor/immutableJsonPatch.js @@ -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) { diff --git a/src/jsoneditor/types.js b/src/jsoneditor/types.js index e10c16b..ecca48f 100644 --- a/src/jsoneditor/types.js +++ b/src/jsoneditor/types.js @@ -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 */ /**