time-to-botec

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

factory.js (2235B)


      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 isObjectLike = require( '@stdlib/assert/is-object-like' );
     24 var copy = require( './../../copy' );
     25 var defaults = require( './defaults.js' );
     26 var validate = require( './validate.js' );
     27 var flatten = require( './flatten.js' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Returns a function to flatten an object.
     34 *
     35 * @param {Options} options - function options
     36 * @param {NonNegativeInteger} [options.depth] - maximum depth to flatten
     37 * @param {boolean} [options.copy=false] - boolean indicating whether to deep copy
     38 * @param {boolean} [options.flattenArrays=false] - boolean indicating whether to flatten arrays
     39 * @param {string} [options.delimiter='.'] - key path delimiter
     40 * @throws {TypeError} options argument must be an object
     41 * @throws {TypeError} must provide valid options
     42 * @returns {Function} flatten function
     43 *
     44 * @example
     45 * var flatten = factory({
     46 *     'copy': true,
     47 *     'delimiter': '|'
     48 * });
     49 *
     50 * var obj = {'a':{'b':{'c':'d'}}};
     51 * var out = flatten( obj );
     52 * // returns {'a|b|c':'d'}
     53 */
     54 function factory( options ) {
     55 	var opts;
     56 	var err;
     57 
     58 	opts = copy( defaults );
     59 	err = validate( opts, options );
     60 	if ( err ) {
     61 		throw err;
     62 	}
     63 	return flattenObject;
     64 
     65 	/**
     66 	* Flattens an object.
     67 	*
     68 	* @private
     69 	* @param {ObjectLike} obj - object to flatten
     70 	* @throws {TypeError} first argument must be object-like
     71 	* @returns {Object} flattened object
     72 	*/
     73 	function flattenObject( obj ) {
     74 		if ( !isObjectLike( obj ) ) {
     75 			throw new TypeError( 'invalid argument. Must provide an object-like value. Value: `' + obj + '`.' );
     76 		}
     77 		return flatten( obj, opts );
     78 	}
     79 }
     80 
     81 
     82 // EXPORTS //
     83 
     84 module.exports = factory;