diff --git a/README.md b/README.md index 2397fc6..a8b5c7b 100644 --- a/README.md +++ b/README.md @@ -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) @@ -208,7 +207,7 @@ cloned = _.deepClone(obj) ``` ##### `_.prettify` -Returns a prettify JSON object +Returns a pretty-print formatted JSON string. ```coffeescript obj = foo: 'bar' @@ -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' @@ -231,13 +229,6 @@ prettyOrStack = _.prettifyError(e) # at Object.InjectedScript._evaluateOn (:613:39) # at Object.InjectedScript._evaluateAndWrap (:573:52) # at Object.InjectedScript.evaluate (:492:21)" - -obj = foo: 'bar' -pretty = _.prettifyError(obj) -# => pretty JSON -# "{ -# "foo": "bar" -# }" ``` ##### `_.percentage` diff --git a/src/coffee/mixins/underscore.coffee b/src/coffee/mixins/underscore.coffee index 125f190..2f1b2e5 100644 --- a/src/coffee/mixins/underscore.coffee +++ b/src/coffee/mixins/underscore.coffee @@ -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 diff --git a/src/spec/mixins/underscore.spec.coffee b/src/spec/mixins/underscore.spec.coffee index be7c1ac..976c0ff 100644 --- a/src/spec/mixins/underscore.spec.coffee +++ b/src/spec/mixins/underscore.spec.coffee @@ -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() @@ -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', ->