diff --git a/src/TreeMode.js b/src/TreeMode.js index 3e55616..ab0594c 100644 --- a/src/TreeMode.js +++ b/src/TreeMode.js @@ -18,7 +18,7 @@ export default class TreeMode extends Component { super(props) const expand = this.props.options && this.props.options.expand || TreeMode.expand - const data = jsonToData([], this.props.data || {}, expand) + const data = jsonToData(this.props.data || {}, expand, []) this.state = { options: { @@ -266,7 +266,7 @@ export default class TreeMode extends Component { */ set (json, options = {}) { const name = options && options.name || null // the root name - const data = jsonToData([], json, options.expand || TreeMode.expand) + const data = jsonToData(json, options.expand || TreeMode.expand, []) this.setState({ options: setIn(this.state.options, ['name'], name), diff --git a/src/jsonData.js b/src/jsonData.js index 57cfb68..c828046 100644 --- a/src/jsonData.js +++ b/src/jsonData.js @@ -16,18 +16,17 @@ const expandAll = function (path) { /** * Convert a JSON object into the internally used data model - * @param {Path} path * @param {Object | Array | string | number | boolean | null} json - * @param {function(path: Path)} expand + * @param {function(path: Path)} [expand] + * @param {Path} [path=[]] * @return {JSONData} */ -// TODO: change signature to jsonToData(json [, expand=(path) => false [, path=[]]]) -export function jsonToData (path, json, expand) { +export function jsonToData (json, expand = expandAll, path = []) { if (Array.isArray(json)) { return { type: 'Array', expanded: expand(path), - items: json.map((child, index) => jsonToData(path.concat(index), child, expand)) + items: json.map((child, index) => jsonToData(child, expand, path.concat(index))) } } else if (isObject(json)) { @@ -37,7 +36,7 @@ export function jsonToData (path, json, expand) { props: Object.keys(json).map(name => { return { name, - value: jsonToData(path.concat(name), json[name], expand) + value: jsonToData(json[name], expand, path.concat(name)) } }) } @@ -120,7 +119,7 @@ export function patchData (data, patch) { switch (action.op) { case 'add': { const path = parseJSONPointer(action.path) - const newValue = jsonToData(path, action.value, expand) + const newValue = jsonToData(action.value, expand, path) // TODO: move setting type to jsonToData if (options && options.type) { @@ -146,7 +145,7 @@ export function patchData (data, patch) { case 'replace': { const path = parseJSONPointer(action.path) - let newValue = jsonToData(path, action.value, expand) + let newValue = jsonToData(action.value, expand, path) // TODO: move setting type to jsonToData if (options && options.type) { diff --git a/test/jsonData.test.js b/test/jsonData.test.js index ceaa16b..74da9e6 100644 --- a/test/jsonData.test.js +++ b/test/jsonData.test.js @@ -241,7 +241,7 @@ test('jsonToData', t => { return true } - t.deepEqual(jsonToData([], JSON_EXAMPLE, expand), JSON_DATA_EXAMPLE) + t.deepEqual(jsonToData(JSON_EXAMPLE, expand, []), JSON_DATA_EXAMPLE) }) test('dataToJson', t => { @@ -302,7 +302,7 @@ test('jsonpatch add', t => { {op: 'add', path: '/obj/b', value: {foo: 'bar'}} ] - const data = jsonToData([], json, (path) => false) + const data = jsonToData(json) const result = patchData(data, patch) const patchedData = result.data const revert = result.revert @@ -327,7 +327,7 @@ test('jsonpatch add: append to matrix', t => { {op: 'add', path: '/arr/-', value: 4} ] - const data = jsonToData([], json, (path) => false) + const data = jsonToData(json) const result = patchData(data, patch) const patchedData = result.data const revert = result.revert @@ -354,7 +354,7 @@ test('jsonpatch remove', t => { {op: 'remove', path: '/arr/1'}, ] - const data = jsonToData([], json, (path) => false) + const data = jsonToData(json) const result = patchData(data, patch) const patchedData = result.data const revert = result.revert @@ -370,7 +370,7 @@ test('jsonpatch remove', t => { ]) // test revert - const data2 = jsonToData([], patchedJson, (path) => false) + const data2 = jsonToData(patchedJson) const result2 = patchData(data2, revert) const patchedData2 = result2.data const revert2 = result2.revert @@ -391,7 +391,7 @@ test('jsonpatch replace', t => { {op: 'replace', path: '/arr/1', value: 200}, ] - const data = jsonToData([], json, (path) => false) + const data = jsonToData(json) const result = patchData(data, patch) const patchedData = result.data const revert = result.revert @@ -407,7 +407,7 @@ test('jsonpatch replace', t => { ]) // test revert - const data2 = jsonToData([], patchedJson, (path) => false) + const data2 = jsonToData(patchedJson) const result2 = patchData(data2, revert) const patchedData2 = result2.data const revert2 = result2.revert @@ -430,7 +430,7 @@ test('jsonpatch copy', t => { {op: 'copy', from: '/obj', path: '/arr/2'}, ] - const data = jsonToData([], json, (path) => false) + const data = jsonToData(json) const result = patchData(data, patch) const patchedData = result.data const revert = result.revert @@ -445,7 +445,7 @@ test('jsonpatch copy', t => { ]) // test revert - const data2 = jsonToData([], patchedJson, (path) => false) + const data2 = jsonToData(patchedJson) const result2 = patchData(data2, revert) const patchedData2 = result2.data const revert2 = result2.revert @@ -467,7 +467,7 @@ test('jsonpatch move', t => { {op: 'move', from: '/obj', path: '/arr/2'}, ] - const data = jsonToData([], json, (path) => false) + const data = jsonToData(json) const result = patchData(data, patch) const patchedData = result.data const revert = result.revert @@ -482,7 +482,7 @@ test('jsonpatch move', t => { ]) // test revert - const data2 = jsonToData([], patchedJson, (path) => false) + const data2 = jsonToData(patchedJson) const result2 = patchData(data2, revert) const patchedData2 = result2.data const revert2 = result2.revert @@ -502,7 +502,7 @@ test('jsonpatch move and replace', t => { {op: 'move', from: '/obj', path: '/arr'}, ] - const data = jsonToData([], json, (path) => false) + const data = jsonToData(json) const result = patchData(data, patch) const patchedData = result.data const revert = result.revert @@ -517,7 +517,7 @@ test('jsonpatch move and replace', t => { ]) // test revert - const data2 = jsonToData([], patchedJson, (path) => false) + const data2 = jsonToData(patchedJson) const result2 = patchData(data2, revert) const patchedData2 = result2.data const revert2 = result2.revert @@ -540,7 +540,7 @@ test('jsonpatch test (ok)', t => { {op: 'add', path: '/added', value: 'ok'} ] - const data = jsonToData([], json, (path) => false) + const data = jsonToData(json) const result = patchData(data, patch) const patchedData = result.data const revert = result.revert @@ -568,7 +568,7 @@ test('jsonpatch test (fail: path not found)', t => { {op: 'add', path: '/added', value: 'ok'} ] - const data = jsonToData([], json, (path) => false) + const data = jsonToData(json) const result = patchData(data, patch) const patchedData = result.data const revert = result.revert @@ -594,7 +594,7 @@ test('jsonpatch test (fail: value not equal)', t => { {op: 'add', path: '/added', value: 'ok'} ] - const data = jsonToData([], json, (path) => false) + const data = jsonToData(json) const result = patchData(data, patch) const patchedData = result.data const revert = result.revert