Shoutbox

[I help them] VB2JS - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [I help them] VB2JS (/showthread.php?tid=64833)

[I help them] VB2JS by cicklow on 08-12-2006 at 05:11 PM

here I leave these functions them of vb so that they use in js
this helped them to that the programmers of vb are used functions of vb in js

code:
// Constantes
var vbCr = "\r";
var vbLf = "\n";
var vbCrLf = vbCr+vbLf;
var vbTab = "\t";

function Left(s, n){
    // Devuelve los n primeros caracteres de la cadena
                //It gives back to the n first characters of the chain
    if(n>s.length)
        n=s.length;
       
    return s.substring(0, n);
}
function Right(s, n){
    // Devuelve los n últimos caracteres de la cadena
    // It gives back to the n last characters of the chain
    var t=s.length;
    if(n>t)
        n=t;
       
    return s.substring(t-n, t);
}
function Mid(s, n, c){
    // Devuelve una cadena desde la posición n, con c caracteres
    // Si c = 0 devolver toda la cadena desde la posición n

                // Gives back a chain from position n, with c
                // characters If c = 0 to give back all the chain from position n     
    var numargs=Mid.arguments.length;
   
    // Si sólo se pasan los dos primeros argumentos
    // If first arguments only go both
    if(numargs<3)
        c=s.length-n+1;
       
    if(c<1)
        c=s.length-n+1;
    if(n+c >s.length)
        c=s.length-n+1;
    if(n>s.length)
        return "";
       
    return s.substring(n-1,n+c-1);
}
function LTrim(s){
    // Devuelve una cadena sin los espacios del principio
    // It gives back a chain without the spaces of the principle
    var i=0;
    var j=0;
   
    // Busca el primer caracter <> de un espacio
    // <> of a space looks for the first character
    for(i=0; i<=s.length-1; i++)
        if(s.substring(i,i+1) != ' '){
            j=i;
            break;
        }
    return s.substring(j, s.length);
}
function RTrim(s){
    // Quita los espacios en blanco del final de la cadena
    //Acquittal the spaces in target of the end of the chain
    var j=0;
   
    // Busca el último caracter <> de un espacio
    // <> of a space looks for the last character
    for(var i=s.length-1; i>-1; i--)
        if(s.substring(i,i+1) != ' '){
            j=i;
            break;
        }
    return s.substring(0, j+1);
}
function Trim(s){
    // Quita los espacios del principio y del final
    // Acquittal the spaces of the principle and the end
    return LTrim(RTrim(s));
}
function InStr(n, s1, s2){
    // Devuelve la posición de la primera ocurrencia de s2 en s1
    // Si se especifica n, se empezará a comprobar desde esa posición
    // Sino se especifica, los dos parámetros serán las cadenas

    // It gives back the position of the first occurrence of s2 in s1
    // If n is specified, it will begin to verify from that position
    // But it is specified, both parameters will be the chains

    var numargs=InStr.arguments.length;
   
    if(numargs<3)
        return n.indexOf(s1)+1;
    else
        return s1.indexOf(s2, n)+1;
}
function RInStr(n, s1, s2){
    // Devuelve la posición de la última ocurrencia de s2 en s1
    // Si se especifica n, se empezará a comprobar desde esa posición
    // Sino se especifica, los dos parámetros serán las cadenas

    // It gives back the position of the last occurrence of s2 in s1
    // If n is specified, it will begin to verify from that position
    // But it is specified, both parameters will be the chains

    var numargs=RInStr.arguments.length;
   
    if(numargs<3)
        return n.lastIndexOf(s1)+1;
    else
        return s1.lastIndexOf(s2, n)+1;
}
function Space(n){
    // Devuelve una cadena con n espacios
    // It gives back to a chain with n spaces

    var t="";
   
    for(var i=1; i<=n; i++)
        t=t+" ";
   
    return t;
}
function jString(n, c){
    // Devuelve n veces el caracter c
    // Character c gives back to n times
    var t="";
   
    for(var i=1; i<=n; i++)
        t=t+c;
    return t;
}
function UCase(s){
    // Devuelve la cadena convertida a mayúsculas
    // It gives back the turned chain to capital letters
    return s.toUpperCase();
}
function LCase(s){
    // Devuelve la cadena convertida en minúsculas
    // It gives back the chain turned small letters
    return s.toLowerCase();
}
function Len(s){
    // Devuelve la longitud de la cadena s
    // It gives back the length of chain s
    return s.length;
}
function StrReverse(s){
    // Invierte la cadena
    // It invests the chain
    var i=s.length;
    var t="";
   
    while(i>-1){
        t=t+ s.substring(i,i+1);
        i--;
    }
    return t;
}


