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: add util.types.isBoxedPrimitive #22620

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -1049,10 +1049,31 @@ by `new Boolean()`.
```js
util.types.isBooleanObject(false); // Returns false
util.types.isBooleanObject(true); // Returns false
util.types.isBooleanObject(new Boolean(false)); // Returns true
util.types.isBooleanObject(new Boolean(true)); // Returns true
util.types.isBooleanObject(new Boolean(false)); // Returns true
util.types.isBooleanObject(new Boolean(true)); // Returns true
util.types.isBooleanObject(Boolean(false)); // Returns false
util.types.isBooleanObject(Boolean(true)); // Returns false
util.types.isBooleanObject(Boolean(true)); // Returns false
```

### util.types.isBoxedPrimitive(value)
<!-- YAML
added: REPLACEME
-->

* `value` {any}
* Returns: {boolean}

Returns `true` if the value is any boxed primitive object, e.g. created
by `new Boolean()`, `new String()` or `Object(Symbol())`.

For example:

```js
util.types.isBoxedPrimitive(false); // Returns false
util.types.isBoxedPrimitive(new Boolean(false)); // Returns true
util.types.isBoxedPrimitive(Symbol('foo')); // Returns false
util.types.isBoxedPrimitive(Object(Symbol('foo'))); // Returns true
util.types.isBoxedPrimitive(Object(BigInt(5))); // Returns true
```

### util.types.isDataView(value)
Expand Down
10 changes: 10 additions & 0 deletions src/node_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ static void IsAnyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer());
}

static void IsBoxedPrimitive(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(
args[0]->IsNumberObject() ||
args[0]->IsStringObject() ||
args[0]->IsBooleanObject() ||
args[0]->IsBigIntObject() ||
args[0]->IsSymbolObject());
}

void InitializeTypes(Local<Object> target,
Local<Value> unused,
Local<Context> context) {
Expand All @@ -63,6 +72,7 @@ void InitializeTypes(Local<Object> target,
#undef V

env->SetMethodNoSideEffect(target, "isAnyArrayBuffer", IsAnyArrayBuffer);
env->SetMethodNoSideEffect(target, "isBoxedPrimitive", IsBoxedPrimitive);
}

} // anonymous namespace
Expand Down
12 changes: 11 additions & 1 deletion test/parallel/test-util-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ for (const [ value, _method ] of [

for (const key of Object.keys(types)) {
if ((types.isArrayBufferView(value) ||
types.isAnyArrayBuffer(value)) && key.includes('Array')) {
types.isAnyArrayBuffer(value)) && key.includes('Array') ||
key === 'isBoxedPrimitive') {
continue;
}

Expand All @@ -68,6 +69,15 @@ for (const [ value, _method ] of [
}
}

// Check boxed primitives.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add some negative results to the test?

Copy link
Member Author

Choose a reason for hiding this comment

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

This seems somewhat redundant to me, since this is just a helper combined out of different helpers that are already tested against all other types.
But I'll add negative results as well if you feel strongly about it!

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 makes sense to test - but don't feel strongly about it.

[
new Boolean(),
new Number(),
new String(),
Object(Symbol()),
Object(BigInt(0))
].forEach((entry) => assert(types.isBoxedPrimitive(entry)));

{
assert(!types.isUint8Array({ [Symbol.toStringTag]: 'Uint8Array' }));
assert(types.isUint8Array(vm.runInNewContext('new Uint8Array')));
Expand Down