[Plugin] samp.js - JavaScript for SA-MP
#64

Quote:
Originally Posted by HardWar
Посмотреть сообщение
and how to escape the string for use in SQLite functions? in pawn I use format and %q
You'll be interested in:
Код:
var mysql_escape_string = function(str) {
    return str.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, function (char) {
        switch (char) {
            case "\0":
                return "\\0";
            case "\x08":
                return "\\b";
            case "\x09":
                return "\\t";
            case "\x1a":
                return "\\z";
            case "\n":
                return "\\n";
            case "\r":
                return "\\r";
            case "\"":
            case "'":
            case "\\":
            case "%":
                return "\\"+char; // prepends a backslash to backslash, percent,
                                  // and double/single quotes
        }
    });
}

String.format = function(str, arr) {
    var i = -1;
	var callback;
	
    callback = function(exp, p0, p1, p2, p3, p4) {
        if(exp=='%%') {
			return '%';
		}
		
        if(arr[++i] === undefined) {
			return undefined;
		}
		
        var exp  = p2 ? parseInt(p2.substr(1)) : undefined;
        var base = p3 ? parseInt(p3.substr(1)) : undefined;
        var val;
		
        switch(p4) {
            case 's': val = arr[i]; break;
            case 'c': val = arr[i][0]; break;
	    case 'i': val = arr[i].toString(); break;
	    case 'd': val = arr[i].toString(); break;
            case 'f': val = parseFloat(arr[i]).toFixed(exp); break;
            case 'p': val = parseFloat(arr[i]).toPrecision(exp); break;
            case 'e': val = parseFloat(arr[i]).toExponential(exp); break;
            case 'x': val = parseInt(arr[i]).toString(base?base:16); break;
            case 'd': val = parseFloat(parseInt(arr[i], base?base:10).toPrecision(exp)).toFixed(0); break;
	    case 'q': val = mysql_escape_string(arr[i]); break;
        }
		
        val = (typeof(val) == 'object') ? JSON.stringify(val) : val.toString(base);
		
        var sz = parseInt(p1); /* padding size */
        var ch = p1 && p1[0]=='0' ? '0' : ' '; /* isnull? */
		
        while(val.length<sz) {
			val = p0 !== undefined ? val+ch : ch+val; /* isminus? */
		}
		return val;
    }
	
    var regex = /%(-)?(0?[0-9]+)?([.][0-9]+)?([#][0-9]+)?([scfpexd])/g;
    return str.replace(regex, callback);
}

String.prototype.format = function() {
    return String.format(this, Array.prototype.slice.call(arguments));
}
works similar to SAMP's format function. [https://sampwiki.blast.hk/wiki/Format]

Example:
Код:
const COLOR = {
	WHITE 	: 0xFFFFFFFF,
	TEAL	: 0x008080FF
};

SendClientMessage(player.id, COLOR.WHITE, ("Welcome to {%06x}%s{%06x}, {%06x}%s{%06x}!").format((COLOR.TEAL >>> 8), ServerData.name, (COLOR.WHITE >>> 8), (COLOR.TEAL >>> 8), player.name, (COLOR.WHITE >>> 8)));
^ copy&paste from my code.
Reply


Messages In This Thread
samp.js - JavaScript for SA-MP - v0.1.9.4 - by !damo!spiderman - 02.07.2015, 05:38
Re: samp.js - JavaScript for SA-MP - by RuNBoY - 02.07.2015, 05:43
Re: samp.js - JavaScript for SA-MP - by ev0lution - 02.07.2015, 05:45
Re: samp.js - JavaScript for SA-MP - by Ritzy2K - 02.07.2015, 09:33
AW: samp.js - JavaScript for SA-MP - by FSAOskar - 02.07.2015, 11:54
Re: AW: samp.js - JavaScript for SA-MP - by !damo!spiderman - 02.07.2015, 12:20
Re: samp.js - JavaScript for SA-MP - by theYiin - 02.07.2015, 12:30
Re: samp.js - JavaScript for SA-MP - by DRIFT_HUNTER - 02.07.2015, 12:34
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 02.07.2015, 12:56
Re: samp.js - JavaScript for SA-MP - by DRIFT_HUNTER - 02.07.2015, 13:17
Re: samp.js - JavaScript for SA-MP - by ev0lution - 03.07.2015, 05:50
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 03.07.2015, 14:54
Respuesta: samp.js - JavaScript for SA-MP - by [DOG]irinel1996 - 03.07.2015, 16:01
Re: samp.js - JavaScript for SA-MP - by BonhommeG - 03.07.2015, 16:23
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 03.07.2015, 16:43
Re: samp.js - JavaScript for SA-MP - by ev0lution - 04.07.2015, 00:56
Re: samp.js - JavaScript for SA-MP - by ev0lution - 04.07.2015, 08:43
Re: samp.js - JavaScript for SA-MP - by Crayder - 04.07.2015, 08:54
Re: samp.js - JavaScript for SA-MP - by BonhommeG - 04.07.2015, 12:06
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 04.07.2015, 12:10
Re: samp.js - JavaScript for SA-MP - by BonhommeG - 04.07.2015, 12:38
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 04.07.2015, 12:45
Re: samp.js - JavaScript for SA-MP - by GWMPT - 04.07.2015, 12:46
Re: samp.js - JavaScript for SA-MP - by BonhommeG - 04.07.2015, 13:02
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 04.07.2015, 13:06
Re: samp.js - JavaScript for SA-MP - by prineside - 05.07.2015, 10:38
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 05.07.2015, 14:13
Re: samp.js - JavaScript for SA-MP - by Kapersky™ - 05.07.2015, 15:02
Re: samp.js - JavaScript for SA-MP - by PeterJane - 05.07.2015, 19:47
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 05.07.2015, 23:37
Re: samp.js - JavaScript for SA-MP - by PeterJane - 06.07.2015, 13:15
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 11.07.2015, 04:37
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 12.07.2015, 15:08
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 13.07.2015, 05:28
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 15.07.2015, 02:19
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 16.07.2015, 10:18
Re: samp.js - JavaScript for SA-MP - by KayJ - 16.07.2015, 13:05
Re: samp.js - JavaScript for SA-MP - by iFrame - 16.07.2015, 14:41
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 20.07.2015, 15:19
Re: samp.js - JavaScript for SA-MP - by Laronic - 22.07.2015, 23:18
Re: samp.js - JavaScript for SA-MP - by Crayder - 22.07.2015, 23:38
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 23.07.2015, 01:27
Re: samp.js - JavaScript for SA-MP - by benel1 - 26.07.2015, 14:19
Re: samp.js - JavaScript for SA-MP - by BlackBank - 26.07.2015, 17:03
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 26.07.2015, 18:24
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 26.07.2015, 18:26
Re: samp.js - JavaScript for SA-MP - by Laronic - 04.08.2015, 17:25
Re: samp.js - JavaScript for SA-MP - by DRIFT_HUNTER - 05.08.2015, 12:28
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 05.08.2015, 13:27
Re: samp.js - JavaScript for SA-MP - by Lorenc_ - 05.08.2015, 14:13
Re: samp.js - JavaScript for SA-MP - by DRIFT_HUNTER - 05.08.2015, 19:08
Re: samp.js - JavaScript for SA-MP - by Battlezone - 24.08.2015, 19:03
Re: samp.js - JavaScript for SA-MP - by SkittlesAreFalling - 25.08.2015, 05:15
Re: samp.js - JavaScript for SA-MP - by ev0lution - 25.08.2015, 05:18
Re: samp.js - JavaScript for SA-MP - by SkittlesAreFalling - 25.08.2015, 05:21
Re: samp.js - JavaScript for SA-MP - by prineside - 05.09.2015, 21:06
Re: samp.js - JavaScript for SA-MP - by DRIFT_HUNTER - 06.09.2015, 08:59
Re: samp.js - JavaScript for SA-MP - by ChromeAmazing - 06.09.2015, 21:42
Re: samp.js - JavaScript for SA-MP - by SkittlesAreFalling - 12.09.2015, 18:12
Re: samp.js - JavaScript for SA-MP - by j0n - 17.09.2015, 17:12
Re: samp.js - JavaScript for SA-MP - by DRIFT_HUNTER - 17.09.2015, 17:37
Re: samp.js - JavaScript for SA-MP - by HardWar - 17.09.2015, 18:49
Re: samp.js - JavaScript for SA-MP - by Kamper - 17.09.2015, 22:54
Re: samp.js - JavaScript for SA-MP - by SkittlesAreFalling - 18.09.2015, 08:07
Re: samp.js - JavaScript for SA-MP - by Mowgli - 18.09.2015, 08:36
Re: samp.js - JavaScript for SA-MP - by SkittlesAreFalling - 18.09.2015, 10:28
Re: samp.js - JavaScript for SA-MP - by HardWar - 18.09.2015, 16:40
Re: samp.js - JavaScript for SA-MP - by !damo!spiderman - 19.09.2015, 07:59
Re: samp.js - JavaScript for SA-MP - by Kamper - 19.09.2015, 11:38
Re: samp.js - JavaScript for SA-MP - by Omer. - 22.09.2015, 20:45
Re: samp.js - JavaScript for SA-MP - by DRIFT_HUNTER - 23.09.2015, 18:11
Re: samp.js - JavaScript for SA-MP - by Omer. - 23.09.2015, 18:56
Re: samp.js - JavaScript for SA-MP - by SkittlesAreFalling - 29.09.2015, 01:44
Re: samp.js - JavaScript for SA-MP - by SkittlesAreFalling - 04.10.2015, 01:37
Re: samp.js - JavaScript for SA-MP - by Abagail - 04.10.2015, 03:10
Re: samp.js - JavaScript for SA-MP - by SkittlesAreFalling - 04.10.2015, 03:46
Re: samp.js - JavaScript for SA-MP - by Omer. - 27.10.2015, 07:50
Re: samp.js - JavaScript for SA-MP - by SkittlesAreFalling - 28.10.2015, 00:14
Re: samp.js - JavaScript for SA-MP - by Omer. - 28.10.2015, 04:08
Re: samp.js - JavaScript for SA-MP - by Anarkien - 19.06.2016, 08:54
Re: samp.js - JavaScript for SA-MP - by CoaPsyFactor - 20.06.2016, 16:56
Re: samp.js - JavaScript for SA-MP - by Pyrotech - 10.10.2016, 00:05
Re: samp.js - JavaScript for SA-MP - by CoaPsyFactor - 13.10.2016, 13:08
Re: samp.js - JavaScript for SA-MP - by Yaa - 23.11.2016, 12:02
Re: samp.js - JavaScript for SA-MP - by LifeIsGood - 24.11.2016, 07:37
Re: samp.js - JavaScript for SA-MP - by Yaa - 24.11.2016, 12:58
Re: samp.js - JavaScript for SA-MP - by lepegadore - 16.01.2017, 11:45
Re: samp.js - JavaScript for SA-MP - by Omer. - 12.02.2017, 12:02
Re: samp.js - JavaScript for SA-MP - by PhamHoang - 14.07.2017, 06:32
Re: samp.js - JavaScript for SA-MP - by NekoriDes - 02.12.2017, 22:26
Re: samp.js - JavaScript for SA-MP - by Kaperstone - 03.12.2017, 04:57
Re: samp.js - JavaScript for SA-MP - by LifeIsGood - 03.12.2017, 05:06
Re: samp.js - JavaScript for SA-MP - by Kaperstone - 03.12.2017, 05:58

Forum Jump:


Users browsing this thread: 1 Guest(s)