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

Unexpected semantics for util.is... #2197

Closed
deian opened this issue Jul 16, 2015 · 5 comments
Closed

Unexpected semantics for util.is... #2197

deian opened this issue Jul 16, 2015 · 5 comments
Labels
util Issues and PRs related to the built-in util module.

Comments

@deian
Copy link
Member

deian commented Jul 16, 2015

The semantics for the util is... functions are somewhat unexpected. For example, the documentation for util.isRegExp says:

Returns true if the given "object" is a RegExp. false otherwise.

But I'm seeing that util.isRegExp(Object.create(RegExp.prototype)) === false.
Can we clarify the documentation to explain why this is not a RegExp or change the semantics of isRegExp please? (Same goes with the other is functions.) Thanks.

@deian deian changed the title Unexpected semantics for "is..." Unexpected semantics for util.is... Jul 16, 2015
@brendanashworth brendanashworth added the util Issues and PRs related to the built-in util module. label Jul 17, 2015
@Fishrock123
Copy link
Contributor

The semantics almost certainly won't change. It is suggested you don't use these functions; I've been meaning to put deprecation notices into the docs.

How do you suggest we clarify that that won't work?

For posterity, the internals use Object.prototype.toString.call(input) === '[object RegExp]', since other methods can be bypassed, and this is the most reliable.

@Fishrock123
Copy link
Contributor

See also #2201

@Ginden
Copy link

Ginden commented Jul 26, 2015

For posterity, the internals use Object.prototype.toString.call(input) === '[object RegExp]', since other methods can be bypassed, and this is the most reliable.

Symbol.toStringTag is implemented in Chrome and avalaible in io.js behind --es_staging flag.

Anyway - Object.create(RegExp.prototype) is not a RegExp.

> p = Object.create(RegExp.prototype);
 {}
> p.compile()
illegal access
> p.source
'(?:)'
> p.test('aaa')
TypeError: Method RegExp.prototype.test called on incompatible receiver [object Object]

ES6 allows to create classes extending RegExps, but it isn't implemented in any engine yet.

@deian
Copy link
Member Author

deian commented Jul 27, 2015

@Fishrock123 thanks. I think the deprecation notice is a fine solution.

I'm not sure if it helps, but saying that the function returns true for objects created with the RegExp constructor would also be clear. (@Ginden a description what is not a RegExp is not necessarily useful.)

@Fishrock123
Copy link
Contributor

Deprecated in the docs as of 93e2830, see #2447

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

4 participants