time-to-botec

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

main.js (2572B)


      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 getOwnPropertyDescriptor = require( './../../property-descriptor' );
     26 var getPrototypeOf = require( './../../get-prototype-of' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Returns a property descriptor for an object's inherited property.
     33 *
     34 * ## Notes
     35 *
     36 * -   In contrast to the built-in `Object.getOwnPropertyDescriptor()`, this function returns `null` if provided `undefined` or `null`, rather than throwing an error.
     37 * -   In contrast to the built-in `Object.getOwnPropertyDescriptor()`, this function returns `null` if an object does not have an inherited provided property, rather than `undefined`.
     38 *
     39 * @private
     40 * @param {*} value - input object
     41 * @param {(string|symbol)} property - property
     42 * @param {PositiveInteger} [level] - inheritance level
     43 * @throws {TypeError} third argument must be a positive integer
     44 * @returns {(Object|null)} property descriptor or null
     45 *
     46 * @example
     47 * var desc = inheritedPropertyDescriptor( {}, 'toString' );
     48 * // returns {...}
     49 */
     50 function inheritedPropertyDescriptor( value, property, level ) { // eslint-disable-line id-length
     51 	var desc;
     52 	var obj;
     53 	var N;
     54 	var n;
     55 	if ( arguments.length > 2 ) {
     56 		if ( !isPositiveInteger( level ) ) {
     57 			throw new TypeError( 'invalid argument. Third 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 null;
     65 	}
     66 	// Get the value's prototype:
     67 	obj = getPrototypeOf( value );
     68 
     69 	// Walk the prototype chain in search of a specified property...
     70 	n = 1;
     71 	while ( obj && n <= N ) {
     72 		desc = getOwnPropertyDescriptor( obj, property );
     73 		if ( desc ) {
     74 			return desc;
     75 		}
     76 		obj = getPrototypeOf( obj );
     77 		n += 1;
     78 	}
     79 	return null;
     80 }
     81 
     82 
     83 // EXPORTS //
     84 
     85 module.exports = inheritedPropertyDescriptor;