time-to-botec

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

polyfill.js (2113B)


      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 hasOwnProp = require( '@stdlib/assert/has-own-property' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Returns a property descriptor for an object's own property.
     30 *
     31 * ## Notes
     32 *
     33 * -   In contrast to the built-in `Object.getOwnPropertyDescriptor()`, this function returns `null` if provided `undefined` or `null`, rather than throwing an error.
     34 * -   In contrast to the built-in `Object.getOwnPropertyDescriptor()`, this function returns `null` if an object does not have a provided property, rather than `undefined`.
     35 * -   In environments lacking `Object.getOwnPropertyDescriptor()` support, property descriptors do not exist. In non-supporting environment, if an object has a provided property, this function returns a descriptor object equivalent to that returned in a supporting environment; otherwise, the function returns `null`.
     36 *
     37 * @private
     38 * @param {*} value - input object
     39 * @param {(string|symbol)} property - property
     40 * @returns {(Object|null)} property descriptor or null
     41 *
     42 * @example
     43 * var obj = {
     44 *     'beep': 'boop',
     45 *     'foo': 3.14
     46 * };
     47 *
     48 * var desc = getOwnPropertyDescriptor( obj, 'foo' );
     49 * // returns {'configurable':true,'enumerable':true,'writable':true,'value':3.14}
     50 */
     51 function getOwnPropertyDescriptor( value, property ) {
     52 	if ( hasOwnProp( value, property ) ) {
     53 		return {
     54 			'configurable': true,
     55 			'enumerable': true,
     56 			'writable': true,
     57 			'value': value[ property ]
     58 		};
     59 	}
     60 	return null;
     61 }
     62 
     63 
     64 // EXPORTS //
     65 
     66 module.exports = getOwnPropertyDescriptor;