{"size": 952, "ext": "js", "max_stars_repo_path": "lib/deepMapKeys.js", "max_stars_repo_name": "trayio/connector-utils", "max_stars_repo_stars_event_min_datetime": "2021-07-22T18:52:01.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-08T10:27:17.000Z", "max_issues_repo_path": "lib/deepMapKeys.js", "max_issues_repo_name": "trayio/connector-utils", "max_issues_repo_issues_event_min_datetime": "2020-06-25T13:19:07.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-12T14:10:15.000Z", "max_forks_repo_path": "lib/deepMapKeys.js", "max_forks_repo_name": "trayio/connector-utils", "max_forks_repo_forks_event_min_datetime": "2021-07-22T18:52:02.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-22T18:52:02.000Z"}
{"max_stars_count": 2, "max_issues_count": 14, "max_forks_count": 1, "avg_line_length": 30.7096774194, "max_line_length": 158, "alphanum_fraction": 0.6964285714}
const _ = require('lodash');
const { DeepMapKeysError } = require('./internal/errors');

/**
 * Maps object keys and formats according to specified casing.
 *
 * @param {Object} collection The collection with keys to iterate over and format.
 * @param {Function} iteratee The format function used to format keys IE [Lodash]{@link https://lodash.com/docs/4.17.15#camelCase} _.camelCase('some_string').
 */

const deepMapKeys = (collection, iteratee) => {
	if (typeof iteratee !== 'function') {
		throw new DeepMapKeysError('The iteratee must be a function.');
	}
	if (Array.isArray(collection)) {
		return collection.map(element => deepMapKeys(element, iteratee));
	}
	if (_.isPlainObject(collection)) {
		return _.transform(collection, (result, value, key) => {
			return _.set(
				result,
				iteratee(key, value),
				_.isObjectLike(value) ? deepMapKeys(value, iteratee) : value,
			);
		});
	}
	return collection;
};

module.exports = deepMapKeys;
