Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MAIN //

var bool = ( typeof Object.prototype.propertyIsEnumerable === 'function' );


// EXPORTS //

module.exports = bool;
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var bool;
* @returns {boolean} boolean indicating whether an environment has the bug
*/
function detect() {
if ( typeof isEnum !== 'function' ) {
return false;
}
return !isEnum.call( 'beep', '0' );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,21 @@

// MODULES //

var HAS_BUILTIN = require( './has_builtin.js' );
var polyfill = require( './polyfill.js' );
var main = require( './main.js' );


// MAIN //

var isEnumerableProperty;
if ( HAS_BUILTIN ) {
isEnumerableProperty = main;
} else {
isEnumerableProperty = polyfill;
}


// EXPORTS //

module.exports = main;
module.exports = isEnumerableProperty;
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var Object = require( '@stdlib/object/ctor' );


// VARIABLES //

// NOTE: we explicitly avoid using `@stdlib/utils/keys` in order to avoid circular dependencies, as that package depends on this one.
var objectKeys = Object.keys;


// MAIN //

/**
* Tests if an object's own property is enumerable.
*
* ## Notes
*
* - Enumerable own symbol properties are not detected (`Object.keys` cannot
* see symbols).
*
* @private
* @param {*} value - value to test
* @param {*} property - property to test
* @returns {boolean} boolean indicating if an object property is enumerable
*
* @example
* var beep = {
* 'boop': true
* };
*
* var bool = isEnumerableProperty( beep, 'boop' );
* // returns true
*
* @example
* var beep = {
* 'boop': true
* };
*
* var bool = isEnumerableProperty( beep, 'hasOwnProperty' );
* // returns false
*/
function isEnumerableProperty( value, property ) {
var keys;
var prop;
var i;

if (
value === void 0 ||
value === null
) {
return false;
}
if ( typeof property === 'symbol' ) {
return false;
}
keys = objectKeys( Object( value ) );
prop = String( property );
for ( i = 0; i < keys.length; i++ ) {
if ( keys[ i ] === prop ) {
return true;
}
}
return false;
}


// EXPORTS //

module.exports = isEnumerableProperty;
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

var tape = require( 'tape' );
var proxyquire = require( 'proxyquire' );
var polyfill = require( './../lib/polyfill.js' );
var main = require( './../lib/main.js' );
var isEnumerableProperty = require( './../lib' );


Expand Down Expand Up @@ -155,3 +157,50 @@ tape( 'property values are coerced to strings', function test( t ) {

t.end();
});

tape( 'the main export is a polyfill if an environment does not support native `propertyIsEnumerable`', function test( t ) {
var isEnumerableProperty = proxyquire( './../lib', {
'./has_builtin.js': false
});
t.strictEqual( isEnumerableProperty, polyfill, 'returns expected value' );
t.end();
});

tape( 'the main export is a wrapper around a builtin if an environment supports native `propertyIsEnumerable`', function test( t ) {
var isEnumerableProperty = proxyquire( './../lib', {
'./has_builtin.js': true
});
t.strictEqual( isEnumerableProperty, main, 'returns expected value' );
t.end();
});

tape( 'the function works when native `propertyIsEnumerable` is not supported (polyfill fallback)', function test( t ) {
var isEnumerableProperty = proxyquire( './../lib', {
'./has_builtin.js': false
});
var bool;

bool = isEnumerableProperty({
'a': 'b'
}, 'a' );
t.strictEqual( bool, true, 'returns expected value' );

bool = isEnumerableProperty({
'a': 'b'
}, 'c' );
t.strictEqual( bool, false, 'returns expected value' );

bool = isEnumerableProperty( [ 1, 2, 3 ], '1' );
t.strictEqual( bool, true, 'returns expected value' );

bool = isEnumerableProperty( {}, 'hasOwnProperty' );
t.strictEqual( bool, false, 'returns expected value' );

bool = isEnumerableProperty( null, 'beep' );
t.strictEqual( bool, false, 'returns expected value' );

bool = isEnumerableProperty( void 0, 'beep' );
t.strictEqual( bool, false, 'returns expected value' );

t.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var tape = require( 'tape' );
var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' );
var Symbol = require( '@stdlib/symbol/ctor' );
var polyfill = require( './../lib/polyfill.js' );


// VARIABLES //

var opts = {
'skip': !hasSymbolSupport()
};


// TESTS //

tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.strictEqual( typeof polyfill, 'function', 'main export is a function' );
t.end();
});

tape( 'the function returns `false` if provided `null` or `undefined`', function test( t ) {
var bool;

bool = polyfill( null, 'a' );
t.strictEqual( bool, false, 'returns false when null' );

bool = polyfill( void 0, 'a' );
t.strictEqual( bool, false, 'returns false when undefined' );

t.end();
});

tape( 'the function returns `true` if an object property is enumerable', function test( t ) {
var bool;
var obj;

obj = {
'a': 'b',
'c': 1
};
bool = polyfill( obj, 'a' );
t.strictEqual( bool, true, 'returns expected value' );

bool = polyfill( obj, 'c' );
t.strictEqual( bool, true, 'returns expected value' );

bool = polyfill( [ 1, 2, 3 ], '1' );
t.strictEqual( bool, true, 'returns expected value' );

bool = polyfill( [ 1, 2, 3 ], 1 );
t.strictEqual( bool, true, 'returns expected value' );

t.end();
});

tape( 'the function returns `false` if an object property is not enumerable', function test( t ) {
var bool;
var obj;

obj = {
'a': 'b'
};
bool = polyfill( obj, 'c' );
t.strictEqual( bool, false, 'returns expected value' );

bool = polyfill( [ 'a' ], 'length' );
t.strictEqual( bool, false, 'returns expected value' );

t.end();
});

tape( 'the function returns `false` if provided an inherited property', function test( t ) {
var bool;

bool = polyfill( {}, 'hasOwnProperty' );
t.strictEqual( bool, false, 'returns expected value' );

bool = polyfill( {}, 'toString' );
t.strictEqual( bool, false, 'returns expected value' );

bool = polyfill( {}, 'constructor' );
t.strictEqual( bool, false, 'returns expected value' );

t.end();
});

tape( 'the function returns `true` for enumerable string indices', function test( t ) {
var bool;

bool = polyfill( 'beep', '1' );
t.strictEqual( bool, true, 'returns expected value' );

bool = polyfill( 'beep', 1 );
t.strictEqual( bool, true, 'returns expected value' );

bool = polyfill( 'beep', 'length' );
t.strictEqual( bool, false, 'returns expected value' );

bool = polyfill( 'beep', '10' );
t.strictEqual( bool, false, 'returns expected value' );

t.end();
});

tape( 'the function coerces property values to strings', function test( t ) {
var bool;
var prop;
var obj;

obj = {
'null': true,
'[object Object]': true
};
bool = polyfill( obj, null );
t.strictEqual( bool, true, 'returns expected value' );

bool = polyfill( obj, {} );
t.strictEqual( bool, true, 'returns expected value' );

prop = {
'toString': function toString() {
return 'foo';
},
'valueOf': function valueOf() {
return 'bar';
}
};
obj = {
'foo': true,
'bar': false
};
bool = polyfill( obj, prop );
t.strictEqual( bool, true, 'returns expected value' );

t.end();
});

tape( 'the function returns `false` when provided a symbol', opts, function test( t ) {
var bool;

bool = polyfill( {}, Symbol( 'a' ) );
t.strictEqual( bool, false, 'returns expected value' );
t.end();
});
Loading
Loading