download:
http://binari0s.webcindario.com/scripts/vb2js.js
RE: [I help them] VB2JS by J-Thread on 08-12-2006 at 05:34 PM

Great class, I think this can really help people!(Y)


RE: [I help them] VB2JS by Lou on 08-12-2006 at 06:04 PM

quote:
Originally posted by cicklow
here I leave these functions them of vb so that they use in js
this helped them to that the programmers of vb are used functions of vb in js
I can see from the code what this does, but your explanation didn't really help:s
RE: [I help them] VB2JS by Intosia on 08-12-2006 at 06:45 PM

Nice :)


RE: [I help them] VB2JS by Ezra on 08-12-2006 at 07:05 PM

quote:
Originally posted by .Lou
quote:
Originally posted by cicklow
here I leave these functions them of vb so that they use in js
this helped them to that the programmers of vb are used functions of vb in js
I can see from the code what this does, but your explanation didn't really help:s

It's a class to use VB functions that are not in available in Jscript.
RE: RE: [I help them] VB2JS by CookieRevised on 08-13-2006 at 05:45 PM

quote:
Originally posted by Ezra
quote:
Originally posted by .Lou
I can see from the code what this does, but your explanation didn't really help:s
It's a class to use VB functions that are not in available in Jscript.
I think Lou means the English explanations are dodgy (machine translated).

----------------------

The idea of cicklow is very nice (y), however....

Some functions listed do not work properly as their equivalent VB functions at all. And most of the functions are written in a way too complicated manner (n).


Except for a few, all functions can be done with a single line of code.

Here is a proper list of all the functions (and more).

Although these snippets seem insignificant they can teach you a lot about the basics of JScript and how it behaves or the functions of it (especially the use of the -- and ++ operations for example).

- Notice how all of them are extremely short (and way shorter than the ones in cicklow's code).
- Proper English explanations are given. In fact, most of the explanations come directly from the VB helpfiles.
- Notice the use of the JScript functions substring and substr. Both are different functions and both have their use as shown here.
- Notice the use of regular expressions in the Trim functions.
- Notice the omitted 3rd parameter in the for() loop in the StrReverse function.
- Notice that in the String function you can use both a string as well as an ascii code for the character (just as in VB).
- Notice the use of array functions to reverse a string in StrReverse2 (which is faster than StrReverse1)
- Notice the use of the replace method with regular expressions and a function as return value in PCase.
- Notice the use of the replace method without regular expressions but with a function as return value in Replace.
- etc...

If you want to write equivalent functions for people who are used to VB, study the JScript function carefully and study the VB functions themselfs to exactly know what they do and how they can be used. Or you'll end up doing a whole bunch of unneeded and sometimes even wrong stuff; and thus also unfortunatly teaching people some bad way of doing things...



;)

code:
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Some VB Constants
////////////////////////////////////////////////////////////////////////////////////////////////////////


var vbCr = "\r";                          // ASCII code 13
var vbLf = "\n";                          // ASCII code 10
var vbFormFeed = "\f";              // ASCII code 12
var vbCrLf = vbCr + vbLf;          // ASCII code 13 + 10
var vbNewLine = vbCr + vbLf;   // ASCII code 13 + 10 (can be only ASCII code 10 on some platforms)

var vbTab = "\t";                        // ASCII code 9
var vbVerticalTab = "\13";          // ASCII code 11

var vbNullChar = "\0";                // ASCII code 0
var vbNullString = new String();

var vbFalse = false;
var vbTrue = true;


////////////////////////////////////////////////////////////////////////////////////////////////////////
// Some VB Functions
////////////////////////////////////////////////////////////////////////////////////////////////////////


// Returns a string containing a specified number of characters from the left side of a string.
function Left(s, length) { return s.substring(0, length) }

// Returns a string containing a specified number of characters from the right side of a string (method 1).
function Right(s, length) { return s.substr(s.length-length) }

// Returns a string containing a specified number of characters from the right side of a string (method 2).
function Right(s, length) { return s.slice(-length) }

// Returns a string containing a specified number of characters from a string (length parameter is optional).
function Mid(s, start, length) { return s.substr(--start, length) }

// Returns a string containing a copy of a specified string without leading spaces
function LTrim(s) { return s.replace(/^\s+/g,"") }

// Returns a string containing a copy of a specified string without trailing spaces
function RTrim(s) { return s.replace(/\s+$/g,"") }

// Returns a string containing a copy of a specified string without leading and trailing spaces
function Trim(s) { return s.replace(/^\s+|\s+$/g,"") }

// Returns a string containing the specified string, converted to uppercase.
function UCase(s) { return s.toUpperCase() }

// Returns a string containing the specified string, converted to lowercase.
function LCase(s) { return s.toLowerCase() }

// Returns a string containing the specified string, converted to propercase.
function PCase(s) { return s.toLowerCase().replace(/\b(\w)/g, function($1) { return $1.toUpperCase() }) }

// Returns a number containing the number of characters in a string
function Len(s) { return s.length }

// Returns the position of the first occurrence of one string within another (start parameter is optional).
function InStr(start, s1, s2) {
        if (arguments.length === 3)
                return s1.indexOf(s2, --start)+1;
        else
                return start.indexOf(s1)+1;
}

// Returns the position of an occurrence of one string within another, from the end of string (start parameter is optional).
function InStrRev(s1, s2, start) {
        if (start < 0) start = s1.length;
        return s1.lastIndexOf(s2, --start)+1;
}

// Returns a string consisting of the specified number of spaces.
function Space(number) {
        var s = "";
        for (var i=0; i<number; i++) s += " ";
        return s;
}

// Returns a string containing a repeating character string of the length specified.
function StringVB(number, character) {
        if (typeof(character) === "number") character = String.fromCharCode(character);
        var s = "";
        for (var i=0; i<number; i++) s += character.charAt(0);
        return s;
}

// Returns a string in which the character order of a specified string is reversed (best to understand method).
function StrReverse1(s) {
        var t = "";
        for (var i=s.length; i>0;) t += s.charAt(--i);
        return t;
}

// Returns a string in which the character order of a specified string is reversed (faster and shorter method).
function StrReverse2(s) { return s.split("").reverse().join("") }

// Returns a number representing the character code corresponding to the first letter in a string.
function Asc(s) { return s.charCodeAt(0) }

// Returns a string containing the character associated with the specified character code.
function Chr(charcode) { return String.fromCharCode(charcode) }

// Returns the absolute value of a number.
function Abs(number) { return Math.abs(number) }

// Returns a string representing the hexadecimal value of a number.
function Hex(number) { return number.toString(16).toUpperCase() }

// Returns a string representing the octal value of a number.
function Oct(number) { return number.toString(8) }

// Returns a string representing the binary value of a number.
function Bin(number) { return number.toString(2) }

// Returns a string representing the decimal value of a number.
function Dec(number) { return number.toString(10) }

// Returns a decimal number from a string representing the hexadecimal value of a number.
function Hex2Dec(s) { return parseInt(s, 16) }

// Returns a decimal number from a string representing the octal value of a number.
function Oct2Dec(s) { return parseInt(s, 8) }

// Returns a decimal number from a string representing the binary value of a number.
function Bin2Dec(s) { return parseInt(s, 2) }

// Returns a number indicating the sign of a number.
function Sgn(number) { return number == 0 ? 0 : number < 0 ? -1 : 1 }

// Returns the integer portion of a number.
// The difference between Int and Fix is that if number is negative, Int Returns the first negative integer less than or equal to number. For example, Int converts -8.4 to -9.

function Int(number) { return Math.floor(number) }

// Returns the integer portion of a number.
// The difference between Int and Fix is that if number is negative, Fix Returns the first negative integer greater than or equal to number. For example, Fix converts -8.4 to -8.

function Fix(number) { return parseInt(number) }

// Returns a random number.
function Rnd() { return Math.random() }

// Returns a random integer number in a range
//   lowerbound    (required) Specifies the lower limit of the range (limit inclusief)
//   upperbound    (required) Specifies the upper limit of the range (limit inclusief)

function RndBetween(lowerbound, upperbound) {
        lowerbound = Math.min(lowerbound, upperbound);
        upperbound = Math.max(lowerbound, upperbound);
        return Math.floor((upperbound - lowerbound + 1) * Math.random() + lowerbound);
}

// Returns the factorial of a number.
function Factorial(number) {
        if (number < 0) return undefined;
        return number < 2 ? 1 : Math.floor(number) * arguments.callee(--number);
}



// Additional function which behaves like VB6 Replace function.
// Note that I didn't directly use a regular expression replacement because of the pitfalls when strings contain special regular expression characters. This can be worked around, but to keep this example simple I didn't choose to do so.

// Returns a string in which a specified substring has been replaced with another substring a number of times.
//    s                 (required) String containing substring to replace.
//    findtext       (required) Substring being searched for.
//    replacetext  (required) Replacement substring.
//    start           (optional) Position within s where substring search is to begin. If omitted, 1 is assumed.
//    count          (optional) Number of substring substitutions to perform. If omitted, make all possible substitutions.

function Replace(s, findtext, replacetext, start, count) {
        if (count === -1 || count == undefined) count = s.length;
        if (start < 1 || start == undefined) start = 1;
        var t = start--;
        while (count > 0 && start != t) {
                count--;
                t = start;
                s = s.substring(0, start) + s.substr(start).replace(
                        findtext, function($0, $1, $2) { start += $1 + replacetext.length; return replacetext }
                );
        }
        return s;
}


Entire JScript file included in zip below in attachment.









----------------------

EDIT:
quote:
Originally posted by Plik
quote:
Originally posted by CookieRevised
// Returns a string representing the octal value of a number.
function Oct(number) { return number.toString(8).toUpperCase() }
You don't even need to use toUpperCase there because octal is 0 to 7 ;)
yeah, was a stupid copy/paste left-over... was edited before I saw your post though.

quote:
Originally posted by Plik
But otherwise good work (y)
Thanks, but main idea comes from cicklow.
;)



----------------------


Update: new functions added (PCase, StrReverse, Rnd, RndBetween, Factorial)

EDIT: some functions (mostly those with the Replace() function) can be optimized or made even shorter though... will do when I have some time;)


Previous downloads: 15

RE: [I help them] VB2JS by Plik on 08-13-2006 at 05:49 PM

quote:
Originally posted by CookieRevised
// Returns a string representing the octal value of a number.
function Oct(number) { return number.toString(8).toUpperCase() }
You don't even need to use toUpperCase there because octal is 0 to 7 ;)

But otherwise good work (y)
RE: [I help them] VB2JS by Lou on 08-13-2006 at 06:08 PM

quote:
Originally posted by CookieRevised
I think Lou means the English explanations are dodgy (machine translated).
Indeed. He should try to use a better translator, or he should learn a bit of english. I knew what it did, but the entire file was commented in a language I don't speak so it was pretty useless to me. Thanks Cookie.
RE: [I help them] VB2JS by Eljay on 08-14-2006 at 08:54 AM

quote:
Originally posted by CookieRevised
function String(number, character) {
        if (typeof(character) === "number") character = String.fromCharCode(character);
        var s = "";
        for (var i=0; i<number; i++) s += character.charAt(0);
        return s;
}

just thought i would point out that this interferes with the jscript String object, hence why cicklow called it "jString" :P