Last active
January 18, 2019 03:22
-
-
Save rxluz/0606c8a7afd57d9372db3e35ebc7241a to your computer and use it in GitHub Desktop.
S.O.L.I.D Principles for JS with examples, see more at https://medium.com/p/db95b44e82e
This file contains hidden or 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
class ValidateEmailSimple { | |
isValid(email) { | |
return email.indexOf("@") > 2; | |
} | |
} | |
class ValidateEmailAdvanced { | |
isValid(email) { | |
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
return re.test(String(email).toLowerCase()); | |
} | |
} | |
class Client { | |
constructor({ name, email, address, ValidateEmailService }) { | |
this._emailValidationService = ValidateEmailService; | |
this.setName(name); | |
this.setEmail(email); | |
this.setAddress(address); | |
} | |
setName(name) { | |
this.name = name; | |
} | |
setEmail(email) { | |
if (this._emailValidationService.isValid(email)) { | |
this.email = email; | |
} | |
} | |
setAddress(address) { | |
this.address = address; | |
} | |
} | |
class ClientFacade { | |
constructor({ name, email, address }) { | |
const ClientInstance = new Client({ | |
name, | |
email, | |
address, | |
ValidateEmailService: new ValidateEmailSimple(), | |
}); | |
return ClientInstance; | |
} | |
} | |
const run = () => { | |
const ClientInstance = new ClientFacade({ | |
name: "Some client name", | |
email: "[email protected]", | |
address: "addresss", | |
}); | |
console.log(ClientInstance); | |
}; | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment