Skip to content

Instantly share code, notes, and snippets.

@admataz
Created June 5, 2015 20:07
Show Gist options
  • Select an option

  • Save admataz/ece2ada06470c43ceace to your computer and use it in GitHub Desktop.

Select an option

Save admataz/ece2ada06470c43ceace to your computer and use it in GitHub Desktop.
merging passed with default options for javascript functions

As extracted from keystone.js utils

/**
 * Copies and merges options with defaults.
 *
 * @param {Object} defaults
 * @param {Object} options
 * @return {Object} the options argument merged with defaults
 * @api public
 */

var options = exports.options = function options (defaults, ops) {
	ops = ops || {};
	if (!defaults) return ops;
	var keys = Object.keys(defaults), i = keys.length, k;
	while (i--) {
		k = keys[i];
		if (!(k in ops)) {
			ops[k] = defaults[k];
		}
	}
	return ops;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment