Compare commits

...

13 commits
v0.2.0 ... main

Author SHA1 Message Date
0094c51372
Merge pull request #11 from samsonjs/add-license
Add MIT license file for FOSS report
2023-06-28 12:09:32 -07:00
91b6bd78af Add MIT license file for FOSS report
Closes #10
2023-06-28 12:08:47 -07:00
80570f29f9 Update project link in package.json 2021-12-12 10:33:15 -08:00
72662d55dd add badges to readme 2016-09-06 18:20:14 -07:00
93c6cf623e mention %j specifier in readme 2016-03-19 11:43:50 -06:00
4f89809675 Merge pull request #7 from chocolateboy/json
add JSON support (%j)
2015-08-30 16:54:40 -07:00
chocolateboy
a2f2c59869 add JSON support (%j) 2015-08-17 08:29:41 +01:00
35d4cf5f4f better support for floating point precision (closes #6) 2014-10-17 14:56:01 -07:00
7f14e8bb68 v0.2.1 2013-09-07 11:31:36 -07:00
Sami Samhuri
c9f6afa190 fix up package.json 2013-03-13 20:14:27 -07:00
Sami Samhuri
a581ee703f switch from ISC to MIT license 2013-03-07 23:00:31 -08:00
Sami Samhuri
820d0b8608 fix license URL 2013-03-07 23:00:28 -08:00
Sami Samhuri
56e0dbddc2 add component.json 2013-03-07 20:23:11 -08:00
7 changed files with 106 additions and 46 deletions

18
LICENSE
View file

@ -1,18 +0,0 @@
Copyright 2010 Sami Samhuri. All rights reserved.
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.

9
License.md Normal file
View file

@ -0,0 +1,9 @@
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,6 +3,7 @@ format
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
============
@ -31,14 +32,21 @@ Usage
vsprintf('%d is the answer to %s', [42, what])
// => '42 is the answer to life, the universe, and everything'
Supported format specifiers: b, c, d, f, o, s, x, and X.
// you can format values as JSON with %j
var value = {answer: 42}
format('%j', value)
// => '{"answer":42}'
See `man 3 printf` or `man 1 printf` for details.
Supported format specifiers: b, c, d, f, j, o, s, x, and X.
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
=======
Copyright 2010 - 2013 Sami Samhuri sami@samhuri.net
Copyright 2010 - 2016 Sami Samhuri sami@samhuri.net
[MIT license](http://sjs.mit-license.org)
ISC (like MIT) (see included [LICENSE](/samsonjs/format/blob/master/LICENSE))

9
component.json Normal file
View file

@ -0,0 +1,9 @@
{
"name": "format",
"repo": "samsonjs/format",
"description": "printf, sprintf, and vsprintf for JavaScript",
"keywords": ["format", "printf", "sprintf", "vsprintf", "string"],
"version": "0.2.2",
"main": "format.js",
"scripts": ["format.js"]
}

View file

@ -1,9 +1,12 @@
//
// format, printf-like string formatting for JavaScript
// format - printf-like string formatting for JavaScript
// github.com/samsonjs/format
// @_sjs
//
// Copyright 2010 - 2013 Sami Samhuri <sami@samhuri.net>
// ISC license
//
// MIT License
// http://sjs.mit-license.org
//
;(function() {
@ -46,12 +49,16 @@
, c
, escaped = false
, arg
, tmp
, leadingZero = false
, precision
, nextArg = function() { return args[argIndex++]; }
, slurpNumber = function() {
var digits = '';
while (fmt[i].match(/\d/))
while (/\d/.test(fmt[i])) {
digits += fmt[i++];
c = fmt[i];
}
return digits.length > 0 ? parseInt(digits) : null;
}
;
@ -59,6 +66,18 @@
c = fmt[i];
if (escaped) {
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();
switch (c) {
case 'b': // number in binary
@ -75,7 +94,11 @@
result += parseInt(nextArg(), 10);
break;
case 'f': // floating point number
result += parseFloat(nextArg()).toFixed(precision || 6);
tmp = String(parseFloat(nextArg()).toFixed(precision || 6));
result += leadingZero ? tmp : tmp.replace(/^0/, '');
break;
case 'j': // JSON
result += JSON.stringify(nextArg());
break;
case 'o': // number in octal
result += '0' + parseInt(nextArg(), 10).toString(8);

View file

@ -1,8 +1,8 @@
{
"name": "format",
"description": "printf, sprintf, and vsprintf for JavaScript",
"version": "0.2.0",
"homepage": "http://samhuri.net/proj/format",
"version": "0.2.2",
"homepage": "https://samhuri.net/projects/format",
"author": "Sami Samhuri <sami@samhuri.net>",
"repository": {
"type": "git",
@ -12,16 +12,16 @@
"email": "sami@samhuri.net",
"url": "https://github.com/samsonjs/format/issues"
},
"main": "./format",
"main": "./format.js",
"engines": {
"node": ">=0.4.x"
},
"licenses": [
{
"type": "ISC",
"url": "http://github.com/samsonjs/format/raw/master/LICENSE"
"type": "MIT",
"url": "http://sjs.mit-license.org"
}
],
"dependencies": {},
"devDependencies": {}
}
}

View file

@ -3,23 +3,52 @@ var filename = process.argv[2] || './format.js'
, printf = format.printf
;
console.log('Testing printf:');
printf('hello');
console.log('(expected "hello")');
printf('hello %s', 'sami');
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);
console.log('(expected "b: 101010\nc: A\nd: 1764\nf: 0.000074\no: 0377\ns: sami\nx: 0xfeedface\nX: 0xC0FFEE")');
console.log('(passed if the output looks ok)');
function desc(x, indentLevel) {
indentLevel = indentLevel || 0;
var indent = new Array(indentLevel).join(' ');
if (typeof x == 'string' || (x && x.__proto__ == String.prototype)) {
return indent + '"' + x + '"';
}
else if (Array.isArray(x)) {
return indent + '[ ' + x.map(desc).join(', ') + ' ]';
}
else {
return '' + x;
}
}
function assertEqual(a, b) {
if (a !== b) throw new Error('assertion failed, ' + a + ' !== ' + b);
function assertFormat(args, expected) {
var fmt = args[0];
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:');
assertEqual(format.format('hello'), 'hello');
assertEqual(format.format('hello %s', 'sami'), 'hello sami');
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");
console.log('(pass)');
var tests = [
[['hello'], 'hello'],
[['hello %s', 'sami'], 'hello sami'],
[
['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');