main.js (2254B)
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 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 27 var isWritable = require( '@stdlib/assert/is-writable-property' ); 28 29 30 // MAIN // 31 32 /** 33 * Returns an array of an object's own and inherited writable property names and symbols. 34 * 35 * @param {*} value - input object 36 * @returns {Array} a list of own and inherited writable property names and symbols 37 * 38 * @example 39 * var props = writablePropertiesIn( [] ); 40 * // returns [...] 41 */ 42 function writablePropertiesIn( value ) { 43 var cache; 44 var out; 45 var obj; 46 var tmp; 47 var k; 48 var i; 49 50 if ( value === null || value === void 0 ) { 51 return []; 52 } 53 // Cast the value to an object: 54 obj = Object( value ); 55 56 // Walk the prototype chain collecting writable properties... 57 cache = {}; 58 out = []; 59 do { 60 tmp = getOwnPropertyNames( obj ); 61 for ( i = 0; i < tmp.length; i++ ) { 62 k = tmp[ i ]; 63 if ( 64 hasOwnProp( cache, k ) === false && // guards against processing a name more than once 65 isWritable( obj, k ) 66 ) { 67 out.push( k ); 68 } 69 cache[ k ] = true; 70 } 71 tmp = getOwnPropertySymbols( obj ); 72 for ( i = 0; i < tmp.length; i++ ) { 73 k = tmp[ i ]; 74 if ( 75 hasOwnProp( cache, k ) === false && // guards against processing a symbol more than once 76 isWritable( obj, k ) 77 ) { 78 out.push( k ); 79 } 80 cache[ k ] = true; 81 } 82 obj = getPrototypeOf( obj ); 83 } while ( obj ); 84 85 return out; 86 } 87 88 89 // EXPORTS // 90 91 module.exports = writablePropertiesIn;