started documenting

This commit is contained in:
hydrabolt
2015-08-16 17:48:55 +01:00
parent 9422a56d4e
commit 4773df9fc8
4 changed files with 280 additions and 45 deletions

View File

@@ -1,9 +1,39 @@
/**
* Similar to a Java set. Contains no duplicate elements and includes filter
* functions. Discriminates between elements based on a discriminator passed
* when created. Generally "ID"
* @class List
*/
exports.List = function( discriminator, cap ) {
/**
* What to use to distringuish duplicates
* @attribute discriminator
* @type {String}
*/
this.discriminator = discriminator;
/**
* The maximum amount of elements allowed in the list.
* @default Infinity
* @attribute cap
* @type {Number}
*/
this.cap = cap || Number.MAX_SAFE_INTEGER;
/**
* The Array version of the List.
* @type {Array}
* @attribute contents
*/
this.contents = [];
}
/**
* Adds an element to the list if it isn't already there.
* @method add
* @param {Object/Array} element The element(s) to add
* @example
* List.add( obj );
* List.add( [ obj, obj, obj ] );
*/
exports.List.prototype.add = function( child ) {
var self = this;
@@ -12,17 +42,17 @@ exports.List.prototype.add = function( child ) {
children = child;
for ( child of children ) {
addChild(child);
addChild( child );
}
} else {
addChild(child);
addChild( child );
}
function addChild(child){
function addChild( child ) {
if(self.length() > self.cap){
self.splice(0, 1);
if ( self.length() > self.cap ) {
self.splice( 0, 1 );
}
if ( self.filter( self.discriminator, child[ self.discriminator ] ).length === 0 )
@@ -30,17 +60,28 @@ exports.List.prototype.add = function( child ) {
}
}
/**
* Returns the length of the List
* @method length
* @return {Number}
*/
exports.List.prototype.length = function() {
return this.contents.length;
}
exports.List.prototype.getIndex = function( object ){
/**
* Gets the index of an element in the List or defaults to false
* @param {Object} object The element we want to get the index of
* @return {Number/Boolean} The index if the object is in the list, or false.
* @method getIndex
*/
exports.List.prototype.getIndex = function( object ) {
var index = false;
for(elementIndex in this.contents){
var element = this.contents[elementIndex];
if( element[this.discriminator] == object[this.discriminator] ){
for ( elementIndex in this.contents ) {
var element = this.contents[ elementIndex ];
if ( element[ this.discriminator ] == object[ this.discriminator ] ) {
return elementIndex;
}
@@ -50,28 +91,46 @@ exports.List.prototype.getIndex = function( object ){
}
/**
* Removes an element at the specified index
* @param {Number} index
* @method removeIndex
*/
exports.List.prototype.removeIndex = function( index ) {
this.contents.splice( index, 1 );
}
/**
* Removes an element from the list
* @param {Object} element
* @method removeElement
* @return {Boolean} whether the operation was successful or not.
*/
exports.List.prototype.removeElement = function( child ) {
for ( _element in this.contents ) {
var element = this.contents[_element];
var element = this.contents[ _element ];
if ( child[ this.discriminator ] == element[ this.discriminator ] ) {
this.removeIndex( _element, 1 );
return true;
}
}
return false;
}
exports.List.prototype.updateElement = function( child, newChild ){
/**
* Replaces an element in the list with a specified element
* @param {Object} element Element to update.
* @param {Object} newElement New Element
* @return {Boolean} whether the operation was successful or not.
*/
exports.List.prototype.updateElement = function( child, newChild ) {
for ( _element in this.contents ) {
var element = this.contents[_element];
var element = this.contents[ _element ];
if ( child[ this.discriminator ] == element[ this.discriminator ] ) {
this.contents[_element] = newChild;
this.contents[ _element ] = newChild;
return true;
}
}

View File

@@ -1,15 +1,66 @@
var User = require( "./user.js" ).User;
var List = require( "./list.js" ).List;
/**
* A wrapper for Server information, contains channels and users too. Developers should not instantiate the class, instead they should
* manipulate Server objects given to them.
* @class Server
* @param {String} region The region of the server
*/
exports.Server = function( region, ownerID, name, id, members, icon, afkTimeout, afkChannelId ) {
/**
* The region of the Server
* @type {String}
* @attribute region
*/
this.region = region;
/**
* The ID of the owner of the Server (not a User!)
* @type {String}
* @attribute ownerID
*/
this.ownerID = ownerID;
/**
* The name of the Server
* @type {String}
* @attribute name
*/
this.name = name;
/**
* The ID of the Server
* @type {String}
* @attribute id
*/
this.id = id;
/**
* List containing members of the Server
* @param {List}
* @attribute members
*/
this.members = new List( "id" );
/**
* List containing channelss of the Server
* @param {List}
* @attribute channels
*/
this.channels = new List( "id" );
/**
* ID of the Icon of the Server
* @param {String}
* @attribute icon
*/
this.icon = icon;
/**
* The amount of seconds that should pass before the user is
* @type {Number}
* @attribute afkTimeout
*/
this.afkTimeout = afkTimeout;
/**
* The ID of the AFK Channel, evaluates to false if doesn't exist.
* @type {String}
* @attribute afkChannelid
*/
this.afkChannelId = afkChannelId;
for ( x in members ) {
@@ -18,12 +69,22 @@ exports.Server = function( region, ownerID, name, id, members, icon, afkTimeout,
}
}
/**
* Returns a valid URL pointing towards the server's icon if it has one.
* @method getIconURL
* @return {String/Boolean} If there is an icon, a URL is returned. If not, false is returned.
*/
exports.Server.prototype.getIconURL = function(){
if(!this.icon)
return false;
return "https://discordapp.com/api/guilds/"+this.id+"/icons/"+this.icon+".jpg";
}
/**
* Returns the AFK Channel if a server has one
* @method getAFKChannel
* @return {Channel/Boolean} If there is an AFK Channel, a Channel is returned. If not, false is returned.
*/
exports.Server.prototype.getAFKChannel = function(){
if(!this.afkChannelId)
@@ -33,7 +94,11 @@ exports.Server.prototype.getAFKChannel = function(){
}
/**
* Returns the #general channel of the server.
* @method getDefaultChannel
* @return {Channel} Returns the #general channel of the Server.
*/
exports.Server.prototype.getDefaultChannel = function() {
return this.channels.filter( "name", "general", true );