|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>sprintf.js test</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | +<script src="../src/sprintf.js"></script> |
| 9 | +<script> |
| 10 | +echo = function(s) { document.write(s); }; |
| 11 | +echo("<pre>"); |
| 12 | +/**/ |
| 13 | +echo(sprintf("%7.3f<br />", 123.456)); |
| 14 | +echo(sprintf("%7.3f<br />", 12.46000)); |
| 15 | +echo(sprintf("%7.3f<br />", 3.4)); |
| 16 | +var n = 43951789; |
| 17 | +var u = -43951789; |
| 18 | +var c = 65; |
| 19 | +var d = 123.45678901234567890123456789; |
| 20 | +var start = (new Date()).getTime(); |
| 21 | +echo(sprintf("%%b = '%b'<br />", n)); // binary representation |
| 22 | +echo(sprintf("%%c = '%c'<br />", c)); // print the ascii character |
| 23 | +echo(sprintf("%%d = '%+d'<br />", n)); // standard integer representation |
| 24 | +echo(sprintf("%%d = '%d'<br />", u)); // standard integer representation |
| 25 | +echo(sprintf("%%e = '%.10e'<br />", d)); // scientific notation |
| 26 | +echo(sprintf("%%u = '%u'<br />", n)); // unsigned integer representation of a positive integer |
| 27 | +echo(sprintf("%%u = '%u'<br />", u)); // unsigned integer representation of a negative integer |
| 28 | +echo(sprintf("%%f = '%'-10.2f' and %%f = '%010.10f'<br />", d, d)); // floating point representation |
| 29 | +echo(sprintf("%%o = '%o'<br />", n)); // octal representation |
| 30 | +echo(sprintf("%%s = '%100.10s'<br />", "Ala-bala-portocala")); // string representation |
| 31 | +echo(sprintf("%%x = '%x'<br />", n)); // hexadecimal representation (lower-case) |
| 32 | +echo(sprintf("%%X = '%X'<br />", n)); // hexadecimal representation (upper-case) |
| 33 | +echo(sprintf("<br />%4$s, %3$s, %1$s, %2$s", 'c', 'd', 'b', 'a')); |
| 34 | +echo('<br />'+ ((new Date()).getTime() - start) / 1000 +"<br />"); |
| 35 | + |
| 36 | +// echo(sprintf('%q', 1)); // don't know what this is |
| 37 | + |
| 38 | +function myassert(fact, message) { |
| 39 | +if(!fact) { |
| 40 | +echo("Assertion failed: "+ message +"<br />"); |
| 41 | +} |
| 42 | +} |
| 43 | + |
| 44 | +function assert_equal(a, b, msg) { |
| 45 | +myassert(a == b, '("' + a + '" != "' + b + '") ' + msg); |
| 46 | +} |
| 47 | + |
| 48 | +function test() { |
| 49 | +assert_equal(sprintf('foo%1dbar', 3), 'foo3bar', 'sprintf1'); |
| 50 | +assert_equal(sprintf('foo%02dbar', 3), 'foo03bar', 'sprintf1'); |
| 51 | +assert_equal(sprintf('foo%02dbar', 42), 'foo42bar', 'sprintf1'); |
| 52 | +assert_equal(sprintf('foo%dbar', 123), 'foo123bar', 'sprintf1'); |
| 53 | +a = sprintf('%2s', 'a'); |
| 54 | +b = ' a'; |
| 55 | +assert_equal(a.length, b.length, 'sprintf2.length'); |
| 56 | +assert_equal(a, b, 'sprintf2'); |
| 57 | +a = sprintf('%2s', 'aa'); |
| 58 | +b = 'aa'; |
| 59 | +assert_equal(a.length, b.length, 'sprintf3.length'); |
| 60 | +assert_equal(a, b, 'sprintf3'); |
| 61 | +a = sprintf('%4s', 'a'); |
| 62 | +b = ' a'; |
| 63 | +assert_equal(a.length, b.length, 'sprintf4.length'); |
| 64 | +assert_equal(a, b, 'sprintf4'); |
| 65 | +} |
| 66 | + |
| 67 | +test(); |
| 68 | + |
| 69 | +echo(sprintf("%%+d = %+d<br />", 0)); |
| 70 | +echo(sprintf("%%07d = %07d<br />", -314)); |
| 71 | + |
| 72 | +echo(sprintf("%%+'_10d = %+'_10d<br />", -123)); |
| 73 | + |
| 74 | +echo(sprintf("%%+.4f = %+.4f<br />", -123.456000)); |
| 75 | + |
| 76 | +echo(vsprintf("The first 4 letters of the english alphabet are: %4$s, %3$s, %1$s and %2$s<br />", ["c", "d", "b", "a"])); |
| 77 | +/**/ |
| 78 | + |
| 79 | +//echo(sprintf("Hello %%d => Hello %d<br />", "Dolly")); |
| 80 | +echo(sprintf("Hello %%s => Hello %s<br />", "Dolly")); |
| 81 | +echo(sprintf("Hello %%1$s => Hello %1$s<br />", "Dolly")); |
| 82 | +//echo(sprintf("Hello %%s, %%(name)s and %%s => Hello %s, %(name)s and %s<br />", "Molly", {name: "Dolly"}, "Polly")); |
| 83 | +echo(sprintf("Hello %%(name_1)s, %%(name_2)s and %%(name_3)s => Hello %(name_1)s, %(name_2)s and %(name_3)s<br />", {name_1: "Molly", name_2: "Dolly", name_3: "Polly"})); |
| 84 | + |
| 85 | +var foo = { |
| 86 | +users: [ |
| 87 | +{ |
| 88 | +user: { |
| 89 | +name: [ |
| 90 | +null, |
| 91 | +["Dolly"] |
| 92 | +] |
| 93 | +} |
| 94 | +} |
| 95 | +] |
| 96 | +} |
| 97 | +echo(sprintf("Hello %%(users[0].user.name[1][0])s => Hello %(users[0].user.name[1][0])s<br />", foo)); |
| 98 | +echo(sprintf("Hello %%(users[0].user.name[0])s => Hello %(users[0].user.name[0])s<br />", foo)); |
| 99 | + |
| 100 | +//console.log(sprintf.cache); |
| 101 | + |
| 102 | +var user = { |
| 103 | +name: 'Dolly' |
| 104 | +}; |
| 105 | +echo(sprintf("Hello %(name)s<br />", user)); |
| 106 | + |
| 107 | +var users = [ |
| 108 | +{name: 'Dolly'}, |
| 109 | +{name: 'Molly'}, |
| 110 | +{name: 'Polly'} |
| 111 | +]; |
| 112 | +echo(sprintf("Hello %(users[0].name)s, %(users[1].name)s and %(users[2].name)s<br />", {users: users})); |
| 113 | + |
| 114 | +echo(vsprintf('The first 4 letters of the english alphabet are: %s, %s, %s and %s<br />', ['a', 'b', 'c', 'd'])); |
| 115 | + |
| 116 | +echo(sprintf('%2$s %3$s a %1$s', 'cracker', 'Polly', 'wants')); |
| 117 | + |
| 118 | +echo("</pre>"); |
| 119 | +</script> |
| 120 | +</body> |
| 121 | +</html> |