Permalink
Browse files

Add depth control to %O specifier

Previously, using the %O specifier implied on recursing indefinitely
on the object being formatted.  For large and complicated objects,
this behavior may not be desirable.

With this change, the recursion depth can be controlled through the
precision flag.  If the flag is not specified (`%O`) or set but no
precision is specified (`%.O`), it will, as currently, recurse
indefinitely.  Otherwise (e.g. `%.2O`), it will recurse that many times
in the object.
  • Loading branch information...
1 parent a3942af commit 6636ca4f7fedba769666caccf8df4eae38e44403 @AluisioASG committed Feb 22, 2013
Showing with 30 additions and 7 deletions.
  1. +9 −0 README.md
  2. +9 −3 lib/printf.js
  3. +12 −4 test/printf.coffee
View
@@ -110,6 +110,15 @@ assert.eql("Test: { hello: 'Node' }",
**Important:** it's a capital "O", *not* a zero!
+Specifying a precision lets you control the depth up to which the object is formatted:
+
+``` javascript
+assert.eql("Debug: { depth0: { depth1_: 0, depth1: [Object] } }",
+ printf('Debug: %.1O', {depth0: {depth1: {depth2: {depth3: true}}, depth1_: 0}})
+);
+```
+
+
### Argument mapping
In addition to the old-fashioned `n$`,
View
@@ -125,9 +125,7 @@ Formatter.prototype._specifiers = {
toUpper: true
},
O: {
- setArg: function(token){
- token.arg = util.inspect(token.arg, true, null);
- }
+ isObject: true
},
};
Formatter.prototype.format = function(/*mixed...*/ filler){
@@ -197,6 +195,7 @@ Formatter.prototype.format = function(/*mixed...*/ filler){
token.isUnsigned = false;
token.isInt = false;
token.isDouble = false;
+ token.isObject = false;
token.precision = 1;
if(token.period == '.'){
if(token._precision){
@@ -270,6 +269,8 @@ Formatter.prototype.format = function(/*mixed...*/ filler){
token.precision = 6;
}
this.formatDouble(token);
+ }else if(token.isObject){
+ this.formatObject(token);
}
this.fitField(token);
@@ -392,6 +393,11 @@ Formatter.prototype.formatDouble = function(token) {
token.arg = token.toUpper ? token.arg.toUpperCase() : token.arg.toLowerCase();
};
+Formatter.prototype.formatObject = function(token) {
+ // If no precision is specified, then reset it to null (infinite depth).
+ var precision = (token.period === '.') ? token.precision : null;
+ token.arg = util.inspect(token.arg, true, precision);
+};
Formatter.prototype.zeroPad = function(token, /*Int*/ length) {
length = (arguments.length == 2) ? length : token.precision;
var negative = false;
View
@@ -186,7 +186,15 @@ describe 'sprintf', ->
it 'Escape', ->
printf("%d %", 10).should.eql "10 %"
- it 'Output', ->
- printf("Debug %O",
- hello: "Node"
- ).should.eql "Debug { hello: 'Node' }"
+ it 'Object inspection', ->
+ test =
+ foo:
+ is:
+ bar: true
+ baz: false
+ isnot:
+ array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
+ maybe: undefined
+ 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 } }"
+ printf("%.0O", test).replace(/\s+/g, ' ').should.eql "{ foo: [Object] }"
+ printf("%.2O", test).replace(/\s+/g, ' ').should.eql "{ foo: { is: { bar: true, baz: false }, isnot: { array: [Object] }, maybe: undefined } }"

0 comments on commit 6636ca4

Please sign in to comment.