time-to-botec

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

polyfill.js (2804B)


      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 propertyNames = require( './../../property-names' );
     24 var propertySymbols = require( './../../property-symbols' );
     25 var propertyDescriptor = require( './../../property-descriptor' );
     26 var defineProperty = require( './../../define-property' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Returns an object's own property descriptors.
     33 *
     34 * ## Notes
     35 *
     36 * -   In contrast to the built-in `Object.getOwnPropertyDescriptors()`, this function returns an empty object if provided `undefined` or `null`, rather than throwing an error.
     37 *
     38 * @private
     39 * @param {*} value - input object
     40 * @returns {Object} property descriptors
     41 *
     42 * @example
     43 * var obj = {
     44 *     'beep': 'boop',
     45 *     'foo': 3.14
     46 * };
     47 *
     48 * var desc = getOwnPropertyDescriptors( obj, 'foo' );
     49 * // returns {...}
     50 */
     51 function getOwnPropertyDescriptors( value ) {
     52 	var symbols;
     53 	var names;
     54 	var desc;
     55 	var out;
     56 	var i;
     57 
     58 	out = {};
     59 
     60 	// Get the value's own enumerable and non-enumerable properties:
     61 	names = propertyNames( value );
     62 
     63 	// For each property name, retrieve the property descriptor...
     64 	for ( i = 0; i < names.length; i++ ) {
     65 		desc = propertyDescriptor( value, names[ i ] );
     66 		if ( desc ) {
     67 			// The following is equivalent to `out[ names[i] ] = desc`, but accounts for the possibility of a "poisoned" `Object` prototype (i.e., an `Object.prototype` having a property with a setter which throws).
     68 			defineProperty( out, names[ i ], {
     69 				'configurable': true,
     70 				'enumerable': true,
     71 				'writable': true,
     72 				'value': desc
     73 			});
     74 		}
     75 	}
     76 
     77 	// Get the value's symbol properties:
     78 	symbols = propertySymbols( value );
     79 
     80 	// For each symbol property, retrieve the property descriptor...
     81 	for ( i = 0; i < symbols.length; i++ ) {
     82 		desc = propertyDescriptor( value, symbols[ i ] );
     83 		if ( desc ) {
     84 			// The following is equivalent to `out[ symbols[i] ] = desc`, but accounts for the possibility of a "poisoned" `Object` prototype (i.e., an `Object.prototype` having a property with a setter which throws).
     85 			defineProperty( out, symbols[ i ], {
     86 				'configurable': true,
     87 				'enumerable': true,
     88 				'writable': true,
     89 				'value': desc
     90 			});
     91 		}
     92 	}
     93 
     94 	return out;
     95 }
     96 
     97 
     98 // EXPORTS //
     99 
    100 module.exports = getOwnPropertyDescriptors;