Code coverage report for sc/classlib/Collections/String.js

Statements: 100% (98 / 98)      Branches: 100% (20 / 20)      Functions: 100% (31 / 31)      Lines: 100% (98 / 98)      Ignored: none     

All files » sc/classlib/Collections/ » String.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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 2871     1   1 1 1 1   1 1 26     1 6 4   2 1   1     1 120 424       1 22                   1 2     1 6 6     1 7 7     1 1 1   1 1 3     1             1     30   30 1     29 29 29   29 3 10     26 90     29 50 50 50 50 18       11 1 10 1     11     1 6         1 3         1 3         1 3         1 3         1 3             1 6     1 1     1 1     1   1   1 1     1 1     1 1 1     1 1 1     1 1 1     1 1 1                       1 3         1 5                                                                                                                                                        
SCScript.install(function(sc) {
  "use strict";
 
  require("./ArrayedCollection");
 
  var $  = sc.lang.$;
  var $nil   = $.nil;
  var $false = $.false;
  var $space = $.Char(" ");
 
  sc.lang.klass.refine("String", function(builder) {
    builder.addMethod("__str__", function() {
      return this.valueOf();
    });
 
    builder.addMethod("__elem__", function($item) {
      if (!$item) {
        return $space;
      }
      if ($item.__tag !== sc.TAG_CHAR) {
        throw new TypeError("Wrong type.");
      }
      return $item;
    });
 
    builder.addMethod("valueOf", function() {
      return this._.map(function(elem) {
        return elem.__str__();
      }).join("");
    });
 
    builder.addMethod("toString", function() {
      return this.valueOf();
    });
 
    // TODO: implements unixCmdActions
    // TODO: implements unixCmdActions_
    // TODO: implements $initClass
    // TODO: implements $doUnixCmdAction
    // TODO: implements unixCmd
    // TODO: implements unixCmdGetStdOut
 
    builder.addMethod("asSymbol", function() {
      return $.Symbol(this.__str__());
    });
 
    builder.addMethod("asInteger", function() {
      var m = /^[-+]?\d+/.exec(this.__str__());
      return $.Integer(m ? m[0]|0 : 0);
    });
 
    builder.addMethod("asFloat", function() {
      var m = /^[-+]?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?/.exec(this.__str__());
      return $.Float(m ? +m[0] : 0);
    });
 
    builder.addMethod("ascii", function() {
      var raw = this.__str__();
      var a, i, imax;
 
      a = new Array(raw.length);
      for (i = 0, imax = a.length; i < imax; ++i) {
        a[i] = $.Integer(raw.charCodeAt(i));
      }
 
      return $.Array(a);
    });
 
    // TODO: implements stripRTF
    // TODO: implements stripHTML
    // TODO: implements $scDir
 
    builder.addMethod("compare", {
      args: "aString; ignoreCase=false"
    }, function($aString, $ignoreCase) {
      var araw, braw, length, i, a, b, cmp, func;
 
      if ($aString.__tag !== sc.TAG_STR) {
        return $nil;
      }
 
      araw = this._;
      braw = $aString._;
      length = Math.min(araw.length, braw.length);
 
      if ($ignoreCase.__bool__()) {
        func = function(ch) {
          return ch.toLowerCase();
        };
      } else {
        func = function(ch) {
          return ch;
        };
      }
      for (i = 0; i < length; i++) {
        a = func(araw[i]._).charCodeAt(0);
        b = func(braw[i]._).charCodeAt(0);
        cmp = a - b;
        if (cmp !== 0) {
          return $.Integer(cmp < 0 ? -1 : +1);
        }
      }
 
      if (araw.length < braw.length) {
        cmp = -1;
      } else if (araw.length > braw.length) {
        cmp = 1;
      }
 
      return $.Integer(cmp);
    });
 
    builder.addMethod("<", function($aString) {
      return $.Boolean(
        this.compare($aString, $false).__num__() < 0
      );
    });
 
    builder.addMethod(">", function($aString) {
      return $.Boolean(
        this.compare($aString, $false).__num__() > 0
      );
    });
 
    builder.addMethod("<=", function($aString) {
      return $.Boolean(
        this.compare($aString, $false).__num__() <= 0
      );
    });
 
    builder.addMethod(">=", function($aString) {
      return $.Boolean(
        this.compare($aString, $false).__num__() >= 0
      );
    });
 
    builder.addMethod("==", function($aString) {
      return $.Boolean(
        this.compare($aString, $false).__num__() === 0
      );
    });
 
    builder.addMethod("!=", function($aString) {
      return $.Boolean(
        this.compare($aString, $false).__num__() !== 0
      );
    });
 
    // TODO: implements hash
 
    builder.addMethod("performBinaryOpOnSimpleNumber", function($aSelector, $aNumber) {
      return $aNumber.asString().perform($aSelector, this);
    });
 
    builder.addMethod("performBinaryOpOnComplex", function($aSelector, $aComplex) {
      return $aComplex.asString().perform($aSelector, this);
    });
 
    builder.addMethod("multiChannelPerform", function() {
      throw new Error("String:multiChannelPerform. Cannot expand strings.");
    });
 
    builder.addMethod("isString", sc.TRUE);
 
    builder.addMethod("asString");
 
    builder.addMethod("asCompileString", function() {
      return $.String("\"" + this.__str__() + "\"");
    });
 
    builder.addMethod("species", function() {
      return $("String");
    });
 
    builder.addMethod("postln", function() {
      sc.lang.io.post(this.__str__() + "\n");
      return this;
    });
 
    builder.addMethod("post", function() {
      sc.lang.io.post(this.__str__());
      return this;
    });
 
    builder.addMethod("postcln", function() {
      sc.lang.io.post("// " + this.__str__() + "\n");
      return this;
    });
 
    builder.addMethod("postc", function() {
      sc.lang.io.post("// " + this.__str__());
      return this;
    });
 
    // TODO: implements postf
    // TODO: implements format
    // TODO: implements matchRegexp
    // TODO: implements fformat
    // TODO: implements die
    // TODO: implements error
    // TODO: implements warn
    // TODO: implements inform
 
    builder.addMethod("++", function($anObject) {
      return $.String(
        this.toString() + $anObject.asString().toString()
      );
    });
 
    builder.addMethod("+", function($anObject) {
      return $.String(
        this.toString() + " " + $anObject.asString().toString()
      );
    });
    // TODO: implements catArgs
    // TODO: implements scatArgs
    // TODO: implements ccatArgs
    // TODO: implements catList
    // TODO: implements scatList
    // TODO: implements ccatList
    // TODO: implements split
    // TODO: implements containsStringAt
    // TODO: implements icontainsStringAt
    // TODO: implements contains
    // TODO: implements containsi
    // TODO: implements findRegexp
    // TODO: implements findAllRegexp
    // TODO: implements find
    // TODO: implements findBackwards
    // TODO: implements endsWith
    // TODO: implements beginsWith
    // TODO: implements findAll
    // TODO: implements replace
    // TODO: implements escapeChar
    // TODO: implements shellQuote
    // TODO: implements quote
    // TODO: implements tr
    // TODO: implements insert
    // TODO: implements wrapExtend
    // TODO: implements zeroPad
    // TODO: implements padLeft
    // TODO: implements padRight
    // TODO: implements underlined
    // TODO: implements scramble
    // TODO: implements rotate
    // TODO: implements compile
    // TODO: implements interpret
    // TODO: implements interpretPrint
    // TODO: implements $readNew
    // TODO: implements printOn
    // TODO: implements storeOn
    // TODO: implements inspectorClass
    // TODO: implements standardizePath
    // TODO: implements realPath
    // TODO: implements withTrailingSlash
    // TODO: implements withoutTrailingSlash
    // TODO: implements absolutePath
    // TODO: implements pathMatch
    // TODO: implements load
    // TODO: implements loadPaths
    // TODO: implements loadRelative
    // TODO: implements resolveRelative
    // TODO: implements include
    // TODO: implements exclude
    // TODO: implements basename
    // TODO: implements dirname
    // TODO: implements splittext
    // TODO: implements +/+
    // TODO: implements asRelativePath
    // TODO: implements asAbsolutePath
    // TODO: implements systemCmd
    // TODO: implements gethostbyname
    // TODO: implements getenv
    // TODO: implements setenv
    // TODO: implements unsetenv
    // TODO: implements codegen_UGenCtorArg
    // TODO: implements ugenCodeString
    // TODO: implements asSecs
    // TODO: implements speak
    // TODO: implements toLower
    // TODO: implements toUpper
    // TODO: implements mkdir
    // TODO: implements parseYAML
    // TODO: implements parseYAMLFile
  });
});