What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [I help them] VB2JS

[I help them] VB2JS
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: RE: [I help them] VB2JS
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

.zip File Attachment: VB2JS_FIXED.zip (2.6 KB)
This file has been downloaded 291 time(s).

This post was edited on 07-05-2007 at 11:08 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
08-13-2006 05:45 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
[I help them] VB2JS - by cicklow on 08-12-2006 at 05:11 PM
RE: [I help them] VB2JS - by J-Thread on 08-12-2006 at 05:34 PM
RE: [I help them] VB2JS - by Lou on 08-12-2006 at 06:04 PM
RE: [I help them] VB2JS - by Intosia on 08-12-2006 at 06:45 PM
RE: [I help them] VB2JS - by Ezra on 08-12-2006 at 07:05 PM
RE: RE: [I help them] VB2JS - by CookieRevised on 08-13-2006 at 05:45 PM
RE: [I help them] VB2JS - by Plik on 08-13-2006 at 05:49 PM
RE: [I help them] VB2JS - by Lou on 08-13-2006 at 06:08 PM
RE: [I help them] VB2JS - by Eljay on 08-14-2006 at 08:54 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On