polyfill.js (3019B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 // MODULES // 22 23 var isObjectLike = require( '@stdlib/assert/is-object-like' ); 24 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 25 var isArguments = require( '@stdlib/assert/is-arguments' ); 26 var HAS_ENUM_PROTO_BUG = require( './has_enumerable_prototype_bug.js' ); 27 var HAS_NON_ENUM_PROPS_BUG = require( './has_non_enumerable_properties_bug.js' ); 28 var isConstructorPrototype = require( './is_constructor_prototype_wrapper.js' ); 29 var NON_ENUMERABLE = require( './non_enumerable.json' ); 30 31 32 // MAIN // 33 34 /** 35 * Returns an array of an object's own enumerable property names. 36 * 37 * @private 38 * @param {*} value - input object 39 * @returns {Array} a list of own enumerable property names 40 * 41 * @example 42 * var obj = { 43 * 'beep': 'boop', 44 * 'foo': 3.14 45 * }; 46 * 47 * var k = keys( obj ); 48 * // e.g., returns [ 'beep', 'foo' ] 49 */ 50 function keys( value ) { 51 var skipConstructor; 52 var skipPrototype; 53 var isFcn; 54 var out; 55 var k; 56 var p; 57 var i; 58 59 out = []; 60 if ( isArguments( value ) ) { 61 // Account for environments which treat `arguments` differently... 62 for ( i = 0; i < value.length; i++ ) { 63 out.push( i.toString() ); 64 } 65 // Note: yes, we are precluding the `arguments` array-like object from having other enumerable properties; however, this should (1) be very rare and (2) not be encouraged (e.g., doing something like `arguments.a = 'b'`; in certain engines directly manipulating the `arguments` value results in automatic de-optimization). 66 return out; 67 } 68 if ( typeof value === 'string' ) { 69 // Account for environments which do not treat string character indices as "own" properties... 70 if ( value.length > 0 && !hasOwnProp( value, '0' ) ) { 71 for ( i = 0; i < value.length; i++ ) { 72 out.push( i.toString() ); 73 } 74 } 75 } else { 76 isFcn = ( typeof value === 'function' ); 77 if ( isFcn === false && !isObjectLike( value ) ) { 78 return out; 79 } 80 skipPrototype = ( HAS_ENUM_PROTO_BUG && isFcn ); 81 } 82 for ( k in value ) { 83 if ( !( skipPrototype && k === 'prototype' ) && hasOwnProp( value, k ) ) { 84 out.push( String( k ) ); 85 } 86 } 87 if ( HAS_NON_ENUM_PROPS_BUG ) { 88 skipConstructor = isConstructorPrototype( value ); 89 for ( i = 0; i < NON_ENUMERABLE.length; i++ ) { 90 p = NON_ENUMERABLE[ i ]; 91 if ( !( skipConstructor && p === 'constructor' ) && hasOwnProp( value, p ) ) { 92 out.push( String( p ) ); 93 } 94 } 95 } 96 return out; 97 } 98 99 100 // EXPORTS // 101 102 module.exports = keys;