time-to-botec

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

polyfill.js (1972B)


      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 isObject = require( '@stdlib/assert/is-object' );
     24 var objectKeys = require( './../../keys' );
     25 var defineProperty = require( './../../define-property' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Defines (and/or modifies) properties.
     32 *
     33 * @param {Object} obj - object on which to define the properties
     34 * @param {Object} props - object with property descriptors
     35 * @throws {TypeError} first argument must be an object
     36 * @throws {TypeError} second argument must be an object
     37 * @returns {Object} object with added and/or modified properties
     38 *
     39 * @example
     40 * var obj = {};
     41 * defineProperties( obj, {
     42 *     'foo': {
     43 *         'value': 'bar'
     44 *     },
     45 *     'baz': {
     46 *          'value': 13
     47 *     }
     48 * });
     49 *
     50 * var val = obj.foo;
     51 * // returns 'bar'
     52 *
     53 * val = obj.baz;
     54 * // returns 13
     55 */
     56 function defineProperties( obj, props ) {
     57 	var keys;
     58 	var name;
     59 	var i;
     60 
     61 	if ( !isObject( obj ) ) {
     62 		throw new TypeError( 'invalid argument. First argument must be an object. Value: `' + obj + '`.' );
     63 	}
     64 	if ( !isObject( props ) ) {
     65 		throw new TypeError( 'invalid argument. Second argument must be an object of property descriptors. Value: `' + props + '`.' );
     66 	}
     67 	keys = objectKeys( props );
     68 	for ( i = 0; i < keys.length; i++ ) {
     69 		name = keys[ i ];
     70 		defineProperty( obj, name, props[ name ] );
     71 	}
     72 	return obj;
     73 }
     74 
     75 
     76 // EXPORTS //
     77 
     78 module.exports = defineProperties;