defineToJSON.mjs 563 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import nodejsCustomInspectSymbol from './nodejsCustomInspectSymbol';
/**
 * The `defineToJSON()` function defines toJSON() and inspect() prototype
 * methods, if no function provided they become aliases for toString().
 */

export default function defineToJSON(classObject) {
  var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : classObject.prototype.toString;
  classObject.prototype.toJSON = fn;
  classObject.prototype.inspect = fn;

  if (nodejsCustomInspectSymbol) {
    classObject.prototype[nodejsCustomInspectSymbol] = fn;
  }
}