Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Conversation

@RaeesBhatti
Copy link
Contributor

Use lodash.isPlainObject instead of our own function.

@RaeesBhatti RaeesBhatti requested a review from ehmicky May 6, 2020 17:51
@ehmicky
Copy link
Contributor

ehmicky commented May 6, 2020

Should we use is-plain-obj instead? It is much less bloated code-wise. The only difference functionality-wise is that Lodash handles IE<9 weird "host objects" (which we don't need for this package).

is-plain-obj:

module.exports = value => {
	if (Object.prototype.toString.call(value) !== '[object Object]') {
		return false;
	}

	const prototype = Object.getPrototypeOf(value);
	return prototype === null || prototype === Object.prototype;
};

lodash.isplainobject (simplified):

module.exports = function isPlainObject(value) {
  if (!value || typeof value !== 'object' || Object.prototype.toString.call(value) != '[object Object]' || isHostObject(value)) {
    return false;
  }
  var proto = Object.getPrototypeOf(Object(value));
  if (proto === null) {
    return true;
  }
  var Ctor = Object.prototype.hasOwnProperty.call(proto, 'constructor') && proto.constructor;
  return (typeof Ctor == 'function' && Ctor instanceof Ctor && Function.prototype.toString.call(Ctor) == Function.prototype.toString.call(Object));
}

function isHostObject(value) {
  var result = false;
  if (value != null && typeof value.toString != 'function') {
    try {
      result = !!(value + '');
    } catch (e) {}
  }
  return result;
}

lodash.isplainobject:

var objectTag = '[object Object]';

function isHostObject(value) {
  // Many host objects are `Object` objects that can coerce to strings
  // despite having improperly defined `toString` methods.
  var result = false;
  if (value != null && typeof value.toString != 'function') {
    try {
      result = !!(value + '');
    } catch (e) {}
  }
  return result;
}

function overArg(func, transform) {
  return function(arg) {
    return func(transform(arg));
  };
}

var funcProto = Function.prototype,
    objectProto = Object.prototype;

var funcToString = funcProto.toString;

var hasOwnProperty = objectProto.hasOwnProperty;

var objectCtorString = funcToString.call(Object);

var objectToString = objectProto.toString;

var getPrototype = overArg(Object.getPrototypeOf, Object);

function isObjectLike(value) {
  return !!value && typeof value == 'object';
}

function isPlainObject(value) {
  if (!isObjectLike(value) ||
      objectToString.call(value) != objectTag || isHostObject(value)) {
    return false;
  }
  var proto = getPrototype(value);
  if (proto === null) {
    return true;
  }
  var Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor;
  return (typeof Ctor == 'function' &&
    Ctor instanceof Ctor && funcToString.call(Ctor) == objectCtorString);
}

module.exports = isPlainObject;

@RaeesBhatti
Copy link
Contributor Author

RaeesBhatti commented May 6, 2020

Honestly, I would prefer the more thorough approach just to be sure especially since lodash.isPlainObject doesn't have any dependencies.

@ehmicky
Copy link
Contributor

ehmicky commented May 6, 2020

I think it's more complicatedly written, not more thorough. For the same inputs, it returns the same outputs except for unpractical edge cases (like IE<9 host objects).

However, either library should do the job 👍

@RaeesBhatti RaeesBhatti merged commit 132d510 into master May 6, 2020
@RaeesBhatti RaeesBhatti deleted the raees/plainobject branch May 6, 2020 19:10
@RaeesBhatti RaeesBhatti restored the raees/plainobject branch May 6, 2020 19:10
@RaeesBhatti RaeesBhatti deleted the raees/plainobject branch May 6, 2020 19:10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants