Skip to content

Instantly share code, notes, and snippets.

@rxluz
Last active January 18, 2019 02:32
Show Gist options
  • Save rxluz/c80a83dd7e231e3e5f6ffe0446e4f5e5 to your computer and use it in GitHub Desktop.
Save rxluz/c80a83dd7e231e3e5f6ffe0446e4f5e5 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
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 }) {
this.setName(name);
this.setEmail(email);
this.setAddress(address);
}
setName(name) {
this.name = name;
}
setEmail(email) {
const ValidadeEmailInstance = new ValidateEmailSimple();
if (ValidadeEmailInstance.isValid(email)) {
this.email = email;
}
}
setAddress(address) {
this.address = address;
}
}
const run = () => {
const ClientInstance = new Client({
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