Skip to content

Commit

Permalink
feat: better JSON Expected/Actual output
Browse files Browse the repository at this point in the history
Now displays trivial Objects as indented and sorted by property
  • Loading branch information
dbushong committed Jul 12, 2017
1 parent 25d8d5f commit dc15426
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
25 changes: 15 additions & 10 deletions lib/assertive.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var _, abbreviate, asRegExp, assert, assertSync, clear, error, fn, fn1, getNameOfType, getTypeName, global, green, handleArgs, i, implodeNicely, includes, isArray, isEqual, isFunction, isNumber, isPromiseAlike, isRegExp, isString, isType, len, map, name, nameNegative, positiveAssertions, red, ref, ref1, ref2, stringify, toString, type, types,
var _, abbreviate, asRegExp, assert, assertSync, clear, error, fn, fn1, getNameOfType, getTypeName, global, green, handleArgs, i, implodeNicely, includes, isArray, isEqual, isFunction, isNumber, isPromiseAlike, isRegExp, isString, isType, len, map, name, nameNegative, positiveAssertions, red, ref, ref1, ref2, stringify, stringifyReplacer, toString, type, types,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
slice = [].slice,
hasProp = {}.hasOwnProperty;
Expand Down Expand Up @@ -399,6 +399,19 @@ asRegExp = function(re) {

toString = Object.prototype.toString;

stringifyReplacer = function(key, val) {
if (typeof val === 'function') {
return toString(val);
}
if (isRegExp(val)) {
return asRegExp(val);
}
if (_.isObject(val) && !_.isArray(val)) {
return _(val).toPairs().sortBy(0).fromPairs().value();
}
return val;
};

stringify = function(x) {
var className, e, json;
if (x == null) {
Expand All @@ -413,15 +426,7 @@ stringify = function(x) {
if (typeof x === 'symbol') {
return x.toString();
}
json = JSON.stringify(x, function(key, val) {
if (typeof val === 'function') {
return toString(val);
}
if (isRegExp(val)) {
return asRegExp(val);
}
return val;
});
json = JSON.stringify(x, stringifyReplacer, 2);
if (typeof x !== 'object' || includes(['Object', 'Array'], className = x.constructor.name)) {
return json;
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"lodash": "^4.6.1"
},
"devDependencies": {
"assertive": "^2.1.0",
"bluebird": "^3.3.4",
"coffee-script": "^1.10.0",
"coffeelint": "^1.10.1",
Expand Down
12 changes: 8 additions & 4 deletions src/assertive.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,19 @@ asRegExp = (re) ->

toString = Object::toString

stringifyReplacer = (key, val) ->
return toString val if typeof val is 'function'
return asRegExp val if isRegExp val
if _.isObject(val) and not _.isArray(val)
return _(val).toPairs().sortBy(0).fromPairs().value()
val

stringify = (x) ->
return "#{x}" unless x?
return 'NaN' if _.isNaN x
return asRegExp x if isRegExp x
return x.toString() if typeof x is 'symbol'
json = JSON.stringify x, (key, val) ->
return toString val if typeof val is 'function'
return asRegExp val if isRegExp val
return val
json = JSON.stringify x, stringifyReplacer, 2
if typeof x isnt 'object' \
or includes ['Object', 'Array'], className = x.constructor.name
return json
Expand Down

0 comments on commit dc15426

Please sign in to comment.