3

I have this object which has some symbols properties on it:

{
  foo: 'bar',
  [Symbol(raw.json.bytes)]: 13,
  [Symbol(raw.json.str)]: '{"foo":"bar"}'
} 

now, I added those symbol properties myself, so maybe I can change how I add them using Object.defineProperty. Is there some way to prevent logging the symbols either:

  1. using an option to util.inspect(v, opts)
  2. Or by using Object.defineProperty?

Either way I am using util.inspect() to stringify the object, and my preference is to pass it an option to not log non-enumerable properties or what not.

  • I tried using `util.inspect(v, {showHidden: false}))` and that didn't work, perhaps because the symbol properts are not hidden? But they are just added with `o[Symbol()] = 'x'` –  Aug 03 '19 at 04:40
  • 1
    There's no such thing: `Object.definedProperty`... Possibly you've meant `Object.defineProperty`. – FZs Aug 03 '19 at 05:07

1 Answers1

0

You can do it with the combination of showHidden option false (default) and non-enumerable symbols, but this may have side effects1.

To do this, define symbols like:

Object.defineProperty(obj,symbol,{
  enumerable: false,  //That matters
  configurable: true, //Or false
  writable: true,     //Or false
  value: someValue
})

Properties (not necessarily symbols) defined like above, won't be included in util.inspect calls if showHidden option is false.


1: Non-enumerable properties are hidden when iterating over an object. Read more on MDN or in this SO post

FZs
  • 16,581
  • 13
  • 41
  • 50