Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ This module shares helpers among all [SPHERE.IO](http://sphere.io/) Node.js comp
* [Underscore](#underscore)
* [_.deepClone](#_deepclone)
* [_.prettify](#_prettify)
* [_.prettifyError](#_prettifyerror)
* [_.percentage](#_percentage)
* [_.stringifyQuery](#_stringifyquery)
* [_.parseQuery](#_parsequery)
Expand Down Expand Up @@ -208,7 +207,7 @@ cloned = _.deepClone(obj)
```

##### `_.prettify`
Returns a prettify JSON object
Returns a pretty-print formatted JSON string.

```coffeescript
obj = foo: 'bar'
Expand All @@ -218,9 +217,8 @@ pretty = _.prettify(obj)
# "foo": "bar"
# }"
```

##### `_.prettifyError`
Returns either a prettify JSON object or the Error stack
In case the object is an instance of `Error`, the error stack is returned
untouched.

```coffeescript
e = new Error 'foo'
Expand All @@ -231,13 +229,6 @@ prettyOrStack = _.prettifyError(e)
# at Object.InjectedScript._evaluateOn (<anonymous>:613:39)
# at Object.InjectedScript._evaluateAndWrap (<anonymous>:573:52)
# at Object.InjectedScript.evaluate (<anonymous>:492:21)"

obj = foo: 'bar'
pretty = _.prettifyError(obj)
# => pretty JSON
# "{
# "foo": "bar"
# }"
```

##### `_.percentage`
Expand Down
18 changes: 5 additions & 13 deletions src/coffee/mixins/underscore.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,17 @@ module.exports =
JSON.parse(JSON.stringify(obj))

###*
* Stringifies a JSON object in a pretty format
* Stringifies a JSON object in a pretty format. In case of an error object,
* the error stack is returned untouched.
* @param {Object} obj A JSON object
* @param {Number} [indentation] The indentation number (default 2)
* @return {String} A pretty string
###
prettify: (obj, indentation = 2) ->
JSON.stringify obj, null, indentation

###*
* Returns either a pretty string (from JSON object) or the error stack
* @param {Object} objOrError Either a JSON object or an Error
* @param {Number} [indentation] The indentation number (default 2)
* @return {String} A pretty string or the error stack
###
prettifyError: (objOrError, indentation = 2) ->
if objOrError instanceof Error
objOrError.stack
if obj instanceof Error
obj.stack
else
@prettify objOrError, indentation
JSON.stringify obj, null, indentation

###*
* Returns the percentage of the given values
Expand Down
8 changes: 1 addition & 7 deletions src/spec/mixins/underscore.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe 'Mixins', ->
_.mixin _u
expect(_.deepClone).toBeDefined()
expect(_.prettify).toBeDefined()
expect(_.prettifyError).toBeDefined()
expect(_.percentage).toBeDefined()
expect(_.stringifyQuery).toBeDefined()
expect(_.parseQuery).toBeDefined()
Expand Down Expand Up @@ -66,14 +65,9 @@ describe 'Mixins', ->
it 'should prettify JSON with custom indentation', ->
expect(_u.prettify {foo: 'bar'}, 4).toBe '{\n "foo": "bar"\n}'

describe '_u :: prettifyError', ->

it 'should return stack trace', ->
e = new Error 'foo'
expect(_u.prettifyError e).toEqual e.stack

it 'should return pretty JSON', ->
expect(_u.prettifyError {foo: 'bar'}).toBe '{\n "foo": "bar"\n}'
expect(_u.prettify e).toEqual e.stack

describe '_u :: percentage', ->

Expand Down