Fixed insert not working
This commit is contained in:
parent
9d05195aec
commit
099c5ee8ba
|
@ -145,7 +145,7 @@ export function insert (data, path, type) {
|
||||||
const index = parseInt(path[path.length - 1]) + 1
|
const index = parseInt(path[path.length - 1]) + 1
|
||||||
return [{
|
return [{
|
||||||
op: 'add',
|
op: 'add',
|
||||||
path: compileJSONPointer(parentPath.concat(index + '')),
|
path: compileJSONPointer(parentPath.concat(index)),
|
||||||
value,
|
value,
|
||||||
jsoneditor: {
|
jsoneditor: {
|
||||||
type
|
type
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Develop JSONEditor Next</title>
|
<title>Develop JSONEditor</title>
|
||||||
|
|
||||||
<!-- For IE and Edge -->
|
<!-- For IE and Edge -->
|
||||||
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.1/es6-shim.min.js"></script>-->
|
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.1/es6-shim.min.js"></script>-->
|
||||||
|
|
|
@ -117,11 +117,11 @@ export function toDataPath (data, path) {
|
||||||
export function patchData (data, patch) {
|
export function patchData (data, patch) {
|
||||||
const expand = expandAll // TODO: customizable expand function
|
const expand = expandAll // TODO: customizable expand function
|
||||||
|
|
||||||
try {
|
|
||||||
let updatedData = data
|
let updatedData = data
|
||||||
let revert = []
|
let revert = []
|
||||||
|
|
||||||
patch.forEach(function (action) {
|
for (let i = 0; i < patch.length; i++) {
|
||||||
|
const action = patch[i]
|
||||||
const options = action.jsoneditor
|
const options = action.jsoneditor
|
||||||
|
|
||||||
switch (action.op) {
|
switch (action.op) {
|
||||||
|
@ -186,30 +186,34 @@ export function patchData (data, patch) {
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'test': {
|
case 'test': {
|
||||||
test(updatedData, action.path, action.value)
|
// when a test fails, cancel the whole patch and return the error
|
||||||
|
const error = test(updatedData, action.path, action.value)
|
||||||
|
if (error) {
|
||||||
|
return { data, revert: [], error}
|
||||||
|
}
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
throw new Error('Unknown jsonpatch op ' + JSON.stringify(action.op))
|
// unknown jsonpatch operation. Cancel the whole patch and return an error
|
||||||
|
return {
|
||||||
|
data,
|
||||||
|
revert: [],
|
||||||
|
error: new Error('Unknown jsonpatch op ' + JSON.stringify(action.op))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
// TODO: Simplify revert when possible:
|
// TODO: Simplify revert when possible:
|
||||||
// when a previous action takes place on the same path, remove the first
|
// when a previous action takes place on the same path, remove the first
|
||||||
|
|
||||||
return {
|
return {
|
||||||
data: updatedData,
|
data: updatedData,
|
||||||
revert: simplifyPatch(revert),
|
revert: simplifyPatch(revert),
|
||||||
error: null
|
error: null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
|
||||||
return {data, revert: [], error}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace an existing item
|
* Replace an existing item
|
||||||
|
@ -439,20 +443,21 @@ export function move (data, path, from, options) {
|
||||||
* @param {JSONData} data
|
* @param {JSONData} data
|
||||||
* @param {string} path
|
* @param {string} path
|
||||||
* @param {*} value
|
* @param {*} value
|
||||||
|
* @return {null | Error} Returns an error when the tests, returns null otherwise
|
||||||
*/
|
*/
|
||||||
export function test (data, path, value) {
|
export function test (data, path, value) {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
throw new Error('Test failed, no value provided')
|
return new Error('Test failed, no value provided')
|
||||||
}
|
}
|
||||||
|
|
||||||
const pathArray = parseJSONPointer(path)
|
const pathArray = parseJSONPointer(path)
|
||||||
if (!pathExists(data, pathArray)) {
|
if (!pathExists(data, pathArray)) {
|
||||||
throw new Error('Test failed, path not found')
|
return new Error('Test failed, path not found')
|
||||||
}
|
}
|
||||||
|
|
||||||
const actualValue = getIn(data, toDataPath(data, pathArray))
|
const actualValue = getIn(data, toDataPath(data, pathArray))
|
||||||
if (!isEqual(dataToJson(actualValue), value)) {
|
if (!isEqual(dataToJson(actualValue), value)) {
|
||||||
throw new Error('Test failed, value differs')
|
return new Error('Test failed, value differs')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -614,15 +619,11 @@ export function findPropertyIndex (object, prop) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a JSON Pointer
|
* Parse a JSON Pointer
|
||||||
* WARNING: this is not a complete string implementation
|
* WARNING: this is not a complete implementation
|
||||||
* @param {string} pointer
|
* @param {string} pointer
|
||||||
* @return {Array}
|
* @return {Array}
|
||||||
*/
|
*/
|
||||||
export function parseJSONPointer (pointer) {
|
export function parseJSONPointer (pointer) {
|
||||||
if (pointer === '/') {
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
|
|
||||||
const path = pointer.split('/')
|
const path = pointer.split('/')
|
||||||
path.shift() // remove the first empty entry
|
path.shift() // remove the first empty entry
|
||||||
|
|
||||||
|
@ -631,12 +632,12 @@ export function parseJSONPointer (pointer) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile a JSON Pointer
|
* Compile a JSON Pointer
|
||||||
* WARNING: this is not a complete string implementation
|
* WARNING: this is not a complete implementation
|
||||||
* @param {Path} path
|
* @param {Path} path
|
||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
export function compileJSONPointer (path) {
|
export function compileJSONPointer (path) {
|
||||||
return '/' + path
|
return path
|
||||||
.map(p => String(p).replace(/~/g, '~0').replace(/\//g, '~1'))
|
.map(p => '/' + String(p).replace(/~/g, '~0').replace(/\//g, '~1'))
|
||||||
.join('/')
|
.join('')
|
||||||
}
|
}
|
||||||
|
|
|
@ -286,12 +286,15 @@ test('parseJSONPointer', t => {
|
||||||
t.deepEqual(parseJSONPointer('/arr/-'), ['arr', '-'])
|
t.deepEqual(parseJSONPointer('/arr/-'), ['arr', '-'])
|
||||||
t.deepEqual(parseJSONPointer('/foo/~1~0 ~0~1'), ['foo', '/~ ~/'])
|
t.deepEqual(parseJSONPointer('/foo/~1~0 ~0~1'), ['foo', '/~ ~/'])
|
||||||
t.deepEqual(parseJSONPointer('/obj'), ['obj'])
|
t.deepEqual(parseJSONPointer('/obj'), ['obj'])
|
||||||
t.deepEqual(parseJSONPointer('/'), [])
|
t.deepEqual(parseJSONPointer('/'), [''])
|
||||||
|
t.deepEqual(parseJSONPointer(''), [])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('compileJSONPointer', t => {
|
test('compileJSONPointer', t => {
|
||||||
t.deepEqual(compileJSONPointer(['foo', 'bar']), '/foo/bar')
|
t.deepEqual(compileJSONPointer(['foo', 'bar']), '/foo/bar')
|
||||||
t.deepEqual(compileJSONPointer(['foo', '/~ ~/']), '/foo/~1~0 ~0~1')
|
t.deepEqual(compileJSONPointer(['foo', '/~ ~/']), '/foo/~1~0 ~0~1')
|
||||||
|
t.deepEqual(compileJSONPointer(['']), '/')
|
||||||
|
t.deepEqual(compileJSONPointer([]), '')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('jsonpatch add', t => {
|
test('jsonpatch add', t => {
|
||||||
|
|
Loading…
Reference in New Issue