defineToJSON.js.flow 579 B

123456789101112131415161718
  1. // @flow strict
  2. import nodejsCustomInspectSymbol from './nodejsCustomInspectSymbol';
  3. /**
  4. * The `defineToJSON()` function defines toJSON() and inspect() prototype
  5. * methods, if no function provided they become aliases for toString().
  6. */
  7. export default function defineToJSON(
  8. classObject: Class<any> | ((...args: Array<any>) => mixed),
  9. fn?: () => mixed = classObject.prototype.toString,
  10. ): void {
  11. classObject.prototype.toJSON = fn;
  12. classObject.prototype.inspect = fn;
  13. if (nodejsCustomInspectSymbol) {
  14. classObject.prototype[nodejsCustomInspectSymbol] = fn;
  15. }
  16. }