title: How to check if object is instanceof firebase.firestore.FieldValue
body: <p>On the firebase js web client:</p>
  <pre><code>var fv = firebase.firestore.FieldValue;
  var sv = fv.serverTimestamp();
  
  sv instanceof fv //true
  </code></pre>
  <p>whereas in node:</p>
  <pre><code>var fb = require('firebase-admin');
  var fv = fb.firestore.FieldValue;
  var sv = fv.serverTimestamp();
  
  sv instanceof fv //false
  </code></pre>
  <p>I don't know why there's this inconsistency. How can i tell if an object is an instance of firebase.firestore.FieldValue? It needs to work for both node and js web.</p>
  <h2>edit</h2>
  <p>Some context. Right now, an error is thrown if I try to write a class instance
  to Firestore. To get around this, I've created a utility <code>get_database_version</code> method that copies
  a class instance down to a plain javascript object that can be written to Firestore.
  If that class instance has certain types of objects within it that Firestore
  treats as special (like Dates and firebase.firestore.FieldValue's), then I
  just copy the reference to those objects.</p>
  <h3>index.js</h3>
  <pre><code>class Utils {
    static get_database_version(inst, fb) {
      if (Utils.is_function(inst)) { Error(); }
  
      /* make sure some firebase is defined. it could be client or admin. */
      if (!Utils.is_object(fb) || Utils.is_undefined(fb.firestore)) { Error(); }
  
      /* if its a basic primitive just return it. */
      if (!Utils.is_null(inst) &amp;&amp; !Utils.is_undefined(inst) &amp;&amp; !Utils.is_object(inst)) {
        return inst;
      }
  
      /* return undefined instead of nulls */
      if (Utils.is_null(inst) || Utils.is_undefined(inst)) { return undefined; }
  
      /* preserve Date instances for firestore writes. */
      if (inst instanceof Date) { return inst; }
  
      /* preserve fb.firestore.FieldValue instances for firestore writes. */
      if (inst instanceof fb.firestore.FieldValue) { return inst; }
  
      /* possible return object */
      var obj = {};
  
      /* inst must be an object at this point, so copy it, stripping it of its class. */
      for (var key in inst) {
        if (!inst.hasOwnProperty(key)) { continue; }
  
        /* recurse on every value on this object */
        var res = Utils.get_database_version(inst[key], fb);
  
        /* only add it to the return object if its defined. */
        if (Utils.is_defined(res)) { obj[key] = res; }
      }
      /* i dont want blank objects going to firestore, so make sure it isnt. */
      return Utils.key_count(obj) ? obj : undefined;
    };
  
    static is_object(value) { return value !== null &amp;&amp; (value instanceof Object); };
    static is_function(value) { return typeof value === 'function'; };
    static is_defined(value) { return typeof value !== 'undefined'; };
    static is_undefined(value) { return !Utils.is_defined(value); };
    static is_null(some_var) { return some_var === null; };
    static key_count(some_object) {
      if (!Utils.is_object(some_object)) { throw Error('must pass in an object!'); }
      return Object.keys(some_object).length;
    };
  };
  
  module.exports = Utils;
  </code></pre>
  <p>One sample input/output:</p>
  <pre><code>var obj = {
    a: undefined,
    b: 55,
    c: {
      d: {},
      e: {
        f: 10
      }
    }
  };
  console.log(Utils.get_database_version(new Foo(obj), firebase))
  
  //output is an object stripped of Foo
  //{
  //  b: 55,
  //  c: {
  //    e: {
  //      f: 10
  //    }
  //  }
  //}
  </code></pre>