time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

uncapitalize_keys.js (1543B)


      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 hasOwnProp = require( '@stdlib/assert/has-own-property' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Converts the first letter of each object key to lowercase.
     30 *
     31 * @param {Object} obj - source object
     32 * @throws {TypeError} must provide an object
     33 * @returns {Object} new object
     34 *
     35 * @example
     36 * var obj1 = {
     37 *     'AA': 1,
     38 *     'BB': 2
     39 * };
     40 *
     41 * var obj2 = uncapitalizeKeys( obj1 );
     42 * // returns { 'aA': 1, 'bB': 2 }
     43 */
     44 function uncapitalizeKeys( obj ) {
     45 	var out;
     46 	var key;
     47 	var k;
     48 	if ( typeof obj !== 'object' || obj === null ) {
     49 		throw new TypeError( 'invalid argument. Must provide an object. Value: `'+obj+'`.' );
     50 	}
     51 	out = {};
     52 	for ( key in obj ) {
     53 		if ( hasOwnProp( obj, key ) ) {
     54 			if ( key === '' ) {
     55 				out[ key ] = obj[ key ];
     56 			} else {
     57 				k = key.charAt( 0 ).toLowerCase() + key.slice( 1 );
     58 				out[ k ] = obj[ key ];
     59 			}
     60 		}
     61 	}
     62 	return out;
     63 }
     64 
     65 
     66 // EXPORTS //
     67 
     68 module.exports = uncapitalizeKeys;