main.js (2515B)
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 MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' ); 24 var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; 25 var isNonEnumerableProperty = require( '@stdlib/assert/is-nonenumerable-property' ); 26 var getOwnPropertyNames = require( './../../property-names' ); 27 var getPrototypeOf = require( './../../get-prototype-of' ); 28 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 29 30 31 // MAIN // 32 33 /** 34 * Returns an array of an object's inherited non-enumerable property names. 35 * 36 * @param {*} value - input object 37 * @param {PositiveInteger} [level] - inheritance level 38 * @throws {TypeError} second argument must be a positive integer 39 * @returns {Array} a list of inherited non-enumerable property names 40 * 41 * @example 42 * var keys = inheritedNonEnumerablePropertyNames( {} ); 43 */ 44 function inheritedNonEnumerablePropertyNames( value, level ) { // eslint-disable-line id-length 45 var names; 46 var cache; 47 var obj; 48 var tmp; 49 var N; 50 var n; 51 var k; 52 var i; 53 54 if ( arguments.length > 1 ) { 55 if ( !isPositiveInteger( level ) ) { 56 throw new TypeError( 'invalid argument. Second argument must be a positive integer. Value: `' + level + '`.' ); 57 } 58 N = level; 59 } else { 60 N = MAX_SAFE_INTEGER; 61 } 62 if ( value === null || value === void 0 ) { 63 return []; 64 } 65 // Get the value's prototype: 66 obj = getPrototypeOf( value ); 67 68 // Walk the prototype chain collecting non-enumerable property names... 69 names = []; 70 cache = {}; 71 n = 1; 72 while ( obj && n <= N ) { 73 tmp = getOwnPropertyNames( obj ); 74 for ( i = 0; i < tmp.length; i++ ) { 75 k = tmp[ i ]; 76 if ( 77 hasOwnProp( cache, k ) === false && 78 isNonEnumerableProperty( obj, k ) 79 ) { 80 names.push( k ); 81 } 82 cache[ k ] = true; 83 } 84 obj = getPrototypeOf( obj ); 85 n += 1; 86 } 87 88 return names; 89 } 90 91 92 // EXPORTS // 93 94 module.exports = inheritedNonEnumerablePropertyNames;