diff --git a/doc/api/util.md b/doc/api/util.md index a9340847c52551..44495e977d13c3 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -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) + + +* `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) diff --git a/src/node_types.cc b/src/node_types.cc index 4872491c924989..97f6ef4e28c13c 100644 --- a/src/node_types.cc +++ b/src/node_types.cc @@ -51,6 +51,15 @@ static void IsAnyArrayBuffer(const FunctionCallbackInfo& args) { args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer()); } +static void IsBoxedPrimitive(const FunctionCallbackInfo& args) { + args.GetReturnValue().Set( + args[0]->IsNumberObject() || + args[0]->IsStringObject() || + args[0]->IsBooleanObject() || + args[0]->IsBigIntObject() || + args[0]->IsSymbolObject()); +} + void InitializeTypes(Local target, Local unused, Local context) { @@ -63,6 +72,7 @@ void InitializeTypes(Local target, #undef V env->SetMethodNoSideEffect(target, "isAnyArrayBuffer", IsAnyArrayBuffer); + env->SetMethodNoSideEffect(target, "isBoxedPrimitive", IsBoxedPrimitive); } } // anonymous namespace diff --git a/test/parallel/test-util-types.js b/test/parallel/test-util-types.js index d3ea866dea9580..2978cbf98deb0c 100644 --- a/test/parallel/test-util-types.js +++ b/test/parallel/test-util-types.js @@ -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; } @@ -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')));