Skip to content

Commit 8cfa88a

Browse files
BridgeARtargos
authored andcommitted
util: add util.types.isBoxedPrimitive
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 <anna@addaleax.net> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 7c831b7 commit 8cfa88a

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎doc/api/util.md‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,10 +1054,31 @@ by `new Boolean()`.
10541054
```js
10551055
util.types.isBooleanObject(false); // Returns false
10561056
util.types.isBooleanObject(true); // Returns false
1057-
util.types.isBooleanObject(newBoolean(false)); // Returns true
1058-
util.types.isBooleanObject(newBoolean(true)); // Returns true
1057+
util.types.isBooleanObject(newBoolean(false)); // Returns true
1058+
util.types.isBooleanObject(newBoolean(true)); // Returns true
10591059
util.types.isBooleanObject(Boolean(false)); // Returns false
1060-
util.types.isBooleanObject(Boolean(true)); // Returns false
1060+
util.types.isBooleanObject(Boolean(true)); // Returns false
1061+
```
1062+
1063+
### util.types.isBoxedPrimitive(value)
1064+
<!-- YAML
1065+
added: REPLACEME
1066+
-->
1067+
1068+
*`value` {any}
1069+
* Returns: {boolean}
1070+
1071+
Returns `true` if the value is any boxed primitive object, e.g. created
1072+
by `new Boolean()`, `new String()` or `Object(Symbol())`.
1073+
1074+
For example:
1075+
1076+
```js
1077+
util.types.isBoxedPrimitive(false); // Returns false
1078+
util.types.isBoxedPrimitive(newBoolean(false)); // Returns true
1079+
util.types.isBoxedPrimitive(Symbol('foo')); // Returns false
1080+
util.types.isBoxedPrimitive(Object(Symbol('foo'))); // Returns true
1081+
util.types.isBoxedPrimitive(Object(BigInt(5))); // Returns true
10611082
```
10621083

10631084
### util.types.isDataView(value)

‎src/node_types.cc‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ static void IsAnyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
5151
args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer());
5252
}
5353

54+
staticvoidIsBoxedPrimitive(const FunctionCallbackInfo<Value>& args) {
55+
args.GetReturnValue().Set(
56+
args[0]->IsNumberObject() ||
57+
args[0]->IsStringObject() ||
58+
args[0]->IsBooleanObject() ||
59+
args[0]->IsBigIntObject() ||
60+
args[0]->IsSymbolObject());
61+
}
62+
5463
voidInitializeTypes(Local<Object> target,
5564
Local<Value> unused,
5665
Local<Context> context) {
@@ -63,6 +72,7 @@ void InitializeTypes(Local<Object> target,
6372
#undef V
6473

6574
env->SetMethodNoSideEffect(target, "isAnyArrayBuffer", IsAnyArrayBuffer);
75+
env->SetMethodNoSideEffect(target, "isBoxedPrimitive", IsBoxedPrimitive);
6676
}
6777

6878
} // anonymous namespace

‎test/parallel/test-util-types.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ for (const [ value, _method ] of [
5757

5858
for(constkeyofObject.keys(types)){
5959
if((types.isArrayBufferView(value)||
60-
types.isAnyArrayBuffer(value))&&key.includes('Array')){
60+
types.isAnyArrayBuffer(value))&&key.includes('Array')||
61+
key==='isBoxedPrimitive'){
6162
continue;
6263
}
6364

@@ -68,6 +69,15 @@ for (const [ value, _method ] of [
6869
}
6970
}
7071

72+
// Check boxed primitives.
73+
[
74+
newBoolean(),
75+
newNumber(),
76+
newString(),
77+
Object(Symbol()),
78+
Object(BigInt(0))
79+
].forEach((entry)=>assert(types.isBoxedPrimitive(entry)));
80+
7181
{
7282
assert(!types.isUint8Array({[Symbol.toStringTag]: 'Uint8Array'}));
7383
assert(types.isUint8Array(vm.runInNewContext('new Uint8Array')));

0 commit comments

Comments
 (0)