home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 October / tst.iso / multimed / IDN / web / tapdogs / JS / WRAP.JS < prev   
Encoding:
JavaScript  |  1999-09-24  |  830 b   |  39 lines

  1. // Lemon (HK) Ltd
  2. // last modified on 30 July 1999
  3.  
  4. function wrap_text(buf,max){
  5.   var buf_ = new Array();
  6.   var len=buf.length;
  7.   var pos=space=i=j=0;
  8.   var h;
  9.   var rslt = "";
  10.   if (!max) max = 60;
  11.  
  12.   while (i < len) {
  13.     ch = buf.charAt(i);
  14.  
  15.     if (ch == '\r' && buf.charAt(i+1) == '\n') {
  16.       buf_[j++] = buf.substring(pos, i);
  17.       pos = space = (i+=2);
  18.       continue;
  19.     }
  20.     if (ch == '\n' || ch == '\r') {
  21.       buf_[j++] = buf.substring(pos, i);
  22.       pos = space = ++i;
  23.       continue;
  24.     }
  25.  
  26.     if (ch == ' ') space = i;
  27.  
  28.     if (i - pos >= max && space != pos) {
  29.       buf_[j++] = buf.substring(pos, space);
  30.       i = space;
  31.       pos = space+1;
  32.     }
  33.     i++;
  34.   }
  35.   for (j=0; j<buf_.length; j++) rslt += buf_[j] + "\n";
  36.   rslt += buf.substring(pos, i);
  37.   return rslt;
  38. }
  39.