Comparing version 0.5.6 to 0.5.8
{ | ||
"name": "lazy-ass", | ||
"main": "index.js", | ||
"version": "0.5.6", | ||
"version": "0.5.8", | ||
"homepage": "https://github.com/bahmutov/lazy-ass", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
60
index.js
@@ -7,2 +7,41 @@ (function initLazyAss() { | ||
function toStringArray(arr) { | ||
return 'array with ' + arr.length + ' items.\n[' + | ||
arr.map(toString).join(',') + ']\n'; | ||
} | ||
function isPrimitive(arg) { | ||
return typeof arg === 'string' || | ||
typeof arg === 'number' || | ||
typeof arg === 'boolean'; | ||
} | ||
function toString(arg, k) { | ||
if (isPrimitive(arg)) { | ||
return JSON.stringify(arg); | ||
} | ||
if (arg instanceof Error) { | ||
return arg.name + ' ' + arg.message; | ||
} | ||
if (Array.isArray(arg)) { | ||
return toStringArray(arg); | ||
} | ||
if (isArrayLike(arg)) { | ||
return toStringArray(Array.prototype.slice.call(arg, 0)); | ||
} | ||
var argString; | ||
try { | ||
argString = JSON.stringify(arg, null, 2); | ||
} catch (err) { | ||
argString = '{ cannot stringify arg ' + k + ', it has type "' + typeof arg + '"'; | ||
if (typeof arg === 'object') { | ||
argString += ' with keys ' + Object.keys(arg).join(', ') + ' }'; | ||
} else { | ||
argString += ' }'; | ||
} | ||
} | ||
return argString; | ||
} | ||
function formMessage(args) { | ||
@@ -26,22 +65,3 @@ var msg = args.reduce(function (total, arg, k) { | ||
} | ||
if (Array.isArray(arg)) { | ||
return total + JSON.stringify(arg); | ||
} | ||
if (isArrayLike(arg)) { | ||
return total + JSON.stringify(Array.prototype.slice.call(arg)); | ||
} | ||
if (arg instanceof Error) { | ||
return total + arg.name + ' ' + arg.message; | ||
} | ||
var argString; | ||
try { | ||
argString = JSON.stringify(arg, null, 2); | ||
} catch (err) { | ||
argString = '[cannot stringify arg ' + k + ', it has type ' + typeof arg; | ||
if (typeof arg === 'object') { | ||
argString += ' with keys ' + Object.keys(arg).join(', ') + ']'; | ||
} else { | ||
argString += ']'; | ||
} | ||
} | ||
var argString = toString(arg, k); | ||
return total + argString; | ||
@@ -48,0 +68,0 @@ }, ''); |
{ | ||
"name": "lazy-ass", | ||
"description": "Lazy assertions without performance penalty", | ||
"version": "0.5.6", | ||
"version": "0.5.8", | ||
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>", | ||
@@ -66,4 +66,5 @@ "bugs": { | ||
"watch": "grunt watch", | ||
"coveralls": "cat coverage/PhantomJS*/lcov.info | ./node_modules/coveralls/bin/coveralls.js" | ||
"coveralls": "cat coverage/PhantomJS*/lcov.info | ./node_modules/coveralls/bin/coveralls.js", | ||
"demo": "grunt gh-pages" | ||
} | ||
} |
@@ -92,3 +92,5 @@ /* global lazyAss, la */ | ||
lazyAss(false, [1, 2, 3]); | ||
}).to.throwException(/^\[1,2,3\]$/); | ||
}).to.throwException(function (err) { | ||
expect(err.message).to.contain('1,2,3'); | ||
}); | ||
}); | ||
@@ -174,3 +176,3 @@ | ||
it('can handle circular object', function () { | ||
it('can handle one circular object', function () { | ||
var msg = 'foo has circular reference'; | ||
@@ -192,3 +194,5 @@ expect(function () { | ||
foo('something'); | ||
}).to.throwException(/something/); | ||
}).to.throwException(function (err) { | ||
expect(err.message).to.contain('something'); | ||
}); | ||
}); | ||
@@ -207,3 +211,3 @@ }); | ||
}).to.throwException(function (err) { | ||
expect(err.message).to.contain('type object'); | ||
expect(err.message).to.contain('type "object"'); | ||
expect(err.message).to.contain('foo'); | ||
@@ -232,2 +236,17 @@ expect(err.message).to.contain('bar'); | ||
it('can handle array with circular objects', function () { | ||
var foo = { | ||
bar: 'bar' | ||
}; | ||
foo.foo = foo; | ||
expect(function () { | ||
lazyAss(false, 'problem with foo', [foo]); | ||
}).to.throwException(function (err) { | ||
// console.log(err.message); | ||
expect(err.message).to.contain('problem with foo'); | ||
expect(err.message).to.contain('bar'); | ||
}); | ||
}); | ||
}); | ||
@@ -234,0 +253,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29309
539