Improved errors. Fixed move before

This commit is contained in:
jos 2016-09-23 11:15:56 +02:00
parent 515e7a6bf2
commit 291cfbfa22
3 changed files with 77 additions and 33 deletions

View File

@ -48,7 +48,7 @@ var compiler = webpack({
},
plugins: [
bannerPlugin,
new webpack.optimize.UglifyJsPlugin() // TODO: don't minify when watching
// new webpack.optimize.UglifyJsPlugin() // TODO: don't minify when watching
],
module: {
loaders: loaders
@ -71,7 +71,7 @@ var compilerMinimalist = webpack({
bannerPlugin,
new webpack.IgnorePlugin(new RegExp('^brace$')),
new webpack.IgnorePlugin(new RegExp('^ajv')),
new webpack.optimize.UglifyJsPlugin() // TODO: don't minify when watching
// new webpack.optimize.UglifyJsPlugin() // TODO: don't minify when watching
],
module: {
loaders: loaders

View File

@ -85,18 +85,26 @@ export function toDataPath (data, path) {
return []
}
let index
if (data.type === 'Array') {
// index of an array
index = path[0]
const index = path[0]
const item = data.items[index]
if (!item) {
throw new Error('Array item ' + index + ' not found')
}
return ['items', index].concat(toDataPath(data.items[index], path.slice(1)))
return ['items', index].concat(toDataPath(item, path.slice(1)))
}
else {
// object property. find the index of this property
index = data.props.findIndex(prop => prop.name === path[0])
const index = data.props.findIndex(prop => prop.name === path[0])
const prop = data.props[index]
if (!prop) {
throw new Error('Object property "' + path[0] + '" not found')
}
return ['props', index, 'value'].concat(toDataPath(data.props[index].value, path.slice(1)))
return ['props', index, 'value']
.concat(toDataPath(prop && prop.value, path.slice(1)))
}
}
@ -320,7 +328,6 @@ export function simplifyPatch(patch) {
*/
export function add (data, path, value, options) {
const pathArray = parseJSONPointer(path)
const parentPath = pathArray.slice(0, pathArray.length - 1)
const dataPath = toDataPath(data, parentPath)
const parent = getIn(data, dataPath)
@ -372,9 +379,7 @@ export function add (data, path, value, options) {
op: 'replace',
path: compileJSONPointer(resolvedPath),
value: dataToJson(oldValue),
jsoneditor: {
type: oldValue.type
}
jsoneditor: { type: oldValue.type }
}]
}
}
@ -414,24 +419,36 @@ export function copy (data, path, from, options) {
* @private
*/
export function move (data, path, from, options) {
const dataValue = getIn(data, toDataPath(data, parseJSONPointer(from)))
const fromArray = parseJSONPointer(from)
const dataValue = getIn(data, toDataPath(data, fromArray))
const parentPath = fromArray.slice(0, fromArray.length - 1)
const parent = getIn(data, toDataPath(data, parentPath))
const result1 = remove(data, from)
const result2 = add(result1.data, path, dataValue, options)
const before = result1.revert[0].jsoneditor.before
const beforeNeeded = (parent.type === 'Object' && before)
if (result2.revert[0].op === 'replace') {
const type = result2.revert[0].jsoneditor.type
return {
data: result2.data,
revert: [
{ op: 'move', from: path, path: from },
{
op: 'move',
from: path,
path: from
},
{
op: 'add',
path,
value: result2.revert[0].value,
jsoneditor: {
before: options.before,
type: result2.revert[0].jsoneditor.type
}
jsoneditor: beforeNeeded
? { type, before }
: { type }
}
]
}
@ -439,16 +456,9 @@ export function move (data, path, from, options) {
else { // result2.revert[0].op === 'remove'
return {
data: result2.data,
revert: [
{
op: 'move',
from: path,
path: from,
jsoneditor: {
before: options.before
}
}
]
revert: beforeNeeded
? [{ op: 'move', from: path, path: from, jsoneditor: {before} }]
: [{ op: 'move', from: path, path: from }]
}
}
}
@ -461,17 +471,16 @@ export function move (data, path, from, options) {
* @param {*} value
*/
export function test (data, path, value) {
const pathArray = parseJSONPointer(path)
const actualValue = getIn(data, toDataPath(data, pathArray))
if (value === undefined) {
throw new Error('Test failed, no value provided')
}
if (actualValue === undefined) {
const pathArray = parseJSONPointer(path)
if (!pathExists(data, pathArray)) {
throw new Error('Test failed, path not found')
}
const actualValue = getIn(data, toDataPath(data, pathArray))
if (!isEqual(dataToJson(actualValue), value)) {
throw new Error('Test failed, value differs')
}

View File

@ -492,6 +492,43 @@ test('jsonpatch move', t => {
t.deepEqual(revert2, patch)
})
test('jsonpatch move before', t => {
const json = {
arr: [1,2,3],
obj: {a : 4},
zzz: 'zzz'
}
const patch = [
{op: 'move', from: '/obj', path: '/arr/2'},
]
const data = jsonToData(json)
const result = patchData(data, patch)
const patchedData = result.data
const revert = result.revert
const patchedJson = dataToJson(patchedData)
t.is(result.error, null)
t.deepEqual(patchedJson, {
arr: [1, 2, {a:4}, 3],
zzz: 'zzz'
})
t.deepEqual(revert, [
{op: 'move', from: '/arr/2', path: '/obj', jsoneditor: {before: 'zzz'}}
])
// test revert
const data2 = jsonToData(patchedJson)
const result2 = patchData(data2, revert)
const patchedData2 = result2.data
const revert2 = result2.revert
const patchedJson2 = dataToJson(patchedData2)
t.deepEqual(patchedJson2, json)
t.deepEqual(revert2, patch)
})
test('jsonpatch move and replace', t => {
const json = {
arr: [1,2,3],
@ -608,5 +645,3 @@ test('jsonpatch test (fail: value not equal)', t => {
t.deepEqual(revert, [])
t.is(result.error.toString(), 'Error: Test failed, value differs')
})
// TODO: test `jsoneditor.before` JSONPatch property