time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (2800B)


      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 MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' );
     24 var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
     25 var isEnumerableProperty = require( '@stdlib/assert/is-enumerable-property' );
     26 var getOwnPropertyNames = require( './../../property-names' );
     27 var getOwnPropertySymbols = require( './../../property-symbols' );
     28 var getPrototypeOf = require( './../../get-prototype-of' );
     29 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Returns an array of an object's inherited enumerable property names and symbols.
     36 *
     37 * @param {*} value - input object
     38 * @param {PositiveInteger} [level] - inheritance level
     39 * @throws {TypeError} second argument must be a positive integer
     40 * @returns {Array} a list of inherited enumerable property names and symbols
     41 *
     42 * @example
     43 * var props = inheritedEnumerableProperties( {} );
     44 */
     45 function inheritedEnumerableProperties( value, level ) { // eslint-disable-line id-length
     46 	var props;
     47 	var cache;
     48 	var obj;
     49 	var tmp;
     50 	var N;
     51 	var n;
     52 	var k;
     53 	var i;
     54 
     55 	if ( arguments.length > 1 ) {
     56 		if ( !isPositiveInteger( level ) ) {
     57 			throw new TypeError( 'invalid argument. Second argument must be a positive integer. Value: `' + level + '`.' );
     58 		}
     59 		N = level;
     60 	} else {
     61 		N = MAX_SAFE_INTEGER;
     62 	}
     63 	if ( value === null || value === void 0 ) {
     64 		return [];
     65 	}
     66 	// Get the value's prototype:
     67 	obj = getPrototypeOf( value );
     68 
     69 	// Walk the prototype chain collecting enumerable properties...
     70 	props = [];
     71 	cache = {};
     72 	n = 1;
     73 	while ( obj && n <= N ) {
     74 		tmp = getOwnPropertyNames( obj );
     75 		for ( i = 0; i < tmp.length; i++ ) {
     76 			k = tmp[ i ];
     77 			if (
     78 				hasOwnProp( cache, k ) === false &&
     79 				isEnumerableProperty( obj, k )
     80 			) {
     81 				props.push( k );
     82 			}
     83 			cache[ k ] = true;
     84 		}
     85 		tmp = getOwnPropertySymbols( obj );
     86 		for ( i = 0; i < tmp.length; i++ ) {
     87 			k = tmp[ i ];
     88 			if (
     89 				hasOwnProp( cache, k ) === false &&
     90 				isEnumerableProperty( obj, k )
     91 			) {
     92 				props.push( k );
     93 			}
     94 			cache[ k ] = true;
     95 		}
     96 		obj = getPrototypeOf( obj );
     97 		n += 1;
     98 	}
     99 
    100 	return props;
    101 }
    102 
    103 
    104 // EXPORTS //
    105 
    106 module.exports = inheritedEnumerableProperties;