Skip to content

Instantly share code, notes, and snippets.

@tfanme
Created March 22, 2018 12:21
Show Gist options
  • Save tfanme/6275f4c411a81a554493d3f41f4e5070 to your computer and use it in GitHub Desktop.
Save tfanme/6275f4c411a81a554493d3f41f4e5070 to your computer and use it in GitHub Desktop.
const fieldsToChildren = (data) => {
return data.map(item => {
if (!item.key) {
item.key = index
index += 1
}
if (item.fields) {
item.children = item.fields
delete item.fields
for (let i = 0; i < item.children.length; i++) {
if (!item.children[i].key) {
item.children[i].key = index
index += 1
}
if (item.children[i].fields) {
item.children[i].children = fieldsToChildren(item.children[i].fields)
delete item.children[i].fields
}
}
}
return item
})
}
const childrenToFields = (data) => {
return data.map(item => {
if (item.key) {
delete item.key
}
if (item.editable !== undefined) {
delete item.editable
}
if (item.children) {
item.fields = item.children
delete item.children
for (let i = 0; i < item.fields.length; i++) {
if (item.fields[i].key) {
delete item.fields[i].key
}
if (item.fields[i].editable !== undefined) {
delete item.fields[i].editable
}
if (item.fields[i].children) {
item.fields[i].fields = childrenToFields(item.fields[i].children)
delete item.fields[i].children
}
}
}
return item
})
}
const childrenToFieldsForTest = (data) => {
const cleanData = childrenToFields(data)
const schemaToParams = (fields) => {
const params = {}
for (let i = 0; i < fields.length; i++) {
const item = fields[i]
if (item.paramType !== 'object') {
if (item.paramType === 'array') {
params[item.originalName] = [].concat(item.paramValue)
} else {
params[item.originalName] = item.paramValue
}
} else {
params[item.originalName] = schemaToParams(item.fields)
}
}
return params
}
// cleanData.map(item => (params[item.originalName] = schemaToParams(item)))
return schemaToParams(cleanData)
}
let index = 0
const preProcessForTableForm = (data) => {
return data.map(item => {
if (item.key) {
return item
}
item.key = `xxxx7685-${index}`
index += 1
return item
})
}
const getInterfaces = (state) => {
const interfaces = state.interface
if (interfaces && interfaces.list) {
for (let i = 0; i < interfaces.list.length; i++) {
if (interfaces.list[i].interfaces) {
interfaces.list[i].interfaces = preProcessForTableForm(interfaces.list[i].interfaces)
}
}
}
return interfaces
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment