Last active
June 20, 2018 20:00
-
-
Save gabrielu/a3963820811118323c1ba384f03742a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// requires babel w/ babel-preset-es2017 & babel-plugin-transform-runtime | |
// for Object.entries support | |
// .babelrc | |
// { | |
// "plugins": ["transform-runtime"], | |
// "presets": ["es2017"] | |
// } | |
const errors = Object.entries({ | |
email: emailValidation.error, | |
password: passwordValidation.error, | |
}).reduce((p, [k, v]) => { | |
if (v) p[k] = v; | |
return p; | |
}, {}); | |
// const errors = Object.entries({ | |
// email: 'Required field', | |
// password: null | |
// }).reduce((p, [...kv]) => { | |
// if (kv[1]) p[kv[0]] = kv[1]; | |
// return p; | |
// }, {}); | |
// VV OLDER APPROACH VV | |
const allErrors = { | |
email: 'Required field', | |
password: null | |
}; | |
const errors = Object.keys(allErrors).reduce((p, c) => { | |
if (allErrors[c]) p[c] = allErrors[c]; | |
return p; | |
}, {}); | |
console.log(errors); // { email: 'Required field' } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment