Skip to content

Instantly share code, notes, and snippets.

@A
Created May 23, 2013 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A/5636589 to your computer and use it in GitHub Desktop.
Save A/5636589 to your computer and use it in GitHub Desktop.
Combine user attributes with known attributes and fill in defaults when needed.
/* exported shortcodeParams */
/**
* Combine user attributes with known attributes and fill in defaults when needed.
*
* The defaults should be considered to be all of the attributes which are
* supported by the caller and given as a list. The returned attributes will
* only contain the attributes in the defaults list.
*
* If the params list has unsupported attributes, then they will be ignored and
* removed from the final returned list.
*
*
* @param array defaults Entire list of supported attributes and their defaults.
* @param array params User defined attributes in shortcode tag.
* @return array Combined and filtered attribute list.
*/
var shortcodeParams = function (defaults, params) {
'use strict';
if (typeof params !== 'object') { return defaults; }
var out = {};
for (var key in defaults) {
if (defaults.hasOwnProperty(key)) {
if (params[key]) {
out[key] = params[key];
} else {
out[key] = defaults[key];
}
}
}
return out;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment