time-to-botec

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

main.js (2756B)


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