time-to-botec

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

main.js (2298B)


      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 getOwnPropertyNames = require( './../../property-names' );
     24 var getPrototypeOf = require( './../../get-prototype-of' );
     25 
     26 
     27 // FUNCTIONS //
     28 
     29 /**
     30 * Returns a boolean indicating if an array contains a provided value.
     31 *
     32 * @private
     33 * @param {Array} arr - array
     34 * @param {*} v - search value
     35 * @returns {boolean} boolean indicating if an array contains a search value
     36 */
     37 function contains( arr, v ) {
     38 	var i;
     39 	for ( i = 0; i < arr.length; i++ ) {
     40 		if ( arr[ i ] === v ) {
     41 			return true;
     42 		}
     43 	}
     44 	return false;
     45 }
     46 
     47 
     48 // MAIN //
     49 
     50 /**
     51 * Returns an array of an object's own and inherited enumerable and non-enumerable property names.
     52 *
     53 * ## Notes
     54 *
     55 * -   In contrast to the built-in `Object.getOwnPropertyNames()`, this function returns an empty array if provided `undefined` or `null`, rather than throwing an error.
     56 *
     57 * @param {*} value - input object
     58 * @returns {Array} a list of own and inherited property names
     59 *
     60 * @example
     61 * var obj = {
     62 *     'beep': 'boop',
     63 *     'foo': 3.14
     64 * };
     65 *
     66 * var keys = propertyNamesIn( obj );
     67 * // e.g., returns [ 'beep', 'foo', ... ]
     68 */
     69 function propertyNamesIn( value ) {
     70 	var names;
     71 	var obj;
     72 	var tmp;
     73 	var i;
     74 
     75 	if ( value === null || value === void 0 ) {
     76 		return [];
     77 	}
     78 	// Cast the value to an object:
     79 	obj = Object( value );
     80 
     81 	// Walk the prototype chain collecting all enumerable and non-enumerable property names...
     82 	names = [];
     83 	do {
     84 		tmp = getOwnPropertyNames( obj );
     85 		for ( i = 0; i < tmp.length; i++ ) {
     86 			if ( contains( names, tmp[ i ] ) === false ) {
     87 				names.push( tmp[ i ] );
     88 			}
     89 		}
     90 		obj = getPrototypeOf( obj );
     91 	} while ( obj );
     92 
     93 	return names;
     94 }
     95 
     96 
     97 // EXPORTS //
     98 
     99 module.exports = propertyNamesIn;