Skip to content

Commit

Permalink
util: add util.types.isBoxedPrimitive
Browse files Browse the repository at this point in the history
Checking all boxed primitives individually requires to cross the C++
barrier multiple times besides being more complicated than just a
single check.

PR-URL: #22620
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: John-David Dalton <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and targos committed Sep 6, 2018
1 parent 7c831b7 commit 8cfa88a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
27 changes: 24 additions & 3 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -1054,10 +1054,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.
[
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

0 comments on commit 8cfa88a

Please sign in to comment.