Most arrays are now using lists.

This commit is contained in:
hydrabolt
2015-08-12 16:05:21 +01:00
parent 401073f897
commit dda5d4272e
4 changed files with 321 additions and 307 deletions

View File

@@ -1,74 +1,74 @@
exports.List = function(discriminator) {
this.discriminator = discriminator;
this.contents = [];
exports.List = function( discriminator ) {
this.discriminator = discriminator;
this.contents = [];
}
exports.List.prototype.add = function(child){
if(child.constructor === Array){
exports.List.prototype.add = function( child ) {
if ( child.constructor === Array ) {
children = child;
for(child of children){
if( this.filter( this.discriminator, child[this.discriminator] ).length === 0 )
this.contents.push(child);
}
children = child;
for ( child of children ) {
if ( this.filter( this.discriminator, child[ this.discriminator ] ).length === 0 )
this.contents.push( child );
}
}else{
if( this.filter( this.discriminator, child[this.discriminator] ).length === 0 )
this.contents.push(child);
}
} else {
if ( this.filter( this.discriminator, child[ this.discriminator ] ).length === 0 )
this.contents.push( child );
}
}
exports.List.prototype.length = function(){
return this.contents.length;
exports.List.prototype.length = function() {
return this.contents.length;
}
exports.List.prototype.removeIndex = function(index){
this.contents.splice(index, 1);
exports.List.prototype.removeIndex = function( index ) {
this.contents.splice( index, 1 );
}
exports.List.prototype.removeChild = function(child){
exports.List.prototype.removeElement = function( child ) {
var index = this.contents.indexOf(child);
var index = this.contents.indexOf( child );
if( index === -1 ){
return false;
}
if ( index === -1 ) {
return false;
}
this.removeIndex(index);
this.removeIndex( index );
}
exports.List.prototype.concatSublists = function(whereList, discriminator){
//this is meant to look at the contents, and assuming the contents are all lists, concatenate their values.
exports.List.prototype.concatSublists = function( whereList, discriminator ) {
//this is meant to look at the contents, and assuming the contents are all lists, concatenate their values.
var concatList = new exports.List(discriminator);
var concatList = new exports.List( discriminator );
for(item of this.contents){
var itemList = item[whereList];
concatList.add(itemList.contents);
}
for ( item of this.contents ) {
var itemList = item[ whereList ];
concatList.add( itemList.contents );
}
return concatList;
return concatList;
}
exports.List.prototype.filter = function(key, value, onlyOne) {
exports.List.prototype.filter = function( key, value, onlyOne ) {
var results = [];
var results = [];
for (index in this.contents) {
var child = this.contents[index];
if (child[key] == value) {
if (onlyOne) {
return child;
} else {
results.push(child);
}
}
}
for ( index in this.contents ) {
var child = this.contents[ index ];
if ( child[ key ] == value ) {
if ( onlyOne ) {
return child;
} else {
results.push( child );
}
}
}
if(onlyOne){
return false;
}
if ( onlyOne ) {
return false;
}
return results;
return results;
}

View File

@@ -1,37 +1,32 @@
var User = require("./user.js").User;
var User = require( "./user.js" ).User;
var List = require( "./list.js" ).List;
exports.Message = function(time, author, content, channel, id, mentions){
exports.Message = function( time, author, content, channel, id, mentions, everyoneMentioned ) {
if(!content){
message = time;
channel = author;
time = message.timestamp;
author = message.author;
content = message.content;
id = message.id;
mentions = message.mentions;
}
if ( !content ) {
message = time;
channel = author;
time = message.timestamp;
author = message.author;
content = message.content;
id = message.id;
mentions = message.mentions;
everyoneMentioned = message.mention_everyone;
}
this.time = Date.parse(time);
this.author = new User(author);
this.content = content.replace(/<[^>]*>/g, "").replace(/\s+/g, ' ').trim();
this.channel = channel;
this.id = id;
this.mentions = mentions || [];
for(x in mentions){
var _mention = mentions[x];
this.mentions.push( new User(_mention.username, _mention.id, _mention.discriminator, _mention.avatar) );
}
this.time = Date.parse( time );
this.author = new User( author );
this.content = content.replace( /\s+/g, ' ' ).trim(); //content.replace(/<[^>]*>/g, "").replace(/\s+/g, ' ').trim();
this.channel = channel;
this.id = id;
this.mentions = new List( "id" );
this.everyoneMentioned = everyoneMentioned;
for ( x in mentions ) {
var _mention = mentions[ x ];
this.mentions.add( new User( _mention ) );
}
}
exports.Message.prototype.isMentioned = function(user){
for(mention of this.mentions){
if(user.equals(mention)){
return true;
}
}
return false;
exports.Message.prototype.isMentioned = function( user ) {
return ( this.mentions.filter( "id", user.id ).length > 0 );
}

View File

@@ -1,24 +1,23 @@
var User = require("./user.js").User;
var List = require("./list.js").List;
var User = require( "./user.js" ).User;
var List = require( "./list.js" ).List;
exports.Server = function( region, ownerID, name, id, members ) {
exports.Server = function(region, ownerID, name, id, members){
this.region = region;
this.ownerID = ownerID;
this.name = name;
this.id = id;
this.members = new List( "id" );
this.channels = new List( "id" );
this.region = region;
this.ownerID = ownerID;
this.name = name;
this.id = id;
this.members = new List("id");
this.channels = new List("id");
for(x in members){
var member = members[x].user;
this.members.add( new User(member) );
}
for ( x in members ) {
var member = members[ x ].user;
this.members.add( new User( member ) );
}
}
exports.Server.prototype.getDefaultChannel = function(){
exports.Server.prototype.getDefaultChannel = function() {
return this.channels.filter("name", "general", true);
return this.channels.filter( "name", "general", true );
}