|
| 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( '@stdlib/utils-property-names' ); |
| 24 | +var getPrototypeOf = require( '@stdlib/utils-get-prototype-of' ); |
| 25 | +var isNonEnumerableProperty = require( '@stdlib/assert-is-nonenumerable-property' ); |
| 26 | +var hasOwnProp = require( '@stdlib/assert-has-own-property' ); |
| 27 | +var Object = require( '@stdlib/object-ctor' ); |
| 28 | + |
| 29 | + |
| 30 | +// MAIN // |
| 31 | + |
| 32 | +/** |
| 33 | +* Returns an array of an object's own and inherited non-enumerable property names. |
| 34 | +* |
| 35 | +* @param {*} value - input object |
| 36 | +* @returns {Array} a list of own and inherited non-enumerable property names |
| 37 | +* |
| 38 | +* @example |
| 39 | +* var defineProperty = require( '@stdlib/utils-define-property' ); |
| 40 | +* |
| 41 | +* var obj = {}; |
| 42 | +* |
| 43 | +* defineProperty( obj, 'beep', { |
| 44 | +* 'configurable': false, |
| 45 | +* 'enumerable': false, |
| 46 | +* 'writable': false, |
| 47 | +* 'value': 'boop' |
| 48 | +* }); |
| 49 | +* |
| 50 | +* var keys = nonEnumerablePropertyNamesIn( obj ); |
| 51 | +* // e.g., returns [ 'beep', ... ] |
| 52 | +*/ |
| 53 | +function nonEnumerablePropertyNamesIn( value ) { // eslint-disable-line id-length |
| 54 | +var names; |
| 55 | +var cache; |
| 56 | +var obj; |
| 57 | +var tmp; |
| 58 | +var k; |
| 59 | +var i; |
| 60 | + |
| 61 | +if ( value === null || value === void 0 ) { |
| 62 | +return []; |
| 63 | +} |
| 64 | +// Cast the value to an object: |
| 65 | +obj = Object( value ); |
| 66 | + |
| 67 | +// Walk the prototype chain collecting non-enumerable property names... |
| 68 | +names = []; |
| 69 | +cache = {}; |
| 70 | +do { |
| 71 | +tmp = getOwnPropertyNames( obj ); |
| 72 | +for ( i = 0; i < tmp.length; i++ ) { |
| 73 | +k = tmp[ i ]; |
| 74 | +if ( |
| 75 | +hasOwnProp( cache, k ) === false && |
| 76 | +isNonEnumerableProperty( obj, k ) |
| 77 | +) { |
| 78 | +names.push( k ); |
| 79 | +} |
| 80 | +cache[ k ] = true; |
| 81 | +} |
| 82 | +obj = getPrototypeOf( obj ); |
| 83 | +} while ( obj ); |
| 84 | + |
| 85 | +return names; |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +// EXPORTS // |
| 90 | + |
| 91 | +module.exports = nonEnumerablePropertyNamesIn; |
0 commit comments