time-to-botec

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

main.js (2610B)


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