objectid.js
function ObjectID(arg) {
  if(!(this instanceof ObjectID)) return new ObjectID(arg);
  if(arg && ((arg instanceof ObjectID) || arg._bsontype==="ObjectID"))
    return arg;

  var buf;

  if(isBuffer(arg) || (Array.isArray(arg) && arg.length===12)) {
    buf = Array.prototype.slice.call(arg);
  }
  else if(typeof arg === "string") {
    if(arg.length!==12 && !ObjectID.isValid(arg))
      throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");

    buf = buffer(arg);
  }
  else if(/number|undefined/.test(typeof arg)) {
    buf = buffer(generate(arg));
  }

  Object.defineProperty(this, "id", {
    enumerable: true,
    get: function() { return String.fromCharCode.apply(this, buf); }
  });
  Object.defineProperty(this, "str", {
    get: function() { return buf.map(hex.bind(this, 2)).join(''); }
  });
}
function ObjectID(arg) {
  if(!(this instanceof ObjectID)) return new ObjectID(arg);
  if(arg && ((arg instanceof ObjectID) || arg._bsontype==="ObjectID"))
    return arg;

  var buf;

  if(isBuffer(arg) || (Array.isArray(arg) && arg.length===12)) {
    buf = Array.prototype.slice.call(arg);
  }
  else if(typeof arg === "string") {
    if(arg.length!==12 && !ObjectID.isValid(arg))
      throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");

    buf = buffer(arg);
  }
  else if(/number|undefined/.test(typeof arg)) {
    buf = buffer(generate(arg));
  }

  Object.defineProperty(this, "id", {
    enumerable: true,
    get: function() { return String.fromCharCode.apply(this, buf); }
  });
  Object.defineProperty(this, "str", {
    get: function() { return buf.map(hex.bind(this, 2)).join(''); }
  });
}
function ObjectID ( arg ) { if ( ! ( this instanceof ObjectID ) ) return new ObjectID ( arg ) ; if ( arg && ( ( arg instanceof ObjectID ) || arg . _bsontype === "ObjectID" ) ) return arg ; var buf ; if ( isBuffer ( arg ) || ( Array . isArray ( arg ) && arg . length === 12 ) ) { buf = Array . prototype . slice . call ( arg ) ; } else if ( typeof arg === "string" ) { if ( arg . length !== 12 && ! ObjectID . isValid ( arg ) ) throw new Error ( "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters" ) ; buf = buffer ( arg ) ; } else if ( / number|undefined / . test ( typeof arg ) ) { buf = buffer ( generate ( arg ) ) ; } Object . defineProperty ( this , "id" , { enumerable : true , get : function ( ) { return String . fromCharCode . apply ( this , buf ) ; } } ) ; Object . defineProperty ( this , "str" , { get : function ( ) { return buf . map ( hex . bind ( this , 2 ) ) . join ( '' ) ; } } ) ; }
Create a new immutable ObjectID instance

@class Represents the BSON ObjectID type
@param {String|Number} arg Can be a 24 byte hex string, 12 byte binary string or a Number.
@return {Object} instance of ObjectID.
Create a new immutable ObjectID instance