main.js (2061B)
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 getOwnPropertyDescriptor = require( './../../property-descriptor' ); 24 var getPrototypeOf = require( './../../get-prototype-of' ); 25 26 27 // MAIN // 28 29 /** 30 * Returns a property descriptor for an object's own or inherited property. 31 * 32 * ## Notes 33 * 34 * - In contrast to the built-in `Object.getOwnPropertyDescriptor()`, this function returns `null` if provided `undefined` or `null`, rather than throwing an error. 35 * - In contrast to the built-in `Object.getOwnPropertyDescriptor()`, this function returns `null` if an object does not have a provided property, rather than `undefined`. 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 = propertyDescriptorIn( obj, 'foo' ); 49 * // returns {'configurable':true,'enumerable':true,'writable':true,'value':3.14} 50 */ 51 function propertyDescriptorIn( value, property ) { 52 var desc; 53 var obj; 54 55 if ( value === null || value === void 0 ) { 56 return null; 57 } 58 // Cast the value to an object: 59 obj = Object( value ); 60 61 // Walk the prototype chain in search of a specified property... 62 do { 63 desc = getOwnPropertyDescriptor( obj, property ); 64 if ( desc ) { 65 return desc; 66 } 67 obj = getPrototypeOf( obj ); 68 } while ( obj ); 69 70 return null; 71 } 72 73 74 // EXPORTS // 75 76 module.exports = propertyDescriptorIn;