diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-08-26 13:46:24 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-09-05 20:53:59 +0200 |
commit | 3209679b7f9ec9bd3ffc6cf2c56c6c5583be6b87 (patch) | |
tree | 3c93455f54a38550f583dc0cc5eec0d4c687584e | |
parent | c9d6e3ff04c4f16eacc0a9756d4c464888444d22 (diff) | |
download | android-node-v8-3209679b7f9ec9bd3ffc6cf2c56c6c5583be6b87.tar.gz android-node-v8-3209679b7f9ec9bd3ffc6cf2c56c6c5583be6b87.tar.bz2 android-node-v8-3209679b7f9ec9bd3ffc6cf2c56c6c5583be6b87.zip |
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: https://github.com/nodejs/node/pull/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>
-rw-r--r-- | doc/api/util.md | 27 | ||||
-rw-r--r-- | src/node_types.cc | 10 | ||||
-rw-r--r-- | test/parallel/test-util-types.js | 12 |
3 files changed, 45 insertions, 4 deletions
diff --git a/doc/api/util.md b/doc/api/util.md index a9340847c5..44495e977d 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) +<!-- 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) diff --git a/src/node_types.cc b/src/node_types.cc index 4872491c92..97f6ef4e28 100644 --- a/src/node_types.cc +++ b/src/node_types.cc @@ -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) { @@ -63,6 +72,7 @@ void InitializeTypes(Local<Object> 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 d3ea866dea..2978cbf98d 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'))); |