Changeset 44

Show
Ignore:
Timestamp:
03/02/06 17:49:37 (3 years ago)
Author:
Jan-Klaas Kollhof
Message:

added strings module for string manipulation e.g. templates with runable JS inside; added repr() to jsolait's core, and some LZW (de)compression code to codecs.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/jsolait/jsolait.js

    r43 r44  
    2121/** 
    2222    The main jsolait script. 
    23     It provides the core functionalities for creating classes, modules and for importing modules. 
     23    It provides the core functionalities for creating classes, modules and for importing modules. 
    2424 
    2525    @author Jan-Klaas Kollhof 
     
    227227/** 
    228228    Returns a string representation of an object. 
    229     The difference to  
    230229    @param obj  The object to return a string repr. of. 
    231230    @return A string repr. the object. 
    232231**/ 
    233 str = function(obj){ 
    234     return "" + obj; 
     232str = String; 
     233 
     234/** 
     235    Return a String containing a printable representation of an object which can be used with eval() to create an equal object. 
     236    Objects can cosutumize the the value being returned by repr(obj) by providing a obj.__repr__() method which is called by repr(obj). 
     237    @param obj  The object to create a repr. String from. 
     238    @return A representation of the object. 
     239**/ 
     240repr = function(obj){ 
     241    if(obj == null){ 
     242        return null; 
     243    }else if(obj.__repr__){ 
     244        return obj.__repr__(); 
     245    }else{ 
     246        switch(typeof obj){ 
     247            case "string": 
     248                obj = obj.replace(/\\/g,"\\\\").replace(/\"/g,"\\\"").replace(/\n/g, "\\n").replace(/\r/g,"\\r"); 
     249                return '"' + obj + '"'; 
     250            case "boolean":case"number": 
     251                return "" + obj; 
     252            case "object": 
     253                var out = []; 
     254                if(obj == null){ 
     255                    return "null"; 
     256                }else if(obj instanceof Array){ 
     257                    for(var i=0;i<obj.length;i++){ 
     258                        out.push(repr(obj[i])); 
     259                    } 
     260                    return "[" + out.join(",") + "]"; 
     261                }else if(obj instanceof Object){ 
     262                    for(var key in obj){ 
     263                        out.push(repr(key) + ":" + repr(obj[key])); 
     264                    } 
     265                    return "{" + out.join(",") + "}"; 
     266                } 
     267        } 
     268    } 
    235269}; 
    236270 
     
    546580    */ 
    547581    mod.loadURI=function(uri, headers) { 
    548         //if callback is defined then the operation is done async 
    549582        headers = (headers !== undefined) ? headers : []; 
    550         //setup the request 
    551583        try{ 
    552             var xmlhttp= getHTTP(); 
     584            var xmlhttp = getHTTP(); 
    553585            xmlhttp.open("GET", uri, false); 
    554586            for(var i=0;i< headers.length;i++){ 
     
    961993        return a.join(this); 
    962994    }; 
    963  
    964995}); 
    965996 
  • trunk/jsolait/lib/codecs.js

    r29 r44  
    2929*/ 
    3030Module("codecs", "$Revision$", function(mod){ 
     31 
    3132    /** 
    3233        Returns all all available encoders. 
     
    170171    }; 
    171172 
    172     mod.__main__=function(){ 
    173     }; 
     173 
     174    String.prototype.encode_lzw=function(){ 
     175        var dict = {}; 
     176        var data = (this + "").split(""); 
     177        var out=[]; 
     178        var currChar; 
     179        var phrase = data[0]; 
     180        var code = 256; 
     181         
     182        for(var i=1;i<data.length;i++){ 
     183            currChar = data[i]; 
     184            if(dict[phrase + currChar] != null){ 
     185                phrase += currChar; 
     186            }else{ 
     187                out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 
     188                dict[phrase + currChar] = code; 
     189                code++; 
     190                phrase=currChar; 
     191            } 
     192        } 
     193        out.push( phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 
     194        for(var i=0;i<out.length;i++){ 
     195 
     196            out[i] = String.fromCharCode(out[i]); 
     197        } 
     198        return out.join(""); 
     199    }; 
     200 
     201    String.prototype.decode_lzw=function(){ 
     202        var dict={}; 
     203        var data = (this + "").split(""); 
     204        var currChar = data[0]; 
     205        var oldPhrase = currChar; 
     206        var out = [currChar]; 
     207        var code = 256; 
     208        var phrase; 
     209        for(var i=1;i<data.length;i++){ 
     210            var currCode = data[i].charCodeAt(0); 
     211            if(currCode < 256){ 
     212                phrase = data[i]; 
     213            }else{ 
     214                phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar); 
     215            } 
     216            out.push(phrase); 
     217            currChar = phrase.charAt(0); 
     218            dict[code] = oldPhrase + currChar; 
     219            code ++; 
     220            oldPhrase = phrase; 
     221        } 
     222        return out.join(""); 
     223    }; 
     224  
    174225}); 
  • trunk/jsolait/lib/jsonrpc.js

    r41 r44  
    11/* 
    2   Copyright (c) 2005 Jan-Klaas Kollhof 
     2  Copyright (c) 2005-2006 Jan-Klaas Kollhof 
    33 
    44  This file is part of the JavaScript o lait library(jsolait). 
     
    2525    @lastchangedby       $LastChangedBy$ 
    2626    @lastchangeddate    $Date$ 
    27 *
     27**
    2828Module("jsonrpc","$Revision$", function(mod){ 
    2929    var urllib = imprt("urllib"); 
     
    7575    }); 
    7676 
    77  
     77     
    7878    /** 
    7979        Marshalls an object to JSON.(Converts an object into JSON conforming source.) 
     
    413413    }); 
    414414     
    415      
    416415    mod.RPCMethod=Class(function(publ,supr){ 
    417416        publ.__init__=function(name,proxy){ 
     
    442441            this._pendingRequests={}; 
    443442        }; 
    444          
    445443         
    446444        /** 
     
    554552    }); 
    555553     
    556      
    557      
     554        
    558555    /** 
    559556        Converts a String to JSON. 
  • trunk/jsolait/lib/net/sockets.js

    r42 r44  
    115115    }; 
    116116 
     117    mod.isReady = function(){ 
     118        return flashSocketProvider != null; 
     119    }; 
    117120}); 
  • trunk/jsolait/lib/operators.js

    r29 r44  
    1919*/ 
    2020 
     21/** 
     22    Module providing functions implementing operators. 
     23     
     24    @creator Jan-Klaas Kollhof 
     25    @created 2005-12-30 
     26    @lastchangedby       $LastChangedBy: Jan-Klaas Kollhof $ 
     27    @lastchangeddate    $Date: 2006-01-30 20:52:35 +0000 (Mon, 30 Jan 2006) $ 
     28**/ 
    2129Module("operators", "$Revision: 20 $", function(mod){ 
    2230