string.ify
A small, simple yet powerful JavaScript object stringifier / pretty-printer
Last updated a year ago by xpl .
Unlicense · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install string.ify 
SYNC missed versions from official npm registry.

String.ify

Build Status npm dependencies Status

A small, simple yet powerful JavaScript object stringifier / pretty-printer. Powers the Ololog library.

Why

  • Humanized output
  • Highly configurable
  • Pluggable rendering (via Symbols)
  • Works in Node and browsers

Recent changes

  • RegExp instances now rendered correctly
  • Chain-style configuration helpers: stringify.pure.noPretty.maxDepth (10) (...)
  • Now understands typed arrays

Installing

npm install string.ify

In your code:

stringify = require ('string.ify')

Pretty Printing

stringify ({ obj: [{ someLongPropertyName: 1, propertyName: 2, anotherProp: 4, moreProps: 5 },
                   { propertyName: { someVeryLongPropertyName: true, qux: 6, zap: "lol" } }] })

Will output:

{ obj: [ { someLongPropertyName: 1,
                   propertyName: 2,
                    anotherProp: 4,
                      moreProps: 5  },
         { propertyName: { someVeryLongPropertyName:  true,
                                                qux:  6,
                                                zap: "lol"  } } ] }

With stringify.noRightAlignKeys (obj) or rightAlignKeys: false, if you don't want the keys alignment:

{ obj: [ { someLongPropertyName: 1,
           propertyName: 2,
           anotherProp: 4,
           moreProps: 5             },
         { propertyName: { someVeryLongPropertyName: true,
                           qux: 6,
                           zap: "lol"                      } } ] }

With stringify.noFancy (obj) or fancy: false, if you want classic nesting:

{
    obj: [
        {
            someLongPropertyName: 1,
            propertyName: 2,
            anotherProp: 4,
            moreProps: 5
        },
        {
            propertyName: {
                someVeryLongPropertyName: true,
                qux: 6,
                zap: "lol"
            }
        }
    ]
}

In the "no fancy" mode you can also set the indentation width by:

stringify.configure ({ fancy: false, indentation: '  ' }) (obj) // 2 spaces instead of 4
{
  obj: [
    {
      propertyName: 2,
      moreProps: 5
    }
  ]
}

As you can see, by default it does some fancy alignment to make complex nested objects look more readable:

GIF Animation

It automatically detects whether the pretty printing is nessesary. If the output isn't lenghty, it renders as single line:

stringify ({ foo: 1, bar: 2 }) // { foo: 1, bar: 2 }

It also works with nested objects. Setting maxLength (defaults to 50):

stringify.maxLength (70) ({ asks: [{ price: "1000", amount: 10 }, { price: "2000", amount: 10 }],
                            bids: [{ price: "500", amount: 10 }, { price: "100", amount: 10 }] })

Example output for maxLength set to 70, 50 and 20, respectively):

{ asks: [{ price: "1000", amount: 10 }, { price: "2000", amount: 10 }],
  bids: [{ price: "500", amount: 10 }, { price: "100", amount: 10 }]    }
{ asks: [ { price: "1000", amount: 10 },
          { price: "2000", amount: 10 }  ],
  bids: [ { price: "500", amount: 10 },
          { price: "100", amount: 10 }  ]   }
{ asks: [ {  price: "1000",
            amount:  10     },
          {  price: "2000",
            amount:  10     }  ],
  bids: [ {  price: "500",
            amount:  10    },
          {  price: "100",
            amount:  10    }  ]   }

Forcing single-line rendering by setting { pretty: false } or with noPretty chain helper:

stringify.noPretty
    ({ nil: null, nope: undefined, fn: function ololo () {}, bar: [{ baz: "garply", qux: [1, 2, 3] }] })
//   { nil: null, nope: undefined, fn: <function:ololo>,     bar: [{ baz: "garply", qux: [1, 2, 3] }] }

Configuring

Configuring goes like this:

stringify.configure ({ /* params */ }) (...)

You can stack .configure calls, as it simply returns a new function instance with config params applied:

stringify = require ('string.ify').configure ({ ... }) // configure at import

...

stringify.configure ({ ... }) (obj) // ad-hoc configuration

Configuration parameters have chain-style setter methods:

stringify.pure.noPretty.maxDepth (10) (...)

It's the same as calling configure with:

stringify.configure ({ pure: true, pretty: false, maxDepth: 10 }) (...)

All (default) config options:

stringify.configure ({

    pure:            false,
    json:            false,
    maxDepth:        5,
    maxLength:       50,
    maxArrayLength:  60,
    maxObjectLength: 200,
    maxStringLength: 60,
    precision:       undefined,
    formatter:       undefined,
    pretty:         'auto',
    rightAlignKeys:  true,
    fancy:           true,
    indentation:    '    ',
    
}) (...)

Collapsing Lengthy Output

It handles global and window references, so it wont mess up your output:

stringify ({ root: global }) // { root: global }

Cyclic references:

var obj = {}
    obj.foo = { bar: [obj] }

stringify (obj) // { foo: { bar: [<cyclic>] } }

Collapsing multiple references to the same object:

var obj = {}

stringify ([obj, obj, obj]) // [{  }, <ref:1>, <ref:1>]

It even understands jQuery objects and DOM nodes:

$('<button id="send" class="red" /><button class="blue" />']).appendTo (document.body)

stringify ($('button'))                           // "[ <button#send.red>, <button.blue> ]"
stringify (document.createTextNode ('some text')) // "@some text"

