Skip to content

Commit

Permalink
feat(undefined): printing undefined property values, closes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Aug 3, 2016
1 parent 6db46b7 commit 1d664bf
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 9 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ arguments - there is no performance penalty!
## Rethrowing errors

If the condition itself is an instance of Error, it is simply rethrown (synchronously or
asynchronously).
asynchronously).

```js
lazyAss(new Error('foo'));
// Uncaught Error: foo
```

Useful to make sure errors in the promise chains are
Useful to make sure errors in the promise chains are
[not silently ignored](https://glebbahmutov.com/blog/why-promises-need-to-be-done/).

For example, a rejected promise below this will be ignored.
Expand Down Expand Up @@ -241,19 +241,19 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

[lazy-ass-icon]: https://nodei.co/npm/lazy-ass.png?downloads=true
[lazy-ass-icon]: https://nodei.co/npm/lazy-ass.svg?downloads=true
[lazy-ass-url]: https://npmjs.org/package/lazy-ass
[lazy-ass-ci-image]: https://travis-ci.org/bahmutov/lazy-ass.png?branch=master
[lazy-ass-ci-image]: https://travis-ci.org/bahmutov/lazy-ass.svg?branch=master
[lazy-ass-ci-url]: https://travis-ci.org/bahmutov/lazy-ass
[lazy-ass-coverage-image]: https://coveralls.io/repos/bahmutov/lazy-ass/badge.png
[lazy-ass-coverage-image]: https://coveralls.io/repos/bahmutov/lazy-ass/badge.svg
[lazy-ass-coverage-url]: https://coveralls.io/r/bahmutov/lazy-ass
[lazy-ass-code-climate-image]: https://codeclimate.com/github/bahmutov/lazy-ass/badges/gpa.svg
[lazy-ass-code-climate-url]: https://codeclimate.com/github/bahmutov/lazy-ass
[lazy-ass-codacy-image]: https://www.codacy.com/project/badge/b60a0810c9af4fe4b2ae685932dbbdb8
[lazy-ass-codacy-url]: https://www.codacy.com/public/bahmutov/lazy-ass.git
[lazy-ass-dependencies-image]: https://david-dm.org/bahmutov/lazy-ass.png
[lazy-ass-dependencies-image]: https://david-dm.org/bahmutov/lazy-ass.svg
[lazy-ass-dependencies-url]: https://david-dm.org/bahmutov/lazy-ass
[lazy-ass-devdependencies-image]: https://david-dm.org/bahmutov/lazy-ass/dev-status.png
[lazy-ass-devdependencies-image]: https://david-dm.org/bahmutov/lazy-ass/dev-status.svg
[lazy-ass-devdependencies-url]: https://david-dm.org/bahmutov/lazy-ass#info=devDependencies
[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-url]: https://github.com/semantic-release/semantic-release
6 changes: 5 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ <h1>lazy-ass</h1>
bar.bar = bar;
lac(false, 'example printing a circular object', bar);

la(typeof foo === 'string', 'sync exception, variable foo is a string', foo);
lac(false, {foo: 'foo', bar: undefined},
'this object has property "bar" with undefined value')

la(typeof foo === 'string',
'sync exception, variable foo is NOT a string, but', typeof foo, foo);
// nothing runs after sync exception
</script>
</body>
Expand Down
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@
return e instanceof Error;
}

/*
custom JSON.stringify replacer to make sure we do not
hide properties that have value "undefined"
var o = {
foo: 42,
bar: undefined
}
// standard JSON.stringify returns '{"foo": 42}'
// this replacer returns '{"foo": 42, "bar": null}'
*/
function replacer(key, value) {
if (value === undefined) {
return null;
}
return value;
}

function toString(arg, k) {
if (isPrimitive(arg)) {
return JSON.stringify(arg);
Expand All @@ -35,7 +52,7 @@
}
var argString;
try {
argString = JSON.stringify(arg, null, 2);
argString = JSON.stringify(arg, replacer, 2);
} catch (err) {
argString = '{ cannot stringify arg ' + k + ', it has type "' + typeof arg + '"';
if (typeof arg === 'object') {
Expand Down
58 changes: 58 additions & 0 deletions test/lazy-ass.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,64 @@
}).to.throwException(JSON.stringify(obj, null, 2));
});

it('JSON.stringify skip undefined property', function () {
var obj = {
foo: 'foo',
bad: undefined
};
const str = JSON.stringify(obj);
expect(str).not.to.contain('bad', str);
});

it('JSON.stringify with custom replacer', function () {
var obj = {
foo: 'foo',
bad: undefined
};
function replacer(key, value) {
if (value === undefined) {
return 'null';
}
return value;
}
const str = JSON.stringify(obj, replacer);
expect(str).to.contain('foo', str);
expect(str).to.contain('bad', str);
});

it('nested JSON.stringify with custom replacer', function () {
var obj = {
foo: 'foo',
bar: {
baz: 'value'
}
};
function replacer(key, value) {
if (value === undefined) {
return null;
}
return value;
}
const str = JSON.stringify(obj, replacer);
expect(str).to.contain('foo', str);
expect(str).to.contain('bar', str);
expect(str).to.contain('baz', str);
expect(str).to.contain('value', str);
});

it('JSON stringifies undefined value of a property', function () {
var obj = {
foo: 'foo',
bad: undefined
};
expect(function () {
lazyAss(false, obj);
}).to.throwException(function (err) {
expect(err.message).to.contain('foo', err.message);
expect(err.message).to.contain('bad', err.message);
});
});

it('takes error name and message', function () {
expect(function () {
lazyAss(false, new Error('hi there'));
Expand Down

0 comments on commit 1d664bf

Please sign in to comment.