Companion Wulf
Lone Wolf
- Xy$
- -0.20
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).
Example: 'hello!'.capitalizeFirstLetter();
Output: Hello!
Example: 'hello world!'.capitalizeEachWord();
Output: Hello World!
Example: 'Hello world!'.capitalizeAllLetters();
Output: HELLO WORLD!
Example: 'Write this backwards'.toReverseText();
Output: sdrawkcb sith etirW
Example: 'Write backwards words'.toReverseWords();
Output: words backwards Write
Example: 347.559.toDecimal();
Output: 347.56
decimals is the number of decimal places to round up.
Example: 646.596553.toRoundUp(3);
Output: 646.597
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();
};
Output: Hello!
JavaScript:
String.prototype.capitalizeEachWord = function() {
return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};
Output: Hello World!
JavaScript:
String.prototype.capitalizeAllLetters = function() {
return this.toUpperCase();
};
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(" ");
};
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.
Example: 'Convert this to Leet'.toLeet();
Output: C0Nv3R7 7H15 70 L337
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)});
};
Output: C0Nv3R7 7H15 70 L337
JavaScript:
Number.prototype.toDecimal = function() {
return Number(this.toFixed(2));
};
Output: 347.56
JavaScript:
Number.prototype.toRoundUp = function(decimals) {
return parseFloat(this.toFixed(decimals));
};
Example: 646.596553.toRoundUp(3);
Output: 646.597
Last edited: