Skip to content

Instantly share code, notes, and snippets.

@bludnic
Created January 24, 2018 22:43
Show Gist options
  • Save bludnic/7ac9c044512e562a363731082dded1cf to your computer and use it in GitHub Desktop.
Save bludnic/7ac9c044512e562a363731082dded1cf to your computer and use it in GitHub Desktop.
const isArray = (str) => {
console.log('str', str);
if (str.indexOf('[].') > -1) {
const split = str.split('[].')
console.log('split', split);
return {
name: split[0],
value: split[1]
}
}
return false
}
export default (...fields) => {
return (ctx, next) => {
const lang = ctx.lang
const body = ctx.request.body
fields.map(key => {
let array = isArray(key)
if (array) {
console.log('array', array);
if (body[array.name]) {
body[array.name].map((item, key) => {
if (typeof item === 'object') {
Object.keys(item).map(attribute => {
body[array.name + '.' + key + '.' + attribute + '.' + lang] = item[attribute]
})
} else {
body[array.name + '.' + key + '.' + array.value + '.' + lang] = item[array.value]
}
console.log('body', key, array.value);
//delete item[array.value]
})
delete body[array.name]
}
} else {
if (body[key] !== undefined) {
body[key + '.' + lang] = body[key]
delete body[key]
}
}
})
ctx.request.bodyLang = body
return next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment