Compare commits

..

1 commit
main ... v0.2.1

Author SHA1 Message Date
7abcd6ef58 v0.2.1 2013-09-07 11:30:39 -07:00
6 changed files with 22 additions and 87 deletions

View file

@ -1,9 +0,0 @@
The MIT License (MIT)
Copyright © 2010-2016 Sami Samhuri, https://samhuri.net <sami@samhuri.net>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -3,7 +3,6 @@ format
printf, sprintf, and vsprintf for JavaScript printf, sprintf, and vsprintf for JavaScript
[![version 0.2.2 on npm](https://img.shields.io/badge/npm-0.2.2-brightgreen.svg?style=flat)](https://www.npmjs.com/package/format) [![node version 0.4 and up](https://img.shields.io/badge/node->=0.4-brightgreen.svg?style=flat)](https://www.npmjs.com/package/format) [![MIT License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat)](https://sjs.mit-license.org)
Installation Installation
============ ============
@ -32,21 +31,15 @@ Usage
vsprintf('%d is the answer to %s', [42, what]) vsprintf('%d is the answer to %s', [42, what])
// => '42 is the answer to life, the universe, and everything' // => '42 is the answer to life, the universe, and everything'
// you can format values as JSON with %j Supported format specifiers: b, c, d, f, o, s, x, and X.
var value = {answer: 42}
format('%j', value)
// => '{"answer":42}'
Supported format specifiers: b, c, d, f, j, o, s, x, and X. See `man 3 printf` or `man 1 printf` for details.
See `man 3 printf` or `man 1 printf` for details. `j` is an extension that formats values as JSON.
Precision is supported for floating point numbers.
License License
======= =======
Copyright 2010 - 2016 Sami Samhuri sami@samhuri.net Copyright 2010 - 2013 Sami Samhuri sami@samhuri.net
[MIT license](http://sjs.mit-license.org) [MIT license](http://sjs.mit-license.org)

View file

@ -3,7 +3,7 @@
"repo": "samsonjs/format", "repo": "samsonjs/format",
"description": "printf, sprintf, and vsprintf for JavaScript", "description": "printf, sprintf, and vsprintf for JavaScript",
"keywords": ["format", "printf", "sprintf", "vsprintf", "string"], "keywords": ["format", "printf", "sprintf", "vsprintf", "string"],
"version": "0.2.2", "version": "0.2.1.0",
"main": "format.js", "main": "format.js",
"scripts": ["format.js"] "scripts": ["format.js"]
} }

View file

@ -49,16 +49,12 @@
, c , c
, escaped = false , escaped = false
, arg , arg
, tmp
, leadingZero = false
, precision , precision
, nextArg = function() { return args[argIndex++]; } , nextArg = function() { return args[argIndex++]; }
, slurpNumber = function() { , slurpNumber = function() {
var digits = ''; var digits = '';
while (/\d/.test(fmt[i])) { while (fmt[i].match(/\d/))
digits += fmt[i++]; digits += fmt[i++];
c = fmt[i];
}
return digits.length > 0 ? parseInt(digits) : null; return digits.length > 0 ? parseInt(digits) : null;
} }
; ;
@ -66,18 +62,6 @@
c = fmt[i]; c = fmt[i];
if (escaped) { if (escaped) {
escaped = false; escaped = false;
if (c == '.') {
leadingZero = false;
c = fmt[++i];
}
else if (c == '0' && fmt[i + 1] == '.') {
leadingZero = true;
i += 2;
c = fmt[i];
}
else {
leadingZero = true;
}
precision = slurpNumber(); precision = slurpNumber();
switch (c) { switch (c) {
case 'b': // number in binary case 'b': // number in binary
@ -94,11 +78,7 @@
result += parseInt(nextArg(), 10); result += parseInt(nextArg(), 10);
break; break;
case 'f': // floating point number case 'f': // floating point number
tmp = String(parseFloat(nextArg()).toFixed(precision || 6)); result += parseFloat(nextArg()).toFixed(precision || 6);
result += leadingZero ? tmp : tmp.replace(/^0/, '');
break;
case 'j': // JSON
result += JSON.stringify(nextArg());
break; break;
case 'o': // number in octal case 'o': // number in octal
result += '0' + parseInt(nextArg(), 10).toString(8); result += '0' + parseInt(nextArg(), 10).toString(8);

View file

@ -1,8 +1,8 @@
{ {
"name": "format", "name": "format",
"description": "printf, sprintf, and vsprintf for JavaScript", "description": "printf, sprintf, and vsprintf for JavaScript",
"version": "0.2.2", "version": "0.2.1.0",
"homepage": "https://samhuri.net/projects/format", "homepage": "http://samhuri.net/proj/format",
"author": "Sami Samhuri <sami@samhuri.net>", "author": "Sami Samhuri <sami@samhuri.net>",
"repository": { "repository": {
"type": "git", "type": "git",

View file

@ -3,52 +3,23 @@ var filename = process.argv[2] || './format.js'
, printf = format.printf , printf = format.printf
; ;
function desc(x, indentLevel) { console.log('Testing printf:');
indentLevel = indentLevel || 0; printf('hello');
var indent = new Array(indentLevel).join(' '); console.log('(expected "hello")');
if (typeof x == 'string' || (x && x.__proto__ == String.prototype)) { printf('hello %s', 'sami');
return indent + '"' + x + '"'; console.log('(expected "hello sami")');
} printf('b: %b\nc: %c\nd: %d\nf: %f\no: %o\ns: %s\nx: %x\nX: %X', 42, 65, 42*42, 42*42*42/1000000000, 255, 'sami', 0xfeedface, 0xc0ffee);
else if (Array.isArray(x)) { console.log('(expected "b: 101010\nc: A\nd: 1764\nf: 0.000074\no: 0377\ns: sami\nx: 0xfeedface\nX: 0xC0FFEE")');
return indent + '[ ' + x.map(desc).join(', ') + ' ]'; console.log('(passed if the output looks ok)');
}
else {
return '' + x;
}
}
function assertFormat(args, expected) { function assertEqual(a, b) {
var fmt = args[0]; if (a !== b) throw new Error('assertion failed, ' + a + ' !== ' + b);
var result = format.format.apply(format, args);
if (result !== expected) {
console.log('FORMAT: "' + fmt + '"');
console.log('ARGS: ' + desc(args.slice(1)));
console.log('RESULT: "' + result + '"');
throw new Error('assertion failed, ' + result + ' !== ' + expected);
}
} }
console.log('Testing format:'); console.log('Testing format:');
assertEqual(format.format('hello'), 'hello');
var tests = [ assertEqual(format.format('hello %s', 'sami'), 'hello sami');
[['hello'], 'hello'], assertEqual(format.format('b: %b\nc: %c\nd: %d\nf: %f\no: %o\ns: %s\nx: %x\nX: %X', 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");
[['hello %s', 'sami'], 'hello sami'], console.log('(pass)');
[
['b: %b\nc: %c\nd: %d\nf: %f\no: %o\ns: %s\nx: %x\nX: %X', 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"
],
[['%.2f', 3.14159], '3.14'],
[['%0.2f', 3.14159], '3.14'],
[['%.2f', 0.1234], '.12'],
[['%0.2f', 0.1234], '0.12'],
[['foo %j', 42], 'foo 42'],
[['foo %j', '42'], 'foo "42"']
];
tests.forEach(function(spec) {
var args = spec[0];
var expected = spec[1];
assertFormat(args, expected);
console.log('pass (format ' + args[0] + ' == ' + expected + ')');
});
console.log('all passed'); console.log('all passed');