Changed the signature of `jsonToData` to pass the json value first and make the other args optional

This commit is contained in:
jos 2016-09-18 21:54:39 +02:00
parent 0a857aaad6
commit 7f979b97dd
3 changed files with 25 additions and 26 deletions

View File

@ -18,7 +18,7 @@ export default class TreeMode extends Component {
super(props) super(props)
const expand = this.props.options && this.props.options.expand || TreeMode.expand 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 = { this.state = {
options: { options: {
@ -266,7 +266,7 @@ export default class TreeMode extends Component {
*/ */
set (json, options = {}) { set (json, options = {}) {
const name = options && options.name || null // the root name 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({ this.setState({
options: setIn(this.state.options, ['name'], name), options: setIn(this.state.options, ['name'], name),

View File

@ -16,18 +16,17 @@ const expandAll = function (path) {
/** /**
* Convert a JSON object into the internally used data model * Convert a JSON object into the internally used data model
* @param {Path} path
* @param {Object | Array | string | number | boolean | null} json * @param {Object | Array | string | number | boolean | null} json
* @param {function(path: Path)} expand * @param {function(path: Path)} [expand]
* @param {Path} [path=[]]
* @return {JSONData} * @return {JSONData}
*/ */
// TODO: change signature to jsonToData(json [, expand=(path) => false [, path=[]]]) export function jsonToData (json, expand = expandAll, path = []) {
export function jsonToData (path, json, expand) {
if (Array.isArray(json)) { if (Array.isArray(json)) {
return { return {
type: 'Array', type: 'Array',
expanded: expand(path), 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)) { else if (isObject(json)) {
@ -37,7 +36,7 @@ export function jsonToData (path, json, expand) {
props: Object.keys(json).map(name => { props: Object.keys(json).map(name => {
return { return {
name, 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) { switch (action.op) {
case 'add': { case 'add': {
const path = parseJSONPointer(action.path) 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 // TODO: move setting type to jsonToData
if (options && options.type) { if (options && options.type) {
@ -146,7 +145,7 @@ export function patchData (data, patch) {
case 'replace': { case 'replace': {
const path = parseJSONPointer(action.path) 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 // TODO: move setting type to jsonToData
if (options && options.type) { if (options && options.type) {

View File

@ -241,7 +241,7 @@ test('jsonToData', t => {
return true return true
} }
t.deepEqual(jsonToData([], JSON_EXAMPLE, expand), JSON_DATA_EXAMPLE) t.deepEqual(jsonToData(JSON_EXAMPLE, expand, []), JSON_DATA_EXAMPLE)
}) })
test('dataToJson', t => { test('dataToJson', t => {
@ -302,7 +302,7 @@ test('jsonpatch add', t => {
{op: 'add', path: '/obj/b', value: {foo: 'bar'}} {op: 'add', path: '/obj/b', value: {foo: 'bar'}}
] ]
const data = jsonToData([], json, (path) => false) const data = jsonToData(json)
const result = patchData(data, patch) const result = patchData(data, patch)
const patchedData = result.data const patchedData = result.data
const revert = result.revert const revert = result.revert
@ -327,7 +327,7 @@ test('jsonpatch add: append to matrix', t => {
{op: 'add', path: '/arr/-', value: 4} {op: 'add', path: '/arr/-', value: 4}
] ]
const data = jsonToData([], json, (path) => false) const data = jsonToData(json)
const result = patchData(data, patch) const result = patchData(data, patch)
const patchedData = result.data const patchedData = result.data
const revert = result.revert const revert = result.revert
@ -354,7 +354,7 @@ test('jsonpatch remove', t => {
{op: 'remove', path: '/arr/1'}, {op: 'remove', path: '/arr/1'},
] ]
const data = jsonToData([], json, (path) => false) const data = jsonToData(json)
const result = patchData(data, patch) const result = patchData(data, patch)
const patchedData = result.data const patchedData = result.data
const revert = result.revert const revert = result.revert
@ -370,7 +370,7 @@ test('jsonpatch remove', t => {
]) ])
// test revert // test revert
const data2 = jsonToData([], patchedJson, (path) => false) const data2 = jsonToData(patchedJson)
const result2 = patchData(data2, revert) const result2 = patchData(data2, revert)
const patchedData2 = result2.data const patchedData2 = result2.data
const revert2 = result2.revert const revert2 = result2.revert
@ -391,7 +391,7 @@ test('jsonpatch replace', t => {
{op: 'replace', path: '/arr/1', value: 200}, {op: 'replace', path: '/arr/1', value: 200},
] ]
const data = jsonToData([], json, (path) => false) const data = jsonToData(json)
const result = patchData(data, patch) const result = patchData(data, patch)
const patchedData = result.data const patchedData = result.data
const revert = result.revert const revert = result.revert
@ -407,7 +407,7 @@ test('jsonpatch replace', t => {
]) ])
// test revert // test revert
const data2 = jsonToData([], patchedJson, (path) => false) const data2 = jsonToData(patchedJson)
const result2 = patchData(data2, revert) const result2 = patchData(data2, revert)
const patchedData2 = result2.data const patchedData2 = result2.data
const revert2 = result2.revert const revert2 = result2.revert
@ -430,7 +430,7 @@ test('jsonpatch copy', t => {
{op: 'copy', from: '/obj', path: '/arr/2'}, {op: 'copy', from: '/obj', path: '/arr/2'},
] ]
const data = jsonToData([], json, (path) => false) const data = jsonToData(json)
const result = patchData(data, patch) const result = patchData(data, patch)
const patchedData = result.data const patchedData = result.data
const revert = result.revert const revert = result.revert
@ -445,7 +445,7 @@ test('jsonpatch copy', t => {
]) ])
// test revert // test revert
const data2 = jsonToData([], patchedJson, (path) => false) const data2 = jsonToData(patchedJson)
const result2 = patchData(data2, revert) const result2 = patchData(data2, revert)
const patchedData2 = result2.data const patchedData2 = result2.data
const revert2 = result2.revert const revert2 = result2.revert
@ -467,7 +467,7 @@ test('jsonpatch move', t => {
{op: 'move', from: '/obj', path: '/arr/2'}, {op: 'move', from: '/obj', path: '/arr/2'},
] ]
const data = jsonToData([], json, (path) => false) const data = jsonToData(json)
const result = patchData(data, patch) const result = patchData(data, patch)
const patchedData = result.data const patchedData = result.data
const revert = result.revert const revert = result.revert
@ -482,7 +482,7 @@ test('jsonpatch move', t => {
]) ])
// test revert // test revert
const data2 = jsonToData([], patchedJson, (path) => false) const data2 = jsonToData(patchedJson)
const result2 = patchData(data2, revert) const result2 = patchData(data2, revert)
const patchedData2 = result2.data const patchedData2 = result2.data
const revert2 = result2.revert const revert2 = result2.revert
@ -502,7 +502,7 @@ test('jsonpatch move and replace', t => {
{op: 'move', from: '/obj', path: '/arr'}, {op: 'move', from: '/obj', path: '/arr'},
] ]
const data = jsonToData([], json, (path) => false) const data = jsonToData(json)
const result = patchData(data, patch) const result = patchData(data, patch)
const patchedData = result.data const patchedData = result.data
const revert = result.revert const revert = result.revert
@ -517,7 +517,7 @@ test('jsonpatch move and replace', t => {
]) ])
// test revert // test revert
const data2 = jsonToData([], patchedJson, (path) => false) const data2 = jsonToData(patchedJson)
const result2 = patchData(data2, revert) const result2 = patchData(data2, revert)
const patchedData2 = result2.data const patchedData2 = result2.data
const revert2 = result2.revert const revert2 = result2.revert
@ -540,7 +540,7 @@ test('jsonpatch test (ok)', t => {
{op: 'add', path: '/added', value: 'ok'} {op: 'add', path: '/added', value: 'ok'}
] ]
const data = jsonToData([], json, (path) => false) const data = jsonToData(json)
const result = patchData(data, patch) const result = patchData(data, patch)
const patchedData = result.data const patchedData = result.data
const revert = result.revert const revert = result.revert
@ -568,7 +568,7 @@ test('jsonpatch test (fail: path not found)', t => {
{op: 'add', path: '/added', value: 'ok'} {op: 'add', path: '/added', value: 'ok'}
] ]
const data = jsonToData([], json, (path) => false) const data = jsonToData(json)
const result = patchData(data, patch) const result = patchData(data, patch)
const patchedData = result.data const patchedData = result.data
const revert = result.revert const revert = result.revert
@ -594,7 +594,7 @@ test('jsonpatch test (fail: value not equal)', t => {
{op: 'add', path: '/added', value: 'ok'} {op: 'add', path: '/added', value: 'ok'}
] ]
const data = jsonToData([], json, (path) => false) const data = jsonToData(json)
const result = patchData(data, patch) const result = patchData(data, patch)
const patchedData = result.data const patchedData = result.data
const revert = result.revert const revert = result.revert