Last active
October 23, 2017 12:48
-
-
Save maciejsikora/0aefa5d4d4bd9527ca78aaec85dccc10 to your computer and use it in GitHub Desktop.
es6-module-test-user-module
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
function hasProp (obj, prop) { | |
return obj.hasOwnProperty(prop) | |
} | |
function hasProps (obj, props) { | |
return props.filter(prop => hasProp(obj, prop)).length === props.length | |
} | |
function isString (value) { | |
return typeof value === 'string' | |
} | |
function arePropsStrings (obj, props) { | |
return props.filter(prop => isString(obj[prop])).length === props.length | |
} | |
// PUBLIC | |
function createUser (user) { | |
const props = ['name', 'lastname'] | |
if (!hasProps(user, props) || !arePropsStrings(user, props)) { | |
throw new Error('Wrong user data') | |
} | |
return { | |
getName: () => user.name, | |
getLastName: () => user.lastname, | |
getFullName: () => user.name + ' ' + user.lastname | |
} | |
} | |
module.exports = { createUser } // export outside only public function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment