Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useToJSON option #71

Merged
merged 5 commits into from
Apr 4, 2022
Merged

Conversation

fregante
Copy link
Contributor

@fregante fregante commented Apr 2, 2022

Closes #66

@fregante fregante marked this pull request as ready for review April 2, 2022 16:25
index.d.ts Outdated
@@ -28,6 +28,36 @@ export interface Options {
```
*/
readonly maxDepth?: number;

/**
Indicate whether to use a `toJSON` method if encountered in the object.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Indicate whether to use a `toJSON` method if encountered in the object.
Indicate whether to use a `.toJSON()` method if encountered in the object.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should explain in prose when you would want to turn it off.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index.d.ts Outdated

toJSON() {
// Break serialization
return {};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not feel like a realistic example. No one would be doing this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need an example? Just explain the scenarios of when it would be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped the example from the readme too. I suppose this could work in case you change your mind:

```js
import {serializeError} from 'serialize-error';

class CustomError extends Error {
	name = 'CustomError';
	constructor(message) {
		this.time = new Date()
	}
	toJSON() {
		return {
			name: 'CustomError'
		};
	}
}

const error = new CustomError('🦄');

console.log(serializeError(error));
//=> {name: 'CustomError'}

console.log(serializeError(error, {useToJSON: false}));
//=> {name: 'CustomError', message: '🦄', time: '1970-01-01T00:00:00.000Z' stack: 'etc'}
```

test.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.d.ts Outdated Show resolved Hide resolved
index.d.ts Outdated Show resolved Hide resolved
amount: `$${this.value}`,
};
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too cramped :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copy-pasted your code 😎

C-658VsXoAo3ovC

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who reviewed and merged #38? 😜

image

@sindresorhus
Copy link
Owner

Can you fix the merge conflict?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optionally ignore toJSON
2 participants