Create word list based on Array - Node.js Array

Node.js examples for Array:Array Value

Description

Create word list based on Array

Demo Code

;(function (exports){
  function WordList(){
    var args = Array.prototype.slice.call(arguments, 0);
    //if the argument passed is an array, treat it as the word list
    this.wordArray = Array.isArray(args[0]) ? args[0] : args;
    /*from  w ww .j  a  v a  2s .  c  o m*/
    if (this.wordArray[0] === undefined){
      this.wordArray = [];
    }

    this.refill();
    return this;
  }

  WordList.prototype.length = Array.prototype.length;
  WordList.prototype.concat = Array.prototype.concat;
  WordList.prototype.forEach = Array.prototype.forEach;
  WordList.prototype.push = Array.prototype.push;

  WordList.prototype.filter = function(fn){
    this.initialize();
    var outputArray = Array.prototype.filter.call(this, fn);
    return new WordList(outputArray);
  }

  WordList.prototype.some = function(fn){
    var someFunction = function(element){
      return typeof element === 'function' ? fn(element()) : fn(element);
    }
    return Array.prototype.some.call(this, someFunction);
  }

  WordList.prototype.every = function(fn){
    var everyFunction = function(element){
      return typeof element === 'function' ? fn(element()) : fn(element);
    }
    return Array.prototype.some.call(this, everyFunction);   
  }

  WordList.prototype.map = function(fn){
    this.initialize();
    var outputArray = Array.prototype.map.call(this, fn);
    if (outputArray[0].isWord){
      return new WordList(array);
    } else {
      return outputArray;
    }
  }

  WordList.prototype.push = function(word){
    if (typeof word === 'function'){
      this.initialized = false;
    }
    Array.prototype.push.call(this, word);
  }

  WordList.prototype.pop = function(){
    var popped = Array.prototype.pop.call(this);
    if (this.length === 0){
      this.refill();
    }
    return Tools.get(popped);
  }

  WordList.prototype.splice = function(index, num){
    var spliced = Array.prototype.splice.call(this, index, num);
    
    if (this.length == 0){
      this.refill;
    }
    return Tools.get(spliced);
  }

  WordList.prototype.indexOf = function(searchWord){
    for (var i = 0; i < this.length; i++){
      if (this[i].data === searchWord.data){
        return i;
      }
    }
    return -1;
  }

  WordList.prototype.initialize = function(){
    if (!this.initialized){
      // console.log('INITIALIZING WORD LIST -- ', this.length + ' words');
      this.forEach(function(word, index, array){
        if (typeof word === 'function'){
          array[index] = new word();
        }
      });

      this.initialized = true;
    }
  }

  WordList.prototype.refill = function(){
    var self = this;
    self.length = 0;
    self.initialized = true;
    
    self.wordArray.forEach(function(word){
      self.push(word);
      if (typeof word === 'function'){
        self.initialized = false;
      }
    });
  }

  WordList.prototype.print = function(){
    this.forEach(function(word){
      console.log(word.data.pParts[0] + ' (' + word.data.meanings[0] + ')');
    });
  }

  WordList.prototype.getWordAt = function(index){
    var word = this[index];

    if (this.length === 1){
      this.refill();
    } else {
      this.splice(index, 1);
    }

    return Tools.get(word);
  }

  WordList.prototype.copy = function(){
    return new WordList(this.wordArray);
  }

  WordList.prototype.combine = function(addArray){
    if (!addArray || addArray.length === 0) return this;

    var array1 = this.wordArray;
    var array2 = addArray.wordArray || addArray;
    var newArray = array1.concat(array2);
    return new WordList(newArray);
  }

  //check to see if the list contains a particular word
  WordList.prototype.contains = function(myWord){
    return this.some(function(word){
      return word.data === myWord.data;
    });
  }

  //make sure that there are X instances of a given word in a list
  WordList.prototype.wordAdd = function(word, numToAdd){
    if (typeof word === 'function'){
      this.initialized = false;
    }

    numToAdd = numToAdd || 1;

    for (var i = 0; i < numToAdd; i ++){
      this.push(word);
    }

    this.wordArray.push(word);

    return this;
  }

  //remove a particular word from the list
  WordList.prototype.wordRemove = function(word){
    var index;
    for (var w = 0; w < arguments.length; w ++) {
      index = this.indexOf(arguments[w]);
      while (index !== -1){
        this.splice(index, 1);
        index = this.indexOf(arguments[w]);
      }
    }
    return this;
  }

  //count instances of a specific word in a list
  WordList.prototype.wordCount = function(word){
    var count = 0;
    for (var i = 0; i < this.length; i ++){
      if (this[i].data === word.data){
        count ++;
      }
    }
    return count;
  }

  exports.WordList = WordList;
})(this);

Related Tutorials