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