Skip to content

Commit addc2d7

Browse files
committed
Use Object.prototype.toString to resolve internal class
Resolves stdlib-js#375.
1 parent 8b42836 commit addc2d7

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

lib/node_modules/@stdlib/assert/is-node/lib/main.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var getGlobal = require( '@stdlib/utils/global' );
2525
var nativeClass = require( '@stdlib/utils/native-class' );
2626
var isObject = require( '@stdlib/assert/is-plain-object' );
2727
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
28+
var toStr = require( './to_string.js' );
2829
var globalScope = require( './global_scope.js' );
2930

3031

@@ -74,14 +75,8 @@ function isNode() {
7475
// Check for a `process` global variable:
7576
typeof proc === 'object' &&
7677

77-
// Check that the `process` global variable has the expected internal class:
78-
(
79-
// Node < v14.6.0
80-
nativeClass( proc ) === '[object process]' ||
81-
82-
// Node >= v14.6.0
83-
nativeClass( proc ) === '[object Object]'
84-
) &&
78+
// Check that the `process` global variable has the expected internal class (NOTE: we use `toStr`, rather than `nativecClass` to address changes introduced in Node >= v14.6.0; see https://github.com/stdlib-js/stdlib/issues/375):
79+
toStr( proc ) === '[object process]' &&
8580

8681
// Check for a `versions` property:
8782
isObject( proc.versions ) &&
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// VARIABLES //
22+
23+
var toStr = Object.prototype.toString;
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Returns the internal class of a provided value.
30+
*
31+
* @private
32+
* @param {*} value - input value
33+
* @returns {string} internal class
34+
*/
35+
function toString( value ) { // eslint-disable-line stdlib/no-redeclare
36+
return toStr.call( value );
37+
}
38+
39+
40+
// EXPORTS //
41+
42+
module.exports = toString;

lib/node_modules/@stdlib/assert/is-node/test/test.main.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ tape( 'the function returns `false` if runtime is not Node.js (`global` variable
128128
tape( 'the function returns `false` if runtime is not Node.js (`global` variable has wrong class)', function test( t ) {
129129
var isNode;
130130

131-
function nativeClass( val ) {
132-
if ( val === proc ) {
133-
return '[object process]';
134-
}
131+
function nativeClass() {
135132
return '[object beeeeeeep]';
136133
}
137134

@@ -183,12 +180,16 @@ tape( 'the function returns `false` if runtime is not Node.js (`global` variable
183180
tape( 'the function returns `false` if runtime is not Node.js (`process` variable has wrong class)', function test( t ) {
184181
var isNode;
185182

186-
function nativeClass( val ) {
183+
function nativeClass() {
184+
// Node.js version <7 (https://github.com/nodejs/node/issues/9274):
185+
return '[object global]';
186+
}
187+
188+
function toStr( val ) {
187189
if ( val === proc ) {
188190
return '[object beeeeeeep]';
189191
}
190-
// Node.js version <7 (https://github.com/nodejs/node/issues/9274):
191-
return '[object global]';
192+
return Object.prototype.toString.call( val );
192193
}
193194

194195
function isString() {}
@@ -202,7 +203,8 @@ tape( 'the function returns `false` if runtime is not Node.js (`process` variabl
202203
'@stdlib/utils/native-class': nativeClass,
203204
'@stdlib/assert/is-plain-object': alwaysTrue,
204205
'@stdlib/assert/is-string': isString,
205-
'./global_scope.js': true
206+
'./global_scope.js': true,
207+
'./to_string.js': toStr
206208
});
207209
t.equal( isNode(), false, 'returns false' );
208210
t.end();

0 commit comments

Comments
 (0)