main.js (1652B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2019 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 objectKeys = require( './../../keys' ); 24 25 26 // VARIABLES // 27 28 var RE_INTEGER_INDEX = /^[0-9]+$/; 29 30 31 // MAIN // 32 33 /** 34 * Returns an array of an object's own enumerable property names which are not integer indices. 35 * 36 * @param {ObjectLike} obj - input object 37 * @returns {Array} key array 38 * 39 * @example 40 * function Foo() { 41 * this[ 0 ] = 3.14; 42 * this.beep = 'boop'; 43 * return this; 44 * } 45 * 46 * Foo.prototype.foo = 'bar'; 47 * 48 * var obj = new Foo(); 49 * 50 * var keys = nonIndexKeys( obj ); 51 * // e.g., returns [ 'beep' ] 52 */ 53 function nonIndexKeys( obj ) { 54 var keys; 55 var len; 56 var N; 57 var i; 58 var j; 59 60 keys = objectKeys( obj ); 61 len = keys.length; 62 N = len; 63 j = 0; 64 65 // Compress the list of keys by using a lagging index and moving non-integer indices to earlier positions in the array... 66 for ( i = 0; i < len; i++ ) { 67 if ( RE_INTEGER_INDEX.test( keys[ i ] ) ) { 68 N -= 1; 69 } else { 70 keys[ j ] = keys[ i ]; 71 j += 1; 72 } 73 } 74 keys.length = N; 75 return keys; 76 } 77 78 79 // EXPORTS // 80 81 module.exports = nonIndexKeys;