Setting maxDepth (defaults to 5) and maxArrayLength (defaults to 60):

stringify.maxDepth (2).maxArrayLength (5) ({ a: { b: { c: 0 } }, qux: [1,2,3,4,5,6] }),
                                        // { a: { b: <object> }, qux: <array[6]> }

Setting maxObjectLength (defaults to 200):

stringify.maxObjectLength (6) ({ long: { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 } })
                            // { long: <object[6]> }

Setting maxStringLength (default is 60):

stringify.maxStringLength (4) ({ yo: 'blablablabla' }) // { yo: "bla…" }

Empty argument means no limit:

stringify.maxDepth () (...) // will render arbitrarily deep

Other Configuration Options

JSON-compatible output:

stringify.json ({ foo: { bar: 'baz' } }) // { "foo": { "bar": "baz" } }

JavaScript output:

stringify.pure ({ yo: function () { return 123 } }) // { yo: function () { return 123 } }

Setting floating-point output precision:

stringify               ({ a: 123, b: 123.000001 }) // { a: 123, b: 123.000001 }
stringify.precision (2) ({ a: 123, b: 123.000001 }) // { a: 123, b: 123.00 }

Custom rendering

With ad-hoc formatter

booleansAsYesNo = stringify.formatter (x => (typeof x === 'boolean' ? (x ? 'yes' : 'no') : undefined))
booleansAsYesNo  ({ a: { b: true }, c: false }),
//                { a: { b: yes }, c: no }

Return undefined to fallback to the default formatter.

With Symbols

If you don't know what they are, read this article. Symbols are awesome! They allow to add hidden properties (i.e. metadata) to arbitrary objects. String.ify uses this mechanism to implement custom formatters on rendered objects:

Boolean.prototype[Symbol.for ('String.ify')] = function (stringify) {
                                                   return this ? 'yes' : 'no' }

stringify ({ a: { b: true }, c: false })
//        '{ a: { b: yes }, c: no }'

Note how a stringify is passed as an argument to a renderer function. Call it to render nested contents. Current config options are available as properties of that function. You can override them by calling the configure method. Here's an example of adding purple ANSI color to rendered arrays:

Array.prototype[Symbol.for ('String.ify')] = function (stringify) {

    return '\u001B[35m[' + this.map (stringify).join (', ') + ']\u001b[0m'
}

stringify ({ a:           [{ foo: 42, bar: 43 }, 44, 45, 46] })
//        '{ a: \u001B[35m[{ foo: 42, bar: 43 }, 44, 45, 46]\u001b[0m }')

Powered by

Applications

  • Ololog — a platform-agnostic logging powered with String.ify

Current Tags

  • 1.0.61                                ...           latest (a year ago)

59 Versions

  • 1.0.61                                ...           a year ago
  • 1.0.58                                ...           a year ago
  • 1.0.57                                ...           a year ago
  • 1.0.56                                ...           a year ago
  • 1.0.55                                ...           a year ago
  • 1.0.54                                ...           a year ago
  • 1.0.53                                ...           a year ago
  • 1.0.52                                ...           a year ago
  • 1.0.51                                ...           a year ago
  • 1.0.50                                ...           a year ago
  • 1.0.49                                ...           2 years ago
  • 1.0.46                                ...           2 years ago
  • 1.0.45                                ...           2 years ago
  • 1.0.44                                ...           2 years ago
  • 1.0.43                                ...           3 years ago
  • 1.0.42                                ...           3 years ago
  • 1.0.41                                ...           3 years ago
  • 1.0.40                                ...           3 years ago
  • 1.0.39                                ...           3 years ago
  • 1.0.38                                ...           3 years ago
  • 1.0.37                                ...           3 years ago
  • 1.0.36                                ...           3 years ago
  • 1.0.35                                ...           3 years ago
  • 1.0.34                                ...           3 years ago
  • 1.0.33                                ...           3 years ago
  • 1.0.32                                ...           3 years ago
  • 1.0.31                                ...           3 years ago
  • 1.0.30                                ...           3 years ago
  • 1.0.29                                ...           3 years ago
  • 1.0.28                                ...           3 years ago
  • 1.0.27                                ...           3 years ago
  • 1.0.26                                ...           3 years ago
  • 1.0.25                                ...           3 years ago
  • 1.0.24                                ...           3 years ago
  • 1.0.23                                ...           3 years ago
  • 1.0.22                                ...           3 years ago
  • 1.0.21                                ...           3 years ago
  • 1.0.20                                ...           3 years ago
  • 1.0.19                                ...           3 years ago
  • 1.0.18                                ...           3 years ago
  • 1.0.17                                ...           3 years ago
  • 1.0.16                                ...           3 years ago
  • 1.0.15                                ...           3 years ago
  • 1.0.14                                ...           3 years ago
  • 1.0.13                                ...           4 years ago
  • 1.0.12                                ...           4 years ago
  • 1.0.11                                ...           4 years ago
  • 1.0.10                                ...           4 years ago
  • 1.0.9                                ...           4 years ago
  • 1.0.8                                ...           4 years ago
  • 1.0.7                                ...           4 years ago
  • 1.0.6                                ...           4 years ago
  • 1.0.5                                ...           4 years ago
  • 1.0.4                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
  • 0.0.1                                ...           4 years ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 4
Last Day 0
Last Week 4
Last Month 83
Dependencies (2)
Dev Dependencies (9)
Dependents (3)

Copyright 2014 - 2016 © taobao.org |