mirror of
https://github.com/samsonjs/format.git
synced 2026-04-27 14:57:39 +00:00
fixed format function and aliased Array.prototype.slice
This commit is contained in:
parent
4b3bc26bae
commit
a7a1043d59
2 changed files with 31 additions and 7 deletions
16
format.js
16
format.js
|
|
@ -8,26 +8,30 @@
|
||||||
|
|
||||||
var sys = require('sys');
|
var sys = require('sys');
|
||||||
|
|
||||||
|
function $args(args) {
|
||||||
|
return Array.prototype.slice.call(args);
|
||||||
|
}
|
||||||
|
|
||||||
exports.extendNativeStrings = function() {
|
exports.extendNativeStrings = function() {
|
||||||
String.prototype.printf = function(/* ... */) {
|
String.prototype.printf = function(/* ... */) {
|
||||||
var args = Array.prototype.slice.call(arguments);
|
var args = $args(arguments);
|
||||||
args.unshift(this);
|
if (args[0] !== this) args.unshift(this);
|
||||||
sys.puts(exports.format.apply(this, args));
|
sys.puts(exports.format.apply(this, args));
|
||||||
};
|
};
|
||||||
String.prototype.format = function(/* ... */) {
|
String.prototype.format = function(/* ... */) {
|
||||||
var args = Array.prototype.slice.call(arguments);
|
var args = $args(arguments);
|
||||||
if (args[0] !== this) args.unshift(this);
|
if (args[0] !== this) args.unshift(this);
|
||||||
exports.format.apply(this, args);
|
return exports.format.apply(this, args);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.printf = function(/* ... */) {
|
exports.printf = function(/* ... */) {
|
||||||
sys.puts(exports.format.apply(this, Array.prototype.slice.call(arguments)));
|
sys.puts(exports.format.apply(this, arguments));
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.format = function(format) {
|
exports.format = function(format) {
|
||||||
var argIndex = 1 // skip initial format argument
|
var argIndex = 1 // skip initial format argument
|
||||||
, args = Array.prototype.slice.call(arguments)
|
, args = $args(arguments)
|
||||||
, i = 0
|
, i = 0
|
||||||
, n = format.length
|
, n = format.length
|
||||||
, result = ''
|
, result = ''
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,25 @@
|
||||||
var format = require('./format');
|
var sys = require('sys')
|
||||||
|
, format = require('./format')
|
||||||
|
;
|
||||||
format.extendNativeStrings();
|
format.extendNativeStrings();
|
||||||
|
|
||||||
|
sys.puts('Testing printf:');
|
||||||
'hello'.printf();
|
'hello'.printf();
|
||||||
|
sys.puts('(expected "hello")');
|
||||||
'hello %s'.printf('sami');
|
'hello %s'.printf('sami');
|
||||||
|
sys.puts('(expected "hello sami")');
|
||||||
'b: %b\nc: %c\nd: %d\nf: %f\no: %o\ns: %s\nx: %x\nX: %X'.printf(42, 65, 42*42, 42*42*42/1000000000, 255, 'sami', 0xfeedface, 0xc0ffee);
|
'b: %b\nc: %c\nd: %d\nf: %f\no: %o\ns: %s\nx: %x\nX: %X'.printf(42, 65, 42*42, 42*42*42/1000000000, 255, 'sami', 0xfeedface, 0xc0ffee);
|
||||||
|
sys.puts('(expected "b: 101010\nc: A\nd: 1764\nf: 0.000074\no: 0377\ns: sami\nx: 0xfeedface\nX: 0xC0FFEE")');
|
||||||
|
sys.puts('(passed if the output looks ok)');
|
||||||
|
|
||||||
|
function assertEqual(a, b) {
|
||||||
|
if (a !== b) throw new Error('assertion failed, ' + a + ' !== ' + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
sys.puts('Testing format:');
|
||||||
|
assertEqual('hello'.format(), 'hello');
|
||||||
|
assertEqual('hello %s'.format('sami'), 'hello sami');
|
||||||
|
assertEqual('b: %b\nc: %c\nd: %d\nf: %f\no: %o\ns: %s\nx: %x\nX: %X'.format(42, 65, 42*42, 42*42*42/1000000000, 255, 'sami', 0xfeedface, 0xc0ffee), "b: 101010\nc: A\nd: 1764\nf: 0.000074\no: 0377\ns: sami\nx: 0xfeedface\nX: 0xC0FFEE");
|
||||||
|
sys.puts('(pass)');
|
||||||
|
|
||||||
|
sys.puts('all passed');
|
||||||
Loading…
Reference in a new issue