Code coverage report for nodash/lib/List.js

Statements: 100% (49 / 49)      Branches: 100% (10 / 10)      Functions: 100% (14 / 14)      Lines: 100% (49 / 49)      Ignored: none     

All files » nodash/lib/ » List.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97      1     5   1 113 113 111 3 3 3   108   113 90 83 83 83   7     5 5 5 4   5 2 2 10 10       5 5   5   5 5   1 21     1 14 14 33   14     1 4 4 4 1 17 17   4     5         6       34                       4        
/* vim: set et sw=2 ts=2: */
'use strict';
 
module.exports = [ 'Thunk', 'idf', 'freeze', 'create',
  function (Thunk, idf, freeze, create) {
 
  var Nodash = this;
 
  function List(head, tail) {
    var self = this;
    this.head = function () {
      if (Nodash.is(Thunk, head)) {
        var value = head.get();
        self.head = head.get;
        return value;
      }
      return head;
    };
    this.tail = function () {
      if (Nodash.is(Thunk, tail)) {
        var value = tail.get();
        self.tail = tail.get;
        return value;
      }
      return tail;
    };
  }
  List.__type = 'list';
  List.prototype.isEmpty = idf(false);
  List.prototype.toString = function () {
    return '[object List ' + listToArray(this) + ']';
  };
  List.prototype.forEach = function (f) {
    var xs = this;
    while (!xs.isEmpty()) {
      f(xs.head());
      xs = xs.tail();
    }
  };
 
  var emptyList = new List();
  emptyList.isEmpty = idf(true);
 
  List.emptyList = emptyList;
 
  freeze(emptyList);
  freeze(List);
 
  function arrayToString(array) {
    return array.join('');
  }
  
  function listToArray(xs) {
    var array = [];
    Nodash.each(function (x) {
      array.push(x);
    }, xs);
    return array;
  }
 
  function rangeStep(step, from, to) {
    step = from < to ? step : -step;
    to = step > 0 ? Math.floor(to) : Math.ceil(to);
    var current = from;
    function gen() {
      current += step;
      return new List(current, current === to ? emptyList : new Thunk(gen));
    }
    return new List(from, new Thunk(gen));
  }
 
  return {
 
    List: List,
 
    singleton: function (thing) {
      return new List(thing, emptyList);
    },
 
    emptyList: function () {
      return emptyList;
    },
 
    arrayToString: arrayToString,
 
    listToArray: listToArray,
    
    listToString: Nodash.compose(arrayToString, listToArray),
 
    rangeStep: rangeStep,
 
    '.. range': function (from, to) {
      return rangeStep(1, from, to);
    }
  };
}];