Skip to content

Instantly share code, notes, and snippets.

@moshmage
Last active October 7, 2016 14:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save moshmage/853817a86cd59c766e979ff7037cecc9 to your computer and use it in GitHub Desktop.
Simple Agenda Class
class Agenda {
constructor() {
this._AGENDA = [new Pessoa()];
}
/**
*
* @param name {String}
* @param address {String}
* @param mobileNumber {String}
* @param age {Int}
*/
addEntry(name, address, mobileNumber, age) {
if (!name || !address || !mobileNumber || !age) throw "Not enough arguments";
if (!this.findEntry(name, {searchMobile: mobileNumber, searchAddress: address})) {
this._AGENDA.push(new Pessoa(name, address, mobileNumber, age));
} else throw "Entry already exists";
}
/**
*
* @param searchString {String}
* @param options {Object} {address: Boolean || String, mobile: Boolean || String}
*/
removeEntry(searchString = 'Joao', options = {address: false, mobile: false}) {
let removable = this.findEntry(searchString);
if (!removable && (options.address || options.mobile)) {
removable = this.findEntry(searchString,{searchMobile: options.mobile, searchAddress: options.address});
}
if (!removable) throw "No entry found";
this._AGENDA.splice(removable.index, 1);
}
/**
*
* @param searchString {String}
* @param options {Object} {returnIndexArray: Boolean || String, searchMobile: Boolean || String, searchAddress: Boolean || String}
* @returns {Array | Object} [0,1,2] | {person, index}
*/
findEntry(searchString = 'Joao', options = {returnIndexArray: false, searchMobile: false, searchAddress: false}) {
let personOfInterest = (options.returnIndexArray) ? [] : false;
this._AGENDA.some((person, index) => {
if (options.searchAddress && options.searchMobile) {
if (person.mobileNumber.indexOf(options.searchMobile) &&
person.address.indexOf(options.searchAddress) &&
person.name.indexOf(searchString) > -1) {
if (options.returnIndexArray) {
personOfInterest.push(index);
} else { personOfInterest = {person, index}}
}
} else if (options.searchAddress || options.searchMobile) {
if ((person.mobileNumber.indexOf(options.searchMobile) ||
person.address.indexOf(options.searchAddress)) &&
person.name.indexOf(searchString) > -1) {
if (options.returnIndexArray) {
personOfInterest.push(index);
} else { personOfInterest = {person, index}}
}
} else {
if (person.name.indexOf(searchString) > -1) {
if (options.returnIndexArray) {
personOfInterest.push(index);
} else { personOfInterest = {person, index}}
}
}
if (personOfInterest.person && !options.returnIndexArray) return true;
});
return personOfInterest;
}
/**
*
* @param searchString {string}
* @param useLocation {Boolean || String}
* @returns {Person}
*/
getInfo(searchString = 'Joao', useLocation = 'Banda' || false) {
let entry = this.findEntry(searchString, {searchAddress: useLocation});
if (!entry) throw "No such entry found";
return entry.person;
}
/**
*
* @param index {Int}
* @returns {Person}
*/
getEntry(index = 0) {
if (typeof index !== "number") throw "Wrong argument type getEntry(Number)";
if (index > this._AGENDA.length) throw "No such index, total of indexes: " + this._AGENDA.length;
return this._AGENDA[index - 1];
}
get Lista() {
return this._AGENDA;
}
/**
* Return always false so it cant be overwritten
*/
set Lista(value) { return false; }
}
const Agenda = require('agenda-class.js');
let agenda = new Agenda();
//agenda.addEntry(); // throw not enough params
agenda.addEntry('André', 'Banda Y', '939999999', 12);
agenda.addEntry('Vitor', 'Banda U', '949999999', 12);
//agenda.addEntry('Vitor', 'Banda U', '949999999', 12); // throws already exists
agenda.addEntry('Vitor', 'Banda Z', '949999999', 12); // does not throw because different location (while same name)
agenda.removeEntry('Vitor',{address: 'Banda U'});
console.log('Encontrar Pessoas em endereço "Banda" (lista de posição)',
agenda.findEntry('', {searchAddress: 'Banda', returnIndexArray: true}));
console.log('Informação por Index',
agenda.getEntry(0));
console.log('Encontrar Informação de Pessoa',
agenda.getInfo('Joao'));
console.log('Lista da Agenda',
agenda.Lista);
class Pessoa {
constructor(name = 'Joao', address = 'Banda T', mobileNumber = '929999999', age = 12) {
this.name = name || '';
this.address = address || '';
this.mobileNumber = mobileNumber || '00000000';
this.age = age || 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment