Skip to content

Instantly share code, notes, and snippets.

@angelomiranda
Last active August 5, 2017 04:34
Show Gist options
  • Save angelomiranda/f2906d04b37afde252b27ac1711af21d to your computer and use it in GitHub Desktop.
Save angelomiranda/f2906d04b37afde252b27ac1711af21d to your computer and use it in GitHub Desktop.
this.remoteLoggingOptions = ['warn', 'info', 'error', 'debug'];
const form = {
value: {
waitTime: 300,
maxMsgs: 100,
warn: true,
info: true,
debug: false,
error: false
}
}
const remoteLoggingModel = {
key: 'loggerRemoteLevels',
value: ''
}
const formValue = form.value; // model values coming from ngForm model
let modelKeys = Object.keys(formValue);
let transformedObject = modelKeys.reduce((result, current) => {
let value = formValue[current]; // current model value
// remoteLoggingOptions are comma separated options
// 'warn,error' | 'warn,info,debug,error'
if (_.includes(this.remoteLoggingOptions, current) && value) {
result.push({
key: 'loggerRemoteLevels',
value: current
});
}
if (current === 'waitTime') {
result.push({
key: 'loggerRemoteWaitTime',
value: value
});
}
if (current === 'maxMsgs') {
result.push({
key: 'loggerRemoteMaxMsgs',
value: value
});
}
return result;
}, []);
let otherRemoteLoggingOptions = _.reject(transformedObject, (current) => {
return current.key === 'loggerRemoteLevels'
})
let getLoggerRemoteLevels = _(transformedObject)
.filter(function(currentObj) {
return currentObj.key === 'loggerRemoteLevels'
})
.map('value')
.value();
let remoteLoggingOptions = {
value: getLoggerRemoteLevels.join(',')
}
let finalRemoteLoggingModel = _.assign(remoteLoggingModel, remoteLoggingOptions)
let finalRemoteLoggingOptions = otherRemoteLoggingOptions.concat(finalRemoteLoggingModel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment