Indie Dev

Hello Guest!. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, sell your games, upload content, as well as connect with other members through your own private inbox!

Useful (Missing?) Javascript Functions

While looking through various MV functions, notably String and Number, I noticed a distinct lack of them. So I created some of my own (some of these I already use in Otherworld) and thought I'd share them. And it took me AGES to figure out how to do some of them! No doubt there are better methods for doing these too; they are presented "as is" and they work.

Some are simple, some are well-known, and some aren't. I put them together (mainly for personal use) in a single plugin, but someone may benefit from any or all of these, easily implemented in your code.

If you have your own "one liner" functions that you'd like to share, I'll add them here as well (with credit).

JavaScript:
String.prototype.capitalizeFirstLetter = function() {
    return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
};
Example: 'hello!'.capitalizeFirstLetter();
Output: Hello!

JavaScript:
String.prototype.capitalizeEachWord = function() {
    return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};
Example: 'hello world!'.capitalizeEachWord();
Output: Hello World!

JavaScript:
String.prototype.capitalizeAllLetters = function() {
     return this.toUpperCase();
};
Example: 'Hello world!'.capitalizeAllLetters();
Output: HELLO WORLD!

JavaScript:
String.prototype.toReverseText = function() {
     return this.split('').reverse().join('');
};

Example: 'Write this backwards'.toReverseText();
Output: sdrawkcb sith etirW

JavaScript:
String.prototype.toReverseWords = function() {
     return this.split(" ").reverse().join(" ");
};
Example: 'Write backwards words'.toReverseWords();
Output: words backwards Write

I couldn't quite figure this out, but wanted to use this for a certain scene in Otherworld, so credit for this goes to Javascript One-Liners.

JavaScript:
String.prototype.toLeet = function() {
     // Credit: Javascript One Liners
     return this.replace(/[a-z]/g,function f(a){return "4BCD3F6H1JKLMN0PQR57"[parseInt(a, 36)-10] || a.replace(/[a-t]/gi,f)});
};
Example: 'Convert this to Leet'.toLeet();
Output: C0Nv3R7 7H15 70 L337

JavaScript:
Number.prototype.toDecimal = function() {
     return Number(this.toFixed(2));
};
Example: 347.559.toDecimal();
Output: 347.56

JavaScript:
Number.prototype.toRoundUp = function(decimals) {
     return parseFloat(this.toFixed(decimals));
};
decimals is the number of decimal places to round up.

Example: 646.596553.toRoundUp(3);
Output: 646.597
 
Last edited:

LTN Games

Master Mind
Resource Team
Xy$
0.01
These are great, I don't know why I never thought of these one liners. I currently have no use for them but will definitely in the near future. Great idea for a thread by the way, I'm probably going to spend an hour or so later today thinking of some one liners :)
 
I thought so too. The only ones I have a use for (and what probably triggered this) in Otherworld are: the Capitalize First Letter (for certain plugin parameters and capitalizing certain things in my Bio Status Add-On), the Reverse Text (for reading in a mirror) and the L33T one (a secret).
 

Chaucer

Towns Guard
Xy$
0.00
Just a headsup you might want to modify your toDecimal code, using .toFixed() changes your number to a string. this could be problematic for certain things, for example.

JavaScript:
var a = 2.012945;
a.toDecimal() + 5;
//the result of this would be "2.015" as where it should be 7.01.
//instead of just using this.toFixed(2); use Number(this.toFixed(2)); and it'll make sure it returns as a number, if it was fed a number.
 
@Chaucer Why thank you! I don't know how I completely overlooked that?! It's been corrected here. (I also checked the plugin I have to make sure it's not the same and...ahem! It's Number(this.toFixed(2))!)(blush)
 
Top