Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 10 commits
  • 4 files changed
  • 0 comments
  • 2 contributors

Showing 4 changed files with 165 additions and 121 deletions. Show diff stats Hide diff stats

  1. +7 0 .travis.yml
  2. +34 8 README.md
  3. +9 3 package.json
  4. +115 110 test/printf.coffee
7 .travis.yml
... ... @@ -0,0 +1,7 @@
  1 +
  2 +language: node_js
  3 +node_js:
  4 + - 0.6
  5 + - 0.7
  6 + - 0.8
  7 + - 0.9
42 README.md
Source Rendered
... ... @@ -1,3 +1,5 @@
  1 +[![Build Status](https://secure.travis-ci.org/wdavidw/node-printf.png)](http://travis-ci.org/wdavidw/node-printf)
  2 +<pre>
1 3 _ _ __
2 4 (_) | | / _|
3 5 _ __ _ __ _ _ __ | |_| |_
@@ -7,6 +9,8 @@
7 9 | |
8 10 |_|
9 11
  12 +</pre>
  13 +
10 14 A complete implementation of the **`printf` C functions family**
11 15 for [Node.JS][node], written in pure JavaScript.
12 16 The code is strongly inspired by the one available in the [Dojo Toolkit][dojo].
@@ -14,7 +18,8 @@ The code is strongly inspired by the one available in the [Dojo Toolkit][dojo].
14 18 **Bonus!** You get extra features, like the `%O` converter (which `inspect`s
15 19 the argument). See [Extra Features](#extra-features) below.
16 20
17   -## Installing
  21 +Installing
  22 +----------
18 23
19 24 Via [NPM][npm]:
20 25
@@ -22,7 +27,8 @@ Via [NPM][npm]:
22 27 $ npm install printf
23 28 ```
24 29
25   -## Usage
  30 +Usage
  31 +-----
26 32
27 33 Use it like you would in C (`printf`/`sprintf`):
28 34
@@ -38,7 +44,8 @@ var printf = require('printf');
38 44 printf(write_stream, format, args...);
39 45 ```
40 46
41   -## Features
  47 +Features
  48 +--------
42 49
43 50 ``` javascript
44 51 var printf = require('printf');
@@ -93,7 +100,8 @@ assert.eql('+hello+', printf('+%s+', 'hello'));
93 100 assert.eql('$', printf('%c", 36));
94 101 ```
95 102
96   -## Extra features!
  103 +Extra features!
  104 +---------------
97 105
98 106 ### Inspector
99 107
@@ -157,14 +165,32 @@ assert.eql('0000003.14', printf('%0*.*f', 3.14159265, 10, 2));
157 165 assert.eql('3.14 ', printf('%-*.*f', 3.14159265, 10, 2));
158 166 ```
159 167
160   -## Test
  168 +Development
  169 +-----------
161 170
162   -Using [Expresso][expresso]:
  171 +Tests are executed with mocha. To install it, simple run `npm install`, it will install
  172 +mocha and its dependencies in your project "node_modules" directory.
163 173
164   -``` bash
165   -expresso
  174 +To run the tests:
  175 +```bash
  176 +npm test
  177 +```
  178 +
  179 +To generate the JavaScript files:
  180 +```bash
  181 +make build
166 182 ```
167 183
  184 +The test suite is run online with [Travis][travis] against Node.js version 0.6, 0.7, 0.8 and 0.9.
  185 +
  186 +Contributors
  187 +------------
  188 +
  189 +* David Worms: <https://github.com/wdavidw>
  190 +* Aluísio Augusto Silva Gonçalves <https://github.com/AluisioASG>
  191 +* Xavier Mendez <https://github.com/jmendeth>
  192 +* LLeo <https://github.com/lleo>
  193 +
168 194
169 195 [dojo]: http://www.dojotoolkit.org "The Dojo Toolkit"
170 196 [node]: http://nodejs.org "The Node.JS platform"
12 package.json
... ... @@ -1,12 +1,17 @@
1 1 {
2 2 "name": "printf",
3   - "version": "0.1.0",
  3 + "version": "0.1.1",
4 4 "description": "Full implementation of the `printf` family in pure JS.",
5 5 "keywords": ["printf", "formatting", "sprintf", "format", "output", "print"],
6 6 "homepage": "http://www.adaltas.com/projects/node-printf",
7 7 "author": "David Worms <david@adaltas.com>",
8 8 "maintainers": [ "David Worms <david@adaltas.com>"],
9   - "contributors": [ "David Worms <david@adaltas.com>"],
  9 + "contributors": [
  10 + "David Worms <david@adaltas.com>",
  11 + "Aluísio Augusto Silva Gonçalves <aluisio@aasg.name>",
  12 + "Xavier Mendez <jmendeth@gmail.com>",
  13 + "LLeo <lleoem@gmail.com>"
  14 + ],
10 15 "main": "./lib/printf",
11 16 "engines": { "node": ">= 0.1.90" },
12 17 "directories": {
@@ -20,7 +25,8 @@
20 25 "devDependencies": {
21 26 "coffee-script": "latest",
22 27 "should": "latest",
23   - "mocha": "latest"
  28 + "mocha": "latest",
  29 + "semver": "latest"
24 30 },
25 31 "repository": {
26 32 "type": "git",
225 test/printf.coffee
... ... @@ -1,190 +1,192 @@
1   -should = require "should"
  1 +
  2 +semver = require 'semver'
  3 +should = require 'should'
2 4 printf = if process.env.PRINTF_COV then require '../lib-cov/printf' else require '../lib/printf'
3 5
4 6 describe 'sprintf', ->
5   - it "Specifier: b", ->
6   - printf("%b", 123).should.eql "1111011"
7   -
8   - it "Flag: (space)", ->
9   - printf("% d", 42).should.eql " 42"
10   - printf("% d", -42).should.eql "-42"
11   - printf("% 5d", 42).should.eql " 42"
12   - printf("% 5d", -42).should.eql " -42"
13   - printf("% 15d", 42).should.eql " 42"
14   - printf("% 15d", -42).should.eql " -42"
15   -
16   - it "Flag: +", ->
17   - printf("%+d", 42).should.eql "+42"
18   - printf("%+d", -42).should.eql "-42"
19   - printf("%+5d", 42).should.eql " +42"
20   - printf("%+5d", -42).should.eql " -42"
21   - printf("%+15d", 42).should.eql " +42"
22   - printf("%+15d", -42).should.eql " -42"
23   -
24   - it "Flag: 0", ->
25   - printf("%0d", 42).should.eql "42"
26   - printf("%0d", -42).should.eql "-42"
27   - printf("%05d", 42).should.eql "00042"
28   - printf("%05d", -42).should.eql "-00042"
29   - printf("%015d", 42).should.eql "000000000000042"
30   - printf("%015d", -42).should.eql "-000000000000042"
31   -
32   - it "Flag: -", ->
33   - printf("%-d", 42).should.eql "42"
34   - printf("%-d", -42).should.eql "-42"
35   - printf("%-5d", 42).should.eql "42 "
36   - printf("%-5d", -42).should.eql "-42 "
37   - printf("%-15d", 42).should.eql "42 "
38   - printf("%-15d", -42).should.eql "-42 "
39   - printf("%-0d", 42).should.eql "42"
40   - printf("%-0d", -42).should.eql "-42"
41   - printf("%-05d", 42).should.eql "42 "
42   - printf("%-05d", -42).should.eql "-42 "
43   - printf("%-015d", 42).should.eql "42 "
44   - printf("%-015d", -42).should.eql "-42 "
45   - printf("%0-d", 42).should.eql "42"
46   - printf("%0-d", -42).should.eql "-42"
47   - printf("%0-5d", 42).should.eql "42 "
48   - printf("%0-5d", -42).should.eql "-42 "
49   - printf("%0-15d", 42).should.eql "42 "
50   - printf("%0-15d", -42).should.eql "-42 "
  7 + it 'Specifier: b', ->
  8 + printf('%b', 123).should.eql '1111011'
  9 +
  10 + it 'Flag: (space)', ->
  11 + printf('% d', 42).should.eql ' 42'
  12 + printf('% d', -42).should.eql '-42'
  13 + printf('% 5d', 42).should.eql ' 42'
  14 + printf('% 5d', -42).should.eql ' -42'
  15 + printf('% 15d', 42).should.eql ' 42'
  16 + printf('% 15d', -42).should.eql ' -42'
  17 +
  18 + it 'Flag: +', ->
  19 + printf('%+d', 42).should.eql '+42'
  20 + printf('%+d', -42).should.eql '-42'
  21 + printf('%+5d', 42).should.eql ' +42'
  22 + printf('%+5d', -42).should.eql ' -42'
  23 + printf('%+15d', 42).should.eql ' +42'
  24 + printf('%+15d', -42).should.eql ' -42'
  25 +
  26 + it 'Flag: 0', ->
  27 + printf('%0d', 42).should.eql '42'
  28 + printf('%0d', -42).should.eql '-42'
  29 + printf('%05d', 42).should.eql '00042'
  30 + printf('%05d', -42).should.eql '-00042'
  31 + printf('%015d', 42).should.eql '000000000000042'
  32 + printf('%015d', -42).should.eql '-000000000000042'
  33 +
  34 + it 'Flag: -', ->
  35 + printf('%-d', 42).should.eql '42'
  36 + printf('%-d', -42).should.eql '-42'
  37 + printf('%-5d', 42).should.eql '42 '
  38 + printf('%-5d', -42).should.eql '-42 '
  39 + printf('%-15d', 42).should.eql '42 '
  40 + printf('%-15d', -42).should.eql '-42 '
  41 + printf('%-0d', 42).should.eql '42'
  42 + printf('%-0d', -42).should.eql '-42'
  43 + printf('%-05d', 42).should.eql '42 '
  44 + printf('%-05d', -42).should.eql '-42 '
  45 + printf('%-015d', 42).should.eql '42 '
  46 + printf('%-015d', -42).should.eql '-42 '
  47 + printf('%0-d', 42).should.eql '42'
  48 + printf('%0-d', -42).should.eql '-42'
  49 + printf('%0-5d', 42).should.eql '42 '
  50 + printf('%0-5d', -42).should.eql '-42 '
  51 + printf('%0-15d', 42).should.eql '42 '
  52 + printf('%0-15d', -42).should.eql '-42 '
51 53
52 54 it 'Precision', ->
53   - printf("%d", 42.8952).should.eql "42"
54   - printf("%.2d", 42.8952).should.eql "42" # Note: the %d format is an int
55   - printf("%.2i", 42.8952).should.eql "42"
56   - printf("%.2f", 42.8952).should.eql "42.90"
57   - printf("%.2F", 42.8952).should.eql "42.90"
58   - printf("%.10f", 42.8952).should.eql "42.8952000000"
59   - printf("%1.2f", 42.8952).should.eql "42.90"
60   - printf("%6.2f", 42.8952).should.eql " 42.90"
61   - printf("%06.2f", 42.8952).should.eql "042.90"
62   - printf("%+6.2f", 42.8952).should.eql "+42.90"
63   - printf("%5.10f", 42.8952).should.eql "42.8952000000"
  55 + printf('%d', 42.8952).should.eql '42'
  56 + printf('%.2d', 42.8952).should.eql '42' # Note: the %d format is an int
  57 + printf('%.2i', 42.8952).should.eql '42'
  58 + printf('%.2f', 42.8952).should.eql '42.90'
  59 + printf('%.2F', 42.8952).should.eql '42.90'
  60 + printf('%.10f', 42.8952).should.eql '42.8952000000'
  61 + printf('%1.2f', 42.8952).should.eql '42.90'
  62 + printf('%6.2f', 42.8952).should.eql ' 42.90'
  63 + printf('%06.2f', 42.8952).should.eql '042.90'
  64 + printf('%+6.2f', 42.8952).should.eql '+42.90'
  65 + printf('%5.10f', 42.8952).should.eql '42.8952000000'
64 66
65 67 it 'Bases', ->
66   - printf("%c", 0x7f).should.eql ""
  68 + printf('%c', 0x7f).should.eql ''
67 69 error = false
68 70 try
69   - printf "%c", -100
  71 + printf '%c', -100
70 72 catch e
71   - e.message.should.eql "invalid character code passed to %c in printf"
  73 + e.message.should.eql 'invalid character code passed to %c in printf'
72 74 error = true
73 75 error.should.be.true
74 76 error = false
75 77 try
76   - printf "%c", 0x200000
  78 + printf '%c', 0x200000
77 79 catch e
78   - e.message.should.eql "invalid character code passed to %c in printf"
  80 + e.message.should.eql 'invalid character code passed to %c in printf'
79 81 error = true
80 82 error.should.be.true
81 83
82 84 it 'Mapping', ->
83 85 # %1$s format
84   - printf("%1$").should.eql "%1$"
85   - printf("%0$s").should.eql "%0$s"
86   - printf("%1$s %2$s", "Hot", "Pocket").should.eql "Hot Pocket"
87   - printf("%1$.1f %2$s %3$ss", 12, "Hot", "Pocket").should.eql "12.0 Hot Pockets"
88   - printf("%1$*.f", "42", 3).should.eql " 42"
  86 + printf('%1$').should.eql '%1$'
  87 + printf('%0$s').should.eql '%0$s'
  88 + printf('%1$s %2$s', 'Hot', 'Pocket').should.eql 'Hot Pocket'
  89 + printf('%1$.1f %2$s %3$ss', 12, 'Hot', 'Pocket').should.eql '12.0 Hot Pockets'
  90 + printf('%1$*.f', '42', 3).should.eql ' 42'
89 91 error = false
90 92 try
91   - printf "%2$*s", "Hot Pocket"
  93 + printf '%2$*s', 'Hot Pocket'
92 94 catch e
93 95 e.message.should.eql "got 1 printf arguments, insufficient for '%2$*s'"
94 96 error = true
95 97 error.should.be.true
96 98 # %(map)s format
97   - printf("%(foo", {}).should.eql "%(foo"
98   - printf("%(temperature)s %(crevace)s",
99   - temperature: "Hot"
100   - crevace: "Pocket"
101   - ).should.eql "Hot Pocket"
102   - printf("%(quantity).1f %(temperature)s %(crevace)ss",
  99 + printf('%(foo', {}).should.eql '%(foo'
  100 + printf('%(temperature)s %(crevace)s',
  101 + temperature: 'Hot'
  102 + crevace: 'Pocket'
  103 + ).should.eql 'Hot Pocket'
  104 + printf('%(quantity).1f %(temperature)s %(crevace)ss',
103 105 quantity: 12
104   - temperature: "Hot"
105   - crevace: "Pocket"
106   - ).should.eql "12.0 Hot Pockets"
  106 + temperature: 'Hot'
  107 + crevace: 'Pocket'
  108 + ).should.eql '12.0 Hot Pockets'
107 109 error = false
108 110 try
109   - printf "%(foo)s", 42
  111 + printf '%(foo)s', 42
110 112 catch e
111   - e.message.should.eql "format requires a mapping"
  113 + e.message.should.eql 'format requires a mapping'
112 114 error = true
113 115 error.should.be.true
114 116 error = false
115 117 try
116   - printf "%(foo)s %(bar)s", "foo", 42
  118 + printf '%(foo)s %(bar)s', 'foo', 42
117 119 catch e
118   - e.message.should.eql "format requires a mapping"
  120 + e.message.should.eql 'format requires a mapping'
119 121 error = true
120 122 error.should.be.true
121 123 error = false
122 124 try
123   - printf "%(foo)*s",
124   - foo: "Hot Pocket"
  125 + printf '%(foo)*s',
  126 + foo: 'Hot Pocket'
125 127 catch e
126   - e.message.should.eql "* width not supported in mapped formats"
  128 + e.message.should.eql '* width not supported in mapped formats'
127 129 error = true
128 130 error.should.be.true
129 131
130 132 it 'Positionals', ->
131   - printf("%*s", "foo", 4).should.eql " foo"
132   - printf("%*.*f", 3.14159265, 10, 2).should.eql " 3.14"
133   - printf("%0*.*f", 3.14159265, 10, 2).should.eql "0000003.14"
134   - printf("%-*.*f", 3.14159265, 10, 2).should.eql "3.14 "
  133 + printf('%*s', 'foo', 4).should.eql ' foo'
  134 + printf('%*.*f', 3.14159265, 10, 2).should.eql ' 3.14'
  135 + printf('%0*.*f', 3.14159265, 10, 2).should.eql '0000003.14'
  136 + printf('%-*.*f', 3.14159265, 10, 2).should.eql '3.14 '
135 137 error = false
136 138 try
137   - printf "%*s", "foo", "bar"
  139 + printf '%*s', 'foo', 'bar'
138 140 catch e
139   - e.message.should.eql "the argument for * width at position 2 is not a number in %*s"
  141 + e.message.should.eql 'the argument for * width at position 2 is not a number in %*s'
140 142 error = true
141 143 error.should.be.true
142 144 error = false
143 145 try
144   - printf "%10.*f", "foo", 42
  146 + printf '%10.*f', 'foo', 42
145 147 catch e
146 148 e.message.should.eql "format argument 'foo' not a float; parseFloat returned NaN"
147 149 error = true
148 150 error.should.be.true
149 151
150   - it "vs. Formatter", ->
  152 + it 'vs. Formatter', ->
151 153 i = 0
152 154 while i < 1000
153   - printf "%d %s Pockets", i, "Hot"
  155 + printf '%d %s Pockets', i, 'Hot'
154 156 i++
155 157
156 158 it 'Formatter', ->
157   - str = new printf.Formatter("%d %s Pockets")
  159 + str = new printf.Formatter('%d %s Pockets')
158 160 i = 0
159 161 while i < 1000
160   - str.format i, "Hot"
  162 + str.format i, 'Hot'
161 163 i++
162 164
163 165 it 'Miscellaneous', ->
164   - printf("+%s+", "hello").should.eql "+hello+"
165   - printf("+%d+", 10).should.eql "+10+"
166   - printf("%c", "a").should.eql "a"
167   - printf("%c", 34).should.eql "\""
168   - printf("%c", 36).should.eql "$"
169   - printf("%d", 10).should.eql "10"
  166 + printf('+%s+', 'hello').should.eql '+hello+'
  167 + printf('+%d+', 10).should.eql '+10+'
  168 + printf('%c', 'a').should.eql 'a'
  169 + printf('%c', 34).should.eql '\"'
  170 + printf('%c', 36).should.eql '$'
  171 + printf('%d', 10).should.eql '10'
170 172 error = false
171 173 try
172   - printf "%s%s", 42
  174 + printf '%s%s', 42
173 175 catch e
174 176 e.message.should.eql "got 1 printf arguments, insufficient for '%s%s'"
175 177 error = true
176 178 error.should.be.true
177 179 error = false
178 180 try
179   - printf "%c"
  181 + printf '%c'
180 182 catch e
181 183 e.message.should.eql "got 0 printf arguments, insufficient for '%c'"
182 184 error = true
183 185 error.should.be.true
184   - printf("%10", 42).should.eql "%10"
  186 + printf('%10', 42).should.eql '%10'
185 187
186 188 it 'Escape', ->
187   - printf("%d %", 10).should.eql "10 %"
  189 + printf('%d %', 10).should.eql '10 %'
188 190
189 191 it 'Object inspection', ->
190 192 test =
@@ -195,7 +197,10 @@ describe 'sprintf', ->
195 197 isnot:
196 198 array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
197 199 maybe: undefined
198   - printf("%O", test).replace(/\s+/g, ' ').should.eql "{ foo: { is: { bar: true, baz: false }, isnot: { array: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, [length]: 10 ] }, maybe: undefined } }"
199   - printf("%.0O", test).replace(/\s+/g, ' ').should.eql "{ foo: [Object] }"
200   - printf("%.2O", test).replace(/\s+/g, ' ').should.eql "{ foo: { is: { bar: true, baz: false }, isnot: { array: [Object] }, maybe: undefined } }"
201   - printf("%A", test.foo.isnot.array).should.eql "[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ]"
  200 + printf('%.0O', test).replace(/\s+/g, ' ').should.eql '{ foo: [Object] }'
  201 + printf('%A', test.foo.isnot.array).should.eql '[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ]'
  202 + # Object inspect serialize object in different order when showHidden is true
  203 + return if semver.lt process.version, 'v0.9.0'
  204 + printf('%O', test).replace(/\s+/g, ' ').should.eql '{ foo: { is: { bar: true, baz: false }, isnot: { array: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, [length]: 10 ] }, maybe: undefined } }'
  205 + printf('%.2O', test).replace(/\s+/g, ' ').should.eql '{ foo: { is: { bar: true, baz: false }, isnot: { array: [Object] }, maybe: undefined } }'
  206 +

No commit comments for this range

Something went wrong with that request. Please try again.