shortcode-params

This utility combine user attributes with known attributes and fill in defaults when needed.

npm install shortcode-params
11 downloads in the last month

Shortcode Your Params

This utility 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.

Example

var MyModule = function(params) {

    'use strict';

    var defaults = {
        majorParam_1: $('.js_mymodule'),
        majorParam_2: 1,
        majorParam_3: false,
        majorParam_4: undefined,
        minorParam_1: 'purchased',
        minorParam_2: 'purchase',
        minorParam_3: 'purchased'
    };


    var init = function() {
        initParams();
        // ... stuff
    };


    var initParams = function() {
        params = shortcodeParams(defaults,params);
    };

    // ... do stuff

    init();

};

new myModule({
    majorParam_1: $('.js_myelement'),
    majorParam_2: 5,
    majorParam_3: true,
    majorParam_4: new SuperModule(),
    undefParam_1: 'bullshit'
})

result

// Params:
{
    majorParam_1: $('.js_myelement'),
    majorParam_2: 5,
    majorParam_3: true,
    majorParam_4: instanceof SuperModule,
    minorParam_1: 'purchased',
    minorParam_2: 'purchase',
    minorParam_3: 'purchased'
    // no undefined params here!
}
npm loves you