Code coverage report for sc/classlib/Core/Char.js

Statements: 100% (80 / 80)      Branches: 100% (49 / 49)      Functions: 100% (29 / 29)      Lines: 100% (80 / 80)      Ignored: none     

All files » sc/classlib/Core/ » Char.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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 1611     1   1   1 1 426     1 1     1 1     1 1     1 1     1 1     1       1 12     1 128 128 10   118 26   92 26   66     1   1 1     1 1     1 1     1 128 128       1 128 128         1 128 128     1 128 128           1 128 128     1 128 128     1 128 128     1 128 128     1 128 128     1 128 128     1 128 128           1 128 128     1 4     1 1             1 1     1 1     1      
SCScript.install(function(sc) {
  "use strict";
 
  require("../Math/Magnitude");
 
  var $ = sc.lang.$;
 
  sc.lang.klass.refine("Char", function(builder) {
    builder.addMethod("__str__", function() {
      return this._;
    });
 
    builder.addClassMethod("nl", function() {
      return $.Char("\n");
    });
 
    builder.addClassMethod("ff", function() {
      return $.Char("\f");
    });
 
    builder.addClassMethod("tab", function() {
      return $.Char("\t");
    });
 
    builder.addClassMethod("space", function() {
      return $.Char(" ");
    });
 
    builder.addClassMethod("comma", function() {
      return $.Char(",");
    });
 
    builder.shouldUseLiterals("new");
 
    // TODO: implements hash
 
    builder.addMethod("ascii", function() {
      return $.Integer(this._.charCodeAt(0));
    });
 
    builder.addMethod("digit", function() {
      var ascii = this._.charCodeAt(0);
      if (0x30 <= ascii && ascii <= 0x39) {
        return $.Integer(ascii - 0x30);
      }
      if (0x41 <= ascii && ascii <= 0x5a) {
        return $.Integer(ascii - 0x37);
      }
      if (0x61 <= ascii && ascii <= 0x7a) {
        return $.Integer(ascii - 0x57);
      }
      throw new Error("digitValue failed");
    });
 
    builder.addMethod("asAscii");
 
    builder.addMethod("asUnicode", function() {
      return this.ascii();
    });
 
    builder.addMethod("toUpper", function() {
      return $.Char(this._.toUpperCase());
    });
 
    builder.addMethod("toLower", function() {
      return $.Char(this._.toLowerCase());
    });
 
    builder.addMethod("isAlpha", function() {
      var ascii = this._.charCodeAt(0);
      return $.Boolean((0x41 <= ascii && ascii <= 0x5a) ||
                         (0x61 <= ascii && ascii <= 0x7a));
    });
 
    builder.addMethod("isAlphaNum", function() {
      var ascii = this._.charCodeAt(0);
      return $.Boolean((0x30 <= ascii && ascii <= 0x39) ||
                         (0x41 <= ascii && ascii <= 0x5a) ||
                         (0x61 <= ascii && ascii <= 0x7a));
    });
 
    builder.addMethod("isPrint", function() {
      var ascii = this._.charCodeAt(0);
      return $.Boolean((0x20 <= ascii && ascii <= 0x7e));
    });
 
    builder.addMethod("isPunct", function() {
      var ascii = this._.charCodeAt(0);
      return $.Boolean((0x21 <= ascii && ascii <= 0x2f) ||
                         (0x3a <= ascii && ascii <= 0x40) ||
                         (0x5b <= ascii && ascii <= 0x60) ||
                         (0x7b <= ascii && ascii <= 0x7e));
    });
 
    builder.addMethod("isControl", function() {
      var ascii = this._.charCodeAt(0);
      return $.Boolean((0x00 <= ascii && ascii <= 0x1f) || ascii === 0x7f);
    });
 
    builder.addMethod("isSpace", function() {
      var ascii = this._.charCodeAt(0);
      return $.Boolean((0x09 <= ascii && ascii <= 0x0d) || ascii === 0x20);
    });
 
    builder.addMethod("isVowl", function() {
      var ch = this._.charAt(0).toUpperCase();
      return $.Boolean("AEIOU".indexOf(ch) !== -1);
    });
 
    builder.addMethod("isDecDigit", function() {
      var ascii = this._.charCodeAt(0);
      return $.Boolean((0x30 <= ascii && ascii <= 0x39));
    });
 
    builder.addMethod("isUpper", function() {
      var ascii = this._.charCodeAt(0);
      return $.Boolean((0x41 <= ascii && ascii <= 0x5a));
    });
 
    builder.addMethod("isLower", function() {
      var ascii = this._.charCodeAt(0);
      return $.Boolean((0x61 <= ascii && ascii <= 0x7a));
    });
 
    builder.addMethod("isFileSafe", function() {
      var ascii = this._.charCodeAt(0);
      return $.Boolean((0x20 <= ascii && ascii <= 0x7e) &&
                         ascii !== 0x2f && // 0x2f is '/'
                         ascii !== 0x3a && // 0x3a is ':'
                         ascii !== 0x22);  // 0x22 is '"'
    });
 
    builder.addMethod("isPathSeparator", function() {
      var ascii = this._.charCodeAt(0);
      return $.Boolean(ascii === 0x2f);
    });
 
    builder.addMethod("<", function($aChar) {
      return $.Boolean(this.ascii() < $aChar.ascii());
    });
 
    builder.addMethod("++", function($that) {
      return $.String(this._ + $that.__str__());
    });
 
    // TODO: implements $bullet
    // TODO: implements printOn
    // TODO: implements storeOn
 
    builder.addMethod("archiveAsCompileString", function() {
      return $.True();
    });
 
    builder.addMethod("asString", function() {
      return $.String(this._);
    });
 
    builder.addMethod("shallowCopy");
  });
});