main.js (2170B)
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 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 symbol properties. 52 * 53 * ## Notes 54 * 55 * - In contrast to the built-in `Object.getOwnPropertySymbols()`, 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 symbol properties 59 * 60 * @example 61 * var symbols = propertySymbolsIn( [] ); 62 */ 63 function propertySymbolsIn( value ) { 64 var symbols; 65 var obj; 66 var tmp; 67 var i; 68 69 if ( value === null || value === void 0 ) { 70 return []; 71 } 72 // Cast the value to an object: 73 obj = Object( value ); 74 75 // Walk the prototype chain collecting all symbol properties... 76 symbols = []; 77 do { 78 tmp = getOwnPropertySymbols( obj ); 79 for ( i = 0; i < tmp.length; i++ ) { 80 if ( contains( symbols, tmp[ i ] ) === false ) { 81 symbols.push( tmp[ i ] ); 82 } 83 } 84 obj = getPrototypeOf( obj ); 85 } while ( obj ); 86 87 return symbols; 88 } 89 90 91 // EXPORTS // 92 93 module.exports = propertySymbolsIn;