Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: simplify isPrimitve #739

Closed
wants to merge 1 commit into from

Conversation

vkurchatkin
Copy link
Contributor

Everything that is not an object should be a primitive.

R=@trevnorris

@seishun
Copy link
Contributor

seishun commented Feb 6, 2015

How about something simpler still:

return Object(arg) !== arg;

@micnic
Copy link
Contributor

micnic commented Feb 6, 2015

Or

return arg === null || typeof arg !== 'object';

@seishun
Copy link
Contributor

seishun commented Feb 6, 2015

@micnic that would return true for functions.

@micnic
Copy link
Contributor

micnic commented Feb 6, 2015

@seishun, oops, I read in the commit description Everything that is not an object should be a primitive..

@vkurchatkin
Copy link
Contributor Author

@seishun looks neat, but probably involves unnecessary allocation

@vkurchatkin
Copy link
Contributor Author

@seishun Here is how it's implemented in v8:

function ToObject(x) {
  if (IS_STRING(x)) return new $String(x);
  if (IS_NUMBER(x)) return new $Number(x);
  if (IS_BOOLEAN(x)) return new $Boolean(x);
  if (IS_SYMBOL(x)) return %NewSymbolWrapper(x);
  if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
    throw %MakeTypeError('undefined_or_null_to_object', []);
  }
  return x;
}

@zloirock
Copy link

zloirock commented Feb 6, 2015

@trevnorris
Copy link
Contributor

I can't find any failures for edge cases. LGTM

typeof arg === 'symbol' || // ES6 symbol
typeof arg === 'undefined';
typeof arg !== 'object' &&
typeof arg !== 'function';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it fits on one line now?

@bnoordhuis
Copy link
Member

LGTM but there's a typo in the commit log: s/isPrimitve/isPrimitive/

Everything that is not an object should be a primitive.
vkurchatkin added a commit that referenced this pull request Feb 7, 2015
Everything that is not an object should be a primitive.

PR-URL: #739

Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
@vkurchatkin
Copy link
Contributor Author

Fixed everything and landed b9d3928

@vkurchatkin vkurchatkin closed this Feb 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants