Created
May 23, 2013 14:44
-
-
Save A/5636589 to your computer and use it in GitHub Desktop.
Combine user attributes with known attributes and fill in defaults when needed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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